Whamcloud - gitweb
- Move recovery setup into the (network-using) connect methods, to fix
[fs/lustre-release.git] / lustre / lib / target.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  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Target-common OBD method implementations and utility functions.
25  */
26
27 #define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_OST /* XXX WRONG */
29
30 #include <linux/module.h>
31 #include <linux/obd_ost.h>
32 #include <linux/lustre_net.h>
33 #include <linux/lustre_dlm.h>
34
35 int target_handle_connect(struct ptlrpc_request *req)
36 {
37         struct obd_device *target;
38         struct obd_export *export;
39         struct obd_import *dlmimp;
40         struct lustre_handle conn;
41         char *tgtuuid, *cluuid;
42         int rc, i;
43         ENTRY;
44
45         tgtuuid = lustre_msg_buf(req->rq_reqmsg, 0);
46         if (req->rq_reqmsg->buflens[0] > 37) {
47                 CERROR("bad target UUID for connect\n");
48                 GOTO(out, rc = -EINVAL);
49         }
50
51         cluuid = lustre_msg_buf(req->rq_reqmsg, 1);
52         if (req->rq_reqmsg->buflens[1] > 37) {
53                 CERROR("bad client UUID for connect\n");
54                 GOTO(out, rc = -EINVAL);
55         }
56
57         i = class_uuid2dev(tgtuuid);
58         if (i == -1) {
59                 CERROR("UUID '%s' not found for connect\n", tgtuuid);
60                 GOTO(out, rc = -ENODEV);
61         }
62
63         target = &obd_dev[i];
64         if (!target)
65                 GOTO(out, rc = -ENODEV);
66
67         conn.addr = req->rq_reqmsg->addr;
68         conn.cookie = req->rq_reqmsg->cookie;
69
70         rc = obd_connect(&conn, target, cluuid, ptlrpc_recovd,
71                          target_revoke_connection);
72         if (rc)
73                 GOTO(out, rc);
74
75         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
76         if (rc)
77                 GOTO(out, rc);
78         req->rq_repmsg->addr = conn.addr;
79         req->rq_repmsg->cookie = conn.cookie;
80
81         export = class_conn2export(&conn);
82         LASSERT(export);
83
84         req->rq_export = export;
85         export->exp_connection = ptlrpc_get_connection(&req->rq_peer, cluuid);
86         if (req->rq_connection != NULL)
87                 ptlrpc_put_connection(req->rq_connection);
88         req->rq_connection = ptlrpc_connection_addref(export->exp_connection);
89
90         spin_lock(&export->exp_connection->c_lock);
91         list_add(&export->exp_conn_chain, &export->exp_connection->c_exports);
92         spin_unlock(&export->exp_connection->c_lock);
93
94         dlmimp = &export->exp_ldlm_data.led_import;
95         dlmimp->imp_connection = req->rq_connection;
96         dlmimp->imp_client = &export->exp_obd->obd_ldlm_client;
97         dlmimp->imp_handle.addr = req->rq_reqmsg->addr;
98         dlmimp->imp_handle.cookie = req->rq_reqmsg->cookie;
99         dlmimp->imp_obd = /* LDLM! */ NULL;
100         
101 #warning Peter: is this the right place to upgrade the server connection level?
102         req->rq_connection->c_level = LUSTRE_CONN_FULL;
103 out:
104         req->rq_status = rc;
105         RETURN(rc);
106 }
107
108 int target_handle_disconnect(struct ptlrpc_request *req)
109 {
110         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
111         int rc;
112         ENTRY;
113
114         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
115         if (rc)
116                 RETURN(rc);
117
118         req->rq_status = obd_disconnect(conn);
119
120         RETURN(0);
121 }
122
123 static int target_disconnect_client(struct ptlrpc_connection *conn)
124 {
125         struct list_head *expiter, *n;
126         struct lustre_handle hdl;
127         struct obd_export *exp;
128         int rc;
129         ENTRY;
130
131         list_for_each_safe(expiter, n, &conn->c_exports) {
132                 exp = list_entry(expiter, struct obd_export, exp_conn_chain);
133
134                 hdl.addr = (__u64)(unsigned long)exp;
135                 hdl.cookie = exp->exp_cookie;
136                 rc = obd_disconnect(&hdl);
137                 if (rc)
138                         CERROR("disconnecting export %p failed: %d\n", exp, rc);
139         }
140         RETURN(0);
141 }
142
143 static int target_fence_failed_connection(struct ptlrpc_connection *conn)
144 {
145         ENTRY;
146
147         conn->c_level = LUSTRE_CONN_RECOVD;
148         conn->c_recovd_data.rd_phase = RD_PREPARED;
149
150         RETURN(0);
151 }
152
153 int target_revoke_connection(struct recovd_data *rd, int phase)
154 {
155         struct ptlrpc_connection *conn = class_rd2conn(rd);
156         
157         LASSERT(conn);
158         ENTRY;
159
160         switch (phase) {
161             case PTLRPC_RECOVD_PHASE_PREPARE:
162                 RETURN(target_fence_failed_connection(conn));
163             case PTLRPC_RECOVD_PHASE_RECOVER:
164                 RETURN(target_disconnect_client(conn));
165             case PTLRPC_RECOVD_PHASE_FAILURE:
166                 LBUG();
167                 RETURN(0);
168         }
169
170         LBUG();
171         RETURN(-ENOSYS);
172 }