blob: 787ccddb85ea6cd8c53550deef5a11e3336a4ee1 [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
2 * net/9p/clnt.c
3 *
4 * 9P Client
5 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06006 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05007 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060029#include <linux/poll.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050030#include <linux/idr.h>
31#include <linux/mutex.h>
32#include <linux/sched.h>
33#include <linux/uaccess.h>
34#include <net/9p/9p.h>
Eric Van Hensbergenfb0466c2007-10-17 14:31:07 -050035#include <linux/parser.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050036#include <net/9p/client.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050037#include <net/9p/transport.h>
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050038#include "protocol.h"
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050039
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060040/*
41 * Client Option Parsing (code inspired by NFS code)
42 * - a little lazy - parse all client options
43 */
44
45enum {
46 Opt_msize,
47 Opt_trans,
48 Opt_legacy,
49 Opt_err,
50};
51
Steven Whitehousea447c092008-10-13 10:46:57 +010052static const match_table_t tokens = {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060053 {Opt_msize, "msize=%u"},
54 {Opt_legacy, "noextend"},
55 {Opt_trans, "trans=%s"},
56 {Opt_err, NULL},
57};
58
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050059static struct p9_req_t *
60p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -050061
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060062/**
63 * v9fs_parse_options - parse mount options into session structure
64 * @options: options string passed from mount
65 * @v9ses: existing v9fs session information
66 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060067 * Return 0 upon success, -ERRNO upon failure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060068 */
69
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060070static int parse_opts(char *opts, struct p9_client *clnt)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060071{
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060072 char *options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060073 char *p;
74 substring_t args[MAX_OPT_ARGS];
75 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060076 int ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060077
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060078 clnt->dotu = 1;
79 clnt->msize = 8192;
80
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060081 if (!opts)
82 return 0;
83
84 options = kstrdup(opts, GFP_KERNEL);
85 if (!options) {
86 P9_DPRINTK(P9_DEBUG_ERROR,
87 "failed to allocate copy of option string\n");
88 return -ENOMEM;
89 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060090
91 while ((p = strsep(&options, ",")) != NULL) {
92 int token;
93 if (!*p)
94 continue;
95 token = match_token(p, tokens, args);
96 if (token < Opt_trans) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060097 int r = match_int(&args[0], &option);
98 if (r < 0) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060099 P9_DPRINTK(P9_DEBUG_ERROR,
100 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600101 ret = r;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600102 continue;
103 }
104 }
105 switch (token) {
106 case Opt_msize:
107 clnt->msize = option;
108 break;
109 case Opt_trans:
Tejun Heo72029fe2008-09-24 16:22:23 -0500110 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600111 break;
112 case Opt_legacy:
113 clnt->dotu = 0;
114 break;
115 default:
116 continue;
117 }
118 }
Tejun Heo72029fe2008-09-24 16:22:23 -0500119
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600120 kfree(options);
121 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600122}
123
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500124/**
125 * p9_tag_alloc - lookup/allocate a request by tag
126 * @c: client session to lookup tag within
127 * @tag: numeric id for transaction
128 *
129 * this is a simple array lookup, but will grow the
130 * request_slots as necessary to accomodate transaction
131 * ids which did not previously have a slot.
132 *
133 * this code relies on the client spinlock to manage locks, its
134 * possible we should switch to something else, but I'd rather
135 * stick with something low-overhead for the common case.
136 *
137 */
138
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500139static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500140{
141 unsigned long flags;
142 int row, col;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500143 struct p9_req_t *req;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500144
145 /* This looks up the original request by tag so we know which
146 * buffer to read the data into */
147 tag++;
148
149 if (tag >= c->max_tag) {
150 spin_lock_irqsave(&c->lock, flags);
151 /* check again since original check was outside of lock */
152 while (tag >= c->max_tag) {
153 row = (tag / P9_ROW_MAXTAG);
154 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
155 sizeof(struct p9_req_t), GFP_ATOMIC);
156
157 if (!c->reqs[row]) {
158 printk(KERN_ERR "Couldn't grow tag array\n");
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500159 spin_unlock_irqrestore(&c->lock, flags);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500160 return ERR_PTR(-ENOMEM);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500161 }
162 for (col = 0; col < P9_ROW_MAXTAG; col++) {
163 c->reqs[row][col].status = REQ_STATUS_IDLE;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500164 c->reqs[row][col].tc = NULL;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500165 }
166 c->max_tag += P9_ROW_MAXTAG;
167 }
168 spin_unlock_irqrestore(&c->lock, flags);
169 }
170 row = tag / P9_ROW_MAXTAG;
171 col = tag % P9_ROW_MAXTAG;
172
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500173 req = &c->reqs[row][col];
174 if (!req->tc) {
175 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
176 if (!req->wq) {
177 printk(KERN_ERR "Couldn't grow tag array\n");
178 return ERR_PTR(-ENOMEM);
179 }
180 init_waitqueue_head(req->wq);
181 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize,
182 GFP_KERNEL);
183 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize,
184 GFP_KERNEL);
185 if ((!req->tc) || (!req->rc)) {
186 printk(KERN_ERR "Couldn't grow tag array\n");
187 kfree(req->tc);
188 kfree(req->rc);
Tom Tucker45abdf12008-10-23 16:33:25 -0500189 kfree(req->wq);
190 req->tc = req->rc = NULL;
191 req->wq = NULL;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500192 return ERR_PTR(-ENOMEM);
193 }
194 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
195 req->tc->capacity = c->msize;
196 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
197 req->rc->capacity = c->msize;
198 }
199
200 p9pdu_reset(req->tc);
201 p9pdu_reset(req->rc);
202
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500203 req->tc->tag = tag-1;
204 req->status = REQ_STATUS_ALLOC;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500205
206 return &c->reqs[row][col];
207}
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500208
209/**
210 * p9_tag_lookup - lookup a request by tag
211 * @c: client session to lookup tag within
212 * @tag: numeric id for transaction
213 *
214 */
215
216struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
217{
218 int row, col;
219
220 /* This looks up the original request by tag so we know which
221 * buffer to read the data into */
222 tag++;
223
224 BUG_ON(tag >= c->max_tag);
225
226 row = tag / P9_ROW_MAXTAG;
227 col = tag % P9_ROW_MAXTAG;
228
229 return &c->reqs[row][col];
230}
231EXPORT_SYMBOL(p9_tag_lookup);
232
233/**
234 * p9_tag_init - setup tags structure and contents
235 * @tags: tags structure from the client struct
236 *
237 * This initializes the tags structure for each client instance.
238 *
239 */
240
241static int p9_tag_init(struct p9_client *c)
242{
243 int err = 0;
244
245 c->tagpool = p9_idpool_create();
246 if (IS_ERR(c->tagpool)) {
247 err = PTR_ERR(c->tagpool);
248 c->tagpool = NULL;
249 goto error;
250 }
251
252 p9_idpool_get(c->tagpool); /* reserve tag 0 */
253
254 c->max_tag = 0;
255error:
256 return err;
257}
258
259/**
260 * p9_tag_cleanup - cleans up tags structure and reclaims resources
261 * @tags: tags structure from the client struct
262 *
263 * This frees resources associated with the tags structure
264 *
265 */
266static void p9_tag_cleanup(struct p9_client *c)
267{
268 int row, col;
269
270 /* check to insure all requests are idle */
271 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
272 for (col = 0; col < P9_ROW_MAXTAG; col++) {
273 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
274 P9_DPRINTK(P9_DEBUG_MUX,
275 "Attempting to cleanup non-free tag %d,%d\n",
276 row, col);
277 /* TODO: delay execution of cleanup */
278 return;
279 }
280 }
281 }
282
283 if (c->tagpool)
284 p9_idpool_destroy(c->tagpool);
285
286 /* free requests associated with tags */
287 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500288 for (col = 0; col < P9_ROW_MAXTAG; col++) {
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500289 kfree(c->reqs[row][col].wq);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500290 kfree(c->reqs[row][col].tc);
291 kfree(c->reqs[row][col].rc);
292 }
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500293 kfree(c->reqs[row]);
294 }
295 c->max_tag = 0;
296}
297
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500298/**
299 * p9_free_req - free a request and clean-up as necessary
300 * c: client state
301 * r: request to release
302 *
303 */
304
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500305static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500306{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500307 int tag = r->tc->tag;
308 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
309
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500310 r->status = REQ_STATUS_IDLE;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500311 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
312 p9_idpool_put(tag, c->tagpool);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500313}
314
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500315/**
316 * p9_client_cb - call back from transport to client
317 * c: client state
318 * req: request received
319 *
320 */
321void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
322{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500323 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500324 wake_up(req->wq);
325 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500326}
327EXPORT_SYMBOL(p9_client_cb);
328
329/**
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500330 * p9_parse_header - parse header arguments out of a packet
331 * @pdu: packet to parse
332 * @size: size of packet
333 * @type: type of request
334 * @tag: tag of packet
335 * @rewind: set if we need to rewind offset afterwards
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500336 */
337
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500338int
339p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
340 int rewind)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500341{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500342 int8_t r_type;
343 int16_t r_tag;
344 int32_t r_size;
345 int offset = pdu->offset;
346 int err;
347
348 pdu->offset = 0;
349 if (pdu->size == 0)
350 pdu->size = 7;
351
352 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
353 if (err)
354 goto rewind_and_exit;
355
356 pdu->size = r_size;
357 pdu->id = r_type;
358 pdu->tag = r_tag;
359
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500360 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
361 pdu->id, pdu->tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500362
363 if (type)
364 *type = r_type;
365 if (tag)
366 *tag = r_tag;
367 if (size)
368 *size = r_size;
369
370
371rewind_and_exit:
372 if (rewind)
373 pdu->offset = offset;
374 return err;
375}
376EXPORT_SYMBOL(p9_parse_header);
377
378/**
379 * p9_check_errors - check 9p packet for error return and process it
380 * @c: current client instance
381 * @req: request to parse and check for error conditions
382 *
383 * returns error code if one is discovered, otherwise returns 0
384 *
385 * this will have to be more complicated if we have multiple
386 * error packet types
387 */
388
389static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
390{
391 int8_t type;
392 int err;
393
394 err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
395 if (err) {
396 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
397 return err;
398 }
399
400 if (type == P9_RERROR) {
401 int ecode;
402 char *ename;
403
404 err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode);
405 if (err) {
406 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
407 err);
408 return err;
409 }
410
411 if (c->dotu)
412 err = -ecode;
413
414 if (!err) {
415 err = p9_errstr2errno(ename, strlen(ename));
416
417 /* string match failed */
418 if (!err)
419 err = -ESERVERFAULT;
420 }
421
422 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
423
424 kfree(ename);
425 } else
426 err = 0;
427
428 return err;
429}
430
431/**
432 * p9_client_flush - flush (cancel) a request
433 * c: client state
434 * req: request to cancel
435 *
436 * This sents a flush for a particular requests and links
437 * the flush request to the original request. The current
438 * code only supports a single flush request although the protocol
439 * allows for multiple flush requests to be sent for a single request.
440 *
441 */
442
443static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
444{
445 struct p9_req_t *req;
446 int16_t oldtag;
447 int err;
448
449 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
450 if (err)
451 return err;
452
453 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
454
455 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
456 if (IS_ERR(req))
457 return PTR_ERR(req);
458
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500459
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500460 /* if we haven't received a response for oldreq,
461 remove it from the list. */
462 spin_lock(&c->lock);
463 if (oldreq->status == REQ_STATUS_FLSH)
464 list_del(&oldreq->req_list);
465 spin_unlock(&c->lock);
466
467 p9_free_req(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500468 return 0;
469}
470
471/**
472 * p9_client_rpc - issue a request and wait for a response
473 * @c: client session
474 * @type: type of request
475 * @fmt: protocol format string (see protocol.c)
476 *
477 * Returns request structure (which client must free using p9_free_req)
478 */
479
480static struct p9_req_t *
481p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
482{
483 va_list ap;
484 int tag, err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500485 struct p9_req_t *req;
486 unsigned long flags;
487 int sigpending;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500488
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500489 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500490
491 if (c->status != Connected)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500492 return ERR_PTR(-EIO);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500493
494 if (signal_pending(current)) {
495 sigpending = 1;
496 clear_thread_flag(TIF_SIGPENDING);
497 } else
498 sigpending = 0;
499
500 tag = P9_NOTAG;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500501 if (type != P9_TVERSION) {
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500502 tag = p9_idpool_get(c->tagpool);
503 if (tag < 0)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500504 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500505 }
506
507 req = p9_tag_alloc(c, tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500508 if (IS_ERR(req))
509 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500510
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500511 /* marshall the data */
512 p9pdu_prepare(req->tc, tag, type);
513 va_start(ap, fmt);
514 err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap);
515 va_end(ap);
516 p9pdu_finalize(req->tc);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500517
518 err = c->trans_mod->request(c, req);
519 if (err < 0) {
520 c->status = Disconnected;
521 goto reterr;
522 }
523
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500524 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500525 err = wait_event_interruptible(*req->wq,
526 req->status >= REQ_STATUS_RCVD);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500527 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
528 req->wq, tag, err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500529
530 if (req->status == REQ_STATUS_ERROR) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500531 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500532 err = req->t_err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500533 }
534
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500535 if ((err == -ERESTARTSYS) && (c->status == Connected)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500536 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500537 sigpending = 1;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500538 clear_thread_flag(TIF_SIGPENDING);
539
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500540 if (c->trans_mod->cancel(c, req))
541 p9_client_flush(c, req);
542
543 /* if we received the response anyway, don't signal error */
544 if (req->status == REQ_STATUS_RCVD)
545 err = 0;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500546 }
547
548 if (sigpending) {
549 spin_lock_irqsave(&current->sighand->siglock, flags);
550 recalc_sigpending();
551 spin_unlock_irqrestore(&current->sighand->siglock, flags);
552 }
553
554 if (err < 0)
555 goto reterr;
556
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500557 err = p9_check_errors(c, req);
558 if (!err) {
559 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
560 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500561 }
562
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500563reterr:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500564 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
565 err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500566 p9_free_req(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500567 return ERR_PTR(err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500568}
569
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500570static struct p9_fid *p9_fid_create(struct p9_client *clnt)
571{
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500572 int ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500573 struct p9_fid *fid;
Tom Tuckercac23d62008-10-23 16:31:02 -0500574 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500575
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500576 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500577 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
578 if (!fid)
579 return ERR_PTR(-ENOMEM);
580
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500581 ret = p9_idpool_get(clnt->fidpool);
Roel Kluin24e94de2009-01-18 21:32:11 -0800582 if (ret < 0) {
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500583 ret = -ENOSPC;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500584 goto error;
585 }
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500586 fid->fid = ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500587
588 memset(&fid->qid, 0, sizeof(struct p9_qid));
589 fid->mode = -1;
590 fid->rdir_fpos = 0;
David Howellsf8b9d532008-11-14 10:38:44 +1100591 fid->uid = current_fsuid();
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500592 fid->clnt = clnt;
593 fid->aux = NULL;
594
Tom Tuckercac23d62008-10-23 16:31:02 -0500595 spin_lock_irqsave(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500596 list_add(&fid->flist, &clnt->fidlist);
Tom Tuckercac23d62008-10-23 16:31:02 -0500597 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500598
599 return fid;
600
601error:
602 kfree(fid);
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500603 return ERR_PTR(ret);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500604}
605
606static void p9_fid_destroy(struct p9_fid *fid)
607{
608 struct p9_client *clnt;
Tom Tuckercac23d62008-10-23 16:31:02 -0500609 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500610
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500611 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500612 clnt = fid->clnt;
613 p9_idpool_put(fid->fid, clnt->fidpool);
Tom Tuckercac23d62008-10-23 16:31:02 -0500614 spin_lock_irqsave(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500615 list_del(&fid->flist);
Tom Tuckercac23d62008-10-23 16:31:02 -0500616 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500617 kfree(fid);
618}
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600619
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500620int p9_client_version(struct p9_client *c)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500621{
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500622 int err = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500623 struct p9_req_t *req;
624 char *version;
625 int msize;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500626
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500627 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n",
628 c->msize, c->dotu);
629 req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize,
630 c->dotu ? "9P2000.u" : "9P2000");
631 if (IS_ERR(req))
632 return PTR_ERR(req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500633
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500634 err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version);
635 if (err) {
636 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500637 p9pdu_dump(1, req->rc);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500638 goto error;
639 }
640
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500641 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
642 if (!memcmp(version, "9P2000.u", 8))
643 c->dotu = 1;
644 else if (!memcmp(version, "9P2000", 6))
645 c->dotu = 0;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500646 else {
647 err = -EREMOTEIO;
648 goto error;
649 }
650
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500651 if (msize < c->msize)
652 c->msize = msize;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500653
654error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500655 kfree(version);
656 p9_free_req(c, req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500657
658 return err;
659}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500660EXPORT_SYMBOL(p9_client_version);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500661
662struct p9_client *p9_client_create(const char *dev_name, char *options)
663{
664 int err;
665 struct p9_client *clnt;
666
667 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500668 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
669 if (!clnt)
670 return ERR_PTR(-ENOMEM);
671
Tejun Heo72029fe2008-09-24 16:22:23 -0500672 clnt->trans_mod = NULL;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600673 clnt->trans = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500674 spin_lock_init(&clnt->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500675 INIT_LIST_HEAD(&clnt->fidlist);
676 clnt->fidpool = p9_idpool_create();
Josef 'Jeff' Sipek728fc4e2008-03-07 11:40:33 -0600677 if (IS_ERR(clnt->fidpool)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500678 err = PTR_ERR(clnt->fidpool);
679 clnt->fidpool = NULL;
680 goto error;
681 }
682
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500683 p9_tag_init(clnt);
684
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600685 err = parse_opts(options, clnt);
686 if (err < 0)
687 goto error;
688
Abhishek Kulkarnia17d1722009-07-14 13:24:10 -0500689 if (!clnt->trans_mod)
690 clnt->trans_mod = v9fs_get_default_trans();
691
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600692 if (clnt->trans_mod == NULL) {
693 err = -EPROTONOSUPPORT;
694 P9_DPRINTK(P9_DEBUG_ERROR,
695 "No transport defined or default transport\n");
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500696 goto error;
697 }
698
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500699 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n",
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600700 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
701
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500702 err = clnt->trans_mod->create(clnt, dev_name, options);
703 if (err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600704 goto error;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600705
706 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
707 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
708
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500709 err = p9_client_version(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500710 if (err)
711 goto error;
712
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500713 return clnt;
714
715error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500716 p9_client_destroy(clnt);
717 return ERR_PTR(err);
718}
719EXPORT_SYMBOL(p9_client_create);
720
721void p9_client_destroy(struct p9_client *clnt)
722{
723 struct p9_fid *fid, *fidptr;
724
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500725 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500726
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500727 if (clnt->trans_mod)
728 clnt->trans_mod->close(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500729
Tejun Heo72029fe2008-09-24 16:22:23 -0500730 v9fs_put_trans(clnt->trans_mod);
731
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500732 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
733 p9_fid_destroy(fid);
734
Eric Van Hensbergen0af88872007-07-13 16:47:58 -0500735 if (clnt->fidpool)
736 p9_idpool_destroy(clnt->fidpool);
737
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500738 p9_tag_cleanup(clnt);
739
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500740 kfree(clnt);
741}
742EXPORT_SYMBOL(p9_client_destroy);
743
744void p9_client_disconnect(struct p9_client *clnt)
745{
746 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500747 clnt->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500748}
749EXPORT_SYMBOL(p9_client_disconnect);
750
751struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500752 char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500753{
754 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500755 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500756 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500757 struct p9_qid qid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500758
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500759 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
760 afid ? afid->fid : -1, uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500761 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500762
763 fid = p9_fid_create(clnt);
764 if (IS_ERR(fid)) {
765 err = PTR_ERR(fid);
766 fid = NULL;
767 goto error;
768 }
769
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500770 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
771 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
772 if (IS_ERR(req)) {
773 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500774 goto error;
775 }
776
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500777 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
778 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500779 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500780 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500781 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500782 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500783
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500784 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800785 qid.type,
786 (unsigned long long)qid.path,
787 qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500788
789 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
790
791 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500792 return fid;
793
794error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500795 if (fid)
796 p9_fid_destroy(fid);
797 return ERR_PTR(err);
798}
799EXPORT_SYMBOL(p9_client_attach);
800
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500801struct p9_fid *
802p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500803{
804 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500805 struct p9_req_t *req;
806 struct p9_qid qid;
807 struct p9_fid *afid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500808
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500809 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500810 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500811
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500812 afid = p9_fid_create(clnt);
813 if (IS_ERR(afid)) {
814 err = PTR_ERR(afid);
815 afid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500816 goto error;
817 }
818
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500819 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d",
820 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
821 if (IS_ERR(req)) {
822 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500823 goto error;
824 }
825
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500826 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
827 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500828 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500829 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500830 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500831 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500832
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500833 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800834 qid.type,
835 (unsigned long long)qid.path,
836 qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500837
838 memmove(&afid->qid, &qid, sizeof(struct p9_qid));
839 p9_free_req(clnt, req);
840 return afid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500841
842error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500843 if (afid)
844 p9_fid_destroy(afid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500845 return ERR_PTR(err);
846}
847EXPORT_SYMBOL(p9_client_auth);
848
849struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
850 int clone)
851{
852 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500853 struct p9_client *clnt;
854 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500855 struct p9_qid *wqids;
856 struct p9_req_t *req;
857 int16_t nwqids, count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500858
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500859 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500860 clnt = oldfid->clnt;
861 if (clone) {
862 fid = p9_fid_create(clnt);
863 if (IS_ERR(fid)) {
864 err = PTR_ERR(fid);
865 fid = NULL;
866 goto error;
867 }
868
869 fid->uid = oldfid->uid;
870 } else
871 fid = oldfid;
872
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500873
874 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
875 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
876
877 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
878 nwname, wnames);
879 if (IS_ERR(req)) {
880 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500881 goto error;
882 }
883
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500884 err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500885 if (err) {
886 p9pdu_dump(1, req->rc);
887 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500888 goto clunk_fid;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500889 }
890 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500891
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500892 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
893
894 if (nwqids != nwname) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500895 err = -ENOENT;
896 goto clunk_fid;
897 }
898
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500899 for (count = 0; count < nwqids; count++)
900 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800901 count, wqids[count].type,
902 (unsigned long long)wqids[count].path,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500903 wqids[count].version);
904
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500905 if (nwname)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500906 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500907 else
908 fid->qid = oldfid->qid;
909
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500910 return fid;
911
912clunk_fid:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500913 p9_client_clunk(fid);
914 fid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500915
916error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500917 if (fid && (fid != oldfid))
918 p9_fid_destroy(fid);
919
920 return ERR_PTR(err);
921}
922EXPORT_SYMBOL(p9_client_walk);
923
924int p9_client_open(struct p9_fid *fid, int mode)
925{
926 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500927 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500928 struct p9_req_t *req;
929 struct p9_qid qid;
930 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500931
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500932 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500933 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500934 clnt = fid->clnt;
935
936 if (fid->mode != -1)
937 return -EINVAL;
938
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500939 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
940 if (IS_ERR(req)) {
941 err = PTR_ERR(req);
942 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500943 }
944
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500945 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500946 if (err) {
947 p9pdu_dump(1, req->rc);
948 goto free_and_error;
949 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500950
951 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800952 qid.type,
953 (unsigned long long)qid.path,
954 qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500955
956 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500957 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500958
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500959free_and_error:
960 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500961error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500962 return err;
963}
964EXPORT_SYMBOL(p9_client_open);
965
966int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
967 char *extension)
968{
969 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500970 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500971 struct p9_req_t *req;
972 struct p9_qid qid;
973 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500974
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500975 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
976 fid->fid, name, perm, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500977 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500978 clnt = fid->clnt;
979
980 if (fid->mode != -1)
981 return -EINVAL;
982
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500983 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
984 mode, extension);
985 if (IS_ERR(req)) {
986 err = PTR_ERR(req);
987 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500988 }
989
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500990 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500991 if (err) {
992 p9pdu_dump(1, req->rc);
993 goto free_and_error;
994 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500995
996 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800997 qid.type,
998 (unsigned long long)qid.path,
999 qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001000
1001 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001002 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001003
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001004free_and_error:
1005 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001006error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001007 return err;
1008}
1009EXPORT_SYMBOL(p9_client_fcreate);
1010
1011int p9_client_clunk(struct p9_fid *fid)
1012{
1013 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001014 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001015 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001016
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001017 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001018 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001019 clnt = fid->clnt;
1020
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001021 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1022 if (IS_ERR(req)) {
1023 err = PTR_ERR(req);
1024 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001025 }
1026
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001027 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001028
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001029 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001030 p9_fid_destroy(fid);
1031
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001032error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001033 return err;
1034}
1035EXPORT_SYMBOL(p9_client_clunk);
1036
1037int p9_client_remove(struct p9_fid *fid)
1038{
1039 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001040 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001041 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001042
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001043 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001044 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001045 clnt = fid->clnt;
1046
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001047 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1048 if (IS_ERR(req)) {
1049 err = PTR_ERR(req);
1050 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001051 }
1052
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001053 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001054
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001055 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001056 p9_fid_destroy(fid);
1057
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001058error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001059 return err;
1060}
1061EXPORT_SYMBOL(p9_client_remove);
1062
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001063int
1064p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1065 u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001066{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001067 int err, rsize, total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001068 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001069 struct p9_req_t *req;
1070 char *dataptr;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001071
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001072 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid,
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001073 (long long unsigned) offset, count);
1074 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001075 clnt = fid->clnt;
1076 total = 0;
1077
1078 rsize = fid->iounit;
1079 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1080 rsize = clnt->msize - P9_IOHDRSZ;
1081
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001082 if (count < rsize)
1083 rsize = count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001084
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001085 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize);
1086 if (IS_ERR(req)) {
1087 err = PTR_ERR(req);
1088 goto error;
1089 }
1090
1091 err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001092 if (err) {
1093 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001094 goto free_and_error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001095 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001096
1097 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1098
1099 if (data) {
1100 memmove(data, dataptr, count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001101 }
1102
1103 if (udata) {
1104 err = copy_to_user(udata, dataptr, count);
1105 if (err) {
1106 err = -EFAULT;
1107 goto free_and_error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001108 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001109 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001110
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001111 p9_free_req(clnt, req);
1112 return count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001113
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001114free_and_error:
1115 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001116error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001117 return err;
1118}
1119EXPORT_SYMBOL(p9_client_read);
1120
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001121int
1122p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1123 u64 offset, u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001124{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001125 int err, rsize, total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001126 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001127 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001128
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001129 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1130 fid->fid, (long long unsigned) offset, count);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001131 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001132 clnt = fid->clnt;
1133 total = 0;
1134
1135 rsize = fid->iounit;
1136 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1137 rsize = clnt->msize - P9_IOHDRSZ;
1138
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001139 if (count < rsize)
1140 rsize = count;
1141 if (data)
1142 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset,
1143 rsize, data);
1144 else
1145 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset,
1146 rsize, udata);
1147 if (IS_ERR(req)) {
1148 err = PTR_ERR(req);
1149 goto error;
1150 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001151
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001152 err = p9pdu_readf(req->rc, clnt->dotu, "d", &count);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001153 if (err) {
1154 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001155 goto free_and_error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001156 }
1157
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001158 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001159
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001160 p9_free_req(clnt, req);
1161 return count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001162
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001163free_and_error:
1164 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001165error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001166 return err;
1167}
1168EXPORT_SYMBOL(p9_client_write);
1169
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001170struct p9_wstat *p9_client_stat(struct p9_fid *fid)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001171{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001172 int err;
1173 struct p9_client *clnt;
1174 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1175 struct p9_req_t *req;
1176 u16 ignored;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001177
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001178 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001179
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001180 if (!ret)
1181 return ERR_PTR(-ENOMEM);
1182
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001183 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001184 clnt = fid->clnt;
1185
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001186 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1187 if (IS_ERR(req)) {
1188 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001189 goto error;
1190 }
1191
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001192 err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001193 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001194 p9pdu_dump(1, req->rc);
Abhishek Kulkarnieedfe1c2009-07-14 13:25:41 -05001195 p9_free_req(clnt, req);
1196 goto error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001197 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001198
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001199 P9_DPRINTK(P9_DEBUG_9P,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001200 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1201 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1202 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1203 "<<< uid=%d gid=%d n_muid=%d\n",
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001204 ret->size, ret->type, ret->dev, ret->qid.type,
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001205 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1206 ret->atime, ret->mtime, (unsigned long long)ret->length,
1207 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001208 ret->n_uid, ret->n_gid, ret->n_muid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001209
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001210 p9_free_req(clnt, req);
1211 return ret;
1212
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001213error:
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001214 kfree(ret);
1215 return ERR_PTR(err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001216}
1217EXPORT_SYMBOL(p9_client_stat);
1218
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001219static int p9_client_statsize(struct p9_wstat *wst, int optional)
1220{
1221 int ret;
1222
1223 /* size[2] type[2] dev[4] qid[13] */
1224 /* mode[4] atime[4] mtime[4] length[8]*/
1225 /* name[s] uid[s] gid[s] muid[s] */
1226 ret = 2+2+4+13+4+4+4+8+2+2+2+2;
1227
1228 if (wst->name)
1229 ret += strlen(wst->name);
1230 if (wst->uid)
1231 ret += strlen(wst->uid);
1232 if (wst->gid)
1233 ret += strlen(wst->gid);
1234 if (wst->muid)
1235 ret += strlen(wst->muid);
1236
1237 if (optional) {
1238 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1239 if (wst->extension)
1240 ret += strlen(wst->extension);
1241 }
1242
1243 return ret;
1244}
1245
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001246int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1247{
1248 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001249 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001250 struct p9_client *clnt;
1251
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001252 err = 0;
1253 clnt = fid->clnt;
1254 wst->size = p9_client_statsize(wst, clnt->dotu);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001255 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001256 P9_DPRINTK(P9_DEBUG_9P,
1257 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1258 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1259 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1260 " uid=%d gid=%d n_muid=%d\n",
1261 wst->size, wst->type, wst->dev, wst->qid.type,
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001262 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1263 wst->atime, wst->mtime, (unsigned long long)wst->length,
1264 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001265 wst->n_uid, wst->n_gid, wst->n_muid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001266
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001267 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size, wst);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001268 if (IS_ERR(req)) {
1269 err = PTR_ERR(req);
1270 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001271 }
1272
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001273 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001274
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001275 p9_free_req(clnt, req);
1276error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001277 return err;
1278}
1279EXPORT_SYMBOL(p9_client_wstat);