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