Whamcloud - gitweb
fixes:
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
32 #else
33 # include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lustre_sec.h>
40 #include <linux/lprocfs_status.h>
41 #include <linux/lustre_acl.h>
42 #include <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_ERROR, 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         open_req->rq_replay_cb = mdc_replay_open;
676         open_req->rq_commit_cb = mdc_commit_open;
677         open_req->rq_cb_data = mod;
678         DEBUG_REQ(D_HA, open_req, "set up replay data");
679         return 0;
680 }
681
682 int mdc_clear_open_replay_data(struct obd_export *exp,
683                                struct obd_client_handle *och)
684 {
685         struct mdc_open_data *mod = och->och_mod;
686
687         /* Don't free the structure now (it happens in mdc_commit_open, after
688          * we're sure we won't need to fix up the close request in the future),
689          * but make sure that replay doesn't poke at the och, which is about to
690          * be freed. */
691         LASSERT(mod != LP_POISON);
692         if (mod != NULL)
693                 mod->mod_och = NULL;
694         och->och_mod = NULL;
695         return 0;
696 }
697
698 static void mdc_commit_close(struct ptlrpc_request *req)
699 {
700         struct mdc_open_data *mod = req->rq_cb_data;
701         struct obd_import *imp = req->rq_import;
702         struct ptlrpc_request *open_req;
703
704         DEBUG_REQ(D_HA, req, "close req committed");
705         if (mod == NULL)
706                 return;
707
708         mod->mod_close_req = NULL;
709         req->rq_cb_data = NULL;
710         req->rq_commit_cb = NULL;
711
712         open_req = mod->mod_open_req;
713         LASSERT(open_req != NULL);
714         LASSERT(open_req != LP_POISON);
715         LASSERT(open_req->rq_type != LI_POISON);
716
717         DEBUG_REQ(D_HA, open_req, "open req balanced");
718         if (open_req->rq_transno == 0) {
719                 DEBUG_REQ(D_ERROR, open_req, "BUG 3892  open");
720                 DEBUG_REQ(D_ERROR, req, "BUG 3892 close");
721                 LASSERTF(open_req->rq_transno != 0, "BUG 3892\n");
722         }
723         LASSERT(open_req->rq_import == imp);
724
725         /* We no longer want to preserve this for transno-unconditional
726          * replay. */
727         spin_lock(&open_req->rq_lock);
728         open_req->rq_replay = 0;
729         spin_unlock(&open_req->rq_lock);
730 }
731
732 int mdc_close(struct obd_export *exp, struct mdc_op_data *op_data,
733               struct obd_client_handle *och, struct ptlrpc_request **request)
734 {
735         struct obd_device *obd = class_exp2obd(exp);
736         struct obd_import *imp = class_exp2cliimp(exp);
737         int reqsize[3] = {0, sizeof(struct mds_body),
738                           obd->u.cli.cl_max_mds_cookiesize};
739         int rc, repsize[3] = {sizeof(struct mds_body),
740                               obd->u.cli.cl_max_mds_easize,
741                               obd->u.cli.cl_max_mds_cookiesize};
742         struct ptlrpc_request *req;
743         struct mdc_open_data *mod;
744         ENTRY;
745
746         if (imp->imp_connection == NULL) {
747                 CERROR("request on not connected import %s\n",
748                         imp->imp_obd->obd_name);
749                 RETURN(-EIO);
750         }
751
752         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
753                               MDS_CLOSE, 3, reqsize, NULL);
754         if (req == NULL)
755                 GOTO(out, rc = -ENOMEM);
756         req->rq_request_portal = MDS_CLOSE_PORTAL;
757
758         /* ensure that this close's handle is fixed up during replay. */
759         LASSERT(och != NULL);
760         mod = och->och_mod;
761         if (likely(mod != NULL)) {
762                 mod->mod_close_req = req;
763                 LASSERT(mod->mod_open_req->rq_type != LI_POISON);
764                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
765         } else {
766                 CDEBUG(D_HA, "couldn't find open req; "
767                        "expecting close error\n");
768         }
769
770         mdc_close_pack(req, 1, op_data, och);
771
772         req->rq_replen = lustre_msg_size(3, repsize);
773         req->rq_commit_cb = mdc_commit_close;
774         LASSERT(req->rq_cb_data == NULL);
775         req->rq_cb_data = mod;
776
777         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
778         rc = ptlrpc_queue_wait(req);
779         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
780
781         if (req->rq_repmsg == NULL) {
782                 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
783                        req->rq_status);
784                 if (rc == 0)
785                         rc = req->rq_status ? req->rq_status : -EIO;
786         } else if (rc == 0) {
787                 rc = req->rq_repmsg->status;
788                 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
789                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, "
790                                   "err = %d", rc);
791                         if (rc > 0)
792                                 rc = -rc;
793                 } else {
794                         if (mod == NULL)
795                                 CERROR("Unexpected: can't find mdc_open_data, but "
796                                        "close succeeded. Please tell CFS.\n");
797                         if (!lustre_swab_repbuf(req, 0, sizeof(struct mds_body),
798                                                 lustre_swab_mds_body))
799                         {
800                                 CERROR("Error unpacking mds_body\n");
801                                 rc = -EPROTO;
802                         }
803                 }
804         }
805
806         EXIT;
807  out:
808         *request = req;
809         return rc;
810 }
811
812 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
813 {
814         struct ptlrpc_request *req;
815         struct mds_body *body;
816         int rc, size[2] = {0, sizeof(*body)};
817         ENTRY;
818
819         size[0] = lustre_secdesc_size();
820
821         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
822                               MDS_DONE_WRITING, 2, size, NULL);
823         if (req == NULL)
824                 RETURN(-ENOMEM);
825
826         lustre_pack_secdesc(req, size[0]);
827
828         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, 
829                               sizeof(*body));
830         
831         mdc_pack_id(&body->id1, obdo->o_id, 0, obdo->o_mode, 
832                     obdo->o_mds, obdo->o_fid);
833         
834         body->size = obdo->o_size;
835         body->blocks = obdo->o_blocks;
836         body->flags = obdo->o_flags;
837         body->valid = obdo->o_valid;
838
839         req->rq_replen = lustre_msg_size(1, &size[1]);
840
841         rc = ptlrpc_queue_wait(req);
842         ptlrpc_req_finished(req);
843         RETURN(rc);
844 }
845
846 int mdc_readpage(struct obd_export *exp,
847                  struct lustre_id *id,
848                  __u64 offset, struct page *page,
849                  struct ptlrpc_request **request)
850 {
851         struct obd_import *imp = class_exp2cliimp(exp);
852         struct ptlrpc_request *req = NULL;
853         struct ptlrpc_bulk_desc *desc = NULL;
854         struct mds_body *body;
855         int rc, size[2] = {0, sizeof(*body)};
856         ENTRY;
857
858         CDEBUG(D_INODE, "inode: %ld\n", (long)id->li_stc.u.e3s.l3s_ino);
859
860         size[0] = lustre_secdesc_size();
861
862         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE,
863                               2, size, NULL);
864         if (req == NULL)
865                 GOTO(out, rc = -ENOMEM);
866         /* XXX FIXME bug 249 */
867         req->rq_request_portal = MDS_READPAGE_PORTAL;
868
869         lustre_pack_secdesc(req, size[0]);
870
871         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
872         if (desc == NULL)
873                 GOTO(out, rc = -ENOMEM);
874         /* NB req now owns desc and will free it when it gets freed */
875
876         ptlrpc_prep_bulk_page(desc, page, 0, PAGE_CACHE_SIZE);
877         mdc_readdir_pack(req, 1, offset, PAGE_CACHE_SIZE, id);
878
879         req->rq_replen = lustre_msg_size(1, &size[1]);
880         rc = ptlrpc_queue_wait(req);
881
882         if (rc == 0) {
883                 body = lustre_swab_repbuf(req, 0, sizeof (*body),
884                                           lustre_swab_mds_body);
885                 if (body == NULL) {
886                         CERROR("Can't unpack mds_body\n");
887                         GOTO(out, rc = -EPROTO);
888                 }
889
890                 if (req->rq_bulk->bd_nob_transferred != PAGE_CACHE_SIZE) {
891                         CERROR ("Unexpected # bytes transferred: %d"
892                                 " (%ld expected)\n",
893                                 req->rq_bulk->bd_nob_transferred,
894                                 PAGE_CACHE_SIZE);
895                         GOTO (out, rc = -EPROTO);
896                 }
897         }
898
899         EXIT;
900  out:
901         *request = req;
902         return rc;
903 }
904
905 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
906                          void *karg, void *uarg)
907 {
908         struct obd_device *obd = exp->exp_obd;
909         struct obd_ioctl_data *data = karg;
910         struct obd_import *imp = obd->u.cli.cl_import;
911         struct llog_ctxt *ctxt;
912         int rc;
913         ENTRY;
914
915 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
916         MOD_INC_USE_COUNT;
917 #else
918         if (!try_module_get(THIS_MODULE)) {
919                 CERROR("Can't get module. Is it alive?");
920                 return -EINVAL;
921         }
922 #endif
923         switch (cmd) {
924         case OBD_IOC_CLIENT_RECOVER:
925                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
926                 if (rc < 0)
927                         GOTO(out, rc);
928                 GOTO(out, rc = 0);
929         case IOC_OSC_SET_ACTIVE:
930                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
931                 GOTO(out, rc);
932         case IOC_OSC_CTL_RECOVERY:
933                 rc = ptlrpc_import_control_recovery(imp, data->ioc_offset);
934                 GOTO(out, rc);
935         case OBD_IOC_PARSE: {
936                 ctxt = llog_get_context(&exp->exp_obd->obd_llogs,
937                                         LLOG_CONFIG_REPL_CTXT);
938                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
939                 GOTO(out, rc);
940         }
941 #ifdef __KERNEL__
942         case OBD_IOC_LLOG_INFO:
943         case OBD_IOC_LLOG_PRINT: {
944                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
945                 rc = llog_ioctl(ctxt, cmd, data);
946
947                 GOTO(out, rc);
948         }
949 #endif
950         default:
951                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
952                 GOTO(out, rc = -ENOTTY);
953         }
954 out:
955 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
956         MOD_DEC_USE_COUNT;
957 #else
958         module_put(THIS_MODULE);
959 #endif
960
961         return rc;
962 }
963
964 int mdc_set_info(struct obd_export *exp, obd_count keylen,
965                  void *key, obd_count vallen, void *val)
966 {
967         int rc = -EINVAL;
968
969         if (keylen == strlen("initial_recov") &&
970             memcmp(key, "initial_recov", strlen("initial_recov")) == 0) {
971                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
972                 if (vallen != sizeof(int))
973                         RETURN(-EINVAL);
974                 imp->imp_initial_recov = *(int *)val;
975                 CDEBUG(D_HA, "%s: set imp_no_init_recov = %d\n",
976                        exp->exp_obd->obd_name,
977                        imp->imp_initial_recov);
978                 RETURN(0);
979         } else if ((keylen >= strlen("crypto_type")) && 
980                     strcmp(key, "crypto_type") == 0) {
981                 struct ptlrpc_request *req;
982                 char *bufs[2] = {key, val};
983                 int rc, size[2] = {keylen, vallen};
984
985                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
986                                       OST_SET_INFO, 2, size, bufs);
987                 if (req == NULL)
988                         RETURN(-ENOMEM);
989
990                 req->rq_replen = lustre_msg_size(0, NULL);
991                 rc = ptlrpc_queue_wait(req);
992                 ptlrpc_req_finished(req);
993                 RETURN(rc);
994         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
995                 struct obd_import *imp = class_exp2cliimp(exp);
996                 imp->imp_server_timeout = 1;
997                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
998                 RETURN(0);
999         } else if (keylen == strlen("sec") &&
1000                    memcmp(key, "sec", keylen) == 0) {
1001                 struct client_obd *cli = &exp->exp_obd->u.cli;
1002
1003                 cli->cl_sec_flavor = ptlrpcs_name2flavor(val);
1004                 if (cli->cl_sec_flavor == PTLRPCS_FLVR_INVALID) {
1005                         CERROR("unrecognized security type %s\n", (char*) val);
1006                         RETURN(-EINVAL);
1007                 }
1008
1009                 RETURN(0);
1010         } else if (keylen == strlen("sec_flags") &&
1011                    memcmp(key, "sec_flags", keylen) == 0) {
1012                 struct client_obd *cli = &exp->exp_obd->u.cli;
1013
1014                 cli->cl_sec_flags = *((unsigned long *) val);
1015                 RETURN(0);
1016         } else if (keylen == strlen("flush_cred") &&
1017                    memcmp(key, "flush_cred", keylen) == 0) {
1018                 struct client_obd *cli = &exp->exp_obd->u.cli;
1019
1020                 if (cli->cl_import)
1021                         ptlrpcs_import_flush_current_creds(cli->cl_import);
1022                 RETURN(0);
1023         } else if (keylen == strlen("async") && memcmp(key, "async", keylen) == 0) {
1024                 struct client_obd *cl = &exp->exp_obd->u.cli;
1025                 if (vallen != sizeof(int))
1026                         RETURN(-EINVAL);
1027                 cl->cl_async = *(int *)val;
1028                 CDEBUG(D_HA, "%s: set async = %d\n",
1029                        exp->exp_obd->obd_name, cl->cl_async);
1030                 RETURN(0);
1031         } else if (keylen == strlen("setext") && memcmp(key, "setext", keylen) == 0) {
1032                 struct ptlrpc_request *req;
1033                 char *bufs[2] = {key, val};
1034                 int rc, size[2] = {keylen, vallen};
1035
1036                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1037                                       OST_SET_INFO, 2, size, bufs);
1038                 if (req == NULL)
1039                         RETURN(-ENOMEM);
1040
1041                 req->rq_replen = lustre_msg_size(0, NULL);
1042                 rc = ptlrpc_queue_wait(req);
1043                 ptlrpc_req_finished(req);
1044                 RETURN(rc);
1045         } else if (keylen == 5 && strcmp(key, "audit") == 0) {
1046                 struct ptlrpc_request *req;
1047                 char *bufs[2] = {key, val};
1048                 int rc, size[2] = {keylen, vallen};
1049
1050                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1051                                       OST_SET_INFO, 2, size, bufs);
1052                 if (req == NULL)
1053                         RETURN(-ENOMEM);
1054
1055                 req->rq_replen = lustre_msg_size(0, NULL);
1056                 lustre_swab_reqbuf(req, 1, sizeof(struct audit_attr_msg),
1057                                    lustre_swab_audit_attr);
1058                 rc = ptlrpc_queue_wait(req);
1059                 ptlrpc_req_finished(req);
1060
1061                 RETURN(rc);
1062         } else if (keylen == strlen("ids") && memcmp(key, "ids", keylen) == 0) {
1063                 struct ptlrpc_request *req;
1064                 struct lustre_id *ids = (struct lustre_id *)val;
1065                 char *bufs[3] = {key, (char *)ids, (char *)(ids + 1)};
1066                 int rc, size[3] = {keylen, sizeof(struct lustre_id), 
1067                                    sizeof(struct lustre_id)};
1068
1069                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1070                                       OST_SET_INFO, 3, size, bufs);
1071                 if (req == NULL)
1072                         RETURN(-ENOMEM);
1073
1074                 req->rq_replen = lustre_msg_size(0, NULL);
1075                 rc = ptlrpc_queue_wait(req);
1076                 ptlrpc_req_finished(req);
1077                 RETURN(rc);
1078         }
1079         RETURN(rc);
1080 }
1081
1082 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1083                       unsigned long max_age)
1084 {
1085         struct obd_statfs *msfs;
1086         struct ptlrpc_request *req;
1087         int rc, size = sizeof(*msfs);
1088         ENTRY;
1089
1090         /* We could possibly pass max_age in the request (as an absolute
1091          * timestamp or a "seconds.usec ago") so the target can avoid doing
1092          * extra calls into the filesystem if that isn't necessary (e.g.
1093          * during mount that would help a bit).  Having relative timestamps
1094          * is not so great if request processing is slow, while absolute
1095          * timestamps are not ideal because they need time synchronization. */
1096         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
1097                               MDS_STATFS, 0, NULL, NULL);
1098         if (!req)
1099                 RETURN(-ENOMEM);
1100
1101         req->rq_replen = lustre_msg_size(1, &size);
1102
1103         mdc_get_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
1104         rc = ptlrpc_queue_wait(req);
1105         mdc_put_rpc_lock(obd->u.cli.cl_rpc_lock, NULL);
1106
1107         if (rc) {
1108                 /* this can be LMV fake import, whcih is not connected. */
1109                 if (!req->rq_import->imp_connection)
1110                         memset(osfs, 0, sizeof(*osfs));
1111                 GOTO(out, rc);
1112         }
1113
1114         msfs = lustre_swab_repbuf(req, 0, sizeof(*msfs),
1115                                   lustre_swab_obd_statfs);
1116         if (msfs == NULL) {
1117                 CERROR("Can't unpack obd_statfs\n");
1118                 GOTO(out, rc = -EPROTO);
1119         }
1120
1121         memcpy(osfs, msfs, sizeof (*msfs));
1122         EXIT;
1123 out:
1124         ptlrpc_req_finished(req);
1125         return rc;
1126 }
1127
1128 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
1129                    struct obd_client_handle *handle, int flag)
1130 {
1131         struct ptlrpc_request *req;
1132         struct mds_body *body;
1133         int rc, size[2] = {0, sizeof(*body)};
1134         ENTRY;
1135
1136         //size[0] = lustre_secdesc_size();
1137
1138         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1139                               MDS_PIN, 2, size, NULL);
1140         if (req == NULL)
1141                 RETURN(-ENOMEM);
1142
1143         //lustre_pack_secdesc(req, size[0]);
1144
1145         body = lustre_msg_buf(req->rq_reqmsg, 
1146                               MDS_REQ_REC_OFF, sizeof(*body));
1147
1148         /* FIXME-UMKA: here should be also mdsnum and fid. */
1149         mdc_pack_id(&body->id1, ino, gen, type, 0, 0);
1150         body->flags = flag;
1151
1152         req->rq_replen = lustre_msg_size(1, &size[1]);
1153
1154         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1155         rc = ptlrpc_queue_wait(req);
1156         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1157         if (rc) {
1158                 CERROR("pin failed: %d\n", rc);
1159                 ptlrpc_req_finished(req);
1160                 RETURN(rc);
1161         }
1162
1163         body = lustre_swab_repbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1164         if (body == NULL) {
1165                 ptlrpc_req_finished(req);
1166                 RETURN(rc);
1167         }
1168
1169         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
1170         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1171
1172         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
1173         if (handle->och_mod == NULL) {
1174                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
1175                 RETURN(-ENOMEM);
1176         }
1177         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1178
1179         RETURN(rc);
1180 }
1181
1182 static int mdc_unpin(struct obd_export *exp,
1183                      struct obd_client_handle *handle, int flag)
1184 {
1185         struct ptlrpc_request *req;
1186         struct mds_body *body;
1187         int rc, size[2] = {0, sizeof(*body)};
1188         ENTRY;
1189
1190         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
1191                 RETURN(0);
1192
1193         //size[0] = lustre_secdesc_size();
1194
1195         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1196                               MDS_CLOSE, 2, size, NULL);
1197         if (req == NULL)
1198                 RETURN(-ENOMEM);
1199
1200         //lustre_pack_secdesc(req, size[0]);
1201
1202         body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF, sizeof(*body));
1203         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
1204         body->flags = flag;
1205
1206         req->rq_replen = lustre_msg_size(0, NULL);
1207         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1208         rc = ptlrpc_queue_wait(req);
1209         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1210
1211         if (rc != 0)
1212                 CERROR("unpin failed: %d\n", rc);
1213
1214         ptlrpc_req_finished(req);
1215         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1216         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1217         RETURN(rc);
1218 }
1219
1220 int mdc_sync(struct obd_export *exp, struct lustre_id *id,
1221              struct ptlrpc_request **request)
1222 {
1223         struct ptlrpc_request *req;
1224         struct mds_body *body;
1225         int size[2] = {0, sizeof(*body)};
1226         int rc;
1227         ENTRY;
1228
1229         //size[0] = lustre_secdesc_size();
1230
1231         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1232                               MDS_SYNC, 2, size, NULL);
1233         if (!req)
1234                 RETURN(rc = -ENOMEM);
1235
1236         //lustre_pack_secdesc(req, size[0]);
1237
1238         if (id) {
1239                 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1240                                       sizeof (*body));
1241                 memcpy(&body->id1, id, sizeof(*id));
1242         }
1243
1244         req->rq_replen = lustre_msg_size(1, &size[1]);
1245
1246         rc = ptlrpc_queue_wait(req);
1247         if (rc || request == NULL)
1248                 ptlrpc_req_finished(req);
1249         else
1250                 *request = req;
1251
1252         RETURN(rc);
1253 }
1254
1255 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1256                             enum obd_import_event event)
1257 {
1258         int rc = 0;
1259
1260         LASSERT(imp->imp_obd == obd);
1261
1262         switch (event) {
1263         case IMP_EVENT_DISCON: {
1264                 break;
1265         }
1266         case IMP_EVENT_INACTIVE: {
1267                 if (obd->obd_observer)
1268                         rc = obd_notify(obd->obd_observer, obd, 0, 0);
1269                 break;
1270         }
1271         case IMP_EVENT_INVALIDATE: {
1272                 struct ldlm_namespace *ns = obd->obd_namespace;
1273
1274                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1275
1276                 break;
1277         }
1278         case IMP_EVENT_ACTIVE: {
1279                 if (obd->obd_observer)
1280                         rc = obd_notify(obd->obd_observer, obd, 1, 0);
1281                 break;
1282         }
1283         default:
1284                 CERROR("Unknown import event %d\n", event);
1285                 LBUG();
1286         }
1287         RETURN(rc);
1288 }
1289
1290 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
1291 {
1292         struct lprocfs_static_vars lvars;
1293
1294         lprocfs_init_vars(mdc, &lvars);
1295         return lprocfs_obd_attach(dev, lvars.obd_vars);
1296 }
1297
1298 static int mdc_detach(struct obd_device *dev)
1299 {
1300         return lprocfs_obd_detach(dev);
1301 }
1302
1303 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1304 {
1305         struct client_obd *cli = &obd->u.cli;
1306         int rc;
1307         ENTRY;
1308
1309         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1310         if (!cli->cl_rpc_lock)
1311                 RETURN(-ENOMEM);
1312         mdc_init_rpc_lock(cli->cl_rpc_lock);
1313
1314         ptlrpcd_addref();
1315
1316         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1317         if (!cli->cl_setattr_lock)
1318                 GOTO(err_rpc_lock, rc = -ENOMEM);
1319         mdc_init_rpc_lock(cli->cl_setattr_lock);
1320
1321         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1322         if (!cli->cl_close_lock)
1323                 GOTO(err_setattr_lock, rc = -ENOMEM);
1324         mdc_init_rpc_lock(cli->cl_close_lock);
1325
1326         rc = client_obd_setup(obd, len, buf);
1327         if (rc)
1328                 GOTO(err_close_lock, rc);
1329
1330         rc = obd_llog_init(obd, &obd->obd_llogs, obd, 0, NULL);
1331         if (rc) {
1332                 mdc_cleanup(obd, 0);
1333                 CERROR("failed to setup llogging subsystems\n");
1334         }
1335
1336         RETURN(rc);
1337
1338 err_close_lock:
1339         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1340 err_setattr_lock:
1341         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1342 err_rpc_lock:
1343         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1344         ptlrpcd_decref();
1345         RETURN(rc);
1346 }
1347
1348 static int mdc_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1349 {
1350         struct obd_device *obd = exp->exp_obd;
1351         struct client_obd *cli = &obd->u.cli;
1352         ENTRY;
1353
1354         if (cli->cl_max_mds_easize < easize)
1355                 cli->cl_max_mds_easize = easize;
1356         if (cli->cl_max_mds_cookiesize < cookiesize)
1357                 cli->cl_max_mds_cookiesize = cookiesize;
1358         RETURN(0);
1359 }
1360
1361 static int mdc_precleanup(struct obd_device *obd, int flags)
1362 {
1363         int rc = 0;
1364         
1365         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1366         if (rc != 0)
1367                 CERROR("failed to cleanup llogging subsystems\n");
1368
1369         RETURN(rc);
1370 }
1371
1372 static int mdc_cleanup(struct obd_device *obd, int flags)
1373 {
1374         struct client_obd *cli = &obd->u.cli;
1375
1376         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1377         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1378         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1379
1380         ptlrpcd_decref();
1381
1382         return client_obd_cleanup(obd, flags);
1383 }
1384
1385
1386 static int mdc_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1387                          struct obd_device *tgt, int count,
1388                          struct llog_catid *logid)
1389 {
1390         struct llog_ctxt *ctxt;
1391         int rc;
1392         ENTRY;
1393
1394         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1395                             &llog_client_ops);
1396         if (rc == 0) {
1397                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1398                 ctxt->loc_imp = obd->u.cli.cl_import;
1399         }
1400
1401         RETURN(rc);
1402 }
1403
1404 static int mdc_llog_finish(struct obd_device *obd,
1405                            struct obd_llogs *llogs, int count)
1406 {
1407         int rc;
1408         ENTRY;
1409
1410         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1411         RETURN(rc);
1412 }
1413
1414 static struct obd_device *mdc_get_real_obd(struct obd_export *exp,
1415                                            struct lustre_id *id)
1416 {
1417        ENTRY;
1418        RETURN(exp->exp_obd);
1419 }
1420
1421 static int mdc_get_info(struct obd_export *exp, __u32 keylen,
1422                         void *key, __u32 *valsize, void *val)
1423 {
1424         struct ptlrpc_request *req;
1425         char *bufs[1] = {key};
1426         int rc = 0;
1427         ENTRY;
1428         
1429         if (!valsize || !val)
1430                 RETURN(-EFAULT);
1431
1432         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
1433                 struct obd_import *imp;
1434                 struct obd_connect_data *data;
1435
1436                 imp = class_exp2cliimp(exp);
1437                 if (!imp) {
1438                         LBUG();
1439                         RETURN(-EINVAL);
1440                 }
1441
1442                 if (imp->imp_state != LUSTRE_IMP_FULL) {
1443                         CERROR("import state not full\n");
1444                         RETURN(-EINVAL);
1445                 }
1446
1447                 data = &imp->imp_connect_data;
1448                 if (data->ocd_connect_flags & OBD_CONNECT_REMOTE) {
1449                         *((int *)val) = 1;
1450                         RETURN(0);
1451                 } else if (data->ocd_connect_flags & OBD_CONNECT_LOCAL) {
1452                         *((int *)val) = 0;
1453                         RETURN(0);
1454                 }
1455                 CERROR("no remote flag set?\n");
1456                 RETURN(-EINVAL);
1457         }
1458
1459         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
1460             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
1461             (keylen < strlen("lovdesc") || strcmp(key, "lovdesc") != 0) &&
1462             (keylen < strlen("getext") || strcmp(key, "getext") != 0) &&
1463             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0) &&
1464             (keylen < strlen("auditid") || strcmp(key, "auditid") != 0))
1465                 RETURN(-EPROTO);
1466                 
1467         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1468                               OST_GET_INFO, 1, (int *)&keylen, bufs);
1469         if (req == NULL)
1470                 RETURN(-ENOMEM);
1471
1472         req->rq_replen = lustre_msg_size(1, (int *)valsize);
1473         rc = ptlrpc_queue_wait(req);
1474         if (rc)
1475                 GOTO(out_req, rc);
1476
1477         if ((keylen >= strlen("rootid") && !strcmp(key, "rootid")) ||
1478             (keylen >= strlen("auditid") && !strcmp(key, "auditid"))) {
1479                 struct lustre_id *reply;
1480                 
1481                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1482                                            lustre_swab_lustre_id);
1483                 if (reply == NULL) {
1484                         CERROR("Can't unpack %s\n", (char *)key);
1485                         GOTO(out_req, rc = -EPROTO);
1486                 }
1487
1488                 *(struct lustre_id *)val = *reply;
1489         } else if (keylen >= strlen("lovdesc") && !strcmp(key, "lovdesc")) {
1490                 struct lov_desc *reply;
1491                 
1492                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1493                                            lustre_swab_lov_desc);
1494                 if (reply == NULL) {
1495                         CERROR("Can't unpack %s\n", (char *)key);
1496                         GOTO(out_req, rc = -EPROTO);
1497                 }
1498
1499                 *(struct lov_desc *)val = *reply;
1500                 RETURN(0);
1501         } else if (keylen >= strlen("getext") && !strcmp(key, "getext")) {
1502                 struct fid_extent *reply;
1503                 
1504                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1505                                            lustre_swab_fid_extent);
1506                 if (reply == NULL) {
1507                         CERROR("Can't unpack %s\n", (char *)key);
1508                         GOTO(out_req, rc = -EPROTO);
1509                 }
1510
1511                 *(struct fid_extent *)val = *reply;
1512                 RETURN(0);
1513         } else {
1514                 __u32 *reply;
1515                 
1516                 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
1517                                            lustre_swab_generic_32s);
1518                 if (reply == NULL) {
1519                         CERROR("Can't unpack %s\n", (char *)key);
1520                         GOTO(out_req, rc = -EPROTO);
1521                 }
1522                 *((__u32 *)val) = *reply;
1523         }
1524 out_req:
1525         ptlrpc_req_finished(req);
1526         RETURN(rc);
1527 }
1528
1529 int mdc_obj_create(struct obd_export *exp, struct obdo *oa,
1530                    void *acl, int acl_size, struct lov_stripe_md **ea,
1531                    struct obd_trans_info *oti)
1532 {
1533         struct ptlrpc_request *request;
1534         struct ost_body *body;
1535         char *acl_buf;
1536         int rc, size[2] = { sizeof(*body), acl_size };
1537         ENTRY;
1538
1539         LASSERT(oa);
1540
1541         request = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION,
1542                                   OST_CREATE, 2, size, NULL);
1543         if (!request)
1544                 GOTO(out_req, rc = -ENOMEM);
1545
1546         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1547         memcpy(&body->oa, oa, sizeof(body->oa));
1548
1549         if (acl_size) {
1550                 acl_buf = lustre_msg_buf(request->rq_reqmsg, 1, acl_size);
1551                 memcpy(acl_buf, acl, acl_size);
1552         }
1553
1554         request->rq_replen = lustre_msg_size(1, size);
1555         rc = ptlrpc_queue_wait(request);
1556         if (rc)
1557                 GOTO(out_req, rc);
1558
1559         body = lustre_swab_repbuf(request, 0, sizeof(*body),
1560                                   lustre_swab_ost_body);
1561         if (body == NULL) {
1562                 CERROR ("can't unpack ost_body\n");
1563                 GOTO (out_req, rc = -EPROTO);
1564         }
1565
1566         memcpy(oa, &body->oa, sizeof(*oa));
1567
1568         /* store ino/generation for recovery */
1569         body = lustre_msg_buf(request->rq_reqmsg, 0, sizeof (*body));
1570         body->oa.o_id = oa->o_id;
1571         body->oa.o_generation = oa->o_generation;
1572         body->oa.o_fid = oa->o_fid;
1573         body->oa.o_mds = oa->o_mds;
1574
1575         CDEBUG(D_HA, "transno: "LPD64"\n", request->rq_repmsg->transno);
1576         EXIT;
1577 out_req:
1578         ptlrpc_req_finished(request);
1579         return rc;
1580 }
1581
1582 int mdc_brw(int rw, struct obd_export *exp, struct obdo *oa,
1583             struct lov_stripe_md *ea, obd_count oa_bufs,
1584             struct brw_page *pgarr, struct obd_trans_info *oti)
1585 {
1586         struct ptlrpc_bulk_desc *desc;
1587         struct niobuf_remote *niobuf;
1588         struct ptlrpc_request *req;
1589         struct obd_ioobj *ioobj;
1590         struct ost_body *body;
1591         int err, opc, i;
1592         int size[3];
1593
1594         opc = ((rw & OBD_BRW_WRITE) != 0) ? OST_WRITE : OST_READ;
1595         
1596         size[0] = sizeof(*body);
1597         size[1] = sizeof(*ioobj);
1598         size[2] = oa_bufs * sizeof(*niobuf);
1599
1600         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_OBD_VERSION, opc,
1601                               3, size, NULL);
1602         LASSERT(req != NULL);
1603
1604         if (opc == OST_WRITE)
1605                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_GET_SOURCE,
1606                                             OST_BULK_PORTAL);
1607         else
1608                 desc = ptlrpc_prep_bulk_imp(req, oa_bufs, BULK_PUT_SINK,
1609                                             OST_BULK_PORTAL);
1610         LASSERT(desc != NULL);
1611
1612         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1613         ioobj = lustre_msg_buf(req->rq_reqmsg, 1, sizeof(*ioobj));
1614         niobuf = lustre_msg_buf(req->rq_reqmsg, 2, oa_bufs * sizeof(*niobuf));
1615
1616         memcpy(&body->oa, oa, sizeof(*oa));
1617         obdo_to_ioobj(oa, ioobj);
1618         ioobj->ioo_bufcnt = oa_bufs;
1619
1620         for (i = 0; i < oa_bufs; i++, niobuf++) {
1621                 struct brw_page *pg = &pgarr[i];
1622
1623                 LASSERT(pg->count > 0);
1624                 LASSERT((pg->disk_offset & ~PAGE_MASK) + pg->count <= PAGE_SIZE);
1625
1626                 ptlrpc_prep_bulk_page(desc, pg->pg, pg->disk_offset & ~PAGE_MASK,
1627                                       pg->count);
1628
1629                 niobuf->offset = pg->disk_offset;
1630                 niobuf->len = pg->count;
1631                 niobuf->flags = pg->flag;
1632         }
1633
1634         /* size[0] still sizeof (*body) */
1635         if (opc == OST_WRITE) {
1636                 /* 1 RC per niobuf */
1637                 size[1] = sizeof(__u32) * oa_bufs;
1638                 req->rq_replen = lustre_msg_size(2, size);
1639         } else {
1640                 /* 1 RC for the whole I/O */
1641                 req->rq_replen = lustre_msg_size(1, size);
1642         }
1643         err = ptlrpc_queue_wait(req);
1644         LASSERT(err == 0);
1645
1646         ptlrpc_req_finished(req);
1647         return 0;
1648 }
1649
1650 static int mdc_valid_attrs(struct obd_export *exp,
1651                            struct lustre_id *id)
1652 {
1653         struct ldlm_res_id res_id = { .name = {0} };
1654         struct obd_device *obd = exp->exp_obd;
1655         struct lustre_handle lockh;
1656         ldlm_policy_data_t policy;
1657         int flags;
1658         ENTRY;
1659
1660         res_id.name[0] = id_fid(id);
1661         res_id.name[1] = id_group(id);
1662         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1663
1664         CDEBUG(D_INFO, "trying to match res "LPU64"\n",
1665                res_id.name[0]);
1666
1667         /* FIXME use LDLM_FL_TEST_LOCK instead */
1668         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1669         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1670                             LDLM_IBITS, &policy, LCK_PR, &lockh)) {
1671                 ldlm_lock_decref(&lockh, LCK_PR);
1672                 RETURN(1);
1673         }
1674
1675         if (ldlm_lock_match(obd->obd_namespace, flags, &res_id,
1676                             LDLM_IBITS, &policy, LCK_PW, &lockh)) {
1677                 ldlm_lock_decref(&lockh, LCK_PW);
1678                 RETURN(1);
1679         }
1680         RETURN(0);
1681 }
1682
1683 static int mdc_change_cbdata_name(struct obd_export *exp,
1684                                   struct lustre_id *pid,
1685                                   char *name, int len,
1686                                   struct lustre_id *cid,
1687                                   ldlm_iterator_t it, void *data)
1688 {
1689         int rc;
1690         rc = mdc_change_cbdata(exp, cid, it, data);
1691         RETURN(rc);
1692 }
1693
1694 struct obd_ops mdc_obd_ops = {
1695         .o_owner         = THIS_MODULE,
1696         .o_attach        = mdc_attach,
1697         .o_detach        = mdc_detach,
1698         .o_setup         = mdc_setup,
1699         .o_precleanup    = mdc_precleanup,
1700         .o_cleanup       = mdc_cleanup,
1701         .o_add_conn      = client_import_add_conn,
1702         .o_del_conn      = client_import_del_conn,
1703         .o_connect       = client_connect_import,
1704         .o_disconnect    = client_disconnect_export,
1705         .o_iocontrol     = mdc_iocontrol,
1706         .o_packmd        = mdc_packmd,
1707         .o_unpackmd      = mdc_unpackmd,
1708         .o_statfs        = mdc_statfs,
1709         .o_pin           = mdc_pin,
1710         .o_unpin         = mdc_unpin,
1711         .o_import_event  = mdc_import_event,
1712         .o_llog_init     = mdc_llog_init,
1713         .o_llog_finish   = mdc_llog_finish,
1714         .o_create        = mdc_obj_create,
1715         .o_set_info      = mdc_set_info,
1716         .o_get_info      = mdc_get_info,
1717         .o_brw           = mdc_brw,
1718         .o_cancel_unused = mdc_cancel_unused,
1719         .o_init_ea_size  = mdc_init_ea_size,
1720 };
1721
1722 struct md_ops mdc_md_ops = {
1723         .m_getstatus     = mdc_getstatus,
1724         .m_getattr       = mdc_getattr,
1725         .m_close         = mdc_close,
1726         .m_create        = mdc_create,
1727         .m_done_writing  = mdc_done_writing,
1728         .m_enqueue       = mdc_enqueue,
1729         .m_getattr_lock  = mdc_getattr_lock,
1730         .m_intent_lock   = mdc_intent_lock,
1731         .m_link          = mdc_link,
1732         .m_rename        = mdc_rename,
1733         .m_setattr       = mdc_setattr,
1734         .m_sync          = mdc_sync,
1735         .m_readpage      = mdc_readpage,
1736         .m_unlink        = mdc_unlink,
1737         .m_valid_attrs   = mdc_valid_attrs,
1738         .m_req2lustre_md = mdc_req2lustre_md,
1739         .m_set_open_replay_data   = mdc_set_open_replay_data,
1740         .m_clear_open_replay_data = mdc_clear_open_replay_data,
1741         .m_store_inode_generation = mdc_store_inode_generation,
1742         .m_set_lock_data = mdc_set_lock_data,
1743         .m_get_real_obd  = mdc_get_real_obd,
1744         .m_change_cbdata_name = mdc_change_cbdata_name,
1745         .m_change_cbdata = mdc_change_cbdata,
1746         .m_access_check  = mdc_access_check,
1747 };
1748
1749 int __init mdc_init(void)
1750 {
1751         struct lprocfs_static_vars lvars;
1752         
1753         lprocfs_init_vars(mdc, &lvars);
1754         return class_register_type(&mdc_obd_ops, &mdc_md_ops,
1755                                    lvars.module_vars, OBD_MDC_DEVICENAME);
1756 }
1757
1758 #ifdef __KERNEL__
1759 static void /*__exit*/ mdc_exit(void)
1760 {
1761         class_unregister_type(OBD_MDC_DEVICENAME);
1762 }
1763
1764 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1765 MODULE_DESCRIPTION("Lustre Metadata Client");
1766 MODULE_LICENSE("GPL");
1767
1768 EXPORT_SYMBOL(mdc_req2lustre_md);
1769 EXPORT_SYMBOL(mdc_change_cbdata);
1770 EXPORT_SYMBOL(mdc_getstatus);
1771 EXPORT_SYMBOL(mdc_getattr);
1772 EXPORT_SYMBOL(mdc_getattr_lock);
1773 EXPORT_SYMBOL(mdc_create);
1774 EXPORT_SYMBOL(mdc_unlink);
1775 EXPORT_SYMBOL(mdc_rename);
1776 EXPORT_SYMBOL(mdc_link);
1777 EXPORT_SYMBOL(mdc_readpage);
1778 EXPORT_SYMBOL(mdc_setattr);
1779 EXPORT_SYMBOL(mdc_close);
1780 EXPORT_SYMBOL(mdc_done_writing);
1781 EXPORT_SYMBOL(mdc_sync);
1782 EXPORT_SYMBOL(mdc_set_open_replay_data);
1783 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1784 EXPORT_SYMBOL(mdc_store_inode_generation);
1785
1786 module_init(mdc_init);
1787 module_exit(mdc_exit);
1788 #endif