Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpc_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
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 #define EXPORT_SYMTAB
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #ifdef __KERNEL__
26 # include <linux/module.h>
27 #else 
28 # include <liblustre.h>
29 #endif
30 #include <linux/obd.h>
31 #include <linux/obd_ost.h>
32 #include <linux/lustre_net.h>
33 #include <linux/lustre_dlm.h>
34
35 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
36 {
37         struct ptlrpc_connection *conn;
38         struct obd_ioctl_data* data = buf;
39         struct client_obd *cli = &obddev->u.cli;
40         struct obd_import *imp;
41         struct obd_uuid server_uuid;
42         int rq_portal, rp_portal, connect_op;
43         char *name;
44         ENTRY;
45
46         if (obddev->obd_type->typ_ops->o_brw) {
47                 rq_portal = OST_REQUEST_PORTAL;
48                 rp_portal = OSC_REPLY_PORTAL;
49                 name = "osc";
50                 connect_op = OST_CONNECT;
51         } else {
52                 rq_portal = MDS_REQUEST_PORTAL;
53                 rp_portal = MDC_REPLY_PORTAL;
54                 name = "mdc";
55                 connect_op = MDS_CONNECT;
56         }
57
58         if (data->ioc_inllen1 < 1) {
59                 CERROR("requires a TARGET UUID\n");
60                 RETURN(-EINVAL);
61         }
62
63         if (data->ioc_inllen1 > 37) {
64                 CERROR("client UUID must be less than 38 characters\n");
65                 RETURN(-EINVAL);
66         }
67
68         if (data->ioc_inllen2 < 1) {
69                 CERROR("setup requires a SERVER UUID\n");
70                 RETURN(-EINVAL);
71         }
72
73         if (data->ioc_inllen2 > 37) {
74                 CERROR("target UUID must be less than 38 characters\n");
75                 RETURN(-EINVAL);
76         }
77
78         sema_init(&cli->cl_sem, 1);
79         cli->cl_conn_count = 0;
80         memcpy(server_uuid.uuid, data->ioc_inlbuf2, MIN(data->ioc_inllen2,
81                                                         sizeof(server_uuid)));
82
83         init_MUTEX(&cli->cl_dirty_sem);
84         cli->cl_dirty = 0;
85         cli->cl_dirty_granted = 0;
86         cli->cl_ost_can_grant = 1;
87
88         conn = ptlrpc_uuid_to_connection(&server_uuid);
89         if (conn == NULL)
90                 RETURN(-ENOENT);
91
92         ptlrpc_init_client(rq_portal, rp_portal, name,
93                            &obddev->obd_ldlm_client);
94
95         imp = class_new_import();
96         if (imp == NULL) {
97                 ptlrpc_put_connection(conn);
98                 RETURN(-ENOMEM);
99         }
100         imp->imp_connection = conn;
101         imp->imp_client = &obddev->obd_ldlm_client;
102         imp->imp_obd = obddev;
103         imp->imp_connect_op = connect_op;
104         imp->imp_generation = 0;
105         INIT_LIST_HEAD(&imp->imp_pinger_chain);
106         memcpy(imp->imp_target_uuid.uuid, data->ioc_inlbuf1, data->ioc_inllen1);
107         class_import_put(imp);
108
109         cli->cl_import = imp;
110         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
111         cli->cl_sandev = to_kdev_t(0);
112
113         RETURN(0);
114 }
115
116 int client_obd_cleanup(struct obd_device *obddev, int force, int failover)
117 {
118         struct client_obd *client = &obddev->u.cli;
119
120         if (!client->cl_import)
121                 RETURN(-EINVAL);
122         class_destroy_import(client->cl_import);
123         client->cl_import = NULL;
124         RETURN(0);
125 }