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