Whamcloud - gitweb
Merge of b_md to HEAD:
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24 #define DEBUG_SUBSYSTEM S_MDC
25
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/lustre_mds.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_dlm.h>
31 #include <linux/init.h>
32 #include <linux/lprocfs_status.h>
33
34 #define REQUEST_MINOR 244
35
36 extern int mds_queue_req(struct ptlrpc_request *);
37 extern struct lprocfs_vars status_var_nm_1[];
38 extern struct lprocfs_vars status_class_var[];
39
40 /* should become mdc_getinfo() */
41 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid)
42 {
43         struct ptlrpc_request *req;
44         struct mds_body *body;
45         int rc, size = sizeof(*body);
46         ENTRY;
47
48         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETSTATUS, 1, &size,
49                               NULL);
50         if (!req)
51                 GOTO(out, rc = -ENOMEM);
52
53         body = lustre_msg_buf(req->rq_reqmsg, 0);
54         req->rq_level = LUSTRE_CONN_CON;
55         req->rq_replen = lustre_msg_size(1, &size);
56
57         mds_pack_req_body(req);
58         rc = ptlrpc_queue_wait(req);
59
60         if (!rc) {
61                 body = lustre_msg_buf(req->rq_repmsg, 0);
62                 mds_unpack_body(body);
63                 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
64
65                 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
66                        ", last_xid="LPU64"\n",
67                        rootfid->id, req->rq_repmsg->last_committed,
68                        req->rq_repmsg->last_xid);
69         }
70
71         EXIT;
72  out:
73         ptlrpc_req_finished(req);
74         return rc;
75 }
76
77 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
78                    struct ptlrpc_request **request)
79 {
80         struct ptlrpc_request *req;
81         struct mds_status_req *streq;
82         int rc, size[2] = {sizeof(*streq)};
83         ENTRY;
84
85         req = ptlrpc_prep_req(class_conn2cliimp(mdc_connh), MDS_GETLOVINFO, 1,
86                               size, NULL);
87         if (!req)
88                 GOTO(out, rc = -ENOMEM);
89
90         *request = req;
91         streq = lustre_msg_buf(req->rq_reqmsg, 0);
92         streq->flags = HTON__u32(MDS_STATUS_LOV);
93         streq->repbuf = HTON__u32(8192);
94
95         /* prepare for reply */
96         req->rq_level = LUSTRE_CONN_CON;
97         size[0] = 512;
98         size[1] = 8192;
99         req->rq_replen = lustre_msg_size(2, size);
100
101         rc = ptlrpc_queue_wait(req);
102
103  out:
104         RETURN(rc);
105 }
106
107
108 int mdc_getattr(struct lustre_handle *conn,
109                 obd_id ino, int type, unsigned long valid, size_t ea_size,
110                 struct ptlrpc_request **request)
111 {
112         struct ptlrpc_request *req;
113         struct mds_body *body;
114         int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
115         ENTRY;
116
117         /* XXX do we need to make another request here?  We just did a getattr
118          *     to do the lookup in the first place.
119          */
120         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETATTR, 1, size,
121                               NULL);
122         if (!req)
123                 GOTO(out, rc = -ENOMEM);
124
125         body = lustre_msg_buf(req->rq_reqmsg, 0);
126         ll_ino2fid(&body->fid1, ino, 0, type);
127         body->valid = valid;
128
129         if (ea_size) {
130                 size[bufcount] = ea_size;
131                 bufcount++;
132                 body->size = ea_size;
133                 CDEBUG(D_INODE, "reserving %d bytes for MD/symlink in packet\n",
134                        ea_size);
135         }
136         req->rq_replen = lustre_msg_size(bufcount, size);
137         mds_pack_req_body(req);
138
139         rc = ptlrpc_queue_wait(req);
140
141         if (!rc) {
142                 body = lustre_msg_buf(req->rq_repmsg, 0);
143                 mds_unpack_body(body);
144                 CDEBUG(D_NET, "mode: %o\n", body->mode);
145         }
146
147         EXIT;
148  out:
149         *request = req;
150         return rc;
151 }
152
153 void d_delete_aliases(struct inode *inode)
154 {
155         struct dentry *dentry = NULL;
156         struct list_head *tmp;
157         struct ll_sb_info *sbi = ll_i2sbi(inode);
158         ENTRY;
159
160         spin_lock(&dcache_lock);
161         list_for_each(tmp, &inode->i_dentry) {
162                 dentry = list_entry(tmp, struct dentry, d_alias);
163
164                 list_del_init(&dentry->d_hash);
165                 list_add(&dentry->d_hash, &sbi->ll_orphan_dentry_list);
166         }
167
168         spin_unlock(&dcache_lock);
169         EXIT;
170 }
171
172 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
173                             void *data, __u32 data_len, int flag)
174 {
175         int rc;
176         struct lustre_handle lockh;
177         ENTRY;
178
179         switch (flag) {
180         case LDLM_CB_BLOCKING:
181                 ldlm_lock2handle(lock, &lockh);
182                 rc = ldlm_cli_cancel(&lockh);
183                 if (rc < 0) {
184                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
185                         RETURN(rc);
186                 }
187                 break;
188         case LDLM_CB_CANCELING: {
189                 /* Invalidate all dentries associated with this inode */
190                 struct inode *inode = data;
191
192 #warning "FIXME: what tells us that 'inode' is valid at all?"
193                 if (inode->i_state & I_FREEING)
194                         break;
195
196                 LASSERT(inode != NULL);
197                 LASSERT(data_len == sizeof(*inode));
198
199                 if (S_ISDIR(inode->i_mode)) {
200                         CDEBUG(D_INODE, "invalidating inode %ld\n",
201                                inode->i_ino);
202
203                         ll_invalidate_inode_pages(inode);
204                 }
205
206                 if ( inode != inode->i_sb->s_root->d_inode ) {
207                         /* XXX should this igrab move up 12 lines? */
208                         LASSERT(igrab(inode) == inode);
209                         d_delete_aliases(inode);
210                         iput(inode);
211                 }
212                 break;
213         }
214         default:
215                 LBUG();
216         }
217
218         RETURN(0);
219 }
220
221 /* This should be called with both the request and the reply still packed. */
222 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
223                                 int repoff)
224 {
225         struct mds_rec_create *rec = lustre_msg_buf(req->rq_reqmsg, reqoff);
226         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, repoff);
227
228         DEBUG_REQ(D_HA, req, "storing generation %x for ino "LPD64,
229                   body->fid1.generation, body->fid1.id);
230         memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
231 }
232
233 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
234                 struct lookup_intent *it, int lock_mode, struct inode *dir,
235                 struct dentry *de, struct lustre_handle *lockh,
236                 char *tgt, int tgtlen, void *data, int datalen)
237 {
238         struct ptlrpc_request *req;
239         struct obd_device *obddev = class_conn2obd(conn);
240         __u64 res_id[RES_NAME_SIZE] = {dir->i_ino, (__u64)dir->i_generation};
241         int size[6] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
242         int rc, flags = LDLM_FL_HAS_INTENT;
243         int repsize[3] = {sizeof(struct ldlm_reply),
244                           sizeof(struct mds_body),
245                           obddev->u.cli.cl_max_mds_easize};
246         struct ldlm_reply *dlm_rep;
247         struct ldlm_intent *lit;
248         struct ldlm_request *lockreq;
249         ENTRY;
250
251         LDLM_DEBUG_NOLOCK("mdsintent %s parent dir %ld",
252                           ldlm_it2str(it->it_op), dir->i_ino);
253
254         if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
255                 switch (it->it_op) {
256                 case IT_MKDIR:
257                         it->it_mode |= S_IFDIR;
258                         break;
259                 case (IT_CREAT|IT_OPEN):
260                 case IT_CREAT:
261                         it->it_mode |= S_IFREG;
262                         break;
263                 case IT_SYMLINK:
264                         it->it_mode |= S_IFLNK;
265                         break;
266                 }
267                 it->it_mode &= ~current->fs->umask;
268
269                 size[2] = sizeof(struct mds_rec_create);
270                 size[3] = de->d_name.len + 1;
271                 size[4] = tgtlen + 1;
272                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
273                                       size, NULL);
274                 if (!req)
275                         RETURN(-ENOMEM);
276
277                 /* pack the intent */
278                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
279                 lit->opc = NTOH__u64((__u64)it->it_op);
280
281                 /* pack the intended request */
282                 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
283                                 current->fsgid, CURRENT_TIME, de->d_name.name,
284                                 de->d_name.len, tgt, tgtlen);
285                 req->rq_replen = lustre_msg_size(3, repsize);
286         } else if (it->it_op == IT_RENAME2) {
287                 struct dentry *old_de = it->it_data;
288
289                 size[2] = sizeof(struct mds_rec_rename);
290                 size[3] = old_de->d_name.len + 1;
291                 size[4] = de->d_name.len + 1;
292                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
293                                       size, NULL);
294                 if (!req)
295                         RETURN(-ENOMEM);
296
297                 /* pack the intent */
298                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
299                 lit->opc = NTOH__u64((__u64)it->it_op);
300
301                 /* pack the intended request */
302                 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
303                                 old_de->d_name.name, old_de->d_name.len,
304                                 de->d_name.name, de->d_name.len);
305                 req->rq_replen = lustre_msg_size(3, repsize);
306         } else if (it->it_op == IT_LINK2) {
307                 struct dentry *old_de = it->it_data;
308
309                 size[2] = sizeof(struct mds_rec_link);
310                 size[3] = de->d_name.len + 1;
311                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
312                                       size, NULL);
313                 if (!req)
314                         RETURN(-ENOMEM);
315
316                 /* pack the intent */
317                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
318                 lit->opc = NTOH__u64((__u64)it->it_op);
319
320                 /* pack the intended request */
321                 mds_link_pack(req, 2, old_de->d_inode, dir,
322                               de->d_name.name, de->d_name.len);
323                 req->rq_replen = lustre_msg_size(3, repsize);
324         } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
325                 size[2] = sizeof(struct mds_rec_unlink);
326                 size[3] = de->d_name.len + 1;
327                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
328                                       size, NULL);
329                 if (!req)
330                         RETURN(-ENOMEM);
331
332                 /* pack the intent */
333                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
334                 lit->opc = NTOH__u64((__u64)it->it_op);
335
336                 /* pack the intended request */
337                 mds_unlink_pack(req, 2, dir, NULL,
338                                 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
339                                 de->d_name.name, de->d_name.len);
340
341                 req->rq_replen = lustre_msg_size(3, repsize);
342         } else if (it->it_op & (IT_GETATTR | IT_RENAME | IT_LINK |
343                    IT_OPEN | IT_SETATTR | IT_LOOKUP | IT_READLINK)) {
344                 size[2] = sizeof(struct mds_body);
345                 size[3] = de->d_name.len + 1;
346
347                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
348                                       size, NULL);
349                 if (!req)
350                         RETURN(-ENOMEM);
351
352                 /* pack the intent */
353                 lit = lustre_msg_buf(req->rq_reqmsg, 1);
354                 lit->opc = NTOH__u64((__u64)it->it_op);
355
356                 /* pack the intended request */
357                 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
358
359                 /* get ready for the reply */
360                 req->rq_replen = lustre_msg_size(3, repsize);
361         } else if (it->it_op == IT_READDIR) {
362                 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
363                                       size, NULL);
364                 if (!req)
365                         RETURN(-ENOMEM);
366
367                 /* get ready for the reply */
368                 req->rq_replen = lustre_msg_size(1, repsize);
369         } else {
370                 LBUG();
371                 RETURN(-EINVAL);
372         }
373
374         rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
375                               lock_type, NULL, 0, lock_mode, &flags,
376                               ldlm_completion_ast, mdc_blocking_ast, data,
377                               datalen, lockh);
378
379         if (it->it_op != IT_READDIR) {
380                 /* XXX This should become a lustre_msg flag, but for now... */
381                 __u32 *opp = lustre_msg_buf(req->rq_reqmsg, 2);
382                 *opp |= REINT_REPLAYING;
383         }
384
385         if (rc == -ENOENT) {
386                 /* This can go when we're sure that this can never happen */
387                 LBUG();
388         }
389         if (rc == ELDLM_LOCK_ABORTED) {
390                 lock_mode = 0;
391                 memset(lockh, 0, sizeof(*lockh));
392                 /* rc = 0 */
393         } else if (rc != 0) {
394                 CERROR("ldlm_cli_enqueue: %d\n", rc);
395                 RETURN(rc);
396         } else {
397                 /* The server almost certainly gave us a lock other than the one
398                  * that we asked for.  If we already have a matching lock, then
399                  * cancel this one--we don't need two. */
400                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
401                 struct lustre_handle lockh2;
402                 LASSERT(lock);
403
404                 LDLM_DEBUG(lock, "matching against this");
405
406                 memcpy(&lockh2, lockh, sizeof(lockh2));
407                 if (ldlm_lock_match(NULL, NULL, LDLM_PLAIN, NULL, 0, LCK_NL,
408                                     &lockh2)) {
409                         /* We already have a lock; cancel the old one */
410                         ldlm_lock_decref(lockh, lock_mode);
411                         ldlm_cli_cancel(lockh);
412                         memcpy(lockh, &lockh2, sizeof(lockh2));
413                 }
414                 LDLM_LOCK_PUT(lock);
415         }
416
417         /* On replay, we don't want the lock granted. */
418         lockreq = lustre_msg_buf(req->rq_reqmsg, 0);
419         lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
420
421         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
422         it->it_disposition = (int) dlm_rep->lock_policy_res1;
423         it->it_status = (int) dlm_rep->lock_policy_res2;
424         it->it_lock_mode = lock_mode;
425         it->it_data = req;
426
427         RETURN(0);
428 }
429
430 int mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
431                       int flags)
432 {
433         __u64 res_id[RES_NAME_SIZE] = {inode->i_ino, inode->i_generation};
434         struct obd_device *obddev = class_conn2obd(conn);
435         ENTRY;
436         RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, res_id, flags));
437 }
438
439 struct replay_open_data {
440         struct lustre_handle *fh;
441 };
442
443 static void mdc_replay_open(struct ptlrpc_request *req)
444 {
445         int offset;
446         struct replay_open_data *saved;
447         struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 0);
448
449         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA)
450                 offset = 2;
451         else
452                 offset = 1;
453
454         saved = lustre_msg_buf(req->rq_reqmsg, offset);
455         mds_unpack_body(body);
456         CDEBUG(D_HA, "updating from "LPD64"/"LPD64" to "LPD64"/"LPD64"\n",
457                saved->fh->addr, saved->fh->cookie,
458                body->handle.addr, body->handle.cookie);
459         memcpy(saved->fh, &body->handle, sizeof(body->handle));
460 }
461
462 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
463              struct lov_mds_md *lmm, int lmm_size, struct lustre_handle *fh,
464              struct ptlrpc_request **request)
465 {
466         struct mds_body *body;
467         struct replay_open_data *replay_data;
468         int rc, size[3] = {sizeof(*body), sizeof(*replay_data)}, bufcount = 2;
469         struct ptlrpc_request *req;
470         ENTRY;
471
472         if (lmm && lmm_size) {
473                 bufcount = 3;
474                 size[2] = size[1]; /* shuffle the spare data along */
475                 size[1] = lmm_size;
476         }
477
478         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
479                               NULL);
480         if (!req)
481                 GOTO(out, rc = -ENOMEM);
482
483         req->rq_flags |= PTL_RPC_FL_REPLAY;
484         body = lustre_msg_buf(req->rq_reqmsg, 0);
485
486         ll_ino2fid(&body->fid1, ino, 0, type);
487         body->flags = HTON__u32(flags);
488         memcpy(&body->handle, fh, sizeof(body->handle));
489
490         if (lmm && lmm_size) {
491                 CDEBUG(D_INODE, "sending %u bytes MD for ino "LPU64"\n",
492                        lmm_size, ino);
493                 lustre_msg_set_op_flags(req->rq_reqmsg, MDS_OPEN_HAS_EA);
494                 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), lmm, lmm_size);
495                 body->flags |= HTON__u32(OBD_MD_FLEASIZE);
496         }
497
498         req->rq_replen = lustre_msg_size(1, size);
499
500         rc = ptlrpc_queue_wait(req);
501         if (!rc) {
502                 body = lustre_msg_buf(req->rq_repmsg, 0);
503                 mds_unpack_body(body);
504                 memcpy(fh, &body->handle, sizeof(*fh));
505         }
506
507         /* If open is replayed, we need to fix up the fh. */
508         req->rq_replay_cb = mdc_replay_open;
509         replay_data = lustre_msg_buf(req->rq_reqmsg, lmm ? 2 : 1);
510         replay_data->fh = fh;
511
512         EXIT;
513  out:
514         *request = req;
515         return rc;
516 }
517
518 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
519               struct lustre_handle *fh, struct ptlrpc_request **request)
520 {
521         struct mds_body *body;
522         int rc, size = sizeof(*body);
523         struct ptlrpc_request *req;
524
525         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
526                               NULL);
527         if (!req)
528                 GOTO(out, rc = -ENOMEM);
529
530         body = lustre_msg_buf(req->rq_reqmsg, 0);
531         ll_ino2fid(&body->fid1, ino, 0, type);
532         memcpy(&body->handle, fh, sizeof(body->handle));
533
534         req->rq_replen = lustre_msg_size(0, NULL);
535
536         rc = ptlrpc_queue_wait(req);
537
538         EXIT;
539  out:
540         *request = req;
541         return rc;
542 }
543
544 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
545                  char *addr, struct ptlrpc_request **request)
546 {
547         struct ptlrpc_connection *connection = 
548                 client_conn2cli(conn)->cl_import.imp_connection;
549         struct ptlrpc_request *req = NULL;
550         struct ptlrpc_bulk_desc *desc = NULL;
551         struct ptlrpc_bulk_page *bulk = NULL;
552         struct mds_body *body;
553         int rc, size = sizeof(*body);
554         ENTRY;
555
556         CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
557
558         desc = ptlrpc_prep_bulk(connection);
559         if (desc == NULL)
560                 GOTO(out, rc = -ENOMEM);
561
562         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
563                               NULL);
564         if (!req)
565                 GOTO(out2, rc = -ENOMEM);
566
567         bulk = ptlrpc_prep_bulk_page(desc);
568         bulk->bp_buflen = PAGE_SIZE;
569         bulk->bp_buf = addr;
570         bulk->bp_xid = req->rq_xid;
571         desc->bd_portal = MDS_BULK_PORTAL;
572
573         rc = ptlrpc_register_bulk(desc);
574         if (rc) {
575                 CERROR("couldn't setup bulk sink: error %d.\n", rc);
576                 GOTO(out2, rc);
577         }
578
579         mds_readdir_pack(req, offset, ino, type);
580
581         req->rq_replen = lustre_msg_size(1, &size);
582         rc = ptlrpc_queue_wait(req);
583         if (rc) {
584                 ptlrpc_abort_bulk(desc);
585                 GOTO(out2, rc);
586         } else {
587                 body = lustre_msg_buf(req->rq_repmsg, 0);
588                 mds_unpack_body(body);
589         }
590
591         EXIT;
592  out2:
593         ptlrpc_free_bulk(desc);
594  out:
595         *request = req;
596         return rc;
597 }
598
599 static int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
600 {
601         struct ptlrpc_request *req;
602         int rc, size = sizeof(*osfs);
603         ENTRY;
604
605         req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
606                               NULL);
607         if (!req)
608                 RETURN(-ENOMEM);
609
610         req->rq_replen = lustre_msg_size(1, &size);
611
612         rc = ptlrpc_queue_wait(req);
613
614         if (rc)
615                 GOTO(out, rc);
616
617         obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
618
619         EXIT;
620 out:
621         ptlrpc_req_finished(req);
622
623         return rc;
624 }
625
626 static int mdc_attach(struct obd_device *dev, obd_count len, void *data)
627 {
628         return lprocfs_reg_obd(dev, status_var_nm_1, dev);
629 }
630
631 static int mdc_detach(struct obd_device *dev)
632 {
633         return lprocfs_dereg_obd(dev);
634 }
635
636 static int mdc_recover(struct obd_import *imp, int phase)
637 {
638         int rc;
639         ENTRY;
640
641         switch(phase) {
642             case PTLRPC_RECOVD_PHASE_PREPARE:
643                 ldlm_cli_cancel_unused(imp->imp_obd->obd_namespace,
644                                        NULL, LDLM_FL_LOCAL_ONLY);
645                 RETURN(0);
646             case PTLRPC_RECOVD_PHASE_RECOVER:
647                 rc = ptlrpc_reconnect_import(imp, MDS_CONNECT);
648                 if (rc == EALREADY)
649                         RETURN(ptlrpc_replay(imp, 0));
650                 if (rc)
651                         RETURN(rc);
652
653                 rc = ptlrpc_replay(imp, 0 /* no last flag*/);
654                 if (rc)
655                         RETURN(rc);
656
657                 rc = ldlm_replay_locks(imp);
658                 if (rc)
659                         RETURN(rc);
660
661                 spin_lock(&imp->imp_lock);
662                 imp->imp_level = LUSTRE_CONN_FULL;
663                 spin_unlock(&imp->imp_lock);
664
665                 ptlrpc_wake_delayed(imp);
666
667                 rc = ptlrpc_resend(imp);
668                 if (rc)
669                         RETURN(rc);
670
671                 RETURN(0);
672             default:
673                 RETURN(-EINVAL);
674         }
675 }
676
677 static int mdc_connect(struct lustre_handle *conn, struct obd_device *obd,
678                        obd_uuid_t cluuid, struct recovd_obd *recovd,
679                        ptlrpc_recovery_cb_t recover)
680 {
681         struct obd_import *imp = &obd->u.cli.cl_import;
682         imp->imp_recover = mdc_recover;
683         return client_obd_connect(conn, obd, cluuid, recovd, recover);
684 }
685
686 struct obd_ops mdc_obd_ops = {
687         o_attach: mdc_attach,
688         o_detach: mdc_detach,
689         o_setup:   client_obd_setup,
690         o_cleanup: client_obd_cleanup,
691         o_connect: mdc_connect,
692         o_disconnect: client_obd_disconnect,
693         o_statfs: mdc_statfs,
694 };
695
696 static int __init ptlrpc_request_init(void)
697 {
698         return class_register_type(&mdc_obd_ops, status_class_var,
699                                    LUSTRE_MDC_NAME);
700 }
701
702 static void __exit ptlrpc_request_exit(void)
703 {
704         class_unregister_type(LUSTRE_MDC_NAME);
705 }
706
707 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
708 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
709 MODULE_LICENSE("GPL");
710
711 EXPORT_SYMBOL(d_delete_aliases);
712 EXPORT_SYMBOL(mdc_getstatus);
713 EXPORT_SYMBOL(mdc_getlovinfo);
714 EXPORT_SYMBOL(mdc_enqueue);
715 EXPORT_SYMBOL(mdc_cancel_unused);
716 EXPORT_SYMBOL(mdc_getattr);
717 EXPORT_SYMBOL(mdc_create);
718 EXPORT_SYMBOL(mdc_unlink);
719 EXPORT_SYMBOL(mdc_rename);
720 EXPORT_SYMBOL(mdc_link);
721 EXPORT_SYMBOL(mdc_readpage);
722 EXPORT_SYMBOL(mdc_setattr);
723 EXPORT_SYMBOL(mdc_close);
724 EXPORT_SYMBOL(mdc_open);
725
726 EXPORT_SYMBOL(mdc_store_inode_generation);
727
728 module_init(ptlrpc_request_init);
729 module_exit(ptlrpc_request_exit);