Whamcloud - gitweb
bca065efd1527c8debd6d164d33b5891c1b5ef30
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_MDC
41
42 #ifdef __KERNEL__
43 # include <linux/module.h>
44 # include <linux/pagemap.h>
45 # include <linux/miscdevice.h>
46 # include <linux/init.h>
47 #else
48 # include <liblustre.h>
49 #endif
50
51 #include <obd_class.h>
52 #include <lustre_dlm.h>
53 #include <lprocfs_status.h>
54 #include <lustre_param.h>
55 #include "mdc_internal.h"
56
57 static quota_interface_t *quota_interface;
58
59 #define REQUEST_MINOR 244
60
61 static quota_interface_t *quota_interface;
62 extern quota_interface_t mdc_quota_interface;
63
64 static int mdc_cleanup(struct obd_device *obd);
65
66 extern int mds_queue_req(struct ptlrpc_request *);
67 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
68 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
69 static int send_getstatus(struct obd_export *exp, struct ll_fid *rootfid,
70                           int level, int msg_flags)
71 {
72         struct ptlrpc_request *req;
73         struct mds_body *body;
74         __u32 size[3] = { sizeof(struct ptlrpc_body),
75                           sizeof(struct mdt_body),
76                           sizeof(struct lustre_capa) };
77         int rc;
78         ENTRY;
79
80         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION, MDS_GETSTATUS, 2, size,
81                               NULL);
82         if (!req)
83                 GOTO(out, rc = -ENOMEM);
84
85         req->rq_export = class_export_get(exp);
86         req->rq_send_state = level;
87         ptlrpc_req_set_repsize(req, 3, size);
88
89         mdc_pack_req_body(req, REQ_REC_OFF, 0, NULL, 0, 0);
90         lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
91         rc = ptlrpc_queue_wait(req);
92
93         if (!rc) {
94                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
95                                           lustre_swab_mds_body);
96                 if (body == NULL) {
97                         CERROR ("Can't extract mds_body\n");
98                         GOTO (out, rc = -EPROTO);
99                 }
100
101                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
102
103                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
104                        ", last_xid="LPU64"\n",
105                        rootfid->id,
106                        lustre_msg_get_last_committed(req->rq_repmsg),
107                        lustre_msg_get_last_xid(req->rq_repmsg));
108         }
109
110         EXIT;
111  out:
112         ptlrpc_req_finished(req);
113         return rc;
114 }
115
116 /* This should be mdc_get_info("ROOT") */
117 int mdc_getstatus(struct obd_export *exp, struct ll_fid *rootfid)
118 {
119         return send_getstatus(exp, rootfid, LUSTRE_IMP_FULL, 0);
120 }
121
122 static
123 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size,
124                        unsigned int acl_size, struct ptlrpc_request *req)
125 {
126         struct obd_device *obddev = class_exp2obd(exp);
127         struct mds_body *body;
128         void *eadata;
129         __u32 size[6] = { sizeof(struct ptlrpc_body),
130                           sizeof(struct mdt_body) };
131         int bufcount = 2, rc;
132         ENTRY;
133
134         /* request message already built */
135         if (ea_size != 0) {
136                 size[bufcount++] = ea_size;
137                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
138                        ea_size);
139         }
140         if (acl_size) {
141                 size[bufcount++] = acl_size;
142                 CDEBUG(D_INODE, "reserved %u bytes for ACL\n", acl_size);
143         }
144
145         if (mdc_exp_is_2_0_server(exp)) {
146                 bufcount = 6;
147         }
148
149         ptlrpc_req_set_repsize(req, bufcount, size);
150
151         mdc_enter_request(&obddev->u.cli);
152         rc = ptlrpc_queue_wait(req);
153         mdc_exit_request(&obddev->u.cli);
154         if (rc != 0)
155                 RETURN (rc);
156
157         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
158                                   lustre_swab_mds_body);
159         if (body == NULL) {
160                 CERROR ("Can't unpack mds_body\n");
161                 RETURN (-EPROTO);
162         }
163
164         CDEBUG(D_NET, "mode: %o\n", body->mode);
165
166         lustre_set_rep_swabbed(req, REPLY_REC_OFF + 1);
167         if (body->eadatasize != 0) {
168                 /* reply indicates presence of eadata; check it's there... */
169                 eadata = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
170                                         body->eadatasize);
171                 if (eadata == NULL) {
172                         CERROR ("Missing/short eadata\n");
173                         RETURN (-EPROTO);
174                 }
175         }
176
177         RETURN (0);
178 }
179
180 int mdc_getattr(struct obd_export *exp, struct ll_fid *fid,
181                 obd_valid valid, unsigned int ea_size,
182                 struct ptlrpc_request **request)
183 {
184         struct ptlrpc_request *req;
185         __u32 size[2] = { sizeof(struct ptlrpc_body),
186                           sizeof(struct mdt_body) };
187         int acl_size = 0, rc;
188         ENTRY;
189
190         /* XXX do we need to make another request here?  We just did a getattr
191          *     to do the lookup in the first place.
192          */
193         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
194                               MDS_GETATTR, 2, size, NULL);
195         if (!req)
196                 GOTO(out, rc = -ENOMEM);
197
198         req->rq_export = class_export_get(exp);
199         mdc_pack_req_body(req, REQ_REC_OFF, valid, fid, ea_size,
200                           MDS_BFLAG_EXT_FLAGS/*request "new" flags(bug 9486)*/);
201
202         /* currently only root inode will call us with FLACL */
203         if (valid & OBD_MD_FLACL)
204                 acl_size = LUSTRE_POSIX_ACL_MAX_SIZE;
205
206         rc = mdc_getattr_common(exp, ea_size, acl_size, req);
207         if (rc != 0) {
208                 ptlrpc_req_finished (req);
209                 req = NULL;
210         }
211  out:
212         *request = req;
213         RETURN (rc);
214 }
215
216 int mdc_getattr_name(struct obd_export *exp, struct ll_fid *fid,
217                      const char *filename, int namelen, unsigned long valid,
218                      unsigned int ea_size, struct ptlrpc_request **request)
219 {
220         struct ptlrpc_request *req;
221         __u32 size[4] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
222                           [REQ_REC_OFF] = sizeof(struct mdt_body),
223                           [REQ_REC_OFF + 1] = namelen };
224         int rc;
225         int bufcount = 3;
226         int nameoffset = REQ_REC_OFF + 1;
227         ENTRY;
228
229         if (mdc_exp_is_2_0_server(exp)) {
230                 size[REQ_REC_OFF + 1] = 0;
231                 size[REQ_REC_OFF + 2] = namelen;
232                 bufcount ++;
233                 nameoffset ++;
234         }
235
236         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
237                               MDS_GETATTR_NAME, bufcount, size, NULL);
238         if (!req)
239                 GOTO(out, rc = -ENOMEM);
240
241         req->rq_export = class_export_get(exp);
242         mdc_pack_req_body(req, REQ_REC_OFF, valid, fid, ea_size,
243                           MDS_BFLAG_EXT_FLAGS/*request "new" flags(bug 9486)*/);
244
245         LASSERT(strnlen(filename, namelen) == namelen - 1);
246         memcpy(lustre_msg_buf(req->rq_reqmsg, nameoffset, namelen),
247                filename, namelen);
248
249         rc = mdc_getattr_common(exp, ea_size, 0, req);
250         if (rc != 0) {
251                 ptlrpc_req_finished (req);
252                 req = NULL;
253         }
254  out:
255         *request = req;
256         RETURN(rc);
257 }
258
259 static
260 int mdc_xattr_common(struct obd_export *exp, struct ll_fid *fid,
261                      int opcode, obd_valid valid, const char *xattr_name,
262                      const char *input, int input_size, int output_size,
263                      int flags, struct ptlrpc_request **request)
264 {
265         struct obd_device *obddev = class_exp2obd(exp);
266         struct ptlrpc_request *req;
267         __u32 size[5] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
268                         [REQ_REC_OFF] = sizeof(struct mdt_body),
269                         [REQ_REC_OFF + 1] = 0, /* capa */
270                         [REQ_REC_OFF + 2] = 0, /* name */
271                         [REQ_REC_OFF + 3] = 0 };
272         int rc, xattr_namelen = 0, bufcnt = 2, offset = REQ_REC_OFF + 1;
273         void *tmp;
274         ENTRY;
275
276         if (mdc_exp_is_2_0_server(exp)) {
277                 bufcnt++;
278                 offset++;
279                 if (opcode == MDS_SETXATTR) {
280                         size[REQ_REC_OFF] = sizeof (struct mdt_rec_setxattr);
281                         opcode = MDS_REINT;
282                 }
283         }
284
285         if (xattr_name) {
286                 xattr_namelen = strlen(xattr_name) + 1;
287                 size[bufcnt++] = xattr_namelen;
288         }
289         if (input_size) {
290                 LASSERT(input);
291                 size[bufcnt++] = input_size;
292         }
293
294         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
295                               opcode, bufcnt, size, NULL);
296         if (!req)
297                 GOTO(out, rc = -ENOMEM);
298
299         req->rq_export = class_export_get(exp);
300
301         if (opcode == MDS_REINT && mdc_exp_is_2_0_server(exp)) {
302                 struct mdt_rec_setxattr *rec;
303                 rec = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
304                                      sizeof(struct mdt_rec_setxattr));
305                 rec->sx_opcode = REINT_SETXATTR;
306                 rec->sx_fsuid  = cfs_curproc_fsuid();
307                 rec->sx_fsgid  = cfs_curproc_fsgid();
308                 rec->sx_cap    = cfs_curproc_cap_pack();
309                 rec->sx_suppgid1 = -1;
310                 rec->sx_suppgid2 = -1;
311                 rec->sx_fid    = *((struct lu_fid*)fid);
312                 rec->sx_valid  = valid;
313                 rec->sx_size   = output_size;
314                 rec->sx_flags  = flags;
315         } else {
316                 /* request data */
317                 mdc_pack_req_body(req, REQ_REC_OFF, valid, fid, output_size, flags);
318         }
319
320         if (xattr_name) {
321                 tmp = lustre_msg_buf(req->rq_reqmsg, offset++, xattr_namelen);
322                 memcpy(tmp, xattr_name, xattr_namelen);
323         }
324         if (input_size) {
325                 tmp = lustre_msg_buf(req->rq_reqmsg, offset++, input_size);
326                 memcpy(tmp, input, input_size);
327         }
328
329         size[REPLY_REC_OFF] = sizeof(struct mdt_body);
330         if (mdc_exp_is_2_0_server(exp)) {
331                 bufcnt = 2;
332         } else {
333                 /* reply buffers */
334                 if (opcode == MDS_GETXATTR) {
335                         bufcnt = 2;
336                 } else {
337                         bufcnt = 1;
338                 }
339
340         }
341
342         /* we do this even output_size is 0, because server is doing that */
343         size[bufcnt++] = output_size;
344         ptlrpc_req_set_repsize(req, bufcnt, size);
345
346         /* make rpc */
347         if (opcode == MDS_SETXATTR || opcode == MDS_REINT)
348                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
349         else
350                 mdc_enter_request(&obddev->u.cli);
351
352         rc = ptlrpc_queue_wait(req);
353
354         if (opcode == MDS_SETXATTR || opcode == MDS_REINT)
355                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
356         else
357                 mdc_exit_request(&obddev->u.cli);
358
359         if (rc != 0)
360                 GOTO(err_out, rc);
361
362         if (opcode == MDS_GETXATTR) {
363                 struct mds_body * body = lustre_swab_repbuf(req, REPLY_REC_OFF,
364                                           sizeof(*body),
365                                           lustre_swab_mds_body);
366                 if (body == NULL) {
367                         CERROR ("Can't unpack mds_body\n");
368                         GOTO(err_out, rc = -EPROTO);
369                 }
370         }
371 out:
372         *request = req;
373         RETURN (rc);
374 err_out:
375         ptlrpc_req_finished(req);
376         req = NULL;
377         goto out;
378 }
379
380 int mdc_setxattr(struct obd_export *exp, struct ll_fid *fid,
381                  obd_valid valid, const char *xattr_name,
382                  const char *input, int input_size,
383                  int output_size, int flags,
384                  struct ptlrpc_request **request)
385 {
386         return mdc_xattr_common(exp, fid, MDS_SETXATTR, valid, xattr_name,
387                                 input, input_size, output_size, flags, request);
388 }
389
390 int mdc_getxattr(struct obd_export *exp, struct ll_fid *fid,
391                  obd_valid valid, const char *xattr_name,
392                  const char *input, int input_size,
393                  int output_size, struct ptlrpc_request **request)
394 {
395         return mdc_xattr_common(exp, fid, MDS_GETXATTR, valid, xattr_name,
396                                 input, input_size, output_size, 0, request);
397 }
398
399 /* For the fid-less server */
400 static void mdc_store_inode_generation_18(struct ptlrpc_request *req,
401                                           int reqoff, int repoff)
402 {
403         struct mds_rec_create *rec = lustre_msg_buf(req->rq_reqmsg, reqoff,
404                                                     sizeof(*rec));
405         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, repoff,
406                                                sizeof(*body));
407
408         LASSERT (rec != NULL);
409         LASSERT (body != NULL);
410
411         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
412         if (body->fid1.id == 0) {
413                 DEBUG_REQ(D_ERROR, req, "saving replay request with id = 0 "
414                           "gen = %u", body->fid1.generation);
415                 LBUG();
416         }
417
418         DEBUG_REQ(D_INODE, req, "storing generation %u for ino "LPU64,
419                   rec->cr_replayfid.generation, rec->cr_replayfid.id);
420 }
421
422 static void mdc_store_inode_generation_20(struct ptlrpc_request *req,
423                                           int reqoff, int repoff)
424 {
425         struct mdt_rec_create *rec = lustre_msg_buf(req->rq_reqmsg, reqoff,
426                                                     sizeof(*rec));
427         struct mdt_body *body = lustre_msg_buf(req->rq_repmsg, repoff,
428                                                sizeof(*body));
429
430         LASSERT (rec != NULL);
431         LASSERT (body != NULL);
432
433         rec->cr_fid2 = body->fid1;
434         rec->cr_ioepoch = body->ioepoch;
435         rec->cr_old_handle.cookie = body->handle.cookie;
436
437         if (!fid_is_sane(&body->fid1)) {
438                 DEBUG_REQ(D_ERROR, req, "saving replay request with"
439                           "insane fid");
440                 LBUG();
441         }
442
443         DEBUG_REQ(D_INODE, req, "storing generation %u for ino "LPU64,
444                   rec->cr_fid1.f_oid, rec->cr_fid2.f_seq);
445 }
446
447 /* This should be called with both the request and the reply still packed. */
448 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
449                                 int repoff)
450 {
451         if (mdc_req_is_2_0_server(req))
452                 mdc_store_inode_generation_20(req, reqoff, repoff);
453         else
454                 mdc_store_inode_generation_18(req, reqoff, repoff);
455 }
456
457 #ifdef CONFIG_FS_POSIX_ACL
458 static
459 int mdc_unpack_acl(struct obd_export *exp, struct ptlrpc_request *req,
460                    struct lustre_md *md, unsigned int offset)
461 {
462         struct mds_body  *body = md->body;
463         struct posix_acl *acl;
464         void             *buf;
465         int               rc;
466
467         if (!body->aclsize)
468                 return 0;
469
470         buf = lustre_msg_buf(req->rq_repmsg, offset, body->aclsize);
471         if (!buf) {
472                 CERROR("aclsize %u, bufcount %u, bufsize %u\n",
473                        body->aclsize, lustre_msg_bufcount(req->rq_repmsg),
474                        (lustre_msg_bufcount(req->rq_repmsg) <= offset) ?
475                                 -1 : lustre_msg_buflen(req->rq_repmsg, offset));
476                 return -EPROTO;
477         }
478
479         acl = posix_acl_from_xattr(buf, body->aclsize);
480         if (IS_ERR(acl)) {
481                 rc = PTR_ERR(acl);
482                 CERROR("convert xattr to acl: %d\n", rc);
483                 return rc;
484         }
485
486         rc = posix_acl_valid(acl);
487         if (rc) {
488                 CERROR("validate acl: %d\n", rc);
489                 posix_acl_release(acl);
490                 return rc;
491         }
492
493         md->posix_acl = acl;
494         return 0;
495 }
496 #else
497 #define mdc_unpack_acl(exp, req, md, offset) 0
498 #endif
499
500 int mdc_req2lustre_md(struct ptlrpc_request *req, int offset,
501                       struct obd_export *exp,
502                       struct lustre_md *md)
503 {
504         int rc = 0;
505         int iop = mdc_req_is_2_0_server(req);
506         ENTRY;
507
508         LASSERT(md);
509         memset(md, 0, sizeof(*md));
510
511         md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
512         LASSERT (md->body != NULL);
513         LASSERT(lustre_rep_swabbed(req, offset));
514         offset++;
515
516         if (md->body->valid & OBD_MD_FLEASIZE) {
517                 int lmmsize;
518                 struct lov_mds_md *lmm;
519
520                 if (!S_ISREG(md->body->mode)) {
521                         CERROR("OBD_MD_FLEASIZE set, should be a regular file, "
522                                "but is not\n");
523                         GOTO(err_out, rc = -EPROTO);
524                 }
525
526                 if (md->body->eadatasize == 0) {
527                         CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
528                         GOTO(err_out, rc = -EPROTO);
529                 }
530                 lmmsize = md->body->eadatasize;
531                 lmm = lustre_msg_buf(req->rq_repmsg, offset, lmmsize);
532                 if (!lmm) {
533                         CERROR ("incorrect message: lmm == 0\n");
534                         GOTO(err_out, rc = -EPROTO);
535                 }
536                 LASSERT(lustre_rep_swabbed(req, offset));
537
538                 rc = obd_unpackmd(exp, &md->lsm, lmm, lmmsize);
539                 if (rc < 0)
540                         GOTO(err_out, rc);
541
542                 if (rc < sizeof(*md->lsm)) {
543                         CERROR ("lsm size too small:  rc < sizeof (*md->lsm) "
544                                 "(%d < %d)\n", rc, (int)sizeof(*md->lsm));
545                         GOTO(err_out, rc = -EPROTO);
546                 }
547                 rc = 0;
548
549                 if (!iop)
550                         offset++;
551         } else if (md->body->valid & OBD_MD_FLDIREA) {
552                 if(!S_ISDIR(md->body->mode)) {
553                         CERROR("OBD_MD_FLDIREA set, should be a directory, but "
554                                "is not\n");
555                         GOTO(err_out, rc = -EPROTO);
556                 }
557                 if (!iop)
558                         offset++;
559         }
560         if (iop)
561                 offset++;
562
563         /* for ACL, it's possible that FLACL is set but aclsize is zero.
564          * only when aclsize != 0 there's an actual segment for ACL in
565          * reply buffer.
566          */
567         if ((md->body->valid & OBD_MD_FLACL) && md->body->aclsize) {
568                 rc = mdc_unpack_acl(exp, req, md, offset);
569                 if (rc)
570                         GOTO(err_out, rc);
571                 offset++;
572         }
573 out:
574         RETURN(rc);
575
576 err_out:
577         if (md->lsm)
578                 obd_free_memmd(exp, &md->lsm);
579         goto out;
580 }
581
582 void mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
583 {
584         if (md->lsm)
585                 obd_free_memmd(exp, &md->lsm);
586
587 #ifdef CONFIG_FS_POSIX_ACL
588         if (md->posix_acl) {
589                 posix_acl_release(md->posix_acl);
590                 md->posix_acl = NULL;
591         }
592 #endif
593 }
594
595 static void mdc_commit_open(struct ptlrpc_request *req)
596 {
597         struct mdc_open_data *mod = req->rq_cb_data;
598         if (mod == NULL)
599                 return;
600
601         if (mod->mod_och != NULL)
602                 mod->mod_och->och_mod = NULL;
603
604         OBD_FREE_PTR(mod);
605         req->rq_cb_data = NULL;
606 }
607
608 static void mdc_replay_open(struct ptlrpc_request *req)
609 {
610         struct mdc_open_data *mod = req->rq_cb_data;
611         struct obd_client_handle *och;
612         struct ptlrpc_request *close_req;
613         struct lustre_handle old;
614         struct mds_body *body;
615         ENTRY;
616
617         body = lustre_swab_repbuf(req, DLM_REPLY_REC_OFF, sizeof(*body),
618                                   lustre_swab_mds_body);
619         LASSERT (body != NULL);
620
621         if (mod == NULL) {
622                 DEBUG_REQ(D_ERROR, req,
623                           "can't properly replay without open data");
624                 EXIT;
625                 return;
626         }
627         DEBUG_REQ(D_HA, req, "mdc open data found");
628
629         och = mod->mod_och;
630         if (och != NULL) {
631                 struct lustre_handle *file_fh;
632                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
633                 file_fh = &och->och_fh;
634                 CDEBUG(D_RPCTRACE, "updating handle from "LPX64" to "LPX64"\n",
635                        file_fh->cookie, body->handle.cookie);
636                 old = *file_fh;
637                 *file_fh = body->handle;
638         }
639
640         close_req = mod->mod_close_req;
641
642         if (close_req != NULL) {
643                 LASSERT(lustre_msg_get_opc(close_req->rq_reqmsg) == MDS_CLOSE);
644                 if (mdc_req_is_2_0_server(close_req)) {
645                         struct mdt_epoch *epoch = NULL;
646
647                         epoch = lustre_msg_buf(close_req->rq_reqmsg,
648                                                REQ_REC_OFF, sizeof(*epoch));
649                         LASSERT(epoch);
650                         if (och != NULL)
651                                 LASSERT(!memcmp(&old, &epoch->handle,
652                                         sizeof(old)));
653                         DEBUG_REQ(D_RPCTRACE, close_req,
654                                   "updating close with new fh");
655                         epoch->handle = body->handle;
656                  } else {
657                         struct mds_body *close_body = NULL;
658
659                         close_body = lustre_msg_buf(close_req->rq_reqmsg,
660                                                     REQ_REC_OFF,
661                                                     sizeof(*close_body));
662                         if (och != NULL)
663                                 LASSERT(!memcmp(&old, &close_body->handle,
664                                         sizeof(old)));
665                         DEBUG_REQ(D_RPCTRACE, close_req,
666                                   "updating close with new fh");
667                         close_body->handle = body->handle;
668                  }
669         }
670
671         EXIT;
672 }
673
674 static void mdc_set_open_replay_data_20(struct obd_client_handle *och,
675                                         struct ptlrpc_request *open_req)
676 {
677         struct mdc_open_data  *mod;
678         struct obd_import     *imp = open_req->rq_import;
679         struct mdt_rec_create *rec = lustre_msg_buf(open_req->rq_reqmsg,
680                                                     DLM_INTENT_REC_OFF,
681                                                     sizeof(*rec));
682         struct mdt_body       *body = lustre_msg_buf(open_req->rq_repmsg,
683                                                      DLM_REPLY_REC_OFF,
684                                                      sizeof(*body));
685
686         /* If request is not eligible for replay, just bail out */
687         if (!open_req->rq_replay)
688                 return;
689
690         /* incoming message in my byte order (it's been swabbed) */
691         LASSERT(rec != NULL);
692         LASSERT(lustre_rep_swabbed(open_req, DLM_REPLY_REC_OFF));
693         /* outgoing messages always in my byte order */
694         LASSERT(body != NULL);
695
696         /* Only if the import is replayable, we set replay_open data */
697         if (och && imp->imp_replayable) {
698                 OBD_ALLOC_PTR(mod);
699                 if (mod == NULL) {
700                         DEBUG_REQ(D_ERROR, open_req,
701                                   "can't allocate mdc_open_data");
702                         return;
703                 }
704
705                 spin_lock(&open_req->rq_lock);
706                 och->och_mod = mod;
707                 mod->mod_och = och;
708                 mod->mod_open_req = open_req;
709                 open_req->rq_cb_data = mod;
710                 open_req->rq_commit_cb = mdc_commit_open;
711                 spin_unlock(&open_req->rq_lock);
712         }
713
714         rec->cr_fid2 = body->fid1;
715         rec->cr_ioepoch = body->ioepoch;
716         rec->cr_old_handle.cookie = body->handle.cookie;
717         open_req->rq_replay_cb = mdc_replay_open;
718         if (!fid_is_sane(&body->fid1)) {
719                 DEBUG_REQ(D_ERROR, open_req, "saving replay request with "
720                           "insane fid");
721                 LBUG();
722         }
723
724         DEBUG_REQ(D_RPCTRACE, open_req, "set up replay data");
725 }
726
727 static void mdc_set_open_replay_data_18(struct obd_client_handle *och,
728                                         struct ptlrpc_request *open_req)
729 {
730         struct mdc_open_data *mod;
731         struct mds_rec_create *rec = lustre_msg_buf(open_req->rq_reqmsg,
732                                                     DLM_INTENT_REC_OFF,
733                                                     sizeof(*rec));
734         struct mds_body *body = lustre_msg_buf(open_req->rq_repmsg,
735                                                DLM_REPLY_REC_OFF,
736                                                sizeof(*body));
737
738         /* If request is not eligible for replay, just bail out */
739         if (!open_req->rq_replay)
740                 return;
741
742         /* incoming message in my byte order (it's been swabbed) */
743         LASSERT(rec != NULL);
744         LASSERT(lustre_rep_swabbed(open_req, DLM_REPLY_REC_OFF));
745         /* outgoing messages always in my byte order */
746         LASSERT(body != NULL);
747
748         if (och) {
749                 OBD_ALLOC(mod, sizeof(*mod));
750                 if (mod == NULL) {
751                         DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
752                         return;
753                 }
754
755                 spin_lock(&open_req->rq_lock);
756                 och->och_mod = mod;
757                 mod->mod_och = och;
758                 mod->mod_open_req = open_req;
759                 open_req->rq_cb_data = mod;
760                 open_req->rq_commit_cb = mdc_commit_open;
761                 spin_unlock(&open_req->rq_lock);
762         }
763
764         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
765         open_req->rq_replay_cb = mdc_replay_open;
766         if (body->fid1.id == 0) {
767                 DEBUG_REQ(D_ERROR, open_req, "saving replay request with "
768                           "id = 0 gen = %u", body->fid1.generation);
769                 LBUG();
770         }
771
772         DEBUG_REQ(D_RPCTRACE, open_req, "set up replay data");
773 }
774
775 void mdc_set_open_replay_data(struct obd_client_handle *och,
776                               struct ptlrpc_request *open_req)
777 {
778         if (mdc_req_is_2_0_server(open_req))
779                 mdc_set_open_replay_data_20(och, open_req);
780         else
781                 mdc_set_open_replay_data_18(och, open_req);
782 }
783
784 void mdc_clear_open_replay_data(struct obd_client_handle *och)
785 {
786         struct mdc_open_data *mod = och->och_mod;
787
788         /* Don't free the structure now (it happens in mdc_commit_open, after
789          * we're sure we won't need to fix up the close request in the future),
790          * but make sure that replay doesn't poke at the och, which is about to
791          * be freed. */
792         LASSERT(mod != LP_POISON);
793         if (mod != NULL)
794                 mod->mod_och = NULL;
795         och->och_mod = NULL;
796 }
797
798 int mdc_close(struct obd_export *exp, struct mdc_op_data *data, struct obdo *oa,
799               struct obd_client_handle *och, struct ptlrpc_request **request)
800 {
801         struct obd_device *obd = class_exp2obd(exp);
802         __u32 reqsize[4] = { sizeof(struct ptlrpc_body),
803                              sizeof(struct mdt_body) };
804         __u32 repsize[6] = { sizeof(struct ptlrpc_body),
805                              sizeof(struct mdt_body),
806                              obd->u.cli.cl_max_mds_easize,
807                              obd->u.cli.cl_max_mds_cookiesize,
808                              sizeof(struct lustre_capa),
809                              sizeof(struct lustre_capa) };
810         int rc;
811         struct ptlrpc_request *req;
812         int bufcount = 2;
813         ENTRY;
814
815         if (mdc_exp_is_2_0_server(exp)) {
816                 reqsize[1] = sizeof(struct mdt_epoch);
817                 reqsize[2] = sizeof(struct mdt_rec_create);
818                 reqsize[3] = 0; /* capa */
819                 bufcount = 4;
820         }
821         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
822                               MDS_CLOSE, bufcount, reqsize, NULL);
823         if (req == NULL)
824                 GOTO(out, rc = -ENOMEM);
825         req->rq_export = class_export_get(exp);
826
827         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
828          * portal whose threads are not taking any DLM locks and are therefore
829          * always progressing */
830         req->rq_request_portal = MDS_READPAGE_PORTAL;
831         ptlrpc_at_set_req_timeout(req);
832
833         /* Ensure that this close's handle is fixed up during replay. */
834         LASSERT(och != NULL);
835         LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
836         if (likely(och->och_mod != NULL)) {
837                 struct ptlrpc_request *open_req = och->och_mod->mod_open_req;
838
839                 if (open_req->rq_type == LI_POISON) {
840                         CERROR("LBUG POISONED open %p!\n", open_req);
841                         LBUG();
842                         ptlrpc_req_finished(req);
843                         req = NULL;
844                         GOTO(out, rc = -EIO);
845                 }
846                 och->och_mod->mod_close_req = req;
847                 DEBUG_REQ(D_RPCTRACE, req, "close req");
848                 DEBUG_REQ(D_RPCTRACE, open_req, "clear open replay");
849
850                 /* We no longer want to preserve this open for replay even
851                  * though the open was committed. b=3632, b=3633 */
852                 spin_lock(&open_req->rq_lock);
853                 open_req->rq_replay = 0;
854                 spin_unlock(&open_req->rq_lock);
855         } else {
856                 CDEBUG(D_RPCTRACE, "couldn't find open req; expecting error\n");
857         }
858
859         mdc_close_pack(req, REQ_REC_OFF, data, oa, oa->o_valid, och);
860
861         ptlrpc_req_set_repsize(req, 6, repsize);
862
863         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
864         rc = ptlrpc_queue_wait(req);
865         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
866
867         if (req->rq_repmsg == NULL) {
868                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
869                        req->rq_status);
870                 if (rc == 0)
871                         rc = req->rq_status ? req->rq_status : -EIO;
872         } else if (rc == 0) {
873                 rc = lustre_msg_get_status(req->rq_repmsg);
874                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
875                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
876                                   "= %d", rc);
877                         if (rc > 0)
878                                 rc = -rc;
879                 }
880
881                 if (!lustre_swab_repbuf(req, REPLY_REC_OFF,
882                                         sizeof(struct mds_body),
883                                         lustre_swab_mds_body)) {
884                         CERROR("Error unpacking mds_body\n");
885                         rc = -EPROTO;
886                 }
887         }
888
889         EXIT;
890         *request = req;
891  out:
892         if (rc != 0 && och->och_mod)
893                  och->och_mod->mod_close_req = NULL;
894
895         return rc;
896 }
897
898 int mdc_done_writing(struct obd_export *exp, struct mdc_op_data *data,
899                      struct obdo *obdo)
900 {
901         struct ptlrpc_request *req;
902         struct mds_body *body;
903         __u32 size[2] = { sizeof(struct ptlrpc_body),
904                           sizeof(struct mdt_body) };
905         int rc;
906         ENTRY;
907
908         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
909                               MDS_DONE_WRITING, 2, size, NULL);
910         if (req == NULL)
911                 RETURN(-ENOMEM);
912
913         req->rq_export = class_export_get(exp);
914         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
915         body->fid1 = data->fid1;
916         body->size = obdo->o_size;
917         body->blocks = obdo->o_blocks;
918         body->flags = obdo->o_flags;
919         body->valid = obdo->o_valid;
920 //        memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
921
922         ptlrpc_req_set_repsize(req, 2, size);
923
924         rc = ptlrpc_queue_wait(req);
925         ptlrpc_req_finished(req);
926         RETURN(rc);
927 }
928
929 int mdc_readpage(struct obd_export *exp, struct ll_fid *fid, __u64 offset,
930                  struct page *page, struct ptlrpc_request **request)
931 {
932         struct obd_import *imp = class_exp2cliimp(exp);
933         struct ptlrpc_request *req = NULL;
934         struct ptlrpc_bulk_desc *desc = NULL;
935         struct mds_body *body;
936         __u32 size[2] = { sizeof(struct ptlrpc_body),
937                           sizeof(struct mdt_body) };
938         int rc;
939         ENTRY;
940
941         CDEBUG(D_INODE, "inode: "LPU64"\n", fid->id);
942
943         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE, 2, size,
944                               NULL);
945         if (req == NULL)
946                 GOTO(out, rc = -ENOMEM);
947
948         req->rq_export = class_export_get(exp);
949         req->rq_request_portal = MDS_READPAGE_PORTAL;
950         ptlrpc_at_set_req_timeout(req);
951
952         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
953         if (desc == NULL)
954                 GOTO(out, rc = -ENOMEM);
955         /* NB req now owns desc and will free it when it gets freed */
956
957         ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
958
959         mdc_readdir_pack(req, REQ_REC_OFF, offset, CFS_PAGE_SIZE, fid);
960
961         ptlrpc_req_set_repsize(req, 2, size);
962         rc = ptlrpc_queue_wait(req);
963
964         if (rc == 0) {
965                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
966                                           lustre_swab_mds_body);
967                 if (body == NULL) {
968                         CERROR("Can't unpack mds_body\n");
969                         GOTO(out, rc = -EPROTO);
970                 }
971
972                 if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
973                         CERROR ("Unexpected # bytes transferred: %d"
974                                 " (%lu expected)\n",
975                                 req->rq_bulk->bd_nob_transferred,
976                                 CFS_PAGE_SIZE);
977                         GOTO (out, rc = -EPROTO);
978                 }
979         }
980
981         EXIT;
982  out:
983         *request = req;
984         return rc;
985 }
986
987
988 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
989                          void *karg, void *uarg)
990 {
991         struct obd_device *obd = exp->exp_obd;
992         struct obd_ioctl_data *data = karg;
993         struct obd_import *imp = obd->u.cli.cl_import;
994         struct llog_ctxt *ctxt;
995         int rc;
996         ENTRY;
997
998         if (!try_module_get(THIS_MODULE)) {
999                 CERROR("Can't get module. Is it alive?");
1000                 return -EINVAL;
1001         }
1002         switch (cmd) {
1003         case OBD_IOC_CLIENT_RECOVER:
1004                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
1005                 if (rc < 0)
1006                         GOTO(out, rc);
1007                 GOTO(out, rc = 0);
1008         case IOC_OSC_SET_ACTIVE:
1009                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1010                 GOTO(out, rc);
1011         case OBD_IOC_PARSE: {
1012                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1013                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
1014                 llog_ctxt_put(ctxt);
1015                 GOTO(out, rc);
1016         }
1017 #ifdef __KERNEL__
1018         case OBD_IOC_LLOG_INFO:
1019         case OBD_IOC_LLOG_PRINT: {
1020                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1021                 rc = llog_ioctl(ctxt, cmd, data);
1022                 llog_ctxt_put(ctxt);
1023                 GOTO(out, rc);
1024         }
1025 #endif
1026         case OBD_IOC_POLL_QUOTACHECK:
1027                 rc = lquota_poll_check(quota_interface, exp,
1028                                        (struct if_quotacheck *)karg);
1029                 GOTO(out, rc);
1030         case OBD_IOC_PING_TARGET:
1031                 rc = ptlrpc_obd_ping(obd);
1032                 GOTO(out, rc);
1033         default:
1034                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1035                 GOTO(out, rc = -ENOTTY);
1036         }
1037 out:
1038         module_put(THIS_MODULE);
1039         return rc;
1040 }
1041
1042 int mdc_set_info_async(struct obd_export *exp, obd_count keylen,
1043                        void *key, obd_count vallen, void *val,
1044                        struct ptlrpc_request_set *set)
1045 {
1046         struct obd_import *imp = class_exp2cliimp(exp);
1047         int rc = -EINVAL;
1048
1049         if (KEY_IS(KEY_INIT_RECOV)) {
1050                 if (vallen != sizeof(int))
1051                         RETURN(-EINVAL);
1052                 spin_lock(&imp->imp_lock);
1053                 imp->imp_initial_recov = *(int *)val;
1054                 spin_unlock(&imp->imp_lock);
1055
1056                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
1057                        exp->exp_obd->obd_name, imp->imp_initial_recov);
1058                 RETURN(0);
1059         }
1060         /* Turn off initial_recov after we try all backup servers once */
1061         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1062                 if (vallen != sizeof(int))
1063                         RETURN(-EINVAL);
1064
1065                 spin_lock(&imp->imp_lock);
1066                 imp->imp_initial_recov_bk = *(int *)val;
1067                 if (imp->imp_initial_recov_bk)
1068                         imp->imp_initial_recov = 1;
1069                 spin_unlock(&imp->imp_lock);
1070
1071                 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
1072                        exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
1073                 RETURN(0);
1074         }
1075         /* Accept the broken "read-only" key for 1.6.6 servers. b=17493 */
1076         if (KEY_IS(KEY_READONLY) || KEY_IS(KEY_READONLY_166COMPAT)) {
1077                 struct ptlrpc_request *req;
1078                 __u32 size[3] = { sizeof(struct ptlrpc_body), keylen, vallen };
1079                 char *bufs[3] = { NULL, key, val };
1080
1081                 if (vallen != sizeof(int))
1082                         RETURN(-EINVAL);
1083
1084                 if (*((int *)val)) {
1085                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
1086                         imp->imp_connect_data.ocd_connect_flags |=
1087                                 OBD_CONNECT_RDONLY;
1088                 } else {
1089                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
1090                         imp->imp_connect_data.ocd_connect_flags &=
1091                                 ~OBD_CONNECT_RDONLY;
1092                 }
1093
1094                 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_SET_INFO,
1095                                       3, size, bufs);
1096                 if (req == NULL)
1097                         RETURN(-ENOMEM);
1098
1099                 req->rq_export = class_export_get(exp);
1100                 ptlrpc_req_set_repsize(req, 1, NULL);
1101                 if (set) {
1102                         rc = 0;
1103                         ptlrpc_set_add_req(set, req);
1104                         ptlrpc_check_set(set);
1105                 } else {
1106                         rc = ptlrpc_queue_wait(req);
1107                         ptlrpc_req_finished(req);
1108                 }
1109
1110                 RETURN(rc);
1111         }
1112
1113         RETURN(rc);
1114 }
1115
1116 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1117                  __u32 *vallen, void *val, struct lov_stripe_md *lsm)
1118 {
1119         int rc = -EINVAL;
1120
1121         if (KEY_IS(KEY_MAX_EASIZE)) {
1122                 int mdsize, *max_easize;
1123
1124                 if (*vallen != sizeof(int))
1125                         RETURN(-EINVAL);
1126                 mdsize = *(int*)val;
1127                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
1128                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
1129                 max_easize = val;
1130                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
1131                 RETURN(0);
1132         }
1133         RETURN(rc);
1134 }
1135
1136 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1137                       __u64 max_age, __u32 flags)
1138 {
1139         struct ptlrpc_request *req;
1140         struct obd_statfs *msfs;
1141         struct obd_import     *imp = NULL;
1142         __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*msfs) };
1143         int rc;
1144         ENTRY;
1145
1146         /*Since the request might also come from lprocfs, so we need
1147          *sync this with client_disconnect_export Bug15684*/
1148         down_read(&obd->u.cli.cl_sem);
1149         if (obd->u.cli.cl_import)
1150                 imp = class_import_get(obd->u.cli.cl_import);
1151         up_read(&obd->u.cli.cl_sem);
1152         if (!imp)
1153                 RETURN(-ENODEV);
1154
1155
1156         /* We could possibly pass max_age in the request (as an absolute
1157          * timestamp or a "seconds.usec ago") so the target can avoid doing
1158          * extra calls into the filesystem if that isn't necessary (e.g.
1159          * during mount that would help a bit).  Having relative timestamps
1160          * is not so great if request processing is slow, while absolute
1161          * timestamps are not ideal because they need time synchronization. */
1162         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_STATFS, 1, NULL,
1163                               NULL);
1164         if (!req)
1165                 GOTO(output, rc = -ENOMEM);
1166
1167         ptlrpc_req_set_repsize(req, 2, size);
1168
1169         if (flags & OBD_STATFS_NODELAY) {
1170                 /* procfs requests not want stay in wait for avoid deadlock */
1171                 req->rq_no_resend = 1;
1172                 req->rq_no_delay = 1;
1173         }
1174
1175         rc = ptlrpc_queue_wait(req);
1176
1177         if (rc)
1178                 GOTO(out, rc);
1179
1180         msfs = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*msfs),
1181                                   lustre_swab_obd_statfs);
1182         if (msfs == NULL) {
1183                 CERROR("Can't unpack obd_statfs\n");
1184                 GOTO(out, rc = -EPROTO);
1185         }
1186
1187         memcpy(osfs, msfs, sizeof(*msfs));
1188         EXIT;
1189 out:
1190         ptlrpc_req_finished(req);
1191 output:
1192         class_import_put(imp);
1193         return rc;
1194 }
1195
1196 static int mdc_pin(struct obd_export *exp, struct ll_fid *fid,
1197                    struct obd_client_handle *handle, int flag)
1198 {
1199         struct ptlrpc_request *req;
1200         struct mds_body *body;
1201         __u32 size[3] = { sizeof(struct ptlrpc_body),
1202                           sizeof(struct mdt_body), 0 };
1203         int rc;
1204         int bufcount = 2;
1205         ENTRY;
1206
1207         if (mdc_exp_is_2_0_server(exp))
1208                 bufcount = 3;
1209         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1210                               MDS_PIN, bufcount, size, NULL);
1211         if (req == NULL)
1212                 RETURN(-ENOMEM);
1213
1214         req->rq_export = class_export_get(exp);
1215         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
1216         body->fid1 = *fid;
1217         body->flags = flag;
1218
1219         ptlrpc_req_set_repsize(req, 2, size);
1220
1221         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1222         rc = ptlrpc_queue_wait(req);
1223         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1224         if (rc) {
1225                 CERROR("pin failed: %d\n", rc);
1226                 ptlrpc_req_finished(req);
1227                 RETURN(rc);
1228         }
1229
1230         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
1231                                   lustre_swab_mds_body);
1232         if (body == NULL) {
1233                 ptlrpc_req_finished(req);
1234                 RETURN(rc);
1235         }
1236
1237         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
1238         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1239
1240         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
1241         if (handle->och_mod == NULL) {
1242                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
1243                 RETURN(-ENOMEM);
1244         }
1245         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1246
1247         RETURN(rc);
1248 }
1249
1250 static int mdc_unpin(struct obd_export *exp,
1251                      struct obd_client_handle *handle, int flag)
1252 {
1253         struct ptlrpc_request *req;
1254         struct mds_body *body;
1255         __u32 size[2] = { sizeof(struct ptlrpc_body),
1256                           sizeof(struct mdt_body) };
1257         int rc;
1258         ENTRY;
1259
1260         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
1261                 RETURN(0);
1262
1263         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1264                               MDS_CLOSE, 2, size, NULL);
1265         if (req == NULL)
1266                 RETURN(-ENOMEM);
1267
1268         req->rq_export = class_export_get(exp);
1269         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
1270         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
1271         body->flags = flag;
1272
1273         ptlrpc_req_set_repsize(req, 1, NULL);
1274         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1275         rc = ptlrpc_queue_wait(req);
1276         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1277
1278         if (rc != 0)
1279                 CERROR("unpin failed: %d\n", rc);
1280
1281         ptlrpc_req_finished(req);
1282         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1283         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1284         RETURN(rc);
1285 }
1286
1287 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
1288              struct ptlrpc_request **request)
1289 {
1290         struct ptlrpc_request *req;
1291         __u32 size[3] = { sizeof(struct ptlrpc_body),
1292                           sizeof(struct mdt_body), 0 };
1293         int bufcount = 2;
1294         int rc;
1295         ENTRY;
1296
1297
1298         if (mdc_exp_is_2_0_server(exp))
1299                 bufcount = 3;
1300         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1301                               MDS_SYNC, bufcount, size, NULL);
1302         if (!req)
1303                 RETURN(rc = -ENOMEM);
1304
1305         req->rq_export = class_export_get(exp);
1306         mdc_pack_req_body(req, REQ_REC_OFF, 0, fid, 0, 0);
1307
1308         ptlrpc_req_set_repsize(req, 2, size);
1309
1310         rc = ptlrpc_queue_wait(req);
1311         if (rc || request == NULL)
1312                 ptlrpc_req_finished(req);
1313         else
1314                 *request = req;
1315
1316         RETURN(rc);
1317 }
1318
1319 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1320                             enum obd_import_event event)
1321 {
1322         int rc = 0;
1323
1324         LASSERT(imp->imp_obd == obd);
1325
1326         switch (event) {
1327         case IMP_EVENT_DISCON: {
1328                 ptlrpc_import_setasync(imp, -obd->obd_namespace->ns_max_unused);
1329                 break;
1330         }
1331         case IMP_EVENT_INACTIVE: {
1332                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1333                 break;
1334         }
1335         case IMP_EVENT_INVALIDATE: {
1336                 struct ldlm_namespace *ns = obd->obd_namespace;
1337
1338                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1339
1340                 break;
1341         }
1342         case IMP_EVENT_ACTIVE: {
1343                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1344                 break;
1345         }
1346         case IMP_EVENT_OCD:
1347                 ptlrpc_import_setasync(imp, obd->obd_namespace->ns_max_unused);
1348                 break;
1349
1350         default:
1351                 CERROR("Unknown import event %x\n", event);
1352                 LBUG();
1353         }
1354         RETURN(rc);
1355 }
1356
1357 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1358 {
1359         struct client_obd *cli = &obd->u.cli;
1360         struct lprocfs_static_vars lvars = { 0 };
1361         int rc;
1362         ENTRY;
1363
1364         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1365         if (!cli->cl_rpc_lock)
1366                 RETURN(-ENOMEM);
1367         mdc_init_rpc_lock(cli->cl_rpc_lock);
1368
1369         ptlrpcd_addref();
1370
1371         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1372         if (!cli->cl_setattr_lock)
1373                 GOTO(err_rpc_lock, rc = -ENOMEM);
1374         mdc_init_rpc_lock(cli->cl_setattr_lock);
1375
1376         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1377         if (!cli->cl_close_lock)
1378                 GOTO(err_setattr_lock, rc = -ENOMEM);
1379         mdc_init_rpc_lock(cli->cl_close_lock);
1380
1381         rc = client_obd_setup(obd, len, buf);
1382         if (rc)
1383                 GOTO(err_close_lock, rc);
1384         lprocfs_mdc_init_vars(&lvars);
1385         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0)
1386                 ptlrpc_lprocfs_register_obd(obd);
1387
1388         rc = obd_llog_init(obd, obd, NULL);
1389         if (rc) {
1390                 mdc_cleanup(obd);
1391                 CERROR("failed to setup llogging subsystems\n");
1392         }
1393
1394         RETURN(rc);
1395
1396 err_close_lock:
1397         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1398 err_setattr_lock:
1399         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1400 err_rpc_lock:
1401         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1402         ptlrpcd_decref();
1403         RETURN(rc);
1404 }
1405
1406 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
1407  * us to make MDS RPCs with large enough reply buffers to hold the
1408  * maximum-sized (= maximum striped) EA and cookie without having to
1409  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1410 int mdc_init_ea_size(struct obd_export *mdc_exp, struct obd_export *lov_exp)
1411 {
1412         struct obd_device *obd = mdc_exp->exp_obd;
1413         struct client_obd *cli = &obd->u.cli;
1414         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC_V3 };
1415         struct lov_desc desc;
1416         __u32 valsize = sizeof(desc);
1417         __u32 stripes;
1418         int rc, size;
1419         ENTRY;
1420
1421         rc = obd_get_info(lov_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
1422                           &valsize, &desc, NULL);
1423         if (rc)
1424                 RETURN(rc);
1425
1426         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1427         lsm.lsm_stripe_count = stripes;
1428         size = obd_size_diskmd(lov_exp, &lsm);
1429
1430         if (cli->cl_max_mds_easize < size)
1431                 cli->cl_max_mds_easize = size;
1432
1433         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1434         size = obd_size_diskmd(lov_exp, &lsm);
1435
1436         if (cli->cl_default_mds_easize < size)
1437                 cli->cl_default_mds_easize = size;
1438
1439         size = stripes * sizeof(struct llog_cookie);
1440         if (cli->cl_max_mds_cookiesize < size)
1441                 cli->cl_max_mds_cookiesize = size;
1442
1443         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1444                cli->cl_max_mds_easize, cli->cl_max_mds_cookiesize);
1445
1446         RETURN(0);
1447 }
1448
1449 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1450 {
1451         int rc = 0;
1452         ENTRY;
1453
1454         switch (stage) {
1455         case OBD_CLEANUP_EARLY:
1456         case OBD_CLEANUP_EXPORTS:
1457                 /* If we set up but never connected, the
1458                    client import will not have been cleaned. */
1459                 down_write(&obd->u.cli.cl_sem);
1460                 if (obd->u.cli.cl_import) {
1461                         struct obd_import *imp;
1462                         imp = obd->u.cli.cl_import;
1463                         CERROR("client import never connected\n");
1464                         ptlrpc_invalidate_import(imp);
1465                         class_destroy_import(imp);
1466                         obd->u.cli.cl_import = NULL;
1467                 }
1468                 up_write(&obd->u.cli.cl_sem);
1469
1470                 rc = obd_llog_finish(obd, 0);
1471                 if (rc != 0)
1472                         CERROR("failed to cleanup llogging subsystems\n");
1473                 break;
1474         case OBD_CLEANUP_SELF_EXP:
1475                 break;
1476         case OBD_CLEANUP_OBD:
1477                 break;
1478         }
1479         RETURN(rc);
1480 }
1481
1482 static int mdc_cleanup(struct obd_device *obd)
1483 {
1484         struct client_obd *cli = &obd->u.cli;
1485
1486         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1487         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1488         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1489
1490         ptlrpc_lprocfs_unregister_obd(obd);
1491         lprocfs_obd_cleanup(obd);
1492         ptlrpcd_decref();
1493
1494         return client_obd_cleanup(obd);
1495 }
1496
1497
1498 static int mdc_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
1499                          int *index)
1500 {
1501         struct llog_ctxt *ctxt;
1502         int rc;
1503         ENTRY;
1504
1505         rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, disk_obd, 0, NULL,
1506                         &llog_client_ops);
1507         if (rc == 0) {
1508                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1509                 llog_initiator_connect(ctxt);
1510                 llog_ctxt_put(ctxt);
1511         }
1512
1513         rc = llog_setup(obd, LLOG_LOVEA_REPL_CTXT, disk_obd, 0, NULL,
1514                        &llog_client_ops);
1515         if (rc == 0) {
1516                 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1517                 llog_initiator_connect(ctxt);
1518                 llog_ctxt_put(ctxt);
1519         } else {
1520                 GOTO(err_cleanup, rc);
1521         }
1522
1523         RETURN(rc);
1524 err_cleanup:
1525         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1526         if (ctxt)
1527                 llog_cleanup(ctxt);
1528         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1529         if (ctxt)
1530                 llog_cleanup(ctxt);
1531         return rc;
1532 }
1533
1534 static int mdc_llog_finish(struct obd_device *obd, int count)
1535 {
1536         struct llog_ctxt *ctxt;
1537         int rc = 0;
1538         ENTRY;
1539
1540         ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1541         if (ctxt) {
1542                 rc = llog_cleanup(ctxt);
1543                 if (rc) {
1544                         CERROR("Can not cleanup LLOG_CONFIG_REPL_CTXT "
1545                                "rc %d\n", rc);
1546                 }
1547         }
1548         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1549         if (ctxt)
1550                 rc = llog_cleanup(ctxt);
1551         RETURN(rc);
1552 }
1553
1554 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1555 {
1556         struct lustre_cfg *lcfg = buf;
1557         struct lprocfs_static_vars lvars = { 0 };
1558         int rc = 0;
1559
1560         lprocfs_mdc_init_vars(&lvars);
1561
1562         rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars, lcfg, obd);
1563         return(rc);
1564 }
1565
1566 static int mdc_fid_init(struct obd_export *exp)
1567 {
1568         struct client_obd *cli;
1569         char              *prefix;
1570         int                rc;
1571         ENTRY;
1572
1573         cli = &exp->exp_obd->u.cli;
1574
1575         OBD_ALLOC_PTR(cli->cl_seq);
1576         if (cli->cl_seq == NULL)
1577                 RETURN(-ENOMEM);
1578
1579         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
1580         if (prefix == NULL)
1581                 GOTO(out_free_seq, rc = -ENOMEM);
1582
1583         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", exp->exp_obd->obd_name);
1584
1585         /* Init client side sequence-manager */
1586         rc = seq_client_init(cli->cl_seq, exp,
1587                              LUSTRE_SEQ_METADATA,
1588                              LUSTRE_SEQ_MAX_WIDTH,
1589                              prefix);
1590         OBD_FREE(prefix, MAX_OBD_NAME + 5);
1591         if (rc)
1592                 GOTO(out_free_seq, rc);
1593
1594         RETURN(rc);
1595
1596 out_free_seq:
1597         OBD_FREE_PTR(cli->cl_seq);
1598         cli->cl_seq = NULL;
1599         return rc;
1600 }
1601
1602 static int mdc_fid_fini(struct obd_export *exp)
1603 {
1604         struct client_obd *cli = &exp->exp_obd->u.cli;
1605         ENTRY;
1606
1607         if (cli->cl_seq != NULL) {
1608                 LASSERT(cli->cl_seq->lcs_exp == exp);
1609                 seq_client_fini(cli->cl_seq);
1610                 OBD_FREE_PTR(cli->cl_seq);
1611                 cli->cl_seq = NULL;
1612         }
1613
1614         RETURN(0);
1615 }
1616
1617 struct obd_ops mdc_obd_ops = {
1618         .o_owner        = THIS_MODULE,
1619         .o_setup        = mdc_setup,
1620         .o_precleanup   = mdc_precleanup,
1621         .o_cleanup      = mdc_cleanup,
1622         .o_add_conn     = client_import_add_conn,
1623         .o_del_conn     = client_import_del_conn,
1624         .o_connect      = client_connect_import,
1625         .o_disconnect   = client_disconnect_export,
1626         .o_fid_init     = mdc_fid_init,
1627         .o_fid_fini     = mdc_fid_fini,
1628         .o_iocontrol    = mdc_iocontrol,
1629         .o_set_info_async = mdc_set_info_async,
1630         .o_get_info     = mdc_get_info,
1631         .o_statfs       = mdc_statfs,
1632         .o_pin          = mdc_pin,
1633         .o_unpin        = mdc_unpin,
1634         .o_import_event = mdc_import_event,
1635         .o_llog_init    = mdc_llog_init,
1636         .o_llog_finish  = mdc_llog_finish,
1637         .o_process_config = mdc_process_config,
1638 };
1639
1640 int __init mdc_init(void)
1641 {
1642         int rc;
1643         struct lprocfs_static_vars lvars = { 0 };
1644         lprocfs_mdc_init_vars(&lvars);
1645         request_module("lquota");
1646         quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
1647         init_obd_quota_ops(quota_interface, &mdc_obd_ops);
1648
1649         rc = class_register_type(&mdc_obd_ops, lvars.module_vars,
1650                                  LUSTRE_MDC_NAME);
1651         if (rc && quota_interface)
1652                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1653
1654         RETURN(rc);
1655 }
1656
1657 #ifdef __KERNEL__
1658 static void /*__exit*/ mdc_exit(void)
1659 {
1660         if (quota_interface)
1661                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1662
1663         class_unregister_type(LUSTRE_MDC_NAME);
1664 }
1665
1666 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1667 MODULE_DESCRIPTION("Lustre Metadata Client");
1668 MODULE_LICENSE("GPL");
1669
1670 EXPORT_SYMBOL(mdc_req2lustre_md);
1671 EXPORT_SYMBOL(mdc_free_lustre_md);
1672 EXPORT_SYMBOL(mdc_change_cbdata);
1673 EXPORT_SYMBOL(mdc_getstatus);
1674 EXPORT_SYMBOL(mdc_getattr);
1675 EXPORT_SYMBOL(mdc_getattr_name);
1676 EXPORT_SYMBOL(mdc_create);
1677 EXPORT_SYMBOL(mdc_unlink);
1678 EXPORT_SYMBOL(mdc_rename);
1679 EXPORT_SYMBOL(mdc_link);
1680 EXPORT_SYMBOL(mdc_readpage);
1681 EXPORT_SYMBOL(mdc_setattr);
1682 EXPORT_SYMBOL(mdc_close);
1683 EXPORT_SYMBOL(mdc_done_writing);
1684 EXPORT_SYMBOL(mdc_sync);
1685 EXPORT_SYMBOL(mdc_set_open_replay_data);
1686 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1687 EXPORT_SYMBOL(mdc_store_inode_generation);
1688 EXPORT_SYMBOL(mdc_init_ea_size);
1689 EXPORT_SYMBOL(mdc_getxattr);
1690 EXPORT_SYMBOL(mdc_setxattr);
1691
1692 module_init(mdc_init);
1693 module_exit(mdc_exit);
1694 #endif