Whamcloud - gitweb
- brought lustre-debugging.lyx up to date
[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 Portals, http://www.sf.net/projects/lustre/
7  *
8  *   Portals 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  *   Portals 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 Portals; 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
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/locks.h>
33 #include <linux/unistd.h>
34
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/module.h>
38
39 #include <linux/fs.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
43 #include <linux/miscdevice.h>
44
45 #define DEBUG_SUBSYSTEM S_MDC
46
47 #include <linux/obd_support.h>
48 #include <linux/obd_class.h>
49 #include <linux/lustre_lib.h>
50 #include <linux/lustre_idl.h>
51 #include <linux/lustre_mds.h>
52
53 #define REQUEST_MINOR 244
54
55 extern int mds_queue_req(struct ptlrpc_request *);
56
57
58 int mdc_getattr(struct ptlrpc_client *cl, ino_t ino, int type, int valid, 
59                 struct ptlrpc_request **req)
60 {
61         int rc;
62         struct ptlrpc_request *request;
63
64         ENTRY;
65
66         request = ptlrpc_prep_req(cl, MDS_GETATTR, 0, NULL, 0, NULL); 
67         if (!request) { 
68                 CERROR("llight request: cannot pack\n");
69                 rc = -ENOMEM;
70                 EXIT;
71                 goto out;
72         }
73
74         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
75
76         request->rq_req.mds->valid = valid;
77         request->rq_replen = 
78                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
79
80         rc = ptlrpc_queue_wait(cl, request);
81         rc = ptlrpc_check_status(request, rc);
82
83         if (!rc)
84                 CDEBUG(D_NET, "mode: %o\n", request->rq_rep.mds->mode);
85
86         EXIT;
87  out:
88         *req = request;
89         return rc;
90 }
91
92 int mdc_open(struct ptlrpc_client *cl, ino_t ino, int type, int flags,
93              __u64 *fh, struct ptlrpc_request **req)
94 {
95         struct ptlrpc_request *request;
96         int rc; 
97
98         request = ptlrpc_prep_req(cl, MDS_OPEN, 0, NULL, 0, NULL); 
99         if (!request) { 
100                 CERROR("llight request: cannot pack\n");
101                 rc = -ENOMEM;
102                 goto out;
103         }
104
105         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
106         request->rq_req.mds->flags = flags;
107         request->rq_replen = 
108                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
109
110         rc = ptlrpc_queue_wait(cl, request);
111         if (rc) { 
112                 CERROR("llight request: error in handling %d\n", rc); 
113                 goto out;
114         }
115
116         *fh = request->rq_rep.mds->objid; 
117
118  out: 
119         *req = request;
120         return rc;
121 }
122
123
124 int mdc_close(struct ptlrpc_client *cl, ino_t ino, int type, __u64 fh, 
125               struct ptlrpc_request **req)
126 {
127         struct ptlrpc_request *request;
128         int rc; 
129
130         request = ptlrpc_prep_req(cl, MDS_CLOSE, 0, NULL, 0, NULL); 
131         if (!request) { 
132                 CERROR("llight request: cannot pack\n");
133                 rc = -ENOMEM;
134                 goto out;
135         }
136
137         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
138         request->rq_req.mds->objid = fh; 
139         request->rq_replen = 
140                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
141
142         rc = ptlrpc_queue_wait(cl, request);
143         if (rc) { 
144                 CERROR("llight request: error in handling %d\n", rc); 
145                 goto out;
146         }
147
148  out: 
149         *req = request;
150         return rc;
151 }
152
153 int mdc_readpage(struct ptlrpc_client *cl, ino_t ino, int type, __u64 offset,
154                  char *addr, struct ptlrpc_request **req)
155 {
156         struct ptlrpc_request *request = NULL;
157         struct ptlrpc_bulk_desc *bulk = NULL;
158         struct niobuf niobuf;
159         int rc; 
160
161         niobuf.addr = (__u64) (long) addr;
162
163         CDEBUG(D_INODE, "inode: %ld\n", ino);
164
165         bulk = ptlrpc_prep_bulk(&cl->cli_server);
166         if (bulk == NULL) {
167                 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
168                 rc = -ENOMEM;
169                 goto out;
170         }
171
172         request = ptlrpc_prep_req(cl, MDS_READPAGE, 0, NULL,
173                                   sizeof(struct niobuf), (char *)&niobuf);
174         if (!request) { 
175                 CERROR("%s: cannot pack\n", __FUNCTION__);
176                 rc = -ENOMEM;
177                 goto out;
178         }
179
180         bulk->b_buflen = PAGE_SIZE;
181         bulk->b_buf = (void *)(long)niobuf.addr;
182         bulk->b_portal = MDS_BULK_PORTAL;
183         bulk->b_xid = request->rq_xid;
184
185         rc = ptlrpc_register_bulk(bulk);
186         if (rc) {
187                 CERROR("%s: couldn't setup bulk sink: error %d.\n",
188                        __FUNCTION__, rc);
189                 goto out;
190         }
191
192         request->rq_req.mds->fid1.id = ino;
193         request->rq_req.mds->fid1.f_type = type;
194         request->rq_req.mds->size = offset;
195         request->rq_req.mds->tgtlen = sizeof(niobuf); 
196         request->rq_replen = sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
197
198         rc = ptlrpc_queue_wait(cl, request);
199         if (rc) { 
200                 CERROR("mdc request: error in handling %d\n", rc); 
201                 goto out;
202         }
203
204         CDEBUG(D_INODE, "mode: %o\n", request->rq_rep.mds->mode);
205
206  out:
207         *req = request;
208         if (bulk != NULL)
209                 OBD_FREE(bulk, sizeof(*bulk));
210         return rc;
211 }
212
213 static int request_ioctl(struct inode *inode, struct file *file, 
214                          unsigned int cmd, unsigned long arg)
215 {
216         int err;
217         struct ptlrpc_client cl;
218         struct ptlrpc_request *request;
219
220         ENTRY;
221
222         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
223                 EXIT;
224                 return -EINVAL;
225         }
226
227         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
228              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
229              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
230                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
231                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
232                 EXIT;
233                 return -EINVAL;
234         }
235
236         /* XXX complete this to get debugging working again */
237         err = -1;
238         if (err) {
239                 CERROR("cannot create client"); 
240                 return -EINVAL;
241         }
242         
243         switch (cmd) {
244         case IOC_REQUEST_GETATTR: { 
245                 CERROR("-- getting attr for ino 2\n"); 
246                 err = mdc_getattr(&cl, 2, S_IFDIR, ~0, &request);
247
248                 CERROR("-- done err %d\n", err);
249                 break;
250         }
251
252         case IOC_REQUEST_READPAGE: { 
253                 char *buf;
254                 OBD_ALLOC(buf, PAGE_SIZE);
255                 if (!buf) { 
256                         err = -ENOMEM;
257                         break;
258                 }
259                 CERROR("-- readpage 0 for ino 2\n"); 
260                 err = mdc_readpage(&cl, 2, S_IFDIR, 0, buf, &request);
261                 CERROR("-- done err %d\n", err);
262                 OBD_FREE(buf, PAGE_SIZE);
263                 break;
264         }
265
266         case IOC_REQUEST_SETATTR: { 
267                 struct inode inode;
268                 struct iattr iattr; 
269
270                 inode.i_ino = 2;
271                 iattr.ia_mode = 040777;
272                 iattr.ia_atime = 0;
273                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
274
275                 err = mdc_setattr(&cl, &inode, &iattr, &request);
276                 CERROR("-- done err %d\n", err);
277                 break;
278         }
279
280         case IOC_REQUEST_CREATE: { 
281                 struct inode inode;
282                 struct iattr iattr; 
283
284                 inode.i_ino = 2;
285                 iattr.ia_mode = 040777;
286                 iattr.ia_atime = 0;
287                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
288
289                 err = mdc_create(&cl, &inode, 
290                                  "foofile", strlen("foofile"), 
291                                  NULL, 0, 0100707, 47114711, 
292                                  11, 47, 0, &request);
293                 CERROR("-- done err %d\n", err);
294                 break;
295         }
296
297         default:                
298                 err = -EINVAL;
299                 EXIT;
300                 break;
301         }
302         EXIT;
303         return err;
304 }
305
306
307 static struct file_operations requestdev_fops = {
308         ioctl: request_ioctl,
309 };
310
311
312 static struct miscdevice request_dev = {
313         REQUEST_MINOR,
314         "request",
315         &requestdev_fops
316 };
317
318
319 static int __init ptlrpc_request_init(void)
320 {
321         misc_register(&request_dev);
322         return 0 ;
323 }
324
325
326 static void __exit ptlrpc_request_exit(void)
327 {
328         misc_deregister(&request_dev);
329 }
330
331 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
332 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
333 MODULE_LICENSE("GPL");
334
335 EXPORT_SYMBOL(mdc_create); 
336 EXPORT_SYMBOL(mdc_unlink); 
337 EXPORT_SYMBOL(mdc_rename); 
338 EXPORT_SYMBOL(mdc_link); 
339 EXPORT_SYMBOL(mdc_getattr); 
340 EXPORT_SYMBOL(mdc_readpage); 
341 EXPORT_SYMBOL(mdc_setattr); 
342 EXPORT_SYMBOL(mdc_close);
343 EXPORT_SYMBOL(mdc_open);
344
345 module_init(ptlrpc_request_init);
346 module_exit(ptlrpc_request_exit);