Whamcloud - gitweb
Minor cleanups and debugging statements added.
[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 /* FIXME: this belongs in some sort of service struct */
39 static int mdc_xid = 0;
40
41 struct ptlrpc_request *mds_prep_req(int opcode, int namelen, char *name, int tgtlen, char *tgt)
42 {
43         struct ptlrpc_request *request;
44         int rc;
45         ENTRY; 
46
47         request = (struct ptlrpc_request *)kmalloc(sizeof(*request), GFP_KERNEL); 
48         if (!request) { 
49                 printk("mds_prep_req: request allocation out of memory\n");
50                 return NULL;
51         }
52
53         memset(request, 0, sizeof(*request));
54         request->rq_xid = mdc_xid++;
55
56         rc = mds_pack_req(name, namelen, tgt, tgtlen,
57                           &request->rq_reqhdr, &(request->rq_req.mds),
58                           &request->rq_reqlen, &request->rq_reqbuf);
59         if (rc) { 
60                 printk("llight request: cannot pack request %d\n", rc); 
61                 return NULL;
62         }
63         printk("--> mds_prep_req: len %d, req %p, tgtlen %d\n", 
64                request->rq_reqlen, request->rq_req.mds, 
65                request->rq_req.mds->tgtlen);
66         request->rq_reqhdr->opc = opcode;
67
68         EXIT;
69         return request;
70 }
71
72
73
74
75 static int mds_queue_wait(struct ptlrpc_request *req, struct lustre_peer *peer)
76 {
77         int rc;
78
79         /* XXX fix the race here (wait_for_event?)*/
80         if (peer == NULL) {
81                 /* Local delivery */
82                 printk("--->> %s %d\n", __FUNCTION__, __LINE__);
83                 rc = mds_queue_req(req); 
84         } else {
85                 /* Remote delivery via portals. */
86                 req->rq_req_portal = MDS_REQUEST_PORTAL;
87                 req->rq_reply_portal = MDS_REPLY_PORTAL;
88                 rc = ptl_send_rpc(req, peer);
89         }
90         if (rc) { 
91                 printk(__FUNCTION__ ": error %d, opcode %d\n", rc, 
92                        req->rq_reqhdr->opc); 
93                 return -rc;
94         }
95
96         init_waitqueue_head(&req->rq_wait_for_rep);
97         printk("-- sleeping\n");
98         interruptible_sleep_on(&req->rq_wait_for_rep);
99         printk("-- done\n");
100
101         rc = mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr, 
102                             &req->rq_rep.mds);
103         if (rc) {
104                 printk(__FUNCTION__ ": mds_unpack_rep failed: %d\n", rc);
105                 return rc;
106         }
107
108         if ( req->rq_rephdr->status == 0 )
109                 printk("-->mdc_queue_wait: buf %p len %d status %d\n", 
110                        req->rq_repbuf, req->rq_replen, 
111                        req->rq_rephdr->status); 
112
113         EXIT;
114         return 0;
115 }
116
117 void mds_free_req(struct ptlrpc_request *request)
118 {
119         kfree(request);
120 }
121
122 int mdc_getattr(struct lustre_peer *peer, ino_t ino, int type, int valid, 
123                 struct mds_rep  **rep, struct ptlrep_hdr **hdr)
124 {
125         struct ptlrpc_request *request;
126         int rc; 
127
128         request = mds_prep_req(MDS_GETATTR, 0, NULL, 0, NULL); 
129         if (!request) { 
130                 printk("llight request: cannot pack\n");
131                 return -ENOMEM;
132         }
133
134         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
135
136         request->rq_req.mds->valid = valid;
137         request->rq_replen = 
138                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
139
140         rc = mds_queue_wait(request, peer);
141         if (rc) { 
142                 printk("llight request: error in handling %d\n", rc); 
143                 goto out;
144         }
145
146         printk("mds_getattr: mode: %o\n", request->rq_rep.mds->mode); 
147
148         if (rep) { 
149                 *rep = request->rq_rep.mds;
150         }
151         if (hdr) { 
152                 *hdr = request->rq_rephdr;
153         }
154
155  out: 
156         mds_free_req(request);
157         return rc;
158 }
159
160 int mdc_readpage(struct lustre_peer *peer, ino_t ino, int type, __u64 offset,
161                  char *addr, struct mds_rep  **rep, struct ptlrep_hdr **hdr)
162 {
163         struct ptlrpc_request *request;
164         struct niobuf niobuf;
165         int rc; 
166
167         niobuf.addr = (__u64) (long) addr;
168
169         printk("mdc_readpage: inode: %ld\n", ino); 
170
171         request = mds_prep_req(MDS_READPAGE, 0, NULL,
172                                sizeof(struct niobuf), (char *)&niobuf);
173         if (!request) { 
174                 printk("mdc request: cannot pack\n");
175                 return -ENOMEM;
176         }
177
178         request->rq_req.mds->fid1.id = ino;
179         request->rq_req.mds->fid1.f_type = type;
180         request->rq_req.mds->size = offset;
181         request->rq_req.mds->tgtlen = sizeof(niobuf); 
182
183         request->rq_bulk_portal = MDS_BULK_PORTAL;
184         request->rq_replen = 
185                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
186
187         rc = mds_queue_wait(request, peer);
188         if (rc) { 
189                 printk("mdc request: error in handling %d\n", rc); 
190                 goto out;
191         }
192
193         printk("mdc_readpage: mode: %o\n", request->rq_rep.mds->mode); 
194
195         if (rep) { 
196                 *rep = request->rq_rep.mds;
197         }
198         if (hdr) { 
199                 *hdr = request->rq_rephdr;
200         }
201
202  out: 
203         mds_free_req(request);
204         return rc;
205 }
206
207 int mdc_reint(struct lustre_peer *peer, struct ptlrpc_request *request)
208 {
209         int rc; 
210
211         rc = mds_queue_wait(request, peer);
212         if (rc) { 
213                 printk("mdc request: error in handling %d\n", rc); 
214         }
215
216         return rc;
217 }
218
219
220 static int request_ioctl(struct inode *inode, struct file *file, 
221                        unsigned int cmd, unsigned long arg)
222 {
223         int err;
224         struct lustre_peer peer, *peer_ptr = NULL;
225
226         ENTRY;
227
228         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
229                 EXIT;
230                 return -EINVAL;
231         }
232
233         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
234              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
235              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
236                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
237                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
238                 EXIT;
239                 return -EINVAL;
240         }
241
242         err = kportal_uuid_to_peer("mds", &peer);
243         if (err == 0)
244                 peer_ptr = &peer;
245         
246         switch (cmd) {
247         case IOC_REQUEST_GETATTR: { 
248                 struct ptlrep_hdr *hdr = NULL;
249                 printk("-- getting attr for ino 2\n"); 
250                 err = mdc_getattr(peer_ptr, 2, S_IFDIR, ~0, NULL, &hdr);
251                 if (hdr)
252                         kfree(hdr);
253                 printk("-- done err %d\n", err);
254                 break;
255         }
256
257         case IOC_REQUEST_READPAGE: { 
258                 struct ptlrep_hdr *hdr = NULL;
259                 char *buf;
260                 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 
261                 if (!buf) { 
262                         err = -ENOMEM;
263                         break;
264                 }
265                 printk("-- readpage 0 for ino 2\n"); 
266                 err = mdc_readpage(peer_ptr, 2, S_IFDIR, 0, buf, NULL, &hdr);
267                 printk("-- done err %d\n", err);
268                 if (!err) { 
269                         printk("-- status: %d\n", hdr->status); 
270                         err = hdr->status;
271                         if (hdr) 
272                                 kfree(hdr);
273                 }
274                 kfree(buf); 
275                 break;
276         }
277
278         case IOC_REQUEST_SETATTR: { 
279                 struct inode inode;
280                 struct ptlrep_hdr *hdr;
281                 struct iattr iattr; 
282
283                 inode.i_ino = 2;
284                 iattr.ia_mode = 040777;
285                 iattr.ia_atime = 0;
286                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
287
288                 err = mdc_setattr(peer_ptr, &inode, &iattr, NULL, &hdr);
289                 printk("-- done err %d\n", err);
290                 if (!err) { 
291                         printk("-- status: %d\n", hdr->status); 
292                         err = hdr->status;
293                 } else {
294                         kfree(hdr); 
295                 }
296                 break;
297         }
298
299         case IOC_REQUEST_CREATE: { 
300                 struct inode inode;
301                 struct ptlrep_hdr *hdr;
302                 struct iattr iattr; 
303
304                 inode.i_ino = 2;
305                 iattr.ia_mode = 040777;
306                 iattr.ia_atime = 0;
307                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
308
309                 err = mdc_create(peer_ptr, &inode, "foofile",
310                                  strlen("foofile"), 0100707, 47114711, 
311                                  11, 47, 0, NULL, &hdr);
312                 printk("-- done err %d\n", err);
313                 if (!err) { 
314                         printk("-- status: %d\n", hdr->status); 
315                         err = hdr->status;
316                 }
317                 kfree(hdr); 
318                 break;
319         }
320
321         default:                
322                 err = -EINVAL;
323                 EXIT;
324                 break;
325         }
326         EXIT;
327         return err;
328 }
329
330
331 static struct file_operations requestdev_fops = {
332         ioctl: request_ioctl,
333 };
334
335
336 static struct miscdevice request_dev = {
337         REQUEST_MINOR,
338         "request",
339         &requestdev_fops
340 };
341
342
343 static int __init ptlrpc_request_init(void)
344 {
345         misc_register(&request_dev);
346         return 0 ;
347 }
348
349
350 static void __exit ptlrpc_request_exit(void)
351 {
352         misc_deregister(&request_dev);
353 }
354
355 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
356 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
357 MODULE_LICENSE("GPL");
358
359 EXPORT_SYMBOL(mdc_create); 
360 EXPORT_SYMBOL(mdc_getattr); 
361 EXPORT_SYMBOL(mdc_readpage); 
362 EXPORT_SYMBOL(mdc_setattr); 
363
364 module_init(ptlrpc_request_init);
365 module_exit(ptlrpc_request_exit);