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