Whamcloud - gitweb
- add open and close to MDC interface (not used yet)
[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
57 int mdc_getattr(struct ptlrpc_client *peer, ino_t ino, int type, int valid, 
58                 struct mds_rep  **rep, struct ptlrep_hdr **hdr)
59 {
60         struct ptlrpc_request *request;
61         int rc; 
62
63         request = ptlrpc_prep_req(peer, MDS_GETATTR, 0, NULL, 0, NULL); 
64         if (!request) { 
65                 CERROR("llight request: cannot pack\n");
66                 return -ENOMEM;
67         }
68
69         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
70
71         request->rq_req.mds->valid = valid;
72         request->rq_replen = 
73                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
74
75         rc = ptlrpc_queue_wait(request, peer);
76         if (rc) { 
77                 CERROR("llight request: error in handling %d\n", rc); 
78                 goto out;
79         }
80
81         CDEBUG(0, "mode: %o\n", request->rq_rep.mds->mode);
82
83         if (rep) { 
84                 *rep = request->rq_rep.mds;
85         }
86         if (hdr) { 
87                 *hdr = request->rq_rephdr;
88         }
89
90  out: 
91         ptlrpc_free_req(request);
92         return rc;
93 }
94
95 int mdc_open(struct ptlrpc_client *peer, ino_t ino, int type, int flags,
96                 __u64 *fh, struct mds_rep  **rep, struct ptlrep_hdr **hdr)
97 {
98         struct ptlrpc_request *request;
99         int rc; 
100
101         request = ptlrpc_prep_req(peer, MDS_OPEN, 0, NULL, 0, NULL); 
102         if (!request) { 
103                 CERROR("llight request: cannot pack\n");
104                 return -ENOMEM;
105         }
106
107         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
108         request->rq_req.mds->flags = flags;
109         request->rq_replen = 
110                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
111
112         rc = ptlrpc_queue_wait(request, peer);
113         if (rc) { 
114                 CERROR("llight request: error in handling %d\n", rc); 
115                 goto out;
116         }
117
118         if (rep) { 
119                 *rep = request->rq_rep.mds;
120         }
121         if (hdr) { 
122                 *hdr = request->rq_rephdr;
123         }
124         *fh = request->rq_rep.mds->objid; 
125
126  out: 
127         ptlrpc_free_req(request);
128         return rc;
129 }
130
131
132 int mdc_close(struct ptlrpc_client *peer, ino_t ino, int type, __u64 fh, 
133                 struct mds_rep  **rep, struct ptlrep_hdr **hdr)
134 {
135         struct ptlrpc_request *request;
136         int rc; 
137
138         request = ptlrpc_prep_req(peer, MDS_CLOSE, 0, NULL, 0, NULL); 
139         if (!request) { 
140                 CERROR("llight request: cannot pack\n");
141                 return -ENOMEM;
142         }
143
144         ll_ino2fid(&request->rq_req.mds->fid1, ino, 0, type);
145         request->rq_req.mds->objid = fh; 
146         request->rq_replen = 
147                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
148
149         rc = ptlrpc_queue_wait(request, peer);
150         if (rc) { 
151                 CERROR("llight request: error in handling %d\n", rc); 
152                 goto out;
153         }
154
155         if (rep) { 
156                 *rep = request->rq_rep.mds;
157         }
158         if (hdr) { 
159                 *hdr = request->rq_rephdr;
160         }
161
162  out: 
163         ptlrpc_free_req(request);
164         return rc;
165 }
166
167 int mdc_readpage(struct ptlrpc_client *peer, ino_t ino, int type, __u64 offset,
168                  char *addr, struct mds_rep  **rep, struct ptlrep_hdr **hdr)
169 {
170         struct ptlrpc_request *request;
171         struct niobuf niobuf;
172         int rc; 
173
174         niobuf.addr = (__u64) (long) addr;
175
176         CDEBUG(D_INODE, "inode: %ld\n", ino);
177
178         request = ptlrpc_prep_req(peer, MDS_READPAGE, 0, NULL,
179                                sizeof(struct niobuf), (char *)&niobuf);
180         if (!request) { 
181                 CERROR("mdc request: cannot pack\n");
182                 return -ENOMEM;
183         }
184
185         request->rq_req.mds->fid1.id = ino;
186         request->rq_req.mds->fid1.f_type = type;
187         request->rq_req.mds->size = offset;
188         request->rq_req.mds->tgtlen = sizeof(niobuf); 
189
190         //request->rq_bulklen = PAGE_SIZE;
191         //request->rq_bulkbuf = (void *)(long)niobuf.addr;
192         request->rq_bulk_portal = MDS_BULK_PORTAL;
193         request->rq_replen = 
194                 sizeof(struct ptlrep_hdr) + sizeof(struct mds_rep);
195
196         rc = ptlrpc_queue_wait(request, peer);
197         if (rc) { 
198                 CERROR("mdc request: error in handling %d\n", rc); 
199                 goto out;
200         }
201
202         CDEBUG(0, "mode: %o\n", request->rq_rep.mds->mode);
203
204         if (rep) { 
205                 *rep = request->rq_rep.mds;
206         }
207         if (hdr) { 
208                 *hdr = request->rq_rephdr;
209         }
210
211  out: 
212         ptlrpc_free_req(request);
213         return rc;
214 }
215
216 int mdc_reint(struct ptlrpc_client *peer, struct ptlrpc_request *request)
217 {
218         int rc; 
219
220         rc = ptlrpc_queue_wait(request, peer);
221         if (rc) { 
222                 CERROR("mdc request: error in handling %d\n", rc); 
223         }
224
225         return rc;
226 }
227
228 int mdc_create_client(char *uuid, struct ptlrpc_client *cl)
229 {
230         int err; 
231
232         memset(cl, 0, sizeof(*cl));
233         cl->cli_xid = 0;
234         cl->cli_rep_unpack = mds_unpack_rep;
235         cl->cli_req_pack = mds_pack_req;
236         err = kportal_uuid_to_peer("mds", &cl->cli_server);
237         if (err == 0) { 
238                 cl->cli_request_portal = MDS_REQUEST_PORTAL;
239                 cl->cli_reply_portal = MDS_REPLY_PORTAL;
240                 
241         } else { 
242                 cl->cli_enqueue = mds_queue_req;
243         }
244         return 0;
245 }
246
247 static int request_ioctl(struct inode *inode, struct file *file, 
248                          unsigned int cmd, unsigned long arg)
249 {
250         int err;
251         struct ptlrpc_client peer;
252
253         ENTRY;
254
255         if (MINOR(inode->i_rdev) != REQUEST_MINOR) {
256                 EXIT;
257                 return -EINVAL;
258         }
259
260         if ( _IOC_TYPE(cmd) != IOC_REQUEST_TYPE || 
261              _IOC_NR(cmd) < IOC_REQUEST_MIN_NR  || 
262              _IOC_NR(cmd) > IOC_REQUEST_MAX_NR ) {
263                 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
264                                 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
265                 EXIT;
266                 return -EINVAL;
267         }
268
269         err = mdc_create_client("mds", &peer);
270         if (err) {
271                 CERROR("cannot create client"); 
272                 return -EINVAL;
273         }
274         
275         switch (cmd) {
276         case IOC_REQUEST_GETATTR: { 
277                 struct ptlrep_hdr *hdr = NULL;
278                 CERROR("-- getting attr for ino 2\n"); 
279                 err = mdc_getattr(&peer, 2, S_IFDIR, ~0, NULL, &hdr);
280                 if (hdr) {
281                         /* FIXME: there must be a better way to get the size */
282                         OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
283                                  sizeof(struct mds_rep));
284                 }
285                 CERROR("-- done err %d\n", err);
286                 break;
287         }
288
289         case IOC_REQUEST_READPAGE: { 
290                 struct ptlrep_hdr *hdr = NULL;
291                 char *buf;
292                 OBD_ALLOC(buf, PAGE_SIZE);
293                 if (!buf) { 
294                         err = -ENOMEM;
295                         break;
296                 }
297                 CERROR("-- readpage 0 for ino 2\n"); 
298                 err = mdc_readpage(&peer, 2, S_IFDIR, 0, buf, NULL, &hdr);
299                 CERROR("-- done err %d\n", err);
300                 if (!err) { 
301                         CERROR("-- status: %d\n", hdr->status); 
302                         err = hdr->status;
303                         if (hdr)
304                                 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
305                                          sizeof(struct mds_rep));
306                 }
307                 OBD_FREE(buf, PAGE_SIZE);
308                 break;
309         }
310
311         case IOC_REQUEST_SETATTR: { 
312                 struct inode inode;
313                 struct ptlrep_hdr *hdr;
314                 struct iattr iattr; 
315
316                 inode.i_ino = 2;
317                 iattr.ia_mode = 040777;
318                 iattr.ia_atime = 0;
319                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
320
321                 err = mdc_setattr(&peer, &inode, &iattr, NULL, &hdr);
322                 CERROR("-- done err %d\n", err);
323                 if (!err) { 
324                         CERROR("-- status: %d\n", hdr->status); 
325                         err = hdr->status;
326                 } else {
327                         OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
328                                  sizeof(struct mds_rep));
329                 }
330                 break;
331         }
332
333         case IOC_REQUEST_CREATE: { 
334                 struct inode inode;
335                 struct ptlrep_hdr *hdr;
336                 struct iattr iattr; 
337
338                 inode.i_ino = 2;
339                 iattr.ia_mode = 040777;
340                 iattr.ia_atime = 0;
341                 iattr.ia_valid = ATTR_MODE | ATTR_ATIME;
342
343                 err = mdc_create(&peer, &inode, 
344                                  "foofile", strlen("foofile"), 
345                                  NULL, 0, 0100707, 47114711, 
346                                  11, 47, 0, NULL, &hdr);
347                 CERROR("-- done err %d\n", err);
348                 if (!err) { 
349                         CERROR("-- status: %d\n", hdr->status); 
350                         err = hdr->status;
351                 }
352                 OBD_FREE(hdr, sizeof(struct ptlrep_hdr) +
353                          sizeof(struct mds_rep));
354                 break;
355         }
356
357         default:                
358                 err = -EINVAL;
359                 EXIT;
360                 break;
361         }
362         EXIT;
363         return err;
364 }
365
366
367 static struct file_operations requestdev_fops = {
368         ioctl: request_ioctl,
369 };
370
371
372 static struct miscdevice request_dev = {
373         REQUEST_MINOR,
374         "request",
375         &requestdev_fops
376 };
377
378
379 static int __init ptlrpc_request_init(void)
380 {
381         misc_register(&request_dev);
382         return 0 ;
383 }
384
385
386 static void __exit ptlrpc_request_exit(void)
387 {
388         misc_deregister(&request_dev);
389 }
390
391 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
392 MODULE_DESCRIPTION("Lustre MDS Request Tester v1.0");
393 MODULE_LICENSE("GPL");
394
395 EXPORT_SYMBOL(mdc_create_client); 
396 EXPORT_SYMBOL(mdc_create); 
397 EXPORT_SYMBOL(mdc_unlink); 
398 EXPORT_SYMBOL(mdc_rename); 
399 EXPORT_SYMBOL(mdc_link); 
400 EXPORT_SYMBOL(mdc_getattr); 
401 EXPORT_SYMBOL(mdc_readpage); 
402 EXPORT_SYMBOL(mdc_setattr); 
403
404 module_init(ptlrpc_request_init);
405 module_exit(ptlrpc_request_exit);