Whamcloud - gitweb
Small fixes to get lustre_light mount working over the request handling
[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 mds_request *);
37
38 struct mds_request *mds_prep_req(int size, int opcode)
39 {
40         struct mds_request *request;
41         int rc;
42         ENTRY; 
43
44         request = (struct mds_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(NULL, 0, NULL, 0, 
51                           &request->rq_reqhdr, &request->rq_req, 
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         request->rq_reqhdr->opc = opcode;
58
59         EXIT;
60         return request;
61 }
62
63
64
65
66 static int mds_queue_wait(struct mds_request *req)
67 {
68         int rc;
69
70         /* XXX fix the race here (wait_for_event?)*/
71         /* hand the packet over to the server */
72         rc = mds_queue_req(req); 
73         if (rc) { 
74                 printk("osc_queue_wait: error %d, opcode %d\n", rc, 
75                        req->rq_reqhdr->opc); 
76                 return -rc;
77         }
78
79         init_waitqueue_head(&req->rq_wait_for_rep);
80         printk("-- sleeping\n");
81         interruptible_sleep_on(&req->rq_wait_for_rep);
82         printk("-- done\n");
83
84         mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr, 
85                        &req->rq_rep); 
86         printk("-->osc_queue_wait: buf %p len %d status %d\n", 
87                req->rq_repbuf, req->rq_replen, req->rq_rephdr->status); 
88
89         EXIT;
90         return req->rq_rephdr->status;
91 }
92
93 void mds_free_req(struct mds_request *request)
94 {
95         kfree(request);
96 }
97
98 int mdc_getattr(ino_t ino, int type, int valid, 
99                 struct mds_rep  **rep, struct mds_rep_hdr **hdr)
100 {
101         struct mds_request *request;
102         int rc; 
103
104         request = mds_prep_req(sizeof(*request), MDS_GETATTR); 
105         if (!request) { 
106                 printk("llight request: cannot pack\n");
107                 return -ENOMEM;
108         }
109
110         request->rq_req->fid1.id = ino;
111         request->rq_req->fid1.f_type = type;
112         request->rq_req->valid = valid;
113
114         rc = mds_queue_wait(request);
115         if (rc) { 
116                 printk("llight request: error in handling %d\n", rc); 
117                 goto out;
118         }
119
120         printk("mds_getattr: mode: %o\n", request->rq_rep->mode); 
121
122         if (rep) { 
123                 *rep = request->rq_rep;
124         }
125         if (hdr) { 
126                 *hdr = request->rq_rephdr;
127         }
128
129  out: 
130         mds_free_req(request);
131         return rc;
132 }
133
134 static int request_ioctl(struct inode *inode, struct file *file, 
135                        unsigned int cmd, unsigned long arg)
136 {
137         int err;
138
139         ENTRY;
140
141         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
142                 EXIT;
143                 return -EINVAL;
144         }
145
146         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
147              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
148              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
149                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
150                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
151                 EXIT;
152                 return -EINVAL;
153         }
154
155         
156         switch (cmd) {
157         case IOC_REQUEST_GETATTR: { 
158                 struct mds_rep_hdr *hdr;
159                 printk("-- getting attr for ino 2\n"); 
160                 err = mdc_getattr(2, S_IFDIR, ~0, NULL, &hdr);
161                 kfree(hdr);
162                 printk("-- done err %d\n", err);
163                 break;
164         }
165         default:                
166                 err = -EINVAL;
167                 EXIT;
168                 break;
169         }
170         EXIT;
171         return err;
172 }
173
174
175 static struct file_operations requestdev_fops = {
176         ioctl: request_ioctl,
177 };
178
179
180 static struct miscdevice request_dev = {
181         REQUEST_MINOR,
182         "request",
183         &requestdev_fops
184 };
185
186
187 static int __init mds_request_init(void)
188 {
189         misc_register(&request_dev);
190         return 0 ;
191 }
192
193
194 static void __exit mds_request_exit(void)
195 {
196         misc_deregister(&request_dev);
197 }
198
199 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
200 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
201 MODULE_LICENSE("GPL");
202
203 EXPORT_SYMBOL(mdc_getattr); 
204
205 module_init(mds_request_init);
206 module_exit(mds_request_exit);