Whamcloud - gitweb
- more of the locking infrastructure.
[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 = HTON__u32(flags);
107         request->rq_replen = 
108                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
109
110         rc = ptlrpc_queue_wait(cl, request);
111         rc = ptlrpc_check_status(request, rc);
112
113         if (rc) { 
114                 CERROR("llight request: error in handling %d\n", rc); 
115                 goto out;
116         }
117
118         *fh = request->rq_rep.mds->objid; 
119  out: 
120         *req = request;
121         return rc;
122 }
123
124
125 int mdc_close(struct ptlrpc_client *cl, ino_t ino, int type, __u64 fh, 
126               struct ptlrpc_request **req)
127 {
128         struct ptlrpc_request *request;
129         int rc; 
130
131         request = ptlrpc_prep_req(cl, MDS_CLOSE, 0, NULL, 0, NULL); 
132         if (!request) { 
133                 CERROR("llight request: cannot pack\n");
134                 rc = -ENOMEM;
135                 goto out;
136         }
137
138         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
139         request->rq_req.mds->objid = fh; 
140         request->rq_replen = 
141                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
142
143         rc = ptlrpc_queue_wait(cl, request);
144         rc = ptlrpc_check_status(request, rc);
145
146         if (rc) { 
147                 CERROR("llight request: error in handling %d\n", rc); 
148                 goto out;
149         }
150
151  out: 
152         *req = request;
153         return rc;
154 }
155
156 int mdc_readpage(struct ptlrpc_client *cl, ino_t ino, int type, __u64 offset,
157                  char *addr, struct ptlrpc_request **req)
158 {
159         struct ptlrpc_request *request = NULL;
160         struct ptlrpc_bulk_desc *bulk = NULL;
161         struct niobuf niobuf;
162         int rc; 
163
164         niobuf.addr = (__u64) (long) addr;
165
166         CDEBUG(D_INODE, "inode: %ld\n", ino);
167
168         bulk = ptlrpc_prep_bulk(&cl->cli_server);
169         if (bulk == NULL) {
170                 CERROR("%s: cannot init bulk desc\n", __FUNCTION__);
171                 rc = -ENOMEM;
172                 goto out;
173         }
174
175         request = ptlrpc_prep_req(cl, MDS_READPAGE, 0, NULL,
176                                   sizeof(struct niobuf), (char *)&niobuf);
177         if (!request) { 
178                 CERROR("%s: cannot pack\n", __FUNCTION__);
179                 rc = -ENOMEM;
180                 goto out;
181         }
182
183         bulk->b_buflen = PAGE_SIZE;
184         bulk->b_buf = (void *)(long)niobuf.addr;
185         bulk->b_portal = MDS_BULK_PORTAL;
186         bulk->b_xid = request->rq_xid;
187
188         rc = ptlrpc_register_bulk(bulk);
189         if (rc) {
190                 CERROR("%s: couldn't setup bulk sink: error %d.\n",
191                        __FUNCTION__, rc);
192                 goto out;
193         }
194
195         request->rq_req.mds->fid1.id = ino;
196         request->rq_req.mds->fid1.f_type = type;
197         request->rq_req.mds->size = offset;
198         request->rq_req.mds->tgtlen = sizeof(niobuf); 
199         request->rq_replen = sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
200
201         rc = ptlrpc_queue_wait(cl, request);
202         if (rc) { 
203                 CERROR("mdc request: error in handling %d\n", rc); 
204                 goto out;
205         }
206
207         CDEBUG(D_INODE, "mode: %o\n", request->rq_rep.mds->mode);
208
209  out:
210         *req = request;
211         if (bulk != NULL)
212                 OBD_FREE(bulk, sizeof(*bulk));
213         return rc;
214 }
215
216 static int request_ioctl(struct inode *inode, struct file *file, 
217                          unsigned int cmd, unsigned long arg)
218 {
219         int err;
220         struct ptlrpc_client cl;
221         struct ptlrpc_request *request;
222
223         ENTRY;
224
225         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
226                 EXIT;
227                 return -EINVAL;
228         }
229
230         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
231              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
232              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
233                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
234                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
235                 EXIT;
236                 return -EINVAL;
237         }
238
239         /* XXX complete this to get debugging working again */
240         err = -1;
241         if (err) {
242                 CERROR("cannot create client\n");
243                 return -EINVAL;
244         }
245         
246         switch (cmd) {
247         case IOC_REQUEST_GETATTR: { 
248                 CERROR("-- getting attr for ino 2\n"); 
249                 err = mdc_getattr(&cl, 2, S_IFDIR, ~0, &request);
250
251                 CERROR("-- done err %d\n", err);
252                 break;
253         }
254
255         case IOC_REQUEST_READPAGE: { 
256                 char *buf;
257                 OBD_ALLOC(buf, PAGE_SIZE);
258                 if (!buf) { 
259                         err = -ENOMEM;
260                         break;
261                 }
262                 CERROR("-- readpage 0 for ino 2\n"); 
263                 err = mdc_readpage(&cl, 2, S_IFDIR, 0, buf, &request);
264                 CERROR("-- done err %d\n", err);
265                 OBD_FREE(buf, PAGE_SIZE);
266                 break;
267         }
268
269         case IOC_REQUEST_SETATTR: { 
270                 struct inode inode;
271                 struct iattr iattr; 
272
273                 inode.i_ino = 2;
274                 iattr.ia_mode = 040777;
275                 iattr.ia_atime = 0;
276                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
277
278                 err = mdc_setattr(&cl, &inode, &iattr, &request);
279                 CERROR("-- done err %d\n", err);
280                 break;
281         }
282
283         case IOC_REQUEST_CREATE: { 
284                 struct inode inode;
285                 struct iattr iattr; 
286
287                 inode.i_ino = 2;
288                 iattr.ia_mode = 040777;
289                 iattr.ia_atime = 0;
290                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
291
292                 err = mdc_create(&cl, &inode, 
293                                  "foofile", strlen("foofile"), 
294                                  NULL, 0, 0100707, 47114711, 
295                                  11, 47, 0, &request);
296                 CERROR("-- done err %d\n", err);
297                 break;
298         }
299
300         default:                
301                 err = -EINVAL;
302                 EXIT;
303                 break;
304         }
305         EXIT;
306         return err;
307 }
308
309
310 static struct file_operations requestdev_fops = {
311         ioctl: request_ioctl,
312 };
313
314
315 static struct miscdevice request_dev = {
316         REQUEST_MINOR,
317         "request",
318         &requestdev_fops
319 };
320
321
322 static int __init ptlrpc_request_init(void)
323 {
324         misc_register(&request_dev);
325         return 0 ;
326 }
327
328
329 static void __exit ptlrpc_request_exit(void)
330 {
331         misc_deregister(&request_dev);
332 }
333
334 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
335 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
336 MODULE_LICENSE("GPL");
337
338 EXPORT_SYMBOL(mdc_create); 
339 EXPORT_SYMBOL(mdc_unlink); 
340 EXPORT_SYMBOL(mdc_rename); 
341 EXPORT_SYMBOL(mdc_link); 
342 EXPORT_SYMBOL(mdc_getattr); 
343 EXPORT_SYMBOL(mdc_readpage); 
344 EXPORT_SYMBOL(mdc_setattr); 
345 EXPORT_SYMBOL(mdc_close);
346 EXPORT_SYMBOL(mdc_open);
347
348 module_init(ptlrpc_request_init);
349 module_exit(ptlrpc_request_exit);