Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[fs/lustre-release.git] / lustre / liblustre / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <error.h>
29 #include <assert.h>
30 #include <sys/types.h>
31 #include <sys/queue.h>
32
33 #include <sysio.h>
34 #include <fs.h>
35 #include <mount.h>
36 #include <inode.h>
37 #include <file.h>
38
39 #include <netinet/in.h>
40 #include <sys/socket.h>
41 #include <arpa/inet.h>
42
43 #include <portals/api-support.h> /* needed for ptpctl.h */
44 #include <portals/ptlctl.h>     /* needed for parse_dump */
45
46 #include "llite_lib.h"
47
48
49 ptl_handle_ni_t         tcpnal_ni;
50 struct task_struct *current;
51 struct obd_class_user_state ocus;
52
53 /* portals interfaces */
54 ptl_handle_ni_t *
55 kportal_get_ni (int nal)
56 {
57         return &tcpnal_ni;
58 }
59
60 inline void
61 kportal_put_ni (int nal)
62 {
63         return;
64 }
65
66 struct ldlm_namespace;
67 struct ldlm_res_id;
68 struct obd_import;
69
70 extern int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, struct ldlm_res_id *res_id, int flags);
71 extern int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
72 extern int ldlm_replay_locks(struct obd_import *imp);
73
74 void *inter_module_get(char *arg)
75 {
76         if (!strcmp(arg, "tcpnal_ni"))
77                 return &tcpnal_ni;
78         else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
79                 return ldlm_cli_cancel_unused;
80         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
81                 return ldlm_namespace_cleanup;
82         else if (!strcmp(arg, "ldlm_replay_locks"))
83                 return ldlm_replay_locks;
84         else
85                 return NULL;
86 }
87
88 void init_current(char *comm)
89
90         current = malloc(sizeof(*current));
91         current->fs = malloc(sizeof(*current->fs));
92         current->fs->umask = umask(0777);
93         umask(current->fs->umask);
94         strncpy(current->comm, comm, sizeof(current->comm));
95         current->pid = getpid();
96         current->fsuid = 0;
97         current->fsgid = 0;
98         current->cap_effective = 0;
99         memset(&current->pending, 0, sizeof(current->pending));
100 }
101
102 ptl_nid_t tcpnal_mynid;
103
104 int init_lib_portals()
105 {
106         int rc;
107
108         PtlInit();
109         rc = PtlNIInit(procbridge_interface, 0, 0, 0, &tcpnal_ni);
110         if (rc != 0) {
111                 CERROR("ksocknal: PtlNIInit failed: error %d\n", rc);
112                 PtlFini();
113                 RETURN (rc);
114         }
115         PtlNIDebug(tcpnal_ni, ~0);
116         return rc;
117 }
118
119 extern int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd, unsigned long arg);
120
121 struct mount_option_s mount_option = {NULL, NULL};
122
123 /* FIXME simple arg parser FIXME */
124 void parse_mount_options(void *arg)
125 {
126         char *buf = NULL;
127         struct obd_ioctl_data *data;
128         char *ptr, *comma, *eq, **tgt, *v;
129         int len;
130
131         if (obd_ioctl_getdata(&buf, &len, arg)) {
132                 CERROR("OBD ioctl: data error\n");
133                 return;
134         }
135         data = (struct obd_ioctl_data *)buf;
136         ptr = data->ioc_inlbuf1;
137         printf("mount option: %s\n", ptr);
138
139         while (ptr) {
140                 eq = strchr(ptr, '=');
141                 if (!eq)
142                         return;
143
144                 *eq = 0;
145                 if (!strcmp("osc", ptr))
146                         tgt = &mount_option.osc_uuid;
147                 else if (!strcmp("mdc", ptr))
148                         tgt = &mount_option.mdc_uuid;
149                 else {
150                         printf("Unknown mount option %s\n", ptr);
151                         return;
152                 }
153
154                 v = eq + 1;
155                 comma = strchr(v, ',');
156                 if (comma) {
157                         *comma = 0;
158                         ptr = comma + 1;
159                 } else
160                         ptr = NULL;
161
162                 *tgt = malloc(strlen(v)+1);
163                 strcpy(*tgt, v);
164         }
165
166         if (buf)
167                 obd_ioctl_freedata(buf, len);
168 }
169
170 int lib_ioctl(int dev_id, int opc, void * ptr)
171 {
172         int rc;
173
174         if (dev_id == OBD_DEV_ID) {
175                 struct obd_ioctl_data *ioc = ptr;
176
177                 if (opc == OBD_IOC_MOUNTOPT) {
178                         parse_mount_options(ptr);
179                         return 0;
180                 }
181
182                 rc = class_handle_ioctl(&ocus, opc, (unsigned long)ptr);
183
184                 /* you _may_ need to call obd_ioctl_unpack or some
185                    other verification function if you want to use ioc
186                    directly here */
187                 printf ("processing ioctl cmd: %x buf len: %d, rc %d\n", 
188                         opc,  ioc->ioc_len, rc);
189
190                 if (rc)
191                         return rc;
192         }
193         return (0);
194 }
195
196 int lllib_init(char *arg)
197 {
198         tcpnal_mynid = ntohl(inet_addr(arg));
199         INIT_LIST_HEAD(&ocus.ocus_conns);
200
201         init_current("dummy");
202         if (init_obdclass() ||
203             init_lib_portals() ||
204             ptlrpc_init() ||
205             ldlm_init() ||
206             mdc_init() ||
207             lov_init() ||
208             osc_init())
209                 return -1;
210
211         if (parse_dump("/tmp/DUMP_FILE", lib_ioctl))
212                 return -1;
213
214         return _sysio_fssw_register("llite", &llu_fssw_ops);
215 }
216
217 /* FIXME */
218 void generate_random_uuid(unsigned char uuid_out[16])
219 {
220         int *arr = (int*)uuid_out;
221         int i;
222
223         for (i = 0; i < sizeof(uuid_out)/sizeof(int); i++)
224                 arr[i] = rand();
225 }
226