Whamcloud - gitweb
b=11546
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 # include <linux/module.h>
32 # include <linux/pagemap.h>
33 # include <linux/miscdevice.h>
34 # include <linux/init.h>
35 #else
36 # include <liblustre.h>
37 #endif
38
39 #include <obd_class.h>
40 #include <lustre_dlm.h>
41 #include <lprocfs_status.h>
42 #include <lustre_param.h>
43 #include "mdc_internal.h"
44
45 static quota_interface_t *quota_interface;
46
47 #define REQUEST_MINOR 244
48
49 static quota_interface_t *quota_interface;
50 extern quota_interface_t mdc_quota_interface;
51
52 static int mdc_cleanup(struct obd_device *obd);
53
54 extern int mds_queue_req(struct ptlrpc_request *);
55 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
56 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
57 static int send_getstatus(struct obd_import *imp, struct ll_fid *rootfid,
58                           int level, int msg_flags)
59 {
60         struct ptlrpc_request *req;
61         struct mds_body *body;
62         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
63         ENTRY;
64
65         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_GETSTATUS, 2, size,
66                               NULL);
67         if (!req)
68                 GOTO(out, rc = -ENOMEM);
69
70         req->rq_send_state = level;
71         ptlrpc_req_set_repsize(req, 2, size);
72
73         mdc_pack_req_body(req, REQ_REC_OFF, 0, NULL, 0, 0);
74         lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
75         rc = ptlrpc_queue_wait(req);
76
77         if (!rc) {
78                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
79                                           lustre_swab_mds_body);
80                 if (body == NULL) {
81                         CERROR ("Can't extract mds_body\n");
82                         GOTO (out, rc = -EPROTO);
83                 }
84
85                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
86
87                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
88                        ", last_xid="LPU64"\n",
89                        rootfid->id,
90                        lustre_msg_get_last_committed(req->rq_repmsg),
91                        lustre_msg_get_last_xid(req->rq_repmsg));
92         }
93
94         EXIT;
95  out:
96         ptlrpc_req_finished(req);
97         return rc;
98 }
99
100 /* This should be mdc_get_info("rootfid") */
101 int mdc_getstatus(struct obd_export *exp, struct ll_fid *rootfid)
102 {
103         return send_getstatus(class_exp2cliimp(exp), rootfid, LUSTRE_IMP_FULL,
104                               0);
105 }
106
107 static
108 int mdc_getattr_common(struct obd_export *exp, unsigned int ea_size, 
109                        unsigned int acl_size, struct ptlrpc_request *req)
110 {
111         struct mds_body *body;
112         void *eadata;
113         int size[4] = { sizeof(struct ptlrpc_body), sizeof(*body) };
114         int bufcount = 2, rc;
115         ENTRY;
116
117         /* request message already built */
118
119         if (ea_size != 0) {
120                 size[bufcount++] = ea_size;
121                 CDEBUG(D_INODE, "reserved %u bytes for MD/symlink in packet\n",
122                        ea_size);
123         }
124         if (acl_size) {
125                 size[bufcount++] = acl_size;
126                 CDEBUG(D_INODE, "reserved %u bytes for ACL\n", acl_size);
127         }
128
129         ptlrpc_req_set_repsize(req, bufcount, size);
130
131         rc = ptlrpc_queue_wait(req);
132         if (rc != 0)
133                 RETURN (rc);
134
135         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
136                                   lustre_swab_mds_body);
137         if (body == NULL) {
138                 CERROR ("Can't unpack mds_body\n");
139                 RETURN (-EPROTO);
140         }
141
142         CDEBUG(D_NET, "mode: %o\n", body->mode);
143
144         LASSERT_REPSWAB(req, REPLY_REC_OFF + 1);
145         if (body->eadatasize != 0) {
146                 /* reply indicates presence of eadata; check it's there... */
147                 eadata = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1,
148                                         body->eadatasize);
149                 if (eadata == NULL) {
150                         CERROR ("Missing/short eadata\n");
151                         RETURN (-EPROTO);
152                 }
153         }
154         
155         if (body->valid & OBD_MD_FLMODEASIZE) {
156                 if (exp->exp_obd->u.cli.cl_max_mds_easize < body->max_mdsize) 
157                         exp->exp_obd->u.cli.cl_max_mds_easize = 
158                                                 body->max_mdsize;
159                 if (exp->exp_obd->u.cli.cl_max_mds_cookiesize < 
160                                                 body->max_cookiesize)
161                         exp->exp_obd->u.cli.cl_max_mds_cookiesize = 
162                                                 body->max_cookiesize;
163         }
164
165         RETURN (0);
166 }
167
168 int mdc_getattr(struct obd_export *exp, struct ll_fid *fid,
169                 obd_valid valid, unsigned int ea_size,
170                 struct ptlrpc_request **request)
171 {
172         struct ptlrpc_request *req;
173         int size[2] = { sizeof(struct ptlrpc_body), sizeof(struct mds_body) };
174         int acl_size = 0, rc;
175         ENTRY;
176
177         /* XXX do we need to make another request here?  We just did a getattr
178          *     to do the lookup in the first place.
179          */
180         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
181                               MDS_GETATTR, 2, size, NULL);
182         if (!req)
183                 GOTO(out, rc = -ENOMEM);
184
185         mdc_pack_req_body(req, REQ_REC_OFF, valid, fid, ea_size,
186                           MDS_BFLAG_EXT_FLAGS/*request "new" flags(bug 9486)*/);
187
188         /* currently only root inode will call us with FLACL */
189         if (valid & OBD_MD_FLACL)
190                 acl_size = LUSTRE_POSIX_ACL_MAX_SIZE;
191
192         rc = mdc_getattr_common(exp, ea_size, acl_size, req);
193         if (rc != 0) {
194                 ptlrpc_req_finished (req);
195                 req = NULL;
196         }
197  out:
198         *request = req;
199         RETURN (rc);
200 }
201
202 int mdc_getattr_name(struct obd_export *exp, struct ll_fid *fid,
203                      const char *filename, int namelen, unsigned long valid,
204                      unsigned int ea_size, struct ptlrpc_request **request)
205 {
206         struct ptlrpc_request *req;
207         struct mds_body *body;
208         int rc, size[3] = { sizeof(struct ptlrpc_body), sizeof(*body), namelen};
209         ENTRY;
210
211         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
212                               MDS_GETATTR_NAME, 3, size, NULL);
213         if (!req)
214                 GOTO(out, rc = -ENOMEM);
215
216         mdc_pack_req_body(req, REQ_REC_OFF, valid, fid, ea_size,
217                           MDS_BFLAG_EXT_FLAGS/*request "new" flags(bug 9486)*/);
218  
219         LASSERT(strnlen(filename, namelen) == namelen - 1);
220         memcpy(lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, namelen),
221                filename, namelen);
222
223         rc = mdc_getattr_common(exp, ea_size, 0, req);
224         if (rc != 0) {
225                 ptlrpc_req_finished (req);
226                 req = NULL;
227         }
228  out:
229         *request = req;
230         RETURN(rc);
231 }
232
233 static
234 int mdc_xattr_common(struct obd_export *exp, struct ll_fid *fid,
235                      int opcode, obd_valid valid, const char *xattr_name,
236                      const char *input, int input_size, int output_size,
237                      int flags, struct ptlrpc_request **request)
238 {
239         struct ptlrpc_request *req;
240         int size[4] = { sizeof(struct ptlrpc_body), sizeof(struct mds_body) };
241         // int size[3] = {sizeof(struct mds_body)}, bufcnt = 1;
242         int rc, xattr_namelen = 0, bufcnt = 2, offset;
243         void *tmp;
244         ENTRY;
245
246         if (xattr_name) {
247                 xattr_namelen = strlen(xattr_name) + 1;
248                 size[bufcnt++] = xattr_namelen;
249         }
250         if (input_size) {
251                 LASSERT(input);
252                 size[bufcnt++] = input_size;
253         }
254
255         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
256                               opcode, bufcnt, size, NULL);
257         if (!req)
258                 GOTO(out, rc = -ENOMEM);
259
260         /* request data */
261         mdc_pack_req_body(req, REQ_REC_OFF, valid, fid, output_size, flags);
262
263         offset = REQ_REC_OFF + 1;
264
265         if (xattr_name) {
266                 tmp = lustre_msg_buf(req->rq_reqmsg, offset++, xattr_namelen);
267                 memcpy(tmp, xattr_name, xattr_namelen);
268         }
269         if (input_size) {
270                 tmp = lustre_msg_buf(req->rq_reqmsg, offset++, input_size);
271                 memcpy(tmp, input, input_size);
272         }
273
274         /* reply buffers */
275         if (opcode == MDS_GETXATTR) {
276                 size[REPLY_REC_OFF] = sizeof(struct mds_body);
277                 bufcnt = 2;
278         } else {
279                 bufcnt = 1;
280         }
281
282         /* we do this even output_size is 0, because server is doing that */
283         size[bufcnt++] = output_size;
284
285         ptlrpc_req_set_repsize(req, bufcnt, size);
286
287         /* make rpc */
288         if (opcode == MDS_SETXATTR)
289                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
290
291         rc = ptlrpc_queue_wait(req);
292
293         if (opcode == MDS_SETXATTR)
294                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
295
296         if (rc != 0)
297                 GOTO(err_out, rc);
298
299         if (opcode == MDS_GETXATTR) {
300                 struct mds_body * body = lustre_swab_repbuf(req, REPLY_REC_OFF,
301                                           sizeof(*body),
302                                           lustre_swab_mds_body);
303                 if (body == NULL) {
304                         CERROR ("Can't unpack mds_body\n");
305                         GOTO(err_out, rc = -EPROTO);
306                 }
307         }
308 out:
309         *request = req;
310         RETURN (rc);
311 err_out:
312         ptlrpc_req_finished(req);
313         req = NULL;
314         goto out;
315 }
316
317 int mdc_setxattr(struct obd_export *exp, struct ll_fid *fid,
318                  obd_valid valid, const char *xattr_name,
319                  const char *input, int input_size,
320                  int output_size, int flags,
321                  struct ptlrpc_request **request)
322 {
323         return mdc_xattr_common(exp, fid, MDS_SETXATTR, valid, xattr_name,
324                                 input, input_size, output_size, flags, request);
325 }
326
327 int mdc_getxattr(struct obd_export *exp, struct ll_fid *fid,
328                  obd_valid valid, const char *xattr_name,
329                  const char *input, int input_size,
330                  int output_size, struct ptlrpc_request **request)
331 {
332         return mdc_xattr_common(exp, fid, MDS_GETXATTR, valid, xattr_name,
333                                 input, input_size, output_size, 0, request);
334 }
335
336 /* This should be called with both the request and the reply still packed. */
337 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
338                                 int repoff)
339 {
340         struct mds_rec_create *rec = lustre_msg_buf(req->rq_reqmsg, reqoff,
341                                                     sizeof(*rec));
342         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, repoff,
343                                                sizeof(*body));
344
345         LASSERT (rec != NULL);
346         LASSERT (body != NULL);
347
348         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
349         if (body->fid1.id == 0) {
350                 DEBUG_REQ(D_ERROR, req, "saving replay request with id = 0 "
351                           "gen = %u", body->fid1.generation);
352                 LBUG();
353         }
354
355         DEBUG_REQ(D_HA, req, "storing generation %u for ino "LPU64,
356                   rec->cr_replayfid.generation, rec->cr_replayfid.id);
357 }
358
359 #ifdef CONFIG_FS_POSIX_ACL
360 static
361 int mdc_unpack_acl(struct obd_export *exp, struct ptlrpc_request *req,
362                    struct lustre_md *md, unsigned int offset)
363 {
364         struct mds_body  *body = md->body;
365         struct posix_acl *acl;
366         void             *buf;
367         int               rc;
368
369         if (!body->aclsize)
370                 return 0;
371
372         buf = lustre_msg_buf(req->rq_repmsg, offset, body->aclsize);
373         if (!buf) {
374                 CERROR("aclsize %u, bufcount %u, bufsize %u\n",
375                        body->aclsize, lustre_msg_bufcount(req->rq_repmsg),
376                        (lustre_msg_bufcount(req->rq_repmsg) <= offset) ?
377                                 -1 : lustre_msg_buflen(req->rq_repmsg, offset));
378                 return -EPROTO;
379         }
380
381         acl = posix_acl_from_xattr(buf, body->aclsize);
382         if (IS_ERR(acl)) {
383                 rc = PTR_ERR(acl);
384                 CERROR("convert xattr to acl: %d\n", rc);
385                 return rc;
386         }
387
388         rc = posix_acl_valid(acl);
389         if (rc) {
390                 CERROR("validate acl: %d\n", rc);
391                 posix_acl_release(acl);
392                 return rc;
393         }
394
395         md->posix_acl = acl;
396         return 0;
397 }
398 #else
399 #define mdc_unpack_acl(exp, req, md, offset) 0
400 #endif
401
402 int mdc_req2lustre_md(struct ptlrpc_request *req, int offset,
403                       struct obd_export *exp,
404                       struct lustre_md *md)
405 {
406         int rc = 0;
407         ENTRY;
408
409         LASSERT(md);
410         memset(md, 0, sizeof(*md));
411
412         md->body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*md->body));
413         LASSERT (md->body != NULL);
414         LASSERT_REPSWABBED(req, offset);
415         offset++;
416
417         if (md->body->valid & OBD_MD_FLEASIZE) {
418                 int lmmsize;
419                 struct lov_mds_md *lmm;
420
421                 LASSERT(S_ISREG(md->body->mode));
422
423                 if (md->body->eadatasize == 0) {
424                         CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
425                         RETURN(-EPROTO);
426                 }
427                 lmmsize = md->body->eadatasize;
428                 lmm = lustre_msg_buf(req->rq_repmsg, offset, lmmsize);
429                 LASSERT (lmm != NULL);
430                 LASSERT_REPSWABBED(req, offset);
431
432                 rc = obd_unpackmd(exp, &md->lsm, lmm, lmmsize);
433                 if (rc < 0)
434                         RETURN(rc);
435
436                 LASSERT (rc >= sizeof (*md->lsm));
437                 rc = 0;
438
439                 offset++;
440         }
441
442         /* for ACL, it's possible that FLACL is set but aclsize is zero.
443          * only when aclsize != 0 there's an actual segment for ACL in
444          * reply buffer.
445          */
446         if ((md->body->valid & OBD_MD_FLACL) && md->body->aclsize) {
447                 rc = mdc_unpack_acl(exp, req, md, offset);
448                 if (rc)
449                         GOTO(err_out, rc);
450                 offset++;
451         }
452 out:
453         RETURN(rc);
454
455 err_out:
456         if (md->lsm)
457                 obd_free_memmd(exp, &md->lsm);
458         goto out;
459 }
460
461 void mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
462 {
463         if (md->lsm)
464                 obd_free_memmd(exp, &md->lsm);
465
466 #ifdef CONFIG_FS_POSIX_ACL
467         if (md->posix_acl) {
468                 posix_acl_release(md->posix_acl);
469                 md->posix_acl = NULL;
470         }
471 #endif
472 }
473
474 static void mdc_commit_open(struct ptlrpc_request *req)
475 {
476         struct mdc_open_data *mod = req->rq_cb_data;
477         if (mod == NULL)
478                 return;
479
480         if (mod->mod_close_req != NULL)
481                 mod->mod_close_req->rq_cb_data = NULL;
482
483         if (mod->mod_och != NULL)
484                 mod->mod_och->och_mod = NULL;
485
486         OBD_FREE(mod, sizeof(*mod));
487         req->rq_cb_data = NULL;
488 }
489
490 static void mdc_replay_open(struct ptlrpc_request *req)
491 {
492         struct mdc_open_data *mod = req->rq_cb_data;
493         struct obd_client_handle *och;
494         struct ptlrpc_request *close_req;
495         struct lustre_handle old;
496         struct mds_body *body;
497         ENTRY;
498
499         body = lustre_swab_repbuf(req, DLM_REPLY_REC_OFF, sizeof(*body),
500                                   lustre_swab_mds_body);
501         LASSERT (body != NULL);
502
503         if (mod == NULL) {
504                 DEBUG_REQ(D_ERROR, req,
505                           "can't properly replay without open data");
506                 EXIT;
507                 return;
508         }
509
510         och = mod->mod_och;
511         if (och != NULL) {
512                 struct lustre_handle *file_fh;
513                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
514                 file_fh = &och->och_fh;
515                 CDEBUG(D_HA, "updating handle from "LPX64" to "LPX64"\n",
516                        file_fh->cookie, body->handle.cookie);
517                 memcpy(&old, file_fh, sizeof(old));
518                 memcpy(file_fh, &body->handle, sizeof(*file_fh));
519         }
520
521         close_req = mod->mod_close_req;
522         if (close_req != NULL) {
523                 struct mds_body *close_body;
524                 LASSERT(lustre_msg_get_opc(close_req->rq_reqmsg) == MDS_CLOSE);
525                 close_body = lustre_msg_buf(close_req->rq_reqmsg, REQ_REC_OFF,
526                                             sizeof(*close_body));
527                 if (och != NULL)
528                         LASSERT(!memcmp(&old, &close_body->handle, sizeof old));
529                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
530                 memcpy(&close_body->handle, &body->handle,
531                        sizeof(close_body->handle));
532         }
533
534         EXIT;
535 }
536
537 void mdc_set_open_replay_data(struct obd_client_handle *och,
538                               struct ptlrpc_request *open_req)
539 {
540         struct mdc_open_data *mod;
541         struct mds_rec_create *rec = lustre_msg_buf(open_req->rq_reqmsg,
542                                                     DLM_INTENT_REC_OFF,
543                                                     sizeof(*rec));
544         struct mds_body *body = lustre_msg_buf(open_req->rq_repmsg,
545                                                DLM_REPLY_REC_OFF,
546                                                sizeof(*body));
547
548         /* incoming message in my byte order (it's been swabbed) */
549         LASSERT(rec != NULL);
550         LASSERT_REPSWABBED(open_req, DLM_REPLY_REC_OFF);
551         /* outgoing messages always in my byte order */
552         LASSERT(body != NULL);
553
554         if (och) {
555                 OBD_ALLOC(mod, sizeof(*mod));
556                 if (mod == NULL) {
557                         DEBUG_REQ(D_ERROR, open_req, "can't allocate mdc_open_data");
558                         return;
559                 }
560
561                 spin_lock(&open_req->rq_lock);
562                 if (!open_req->rq_replay) {
563                         OBD_FREE(mod, sizeof(*mod));
564                         spin_unlock(&open_req->rq_lock);
565                         return;
566                 }
567
568                 och->och_mod = mod;
569                 mod->mod_och = och;
570                 mod->mod_open_req = open_req;
571                 open_req->rq_cb_data = mod;
572                 open_req->rq_commit_cb = mdc_commit_open;
573                 spin_unlock(&open_req->rq_lock);
574         }
575
576         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
577         open_req->rq_replay_cb = mdc_replay_open;
578         if (body->fid1.id == 0) {
579                 DEBUG_REQ(D_ERROR, open_req, "saving replay request with "
580                           "id = 0 gen = %u", body->fid1.generation);
581                 LBUG();
582         }
583
584         DEBUG_REQ(D_HA, open_req, "set up replay data");
585 }
586
587 void mdc_clear_open_replay_data(struct obd_client_handle *och)
588 {
589         struct mdc_open_data *mod = och->och_mod;
590
591         /* Don't free the structure now (it happens in mdc_commit_open, after
592          * we're sure we won't need to fix up the close request in the future),
593          * but make sure that replay doesn't poke at the och, which is about to
594          * be freed. */
595         LASSERT(mod != LP_POISON);
596         if (mod != NULL)
597                 mod->mod_och = NULL;
598         och->och_mod = NULL;
599 }
600
601 static void mdc_commit_close(struct ptlrpc_request *req)
602 {
603         struct mdc_open_data *mod = req->rq_cb_data;
604         struct ptlrpc_request *open_req;
605         struct obd_import *imp = req->rq_import;
606
607         DEBUG_REQ(D_HA, req, "close req committed");
608         if (mod == NULL)
609                 return;
610
611         mod->mod_close_req = NULL;
612         req->rq_cb_data = NULL;
613         req->rq_commit_cb = NULL;
614
615         open_req = mod->mod_open_req;
616         LASSERT(open_req != NULL);
617         LASSERT(open_req != LP_POISON);
618         LASSERT(open_req->rq_type != LI_POISON);
619
620         DEBUG_REQ(D_HA, open_req, "open req balanced");
621         LASSERT(open_req->rq_transno != 0);
622         LASSERT(open_req->rq_import == imp);
623
624         /* We no longer want to preserve this for transno-unconditional
625          * replay. */
626         spin_lock(&open_req->rq_lock);
627         open_req->rq_replay = 0;
628         spin_unlock(&open_req->rq_lock);
629 }
630
631 int mdc_close(struct obd_export *exp, struct obdo *oa,
632               struct obd_client_handle *och, struct ptlrpc_request **request)
633 {
634         struct obd_device *obd = class_exp2obd(exp);
635         int reqsize[2] = { sizeof(struct ptlrpc_body),
636                            sizeof(struct mds_body) };
637         int rc, repsize[4] = { sizeof(struct ptlrpc_body),
638                                sizeof(struct mds_body),
639                                obd->u.cli.cl_max_mds_easize,
640                                obd->u.cli.cl_max_mds_cookiesize };
641         struct ptlrpc_request *req;
642         struct mdc_open_data *mod;
643         ENTRY;
644
645         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
646                               MDS_CLOSE, 2, reqsize, NULL);
647         if (req == NULL)
648                 GOTO(out, rc = -ENOMEM);
649
650         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
651          * portal whose threads are not taking any DLM locks and are therefore
652          * always progressing */
653         /* XXX FIXME bug 249 */
654         req->rq_request_portal = MDS_READPAGE_PORTAL;
655
656         /* Ensure that this close's handle is fixed up during replay. */
657         LASSERT(och != NULL);
658         LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
659         mod = och->och_mod;
660         if (likely(mod != NULL)) {
661                 if (mod->mod_open_req->rq_type == LI_POISON) {
662                         CERROR("LBUG POISONED open %p!\n", mod->mod_open_req);
663                         LBUG();
664                         ptlrpc_req_finished(req);
665                         req = NULL;
666                         GOTO(out, rc = -EIO);
667                 }
668                 mod->mod_close_req = req;
669                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
670         } else {
671                 CDEBUG(D_HA, "couldn't find open req; expecting close error\n");
672         }
673
674         mdc_close_pack(req, REQ_REC_OFF, oa, oa->o_valid, och);
675
676         ptlrpc_req_set_repsize(req, 4, repsize);
677         req->rq_commit_cb = mdc_commit_close;
678         LASSERT(req->rq_cb_data == NULL);
679         req->rq_cb_data = mod;
680
681         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
682         rc = ptlrpc_queue_wait(req);
683         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
684
685         if (req->rq_repmsg == NULL) {
686                 CDEBUG(D_HA, "request failed to send: %p, %d\n", req,
687                        req->rq_status);
688                 if (rc == 0)
689                         rc = req->rq_status ? req->rq_status : -EIO;
690         } else if (rc == 0) {
691                 rc = lustre_msg_get_status(req->rq_repmsg);
692                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
693                         DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err "
694                                   "= %d", rc);
695                         if (rc > 0)
696                                 rc = -rc;
697                 } else if (mod == NULL) {
698                         CERROR("Unexpected: can't find mdc_open_data, but the "
699                                "close succeeded.  Please tell CFS.\n");
700                 }
701                 if (!lustre_swab_repbuf(req, REPLY_REC_OFF,
702                                         sizeof(struct mds_body),
703                                         lustre_swab_mds_body)) {
704                         CERROR("Error unpacking mds_body\n");
705                         rc = -EPROTO;
706                 }
707         }
708
709         EXIT;
710         *request = req;
711  out:
712         if (rc != 0 && req && req->rq_commit_cb)
713                 req->rq_commit_cb(req);
714
715         return rc;
716 }
717
718 int mdc_done_writing(struct obd_export *exp, struct obdo *obdo)
719 {
720         struct ptlrpc_request *req;
721         struct mds_body *body;
722         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
723         ENTRY;
724
725         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
726                               MDS_DONE_WRITING, 2, size, NULL);
727         if (req == NULL)
728                 RETURN(-ENOMEM);
729
730         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
731         mdc_pack_fid(&body->fid1, obdo->o_id, 0, obdo->o_mode);
732         body->size = obdo->o_size;
733         body->blocks = obdo->o_blocks;
734         body->flags = obdo->o_flags;
735         body->valid = obdo->o_valid;
736 //        memcpy(&body->handle, &och->och_fh, sizeof(body->handle));
737
738         ptlrpc_req_set_repsize(req, 2, size);
739
740         rc = ptlrpc_queue_wait(req);
741         ptlrpc_req_finished(req);
742         RETURN(rc);
743 }
744
745 int mdc_readpage(struct obd_export *exp, struct ll_fid *fid, __u64 offset,
746                  struct page *page, struct ptlrpc_request **request)
747 {
748         struct obd_import *imp = class_exp2cliimp(exp);
749         struct ptlrpc_request *req = NULL;
750         struct ptlrpc_bulk_desc *desc = NULL;
751         struct mds_body *body;
752         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
753         ENTRY;
754
755         CDEBUG(D_INODE, "inode: "LPU64"\n", fid->id);
756
757         req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_READPAGE, 2, size,
758                               NULL);
759         if (req == NULL)
760                 GOTO(out, rc = -ENOMEM);
761
762         /* XXX FIXME bug 249 */
763         req->rq_request_portal = MDS_READPAGE_PORTAL;
764
765         desc = ptlrpc_prep_bulk_imp(req, 1, BULK_PUT_SINK, MDS_BULK_PORTAL);
766         if (desc == NULL)
767                 GOTO(out, rc = -ENOMEM);
768         /* NB req now owns desc and will free it when it gets freed */
769
770         ptlrpc_prep_bulk_page(desc, page, 0, CFS_PAGE_SIZE);
771
772         mdc_readdir_pack(req, REQ_REC_OFF, offset, CFS_PAGE_SIZE, fid);
773
774         ptlrpc_req_set_repsize(req, 2, size);
775         rc = ptlrpc_queue_wait(req);
776
777         if (rc == 0) {
778                 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
779                                           lustre_swab_mds_body);
780                 if (body == NULL) {
781                         CERROR("Can't unpack mds_body\n");
782                         GOTO(out, rc = -EPROTO);
783                 }
784
785                 if (req->rq_bulk->bd_nob_transferred != CFS_PAGE_SIZE) {
786                         CERROR ("Unexpected # bytes transferred: %d"
787                                 " (%lu expected)\n",
788                                 req->rq_bulk->bd_nob_transferred,
789                                 CFS_PAGE_SIZE);
790                         GOTO (out, rc = -EPROTO);
791                 }
792         }
793
794         EXIT;
795  out:
796         *request = req;
797         return rc;
798 }
799
800
801 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
802                          void *karg, void *uarg)
803 {
804         struct obd_device *obd = exp->exp_obd;
805         struct obd_ioctl_data *data = karg;
806         struct obd_import *imp = obd->u.cli.cl_import;
807         struct llog_ctxt *ctxt;
808         int rc;
809         ENTRY;
810
811 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
812         MOD_INC_USE_COUNT;
813 #else
814         if (!try_module_get(THIS_MODULE)) {
815                 CERROR("Can't get module. Is it alive?");
816                 return -EINVAL;
817         }
818 #endif
819         switch (cmd) {
820         case OBD_IOC_CLIENT_RECOVER:
821                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1);
822                 if (rc < 0)
823                         GOTO(out, rc);
824                 GOTO(out, rc = 0);
825         case IOC_OSC_SET_ACTIVE:
826                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
827                 GOTO(out, rc);
828         case OBD_IOC_PARSE: {
829                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
830                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
831                 GOTO(out, rc);
832         }
833 #ifdef __KERNEL__
834         case OBD_IOC_LLOG_INFO:
835         case OBD_IOC_LLOG_PRINT: {
836                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
837                 rc = llog_ioctl(ctxt, cmd, data);
838
839                 GOTO(out, rc);
840         }
841 #endif
842         case OBD_IOC_POLL_QUOTACHECK:
843                 rc = lquota_poll_check(quota_interface, exp,
844                                        (struct if_quotacheck *)karg);
845                 GOTO(out, rc);
846         default:
847                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
848                 GOTO(out, rc = -ENOTTY);
849         }
850 out:
851 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
852         MOD_DEC_USE_COUNT;
853 #else
854         module_put(THIS_MODULE);
855 #endif
856
857         return rc;
858 }
859
860 int mdc_set_info_async(struct obd_export *exp, obd_count keylen,
861                        void *key, obd_count vallen, void *val,
862                        struct ptlrpc_request_set *set)
863 {
864         struct obd_import *imp = class_exp2cliimp(exp);
865         int rc = -EINVAL;
866
867         if (KEY_IS(KEY_INIT_RECOV)) {
868                 if (vallen != sizeof(int))
869                         RETURN(-EINVAL);
870                 spin_lock(&imp->imp_lock);
871                 imp->imp_initial_recov = *(int *)val;
872                 spin_unlock(&imp->imp_lock);
873
874                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
875                        exp->exp_obd->obd_name, imp->imp_initial_recov);
876                 RETURN(0);
877         }
878         /* Turn off initial_recov after we try all backup servers once */
879         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
880                 if (vallen != sizeof(int))
881                         RETURN(-EINVAL);
882
883                 spin_lock(&imp->imp_lock);
884                 imp->imp_initial_recov_bk = *(int *)val;
885                 if (imp->imp_initial_recov_bk)
886                         imp->imp_initial_recov = 1;
887                 spin_unlock(&imp->imp_lock);
888
889                 CDEBUG(D_HA, "%s: set imp_initial_recov_bk = %d\n",
890                        exp->exp_obd->obd_name, imp->imp_initial_recov_bk);
891                 RETURN(0);
892         }
893         if (KEY_IS("read-only")) {
894                 struct ptlrpc_request *req;
895                 int size[3] = { sizeof(struct ptlrpc_body), keylen, vallen };
896                 char *bufs[3] = { NULL, key, val };
897
898                 if (vallen != sizeof(int))
899                         RETURN(-EINVAL);
900
901                 if (*((int *)val)) {
902                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
903                         imp->imp_connect_data.ocd_connect_flags |=
904                                 OBD_CONNECT_RDONLY;
905                 } else {
906                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
907                         imp->imp_connect_data.ocd_connect_flags &=
908                                 ~OBD_CONNECT_RDONLY;
909                 }
910
911                 req = ptlrpc_prep_req(imp, LUSTRE_MDS_VERSION, MDS_SET_INFO,
912                                       3, size, bufs);
913                 if (req == NULL)
914                         RETURN(-ENOMEM);
915
916                 ptlrpc_req_set_repsize(req, 1, NULL);
917                 if (set) {
918                         rc = 0;
919                         ptlrpc_set_add_req(set, req);
920                         ptlrpc_check_set(set);
921                 } else {
922                         rc = ptlrpc_queue_wait(req);
923                         ptlrpc_req_finished(req);
924                 }
925
926                 RETURN(rc);
927         }
928
929         RETURN(rc);
930 }
931
932 int mdc_get_info(struct obd_export *exp, __u32 keylen, void *key,
933                  __u32 *vallen, void *val)
934 {
935         int rc = -EINVAL;
936
937         if (keylen == strlen("max_easize") &&
938             memcmp(key, "max_easize", strlen("max_easize")) == 0) {
939                 int mdsize, *max_easize;
940
941                 if (*vallen != sizeof(int))
942                         RETURN(-EINVAL);
943                 mdsize = *(int*)val;
944                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
945                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
946                 max_easize = val;
947                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
948                 RETURN(0);
949         }
950         RETURN(rc);
951 }
952
953 static int mdc_statfs(struct obd_device *obd, struct obd_statfs *osfs,
954                       __u64 max_age)
955 {
956         struct ptlrpc_request *req;
957         struct obd_statfs *msfs;
958         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*msfs) };
959         ENTRY;
960
961         /* We could possibly pass max_age in the request (as an absolute
962          * timestamp or a "seconds.usec ago") so the target can avoid doing
963          * extra calls into the filesystem if that isn't necessary (e.g.
964          * during mount that would help a bit).  Having relative timestamps
965          * is not so great if request processing is slow, while absolute
966          * timestamps are not ideal because they need time synchronization. */
967         req = ptlrpc_prep_req(obd->u.cli.cl_import, LUSTRE_MDS_VERSION,
968                               MDS_STATFS, 1, NULL, NULL);
969         if (!req)
970                 RETURN(-ENOMEM);
971
972         ptlrpc_req_set_repsize(req, 2, size);
973
974         rc = ptlrpc_queue_wait(req);
975
976         if (rc)
977                 GOTO(out, rc);
978
979         msfs = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*msfs),
980                                   lustre_swab_obd_statfs);
981         if (msfs == NULL) {
982                 CERROR("Can't unpack obd_statfs\n");
983                 GOTO(out, rc = -EPROTO);
984         }
985
986         memcpy(osfs, msfs, sizeof(*msfs));
987         EXIT;
988 out:
989         ptlrpc_req_finished(req);
990
991         return rc;
992 }
993
994 static int mdc_pin(struct obd_export *exp, obd_id ino, __u32 gen, int type,
995                    struct obd_client_handle *handle, int flag)
996 {
997         struct ptlrpc_request *req;
998         struct mds_body *body;
999         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1000         ENTRY;
1001
1002         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1003                               MDS_PIN, 2, size, NULL);
1004         if (req == NULL)
1005                 RETURN(-ENOMEM);
1006
1007         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
1008         mdc_pack_fid(&body->fid1, ino, gen, type);
1009         body->flags = flag;
1010
1011         ptlrpc_req_set_repsize(req, 2, size);
1012
1013         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1014         rc = ptlrpc_queue_wait(req);
1015         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1016         if (rc) {
1017                 CERROR("pin failed: %d\n", rc);
1018                 ptlrpc_req_finished(req);
1019                 RETURN(rc);
1020         }
1021
1022         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
1023                                   lustre_swab_mds_body);
1024         if (body == NULL) {
1025                 ptlrpc_req_finished(req);
1026                 RETURN(rc);
1027         }
1028
1029         memcpy(&handle->och_fh, &body->handle, sizeof(body->handle));
1030         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
1031
1032         OBD_ALLOC(handle->och_mod, sizeof(*handle->och_mod));
1033         if (handle->och_mod == NULL) {
1034                 DEBUG_REQ(D_ERROR, req, "can't allocate mdc_open_data");
1035                 RETURN(-ENOMEM);
1036         }
1037         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
1038
1039         RETURN(rc);
1040 }
1041
1042 static int mdc_unpin(struct obd_export *exp,
1043                      struct obd_client_handle *handle, int flag)
1044 {
1045         struct ptlrpc_request *req;
1046         struct mds_body *body;
1047         int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1048         ENTRY;
1049
1050         if (handle->och_magic != OBD_CLIENT_HANDLE_MAGIC)
1051                 RETURN(0);
1052
1053         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1054                               MDS_CLOSE, 2, size, NULL);
1055         if (req == NULL)
1056                 RETURN(-ENOMEM);
1057
1058         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
1059         memcpy(&body->handle, &handle->och_fh, sizeof(body->handle));
1060         body->flags = flag;
1061
1062         ptlrpc_req_set_repsize(req, 1, NULL);
1063         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1064         rc = ptlrpc_queue_wait(req);
1065         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
1066
1067         if (rc != 0)
1068                 CERROR("unpin failed: %d\n", rc);
1069
1070         ptlrpc_req_finished(req);
1071         ptlrpc_req_finished(handle->och_mod->mod_open_req);
1072         OBD_FREE(handle->och_mod, sizeof(*handle->och_mod));
1073         RETURN(rc);
1074 }
1075
1076 int mdc_sync(struct obd_export *exp, struct ll_fid *fid,
1077              struct ptlrpc_request **request)
1078 {
1079         struct ptlrpc_request *req;
1080         int size[2] = { sizeof(struct ptlrpc_body), sizeof(struct mds_body) };
1081         int rc;
1082         ENTRY;
1083
1084         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MDS_VERSION,
1085                               MDS_SYNC, 2, size, NULL);
1086         if (!req)
1087                 RETURN(rc = -ENOMEM);
1088
1089         mdc_pack_req_body(req, REQ_REC_OFF, 0, fid, 0, 0);
1090
1091         ptlrpc_req_set_repsize(req, 2, size);
1092
1093         rc = ptlrpc_queue_wait(req);
1094         if (rc || request == NULL)
1095                 ptlrpc_req_finished(req);
1096         else
1097                 *request = req;
1098
1099         RETURN(rc);
1100 }
1101
1102 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
1103                             enum obd_import_event event)
1104 {
1105         int rc = 0;
1106
1107         LASSERT(imp->imp_obd == obd);
1108
1109         switch (event) {
1110         case IMP_EVENT_DISCON: {
1111                 break;
1112         }
1113         case IMP_EVENT_INACTIVE: {
1114                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
1115                 break;
1116         }
1117         case IMP_EVENT_INVALIDATE: {
1118                 struct ldlm_namespace *ns = obd->obd_namespace;
1119
1120                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1121
1122                 break;
1123         }
1124         case IMP_EVENT_ACTIVE: {
1125                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
1126                 break;
1127         }
1128         case IMP_EVENT_OCD:
1129                 break;
1130
1131         default:
1132                 CERROR("Unknown import event %x\n", event);
1133                 LBUG();
1134         }
1135         RETURN(rc);
1136 }
1137
1138 static int mdc_setup(struct obd_device *obd, obd_count len, void *buf)
1139 {
1140         struct client_obd *cli = &obd->u.cli;
1141         struct lprocfs_static_vars lvars;
1142         int rc;
1143         ENTRY;
1144
1145         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1146         if (!cli->cl_rpc_lock)
1147                 RETURN(-ENOMEM);
1148         mdc_init_rpc_lock(cli->cl_rpc_lock);
1149
1150         ptlrpcd_addref();
1151
1152         OBD_ALLOC(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1153         if (!cli->cl_setattr_lock)
1154                 GOTO(err_rpc_lock, rc = -ENOMEM);
1155         mdc_init_rpc_lock(cli->cl_setattr_lock);
1156
1157         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1158         if (!cli->cl_close_lock)
1159                 GOTO(err_setattr_lock, rc = -ENOMEM);
1160         mdc_init_rpc_lock(cli->cl_close_lock);
1161
1162         rc = client_obd_setup(obd, len, buf);
1163         if (rc)
1164                 GOTO(err_close_lock, rc);
1165         lprocfs_init_vars(mdc, &lvars);
1166         lprocfs_obd_setup(obd, lvars.obd_vars);
1167
1168         rc = obd_llog_init(obd, obd, 0, NULL, NULL);
1169         if (rc) {
1170                 mdc_cleanup(obd);
1171                 CERROR("failed to setup llogging subsystems\n");
1172         }
1173
1174         RETURN(rc);
1175
1176 err_close_lock:
1177         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1178 err_setattr_lock:
1179         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1180 err_rpc_lock:
1181         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1182         ptlrpcd_decref();
1183         RETURN(rc);
1184 }
1185
1186 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
1187  * us to make MDS RPCs with large enough reply buffers to hold the
1188  * maximum-sized (= maximum striped) EA and cookie without having to
1189  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
1190 int mdc_init_ea_size(struct obd_export *mdc_exp, struct obd_export *lov_exp)
1191 {
1192         struct obd_device *obd = mdc_exp->exp_obd;
1193         struct client_obd *cli = &obd->u.cli;
1194         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1195         struct lov_desc desc;
1196         __u32 valsize = sizeof(desc);
1197         __u32 stripes;
1198         int rc, size;
1199         ENTRY;
1200
1201         rc = obd_get_info(lov_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
1202                           &valsize, &desc);
1203         if (rc)
1204                 RETURN(rc);
1205
1206         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1207         lsm.lsm_stripe_count = stripes;
1208         size = obd_size_diskmd(lov_exp, &lsm);
1209         
1210         if (cli->cl_max_mds_easize < size)
1211                 cli->cl_max_mds_easize = size;
1212
1213         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1214         size = obd_size_diskmd(lov_exp, &lsm);
1215
1216         if (cli->cl_default_mds_easize < size)
1217                 cli->cl_default_mds_easize = size;
1218
1219         size = stripes * sizeof(struct llog_cookie);
1220         if (cli->cl_max_mds_cookiesize < size)
1221                 cli->cl_max_mds_cookiesize = size;
1222
1223         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1224                cli->cl_max_mds_easize, cli->cl_max_mds_cookiesize);
1225         
1226         RETURN(0);
1227 }
1228
1229 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1230 {
1231         int rc = 0;
1232         ENTRY;
1233
1234         switch (stage) {
1235         case OBD_CLEANUP_EARLY: 
1236         case OBD_CLEANUP_EXPORTS:
1237                 /* If we set up but never connected, the
1238                    client import will not have been cleaned. */
1239                 if (obd->u.cli.cl_import) {
1240                         struct obd_import *imp;
1241                         imp = obd->u.cli.cl_import;
1242                         CERROR("client import never connected\n");
1243                         ptlrpc_invalidate_import(imp);
1244                         ptlrpc_free_rq_pool(imp->imp_rq_pool);
1245                         class_destroy_import(imp);
1246                         obd->u.cli.cl_import = NULL;
1247                 }
1248                 break;
1249         case OBD_CLEANUP_SELF_EXP:
1250                 rc = obd_llog_finish(obd, 0);
1251                 if (rc != 0)
1252                         CERROR("failed to cleanup llogging subsystems\n");
1253         case OBD_CLEANUP_OBD:
1254                 break;
1255         }
1256         RETURN(rc);
1257 }
1258
1259 static int mdc_cleanup(struct obd_device *obd)
1260 {
1261         struct client_obd *cli = &obd->u.cli;
1262
1263         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
1264         OBD_FREE(cli->cl_setattr_lock, sizeof (*cli->cl_setattr_lock));
1265         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
1266
1267         lprocfs_obd_cleanup(obd);
1268         ptlrpcd_decref();
1269
1270         return client_obd_cleanup(obd);
1271 }
1272
1273
1274 static int mdc_llog_init(struct obd_device *obd, struct obd_device *tgt,
1275                          int count, struct llog_catid *logid, 
1276                          struct obd_uuid *uuid)
1277 {
1278         struct llog_ctxt *ctxt;
1279         int rc;
1280         ENTRY;
1281
1282         rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1283                         &llog_client_ops);
1284         if (rc == 0) {
1285                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1286                 ctxt->loc_imp = obd->u.cli.cl_import;
1287         }
1288
1289         rc = llog_setup(obd, LLOG_LOVEA_REPL_CTXT, tgt, 0, NULL,
1290                        &llog_client_ops);
1291         if (rc == 0) {
1292                 ctxt = llog_get_context(obd, LLOG_LOVEA_REPL_CTXT);
1293                 ctxt->loc_imp = obd->u.cli.cl_import;
1294         }
1295
1296         RETURN(rc);
1297 }
1298
1299 static int mdc_llog_finish(struct obd_device *obd, int count)
1300 {
1301         int rc;
1302         ENTRY;
1303
1304         rc = llog_cleanup(llog_get_context(obd, LLOG_LOVEA_REPL_CTXT));
1305         if (rc) {
1306                 CERROR("can not cleanup LLOG_CONFIG_REPL_CTXT rc %d\n", rc);
1307         }
1308         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
1309         RETURN(rc);
1310 }
1311
1312 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
1313 {
1314         struct lustre_cfg *lcfg = buf;
1315         struct lprocfs_static_vars lvars;
1316         int rc = 0;
1317
1318         lprocfs_init_vars(mdc, &lvars);
1319         
1320         rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars, lcfg, obd);
1321         return(rc);
1322 }
1323
1324 struct obd_ops mdc_obd_ops = {
1325         .o_owner        = THIS_MODULE,
1326         .o_setup        = mdc_setup,
1327         .o_precleanup   = mdc_precleanup,
1328         .o_cleanup      = mdc_cleanup,
1329         .o_add_conn     = client_import_add_conn,
1330         .o_del_conn     = client_import_del_conn,
1331         .o_connect      = client_connect_import,
1332         .o_disconnect   = client_disconnect_export,
1333         .o_iocontrol    = mdc_iocontrol,
1334         .o_set_info_async = mdc_set_info_async,
1335         .o_get_info     = mdc_get_info,
1336         .o_statfs       = mdc_statfs,
1337         .o_pin          = mdc_pin,
1338         .o_unpin        = mdc_unpin,
1339         .o_import_event = mdc_import_event,
1340         .o_llog_init    = mdc_llog_init,
1341         .o_llog_finish  = mdc_llog_finish,
1342         .o_process_config = mdc_process_config,
1343 };
1344
1345 int __init mdc_init(void)
1346 {
1347         int rc;
1348         struct lprocfs_static_vars lvars;
1349         lprocfs_init_vars(mdc, &lvars);
1350         request_module("lquota");
1351         quota_interface = PORTAL_SYMBOL_GET(mdc_quota_interface);
1352         init_obd_quota_ops(quota_interface, &mdc_obd_ops);
1353
1354         rc = class_register_type(&mdc_obd_ops, lvars.module_vars,
1355                                  LUSTRE_MDC_NAME);
1356         if (rc && quota_interface)
1357                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1358
1359         RETURN(rc);
1360 }
1361
1362 #ifdef __KERNEL__
1363 static void /*__exit*/ mdc_exit(void)
1364 {
1365         if (quota_interface)
1366                 PORTAL_SYMBOL_PUT(mdc_quota_interface);
1367
1368         class_unregister_type(LUSTRE_MDC_NAME);
1369 }
1370
1371 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1372 MODULE_DESCRIPTION("Lustre Metadata Client");
1373 MODULE_LICENSE("GPL");
1374
1375 EXPORT_SYMBOL(mdc_req2lustre_md);
1376 EXPORT_SYMBOL(mdc_free_lustre_md);
1377 EXPORT_SYMBOL(mdc_change_cbdata);
1378 EXPORT_SYMBOL(mdc_getstatus);
1379 EXPORT_SYMBOL(mdc_getattr);
1380 EXPORT_SYMBOL(mdc_getattr_name);
1381 EXPORT_SYMBOL(mdc_create);
1382 EXPORT_SYMBOL(mdc_unlink);
1383 EXPORT_SYMBOL(mdc_rename);
1384 EXPORT_SYMBOL(mdc_link);
1385 EXPORT_SYMBOL(mdc_readpage);
1386 EXPORT_SYMBOL(mdc_setattr);
1387 EXPORT_SYMBOL(mdc_close);
1388 EXPORT_SYMBOL(mdc_done_writing);
1389 EXPORT_SYMBOL(mdc_sync);
1390 EXPORT_SYMBOL(mdc_set_open_replay_data);
1391 EXPORT_SYMBOL(mdc_clear_open_replay_data);
1392 EXPORT_SYMBOL(mdc_store_inode_generation);
1393 EXPORT_SYMBOL(mdc_init_ea_size);
1394 EXPORT_SYMBOL(mdc_getxattr);
1395 EXPORT_SYMBOL(mdc_setxattr);
1396
1397 module_init(mdc_init);
1398 module_exit(mdc_exit);
1399 #endif