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