Whamcloud - gitweb
- Add mount-epoch checking in most (all?) inode-using operations, in preparation
[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);
71         if (rc)
72                 GOTO(out, rc);
73
74         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
75         if (rc)
76                 GOTO(out, rc);
77         req->rq_repmsg->addr = conn.addr;
78         req->rq_repmsg->cookie = conn.cookie;
79
80         export = class_conn2export(&conn);
81         LASSERT(export);
82
83         req->rq_export = export;
84         export->exp_connection = ptlrpc_get_connection(&req->rq_peer, cluuid);
85         if (req->rq_connection != NULL)
86                 ptlrpc_put_connection(req->rq_connection);
87         req->rq_connection = ptlrpc_connection_addref(export->exp_connection);
88
89         spin_lock(&export->exp_connection->c_lock);
90         list_add(&export->exp_conn_chain, &export->exp_connection->c_exports);
91         spin_unlock(&export->exp_connection->c_lock);
92
93         recovd_conn_manage(export->exp_connection, ptlrpc_recovd,
94                            target_revoke_connection);
95         dlmimp = &export->exp_ldlm_data.led_import;
96         dlmimp->imp_connection = req->rq_connection;
97         dlmimp->imp_client = &export->exp_obd->obd_ldlm_client;
98         dlmimp->imp_handle.addr = req->rq_reqmsg->addr;
99         dlmimp->imp_handle.cookie = req->rq_reqmsg->cookie;
100         dlmimp->imp_obd = /* LDLM! */ NULL;
101         
102 #warning Peter: is this the right place to upgrade the server connection level?
103         req->rq_connection->c_level = LUSTRE_CONN_FULL;
104 out:
105         req->rq_status = rc;
106         RETURN(rc);
107 }
108
109 int target_handle_disconnect(struct ptlrpc_request *req)
110 {
111         struct lustre_handle *conn = (struct lustre_handle *)req->rq_reqmsg;
112         int rc;
113         ENTRY;
114
115         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
116         if (rc)
117                 RETURN(rc);
118
119         req->rq_status = obd_disconnect(conn);
120
121         RETURN(0);
122 }
123
124 static int target_disconnect_client(struct ptlrpc_connection *conn)
125 {
126         struct list_head *expiter, *n;
127         struct lustre_handle hdl;
128         struct obd_export *exp;
129         int rc;
130         ENTRY;
131
132         list_for_each_safe(expiter, n, &conn->c_exports) {
133                 exp = list_entry(expiter, struct obd_export, exp_conn_chain);
134
135                 hdl.addr = (__u64)(unsigned long)exp;
136                 hdl.cookie = exp->exp_cookie;
137                 rc = obd_disconnect(&hdl);
138                 if (rc)
139                         CERROR("disconnecting export %p failed: %d\n", exp, rc);
140         }
141         RETURN(0);
142 }
143
144 static int target_fence_failed_connection(struct ptlrpc_connection *conn)
145 {
146         ENTRY;
147
148         conn->c_level = LUSTRE_CONN_RECOVD;
149         conn->c_recovd_data.rd_phase = RD_PREPARED;
150
151         RETURN(0);
152 }
153
154 int target_revoke_connection(struct recovd_data *rd, int phase)
155 {
156         struct ptlrpc_connection *conn = class_rd2conn(rd);
157         
158         LASSERT(conn);
159         ENTRY;
160
161         switch (phase) {
162             case PTLRPC_RECOVD_PHASE_PREPARE:
163                 RETURN(target_fence_failed_connection(conn));
164             case PTLRPC_RECOVD_PHASE_RECOVER:
165                 RETURN(target_disconnect_client(conn));
166             case PTLRPC_RECOVD_PHASE_FAILURE:
167                 LBUG();
168                 RETURN(0);
169         }
170
171         LBUG();
172         RETURN(-ENOSYS);
173 }