Whamcloud - gitweb
The most tedious patch in history. Changes almost all of the 'printk's to
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Portals, http://www.sf.net/projects/lustre/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/errno.h>
32 #include <linux/locks.h>
33 #include <linux/unistd.h>
34
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include <linux/module.h>
38
39 #include <linux/fs.h>
40 #include <linux/stat.h>
41 #include <asm/uaccess.h>
42 #include <asm/segment.h>
43 #include <linux/miscdevice.h>
44
45 #define DEBUG_SUBSYSTEM S_MDC
46
47 #include <linux/obd_support.h>
48 #include <linux/lustre_lib.h>
49 #include <linux/lustre_idl.h>
50 #include <linux/lustre_mds.h>
51
52 #define REQUEST_MINOR 244
53
54 extern int mds_queue_req(struct ptlrpc_request *);
55
56 /* FIXME: this belongs in some sort of service struct */
57 static int mdc_xid = 0;
58
59 struct ptlrpc_request *mds_prep_req(int opcode, int namelen, char *name,
60                                     int tgtlen, char *tgt)
61 {
62         struct ptlrpc_request *request;
63         int rc;
64         ENTRY; 
65
66         OBD_ALLOC(request, sizeof(*request));
67         if (!request) { 
68                 CERROR("request allocation out of memory\n");
69                 return NULL;
70         }
71
72         memset(request, 0, sizeof(*request));
73         request->rq_xid = mdc_xid++;
74
75         rc = mds_pack_req(name, namelen, tgt, tgtlen,
76                           &request->rq_reqhdr, &(request->rq_req.mds),
77                           &request->rq_reqlen, &request->rq_reqbuf);
78         if (rc) { 
79                 CERROR("llight request: cannot pack request %d\n", rc); 
80                 return NULL;
81         }
82         CDEBUG(0, "--> len %d, req %p, tgtlen %d\n", 
83                request->rq_reqlen, request->rq_req.mds, 
84                request->rq_req.mds->tgtlen);
85         request->rq_reqhdr->opc = opcode;
86
87         EXIT;
88         return request;
89 }
90
91 static int mds_queue_wait(struct ptlrpc_request *req, struct lustre_peer *peer)
92 {
93         int rc;
94
95         /* XXX fix the race here (wait_for_event?)*/
96         if (peer == NULL) {
97                 /* Local delivery */
98                 ENTRY;
99                 rc = mds_queue_req(req); 
100         } else {
101                 /* Remote delivery via portals. */
102                 req->rq_req_portal = MDS_REQUEST_PORTAL;
103                 req->rq_reply_portal = MDS_REPLY_PORTAL;
104                 rc = ptl_send_rpc(req, peer);
105         }
106         if (rc) { 
107                 CERROR("error %d, opcode %d\n", rc, 
108                        req->rq_reqhdr->opc); 
109                 return -rc;
110         }
111
112         init_waitqueue_head(&req->rq_wait_for_rep);
113         CDEBUG(0, "-- sleeping\n");
114         interruptible_sleep_on(&req->rq_wait_for_rep);
115         CDEBUG(0, "-- done\n");
116
117         rc = mds_unpack_rep(req->rq_repbuf, req->rq_replen, &req->rq_rephdr, 
118                             &req->rq_rep.mds);
119         if (rc) {
120                 CERROR("mds_unpack_rep failed: %d\n", rc);
121                 return rc;
122         }
123
124         if ( req->rq_rephdr->status == 0 )
125                 CDEBUG(0, "--> buf %p len %d status %d\n",
126                        req->rq_repbuf, req->rq_replen, 
127                        req->rq_rephdr->status); 
128
129         EXIT;
130         return 0;
131 }
132
133 void mdc_free_req(struct ptlrpc_request *request)
134 {
135         OBD_FREE(request, sizeof(*request));
136 }
137
138 int mdc_getattr(struct lustre_peer *peer, ino_t ino, int type, int valid, 
139                 struct mds_rep  **rep, struct ptlrep_hdr **hdr)
140 {
141         struct ptlrpc_request *request;
142         int rc; 
143
144         request = mds_prep_req(MDS_GETATTR, 0, NULL, 0, NULL); 
145         if (!request) { 
146                 CERROR("llight request: cannot pack\n");
147                 return -ENOMEM;
148         }
149
150         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
151
152         request->rq_req.mds->valid = valid;
153         request->rq_replen = 
154                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
155
156         rc = mds_queue_wait(request, peer);
157         if (rc) { 
158                 CERROR("llight request: error in handling %d\n", rc); 
159                 goto out;
160         }
161
162         CDEBUG(0, "mode: %o\n", request->rq_rep.mds->mode);
163
164         if (rep) { 
165                 *rep = request->rq_rep.mds;
166         }
167         if (hdr) { 
168                 *hdr = request->rq_rephdr;
169         }
170
171  out: 
172         mdc_free_req(request);
173         return rc;
174 }
175
176 int mdc_readpage(struct lustre_peer *peer, ino_t ino, int type, __u64 offset,
177                  char *addr, struct mds_rep  **rep, struct ptlrep_hdr **hdr)
178 {
179         struct ptlrpc_request *request;
180         struct niobuf niobuf;
181         int rc; 
182
183         niobuf.addr = (__u64) (long) addr;
184
185         CDEBUG(D_INODE, "inode: %ld\n", ino);
186
187         request = mds_prep_req(MDS_READPAGE, 0, NULL,
188                                sizeof(struct niobuf), (char *)&niobuf);
189         if (!request) { 
190                 CERROR("mdc request: cannot pack\n");
191                 return -ENOMEM;
192         }
193
194         request->rq_req.mds->fid1.id = ino;
195         request->rq_req.mds->fid1.f_type = type;
196         request->rq_req.mds->size = offset;
197         request->rq_req.mds->tgtlen = sizeof(niobuf); 
198
199         request->rq_bulk_portal = MDS_BULK_PORTAL;
200         request->rq_replen = 
201                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
202
203         rc = mds_queue_wait(request, peer);
204         if (rc) { 
205                 CERROR("mdc request: error in handling %d\n", rc); 
206                 goto out;
207         }
208
209         CDEBUG(0, "mode: %o\n", request->rq_rep.mds->mode);
210
211         if (rep) { 
212                 *rep = request->rq_rep.mds;
213         }
214         if (hdr) { 
215                 *hdr = request->rq_rephdr;
216         }
217
218  out: 
219         mdc_free_req(request);
220         return rc;
221 }
222
223 int mdc_reint(struct lustre_peer *peer, struct ptlrpc_request *request)
224 {
225         int rc; 
226
227         rc = mds_queue_wait(request, peer);
228         if (rc) { 
229                 CERROR("mdc request: error in handling %d\n", rc); 
230         }
231
232         return rc;
233 }
234
235
236 static int request_ioctl(struct inode *inode, struct file *file, 
237                          unsigned int cmd, unsigned long arg)
238 {
239         int err;
240         struct lustre_peer peer, *peer_ptr = NULL;
241
242         ENTRY;
243
244         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
245                 EXIT;
246                 return -EINVAL;
247         }
248
249         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
250              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
251              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
252                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
253                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
254                 EXIT;
255                 return -EINVAL;
256         }
257
258         err = kportal_uuid_to_peer("mds", &peer);
259         if (err == 0)
260                 peer_ptr = &peer;
261         
262         switch (cmd) {
263         case IOC_REQUEST_GETATTR: { 
264                 struct ptlrep_hdr *hdr = NULL;
265                 CERROR("-- getting attr for ino 2\n"); 
266                 err = mdc_getattr(peer_ptr, 2, S_IFDIR, ~0, NULL, &hdr);
267                 if (hdr) {
268                         /* FIXME: there must be a better way to get the size */
269                         OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
270                                  sizeof(struct mds_rep));
271                 }
272                 CERROR("-- done err %d\n", err);
273                 break;
274         }
275
276         case IOC_REQUEST_READPAGE: { 
277                 struct ptlrep_hdr *hdr = NULL;
278                 char *buf;
279                 OBD_ALLOC(buf, PAGE_SIZE);
280                 if (!buf) { 
281                         err = -ENOMEM;
282                         break;
283                 }
284                 CERROR("-- readpage 0 for ino 2\n"); 
285                 err = mdc_readpage(peer_ptr, 2, S_IFDIR, 0, buf, NULL, &hdr);
286                 CERROR("-- done err %d\n", err);
287                 if (!err) { 
288                         CERROR("-- status: %d\n", hdr->status); 
289                         err = hdr->status;
290                         if (hdr)
291                                 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
292                                          sizeof(struct mds_rep));
293                 }
294                 OBD_FREE(buf, PAGE_SIZE);
295                 break;
296         }
297
298         case IOC_REQUEST_SETATTR: { 
299                 struct inode inode;
300                 struct ptlrep_hdr *hdr;
301                 struct iattr iattr; 
302
303                 inode.i_ino = 2;
304                 iattr.ia_mode = 040777;
305                 iattr.ia_atime = 0;
306                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
307
308                 err = mdc_setattr(peer_ptr, &inode, &iattr, NULL, &hdr);
309                 CERROR("-- done err %d\n", err);
310                 if (!err) { 
311                         CERROR("-- status: %d\n", hdr->status); 
312                         err = hdr->status;
313                 } else {
314                         OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
315                                  sizeof(struct mds_rep));
316                 }
317                 break;
318         }
319
320         case IOC_REQUEST_CREATE: { 
321                 struct inode inode;
322                 struct ptlrep_hdr *hdr;
323                 struct iattr iattr; 
324
325                 inode.i_ino = 2;
326                 iattr.ia_mode = 040777;
327                 iattr.ia_atime = 0;
328                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
329
330                 err = mdc_create(peer_ptr, &inode, 
331                                  "foofile", strlen("foofile"), 
332                                  NULL, 0, 0100707, 47114711, 
333                                  11, 47, 0, NULL, &hdr);
334                 CERROR("-- done err %d\n", err);
335                 if (!err) { 
336                         CERROR("-- status: %d\n", hdr->status); 
337                         err = hdr->status;
338                 }
339                 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
340                          sizeof(struct mds_rep));
341                 break;
342         }
343
344         default:                
345                 err = -EINVAL;
346                 EXIT;
347                 break;
348         }
349         EXIT;
350         return err;
351 }
352
353
354 static struct file_operations requestdev_fops = {
355         ioctl: request_ioctl,
356 };
357
358
359 static struct miscdevice request_dev = {
360         REQUEST_MINOR,
361         "request",
362         &requestdev_fops
363 };
364
365
366 static int __init ptlrpc_request_init(void)
367 {
368         misc_register(&request_dev);
369         return 0 ;
370 }
371
372
373 static void __exit ptlrpc_request_exit(void)
374 {
375         misc_deregister(&request_dev);
376 }
377
378 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
379 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
380 MODULE_LICENSE("GPL");
381
382 EXPORT_SYMBOL(mdc_create); 
383 EXPORT_SYMBOL(mdc_unlink); 
384 EXPORT_SYMBOL(mdc_rename); 
385 EXPORT_SYMBOL(mdc_link); 
386 EXPORT_SYMBOL(mdc_getattr); 
387 EXPORT_SYMBOL(mdc_readpage); 
388 EXPORT_SYMBOL(mdc_setattr); 
389
390 module_init(ptlrpc_request_init);
391 module_exit(ptlrpc_request_exit);