Whamcloud - gitweb
It's been a good day: chmod/chown and friends now work for Lustre Light.
[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 opcode, int namelen, char *name, int tgtlen, char *tgt)
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(name, namelen, tgt, tgtlen,
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         printk("--> mds_prep_req: len %d, req %p, tgtlen %d\n", 
58                request->rq_reqlen, request->rq_req, 
59                request->rq_req->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 mds_request *req)
70 {
71         int rc;
72
73         /* XXX fix the race here (wait_for_event?)*/
74         /* hand the packet over to the server */
75         rc = mds_queue_req(req); 
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); 
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 mds_request *request)
99 {
100         kfree(request);
101 }
102
103 int mdc_getattr(ino_t ino, int type, int valid, 
104                 struct mds_rep  **rep, struct mds_rep_hdr **hdr)
105 {
106         struct mds_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->fid1, ino, 0, type);
116
117         request->rq_req->valid = valid;
118
119         rc = mds_queue_wait(request);
120         if (rc) { 
121                 printk("llight request: error in handling %d\n", rc); 
122                 goto out;
123         }
124
125         printk("mds_getattr: mode: %o\n", request->rq_rep->mode); 
126
127         if (rep) { 
128                 *rep = request->rq_rep;
129         }
130         if (hdr) { 
131                 *hdr = request->rq_rephdr;
132         }
133
134  out: 
135         mds_free_req(request);
136         return rc;
137 }
138
139 int mdc_readpage(ino_t ino, int type, __u64 offset, char *addr, 
140                 struct mds_rep  **rep, struct mds_rep_hdr **hdr)
141 {
142         struct mds_request *request;
143         struct niobuf niobuf;
144         int rc; 
145
146         niobuf.addr = (__u64) (long) addr;
147
148         printk("mdc_readpage: inode: %ld\n", ino); 
149
150         request = mds_prep_req(MDS_READPAGE, 0, NULL,
151                                sizeof(struct niobuf), (char *)&niobuf);
152         if (!request) { 
153                 printk("mdc request: cannot pack\n");
154                 return -ENOMEM;
155         }
156
157         request->rq_req->fid1.id = ino;
158         request->rq_req->fid1.f_type = type;
159         request->rq_req->size = offset;
160         request->rq_req->tgtlen = sizeof(niobuf); 
161
162         rc = mds_queue_wait(request);
163         if (rc) { 
164                 printk("mdc request: error in handling %d\n", rc); 
165                 goto out;
166         }
167
168         printk("mdc_readpage: mode: %o\n", request->rq_rep->mode); 
169
170         if (rep) { 
171                 *rep = request->rq_rep;
172         }
173         if (hdr) { 
174                 *hdr = request->rq_rephdr;
175         }
176
177  out: 
178         mds_free_req(request);
179         return rc;
180 }
181
182 int mdc_reint(struct mds_request *request)
183 {
184         int rc; 
185
186         rc = mds_queue_wait(request);
187         if (rc) { 
188                 printk("mdc request: error in handling %d\n", rc); 
189         }
190
191         return rc;
192 }
193
194
195 static int request_ioctl(struct inode *inode, struct file *file, 
196                        unsigned int cmd, unsigned long arg)
197 {
198         int err;
199
200         ENTRY;
201
202         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
203                 EXIT;
204                 return -EINVAL;
205         }
206
207         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
208              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
209              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
210                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
211                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
212                 EXIT;
213                 return -EINVAL;
214         }
215
216         
217         switch (cmd) {
218         case IOC_REQUEST_GETATTR: { 
219                 struct mds_rep_hdr *hdr = NULL;
220                 printk("-- getting attr for ino 2\n"); 
221                 err = mdc_getattr(2, S_IFDIR, ~0, NULL, &hdr);
222                 if (hdr)
223                         kfree(hdr);
224                 printk("-- done err %d\n", err);
225                 break;
226         }
227
228         case IOC_REQUEST_READPAGE: { 
229                 struct mds_rep_hdr *hdr = NULL;
230                 char *buf;
231                 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 
232                 if (!buf) { 
233                         err = -ENOMEM;
234                         break;
235                 }
236                 printk("-- readpage 0 for ino 2\n"); 
237                 err = mdc_readpage(2, S_IFDIR, 0, buf, NULL, &hdr);
238                 printk("-- done err %d\n", err);
239                 if (!err) { 
240                         printk("-- status: %d\n", hdr->status); 
241                         err = hdr->status;
242                         if (hdr) 
243                                 kfree(hdr);
244                 }
245                 kfree(buf); 
246                 break;
247         }
248
249         case IOC_REQUEST_SETATTR: { 
250                 struct inode inode;
251                 struct mds_rep_hdr *hdr;
252                 struct iattr iattr; 
253
254                 inode.i_ino = 2;
255                 iattr.ia_mode = 040777;
256                 iattr.ia_atime = 0;
257                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
258
259                 err = mdc_setattr(&inode, &iattr, NULL, &hdr);
260                 printk("-- done err %d\n", err);
261                 if (!err) { 
262                         printk("-- status: %d\n", hdr->status); 
263                         err = hdr->status;
264                 }
265                 kfree(hdr); 
266                 break;
267         }
268
269         default:                
270                 err = -EINVAL;
271                 EXIT;
272                 break;
273         }
274         EXIT;
275         return err;
276 }
277
278
279 static struct file_operations requestdev_fops = {
280         ioctl: request_ioctl,
281 };
282
283
284 static struct miscdevice request_dev = {
285         REQUEST_MINOR,
286         "request",
287         &requestdev_fops
288 };
289
290
291 static int __init mds_request_init(void)
292 {
293         misc_register(&request_dev);
294         return 0 ;
295 }
296
297
298 static void __exit mds_request_exit(void)
299 {
300         misc_deregister(&request_dev);
301 }
302
303 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
304 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
305 MODULE_LICENSE("GPL");
306
307 EXPORT_SYMBOL(mdc_getattr); 
308 EXPORT_SYMBOL(mdc_readpage); 
309 EXPORT_SYMBOL(mdc_setattr); 
310
311 module_init(mds_request_init);
312 module_exit(mds_request_exit);