Whamcloud - gitweb
Changes to request processing:
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /*
2  * Copryright (C) 2001 Cluster File Systems, Inc.
3  *
4  */
5
6 #define EXPORT_SYMTAB
7
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/errno.h>
15 #include <linux/locks.h>
16 #include <linux/unistd.h>
17
18 #include <asm/system.h>
19 #include <asm/uaccess.h>
20 #include <linux/module.h>
21
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <asm/uaccess.h>
25 #include <linux/vmalloc.h>
26 #include <asm/segment.h>
27 #include <linux/miscdevice.h>
28
29 #include <linux/obd_support.h>
30 #include <linux/lustre_lib.h>
31 #include <linux/lustre_idl.h>
32 #include <linux/lustre_mds.h>
33
34 #define REQUEST_MINOR 244
35
36 extern int mds_queue_req(struct ptlrpc_request *);
37
38 struct ptlrpc_request *mds_prep_req(int opcode, int namelen, char *name, int tgtlen, char *tgt)
39 {
40         struct ptlrpc_request *request;
41         int rc;
42         ENTRY; 
43
44         request = (struct ptlrpc_request *)kmalloc(sizeof(*request), GFP_KERNEL); 
45         if (!request) { 
46                 printk("mds_prep_req: request allocation out of memory\n");
47                 return NULL;
48         }
49
50         rc = mds_pack_req(name, namelen, tgt, tgtlen,
51                           &request->rq_reqhdr, &(request->rq_req.mds),
52                           &request->rq_reqlen, &request->rq_reqbuf);
53         if (rc) { 
54                 printk("llight request: cannot pack request %d\n", rc); 
55                 return NULL;
56         }
57         printk("--> mds_prep_req: len %d, req %p, tgtlen %d\n", 
58                request->rq_reqlen, request->rq_req.mds, 
59                request->rq_req.mds->tgtlen);
60         request->rq_reqhdr->opc = opcode;
61
62         EXIT;
63         return request;
64 }
65
66
67
68
69 static int mds_queue_wait(struct ptlrpc_request *req, struct lustre_peer *peer)
70 {
71         int rc;
72
73         /* XXX fix the race here (wait_for_event?)*/
74         /* hand the packet over to the server */
75         rc = ptl_send_rpc(req, peer);
76         if (rc) { 
77                 printk("mdc_queue_wait: error %d, opcode %d\n", rc, 
78                        req->rq_reqhdr->opc); 
79                 return -rc;
80         }
81
82         init_waitqueue_head(&req->rq_wait_for_rep);
83         printk("-- sleeping\n");
84         interruptible_sleep_on(&req->rq_wait_for_rep);
85         printk("-- done\n");
86
87         mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr, 
88                        &req->rq_rep.mds); 
89         if ( req->rq_rephdr->status == 0 )
90                 printk("-->mdc_queue_wait: buf %p len %d status %d\n", 
91                        req->rq_repbuf, req->rq_replen, 
92                        req->rq_rephdr->status); 
93
94         EXIT;
95         return req->rq_rephdr->status;
96 }
97
98 void mds_free_req(struct ptlrpc_request *request)
99 {
100         kfree(request);
101 }
102
103 int mdc_getattr(struct lustre_peer *peer, ino_t ino, int type, int valid, 
104                 struct mds_rep  **rep, struct ptlrep_hdr **hdr)
105 {
106         struct ptlrpc_request *request;
107         int rc; 
108
109         request = mds_prep_req(MDS_GETATTR, 0, NULL, 0, NULL); 
110         if (!request) { 
111                 printk("llight request: cannot pack\n");
112                 return -ENOMEM;
113         }
114
115         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
116
117         request->rq_req.mds->valid = valid;
118         request->rq_replen = 
119                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
120
121         rc = mds_queue_wait(request, peer);
122         if (rc) { 
123                 printk("llight request: error in handling %d\n", rc); 
124                 goto out;
125         }
126
127         printk("mds_getattr: mode: %o\n", request->rq_rep.mds->mode); 
128
129         if (rep) { 
130                 *rep = request->rq_rep.mds;
131         }
132         if (hdr) { 
133                 *hdr = request->rq_rephdr;
134         }
135
136  out: 
137         mds_free_req(request);
138         return rc;
139 }
140
141 int mdc_readpage(struct lustre_peer *peer, ino_t ino, int type, __u64 offset,
142                  char *addr, struct mds_rep  **rep, struct ptlrep_hdr **hdr)
143 {
144         struct ptlrpc_request *request;
145         struct niobuf niobuf;
146         int rc; 
147
148         niobuf.addr = (__u64) (long) addr;
149
150         printk("mdc_readpage: inode: %ld\n", ino); 
151
152         request = mds_prep_req(MDS_READPAGE, 0, NULL,
153                                sizeof(struct niobuf), (char *)&niobuf);
154         if (!request) { 
155                 printk("mdc request: cannot pack\n");
156                 return -ENOMEM;
157         }
158
159         request->rq_req.mds->fid1.id = ino;
160         request->rq_req.mds->fid1.f_type = type;
161         request->rq_req.mds->size = offset;
162         request->rq_req.mds->tgtlen = sizeof(niobuf); 
163
164         rc = mds_queue_wait(request, peer);
165         if (rc) { 
166                 printk("mdc request: error in handling %d\n", rc); 
167                 goto out;
168         }
169
170         printk("mdc_readpage: mode: %o\n", request->rq_rep.mds->mode); 
171
172         if (rep) { 
173                 *rep = request->rq_rep.mds;
174         }
175         if (hdr) { 
176                 *hdr = request->rq_rephdr;
177         }
178
179  out: 
180         mds_free_req(request);
181         return rc;
182 }
183
184 int mdc_reint(struct lustre_peer *peer, struct ptlrpc_request *request)
185 {
186         int rc; 
187
188         rc = mds_queue_wait(request, peer);
189         if (rc) { 
190                 printk("mdc request: error in handling %d\n", rc); 
191         }
192
193         return rc;
194 }
195
196
197 static int request_ioctl(struct inode *inode, struct file *file, 
198                        unsigned int cmd, unsigned long arg)
199 {
200         int err;
201         struct lustre_peer peer; 
202
203         ENTRY;
204
205         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
206                 EXIT;
207                 return -EINVAL;
208         }
209
210         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
211              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
212              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
213                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
214                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
215                 EXIT;
216                 return -EINVAL;
217         }
218
219         //rc = ptl_peer("mds", peer); 
220         
221         switch (cmd) {
222         case IOC_REQUEST_GETATTR: { 
223                 struct ptlrep_hdr *hdr = NULL;
224                 printk("-- getting attr for ino 2\n"); 
225                 err = mdc_getattr(&peer, 2, S_IFDIR, ~0, NULL, &hdr);
226                 if (hdr)
227                         kfree(hdr);
228                 printk("-- done err %d\n", err);
229                 break;
230         }
231
232         case IOC_REQUEST_READPAGE: { 
233                 struct ptlrep_hdr *hdr = NULL;
234                 char *buf;
235                 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 
236                 if (!buf) { 
237                         err = -ENOMEM;
238                         break;
239                 }
240                 printk("-- readpage 0 for ino 2\n"); 
241                 err = mdc_readpage(&peer, 2, S_IFDIR, 0, buf, NULL, &hdr);
242                 printk("-- done err %d\n", err);
243                 if (!err) { 
244                         printk("-- status: %d\n", hdr->status); 
245                         err = hdr->status;
246                         if (hdr) 
247                                 kfree(hdr);
248                 }
249                 kfree(buf); 
250                 break;
251         }
252
253         case IOC_REQUEST_SETATTR: { 
254                 struct inode inode;
255                 struct ptlrep_hdr *hdr;
256                 struct iattr iattr; 
257
258                 inode.i_ino = 2;
259                 iattr.ia_mode = 040777;
260                 iattr.ia_atime = 0;
261                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
262
263                 err = mdc_setattr(&peer, &inode, &iattr, NULL, &hdr);
264                 printk("-- done err %d\n", err);
265                 if (!err) { 
266                         printk("-- status: %d\n", hdr->status); 
267                         err = hdr->status;
268                 }
269                 kfree(hdr); 
270                 break;
271         }
272
273         case IOC_REQUEST_CREATE: { 
274                 struct inode inode;
275                 struct ptlrep_hdr *hdr;
276                 struct iattr iattr; 
277
278                 inode.i_ino = 2;
279                 iattr.ia_mode = 040777;
280                 iattr.ia_atime = 0;
281                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
282
283                 err = mdc_create(&peer, &inode, "foofile", strlen("foofile"), 
284                                  0100707, 47114711, 
285                                  11, 47, 0, NULL, &hdr);
286                 printk("-- done err %d\n", err);
287                 if (!err) { 
288                         printk("-- status: %d\n", hdr->status); 
289                         err = hdr->status;
290                 }
291                 kfree(hdr); 
292                 break;
293         }
294
295         default:                
296                 err = -EINVAL;
297                 EXIT;
298                 break;
299         }
300         EXIT;
301         return err;
302 }
303
304
305 static struct file_operations requestdev_fops = {
306         ioctl: request_ioctl,
307 };
308
309
310 static struct miscdevice request_dev = {
311         REQUEST_MINOR,
312         "request",
313         &requestdev_fops
314 };
315
316
317 static int __init ptlrpc_request_init(void)
318 {
319         misc_register(&request_dev);
320         return 0 ;
321 }
322
323
324 static void __exit ptlrpc_request_exit(void)
325 {
326         misc_deregister(&request_dev);
327 }
328
329 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
330 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
331 MODULE_LICENSE("GPL");
332
333 EXPORT_SYMBOL(mdc_create); 
334 EXPORT_SYMBOL(mdc_getattr); 
335 EXPORT_SYMBOL(mdc_readpage); 
336 EXPORT_SYMBOL(mdc_setattr); 
337
338 module_init(ptlrpc_request_init);
339 module_exit(ptlrpc_request_exit);