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