blob: 6004fded668267408a65c6862809cb6c40c46e54 [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>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050038
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060039/*
40 * Client Option Parsing (code inspired by NFS code)
41 * - a little lazy - parse all client options
42 */
43
44enum {
45 Opt_msize,
46 Opt_trans,
47 Opt_legacy,
48 Opt_err,
49};
50
Steven Whitehousea447c092008-10-13 10:46:57 +010051static const match_table_t tokens = {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060052 {Opt_msize, "msize=%u"},
53 {Opt_legacy, "noextend"},
54 {Opt_trans, "trans=%s"},
55 {Opt_err, NULL},
56};
57
Eric Van Hensbergen91b85342008-10-13 18:45:21 -050058static int
59p9_client_rpc(struct p9_client *c, struct p9_fcall *tc, struct p9_fcall **rc);
60
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060061/**
62 * v9fs_parse_options - parse mount options into session structure
63 * @options: options string passed from mount
64 * @v9ses: existing v9fs session information
65 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060066 * Return 0 upon success, -ERRNO upon failure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060067 */
68
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060069static int parse_opts(char *opts, struct p9_client *clnt)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060070{
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060071 char *options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060072 char *p;
73 substring_t args[MAX_OPT_ARGS];
74 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060075 int ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060076
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060077 clnt->dotu = 1;
78 clnt->msize = 8192;
79
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060080 if (!opts)
81 return 0;
82
83 options = kstrdup(opts, GFP_KERNEL);
84 if (!options) {
85 P9_DPRINTK(P9_DEBUG_ERROR,
86 "failed to allocate copy of option string\n");
87 return -ENOMEM;
88 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060089
90 while ((p = strsep(&options, ",")) != NULL) {
91 int token;
92 if (!*p)
93 continue;
94 token = match_token(p, tokens, args);
95 if (token < Opt_trans) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060096 int r = match_int(&args[0], &option);
97 if (r < 0) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060098 P9_DPRINTK(P9_DEBUG_ERROR,
99 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600100 ret = r;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600101 continue;
102 }
103 }
104 switch (token) {
105 case Opt_msize:
106 clnt->msize = option;
107 break;
108 case Opt_trans:
Tejun Heo72029fe2008-09-24 16:22:23 -0500109 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600110 break;
111 case Opt_legacy:
112 clnt->dotu = 0;
113 break;
114 default:
115 continue;
116 }
117 }
Tejun Heo72029fe2008-09-24 16:22:23 -0500118
119 if (!clnt->trans_mod)
120 clnt->trans_mod = v9fs_get_default_trans();
121
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600122 kfree(options);
123 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600124}
125
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500126/**
127 * p9_tag_alloc - lookup/allocate a request by tag
128 * @c: client session to lookup tag within
129 * @tag: numeric id for transaction
130 *
131 * this is a simple array lookup, but will grow the
132 * request_slots as necessary to accomodate transaction
133 * ids which did not previously have a slot.
134 *
135 * this code relies on the client spinlock to manage locks, its
136 * possible we should switch to something else, but I'd rather
137 * stick with something low-overhead for the common case.
138 *
139 */
140
141struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
142{
143 unsigned long flags;
144 int row, col;
145
146 /* This looks up the original request by tag so we know which
147 * buffer to read the data into */
148 tag++;
149
150 if (tag >= c->max_tag) {
151 spin_lock_irqsave(&c->lock, flags);
152 /* check again since original check was outside of lock */
153 while (tag >= c->max_tag) {
154 row = (tag / P9_ROW_MAXTAG);
155 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
156 sizeof(struct p9_req_t), GFP_ATOMIC);
157
158 if (!c->reqs[row]) {
159 printk(KERN_ERR "Couldn't grow tag array\n");
160 BUG();
161 }
162 for (col = 0; col < P9_ROW_MAXTAG; col++) {
163 c->reqs[row][col].status = REQ_STATUS_IDLE;
164 c->reqs[row][col].flush_tag = P9_NOTAG;
165 c->reqs[row][col].wq = kmalloc(
166 sizeof(wait_queue_head_t), GFP_ATOMIC);
167 if (!c->reqs[row][col].wq) {
168 printk(KERN_ERR
169 "Couldn't grow tag array\n");
170 BUG();
171 }
172 init_waitqueue_head(c->reqs[row][col].wq);
173 }
174 c->max_tag += P9_ROW_MAXTAG;
175 }
176 spin_unlock_irqrestore(&c->lock, flags);
177 }
178 row = tag / P9_ROW_MAXTAG;
179 col = tag % P9_ROW_MAXTAG;
180
181 c->reqs[row][col].status = REQ_STATUS_ALLOC;
182 c->reqs[row][col].flush_tag = P9_NOTAG;
183
184 return &c->reqs[row][col];
185}
186EXPORT_SYMBOL(p9_tag_alloc);
187
188/**
189 * p9_tag_lookup - lookup a request by tag
190 * @c: client session to lookup tag within
191 * @tag: numeric id for transaction
192 *
193 */
194
195struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
196{
197 int row, col;
198
199 /* This looks up the original request by tag so we know which
200 * buffer to read the data into */
201 tag++;
202
203 BUG_ON(tag >= c->max_tag);
204
205 row = tag / P9_ROW_MAXTAG;
206 col = tag % P9_ROW_MAXTAG;
207
208 return &c->reqs[row][col];
209}
210EXPORT_SYMBOL(p9_tag_lookup);
211
212/**
213 * p9_tag_init - setup tags structure and contents
214 * @tags: tags structure from the client struct
215 *
216 * This initializes the tags structure for each client instance.
217 *
218 */
219
220static int p9_tag_init(struct p9_client *c)
221{
222 int err = 0;
223
224 c->tagpool = p9_idpool_create();
225 if (IS_ERR(c->tagpool)) {
226 err = PTR_ERR(c->tagpool);
227 c->tagpool = NULL;
228 goto error;
229 }
230
231 p9_idpool_get(c->tagpool); /* reserve tag 0 */
232
233 c->max_tag = 0;
234error:
235 return err;
236}
237
238/**
239 * p9_tag_cleanup - cleans up tags structure and reclaims resources
240 * @tags: tags structure from the client struct
241 *
242 * This frees resources associated with the tags structure
243 *
244 */
245static void p9_tag_cleanup(struct p9_client *c)
246{
247 int row, col;
248
249 /* check to insure all requests are idle */
250 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
251 for (col = 0; col < P9_ROW_MAXTAG; col++) {
252 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
253 P9_DPRINTK(P9_DEBUG_MUX,
254 "Attempting to cleanup non-free tag %d,%d\n",
255 row, col);
256 /* TODO: delay execution of cleanup */
257 return;
258 }
259 }
260 }
261
262 if (c->tagpool)
263 p9_idpool_destroy(c->tagpool);
264
265 /* free requests associated with tags */
266 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
267 for (col = 0; col < P9_ROW_MAXTAG; col++)
268 kfree(c->reqs[row][col].wq);
269 kfree(c->reqs[row]);
270 }
271 c->max_tag = 0;
272}
273
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500274/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500275 * p9_client_flush - flush (cancel) a request
276 * c: client state
277 * req: request to cancel
278 *
279 * This sents a flush for a particular requests and links
280 * the flush request to the original request. The current
281 * code only supports a single flush request although the protocol
282 * allows for multiple flush requests to be sent for a single request.
283 *
284 */
285
286static int p9_client_flush(struct p9_client *c, struct p9_req_t *req)
287{
288 struct p9_fcall *tc, *rc = NULL;
289 int err;
290
291 P9_DPRINTK(P9_DEBUG_9P, "client %p tag %d\n", c, req->tc->tag);
292
293 tc = p9_create_tflush(req->tc->tag);
294 if (IS_ERR(tc))
295 return PTR_ERR(tc);
296
297 err = p9_client_rpc(c, tc, &rc);
298
299 /* we don't free anything here because RPC isn't complete */
300
301 return err;
302}
303
304/**
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500305 * p9_free_req - free a request and clean-up as necessary
306 * c: client state
307 * r: request to release
308 *
309 */
310
311void p9_free_req(struct p9_client *c, struct p9_req_t *r)
312{
313 r->flush_tag = P9_NOTAG;
314 r->status = REQ_STATUS_IDLE;
315 if (r->tc->tag != P9_NOTAG && p9_idpool_check(r->tc->tag, c->tagpool))
316 p9_idpool_put(r->tc->tag, c->tagpool);
317
318 /* if this was a flush request we have to free response fcall */
319 if (r->tc->id == P9_TFLUSH) {
320 kfree(r->tc);
321 kfree(r->rc);
322 }
323}
324
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500325/**
326 * p9_client_cb - call back from transport to client
327 * c: client state
328 * req: request received
329 *
330 */
331void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
332{
333 struct p9_req_t *other_req;
334 unsigned long flags;
335
336 P9_DPRINTK(P9_DEBUG_MUX, ": %d\n", req->tc->tag);
337
338 if (req->status == REQ_STATUS_ERROR)
339 wake_up(req->wq);
340
341 if (req->tc->id == P9_TFLUSH) { /* flush receive path */
342 P9_DPRINTK(P9_DEBUG_MUX, "flush: %d\n", req->tc->tag);
343 spin_lock_irqsave(&c->lock, flags);
344 other_req = p9_tag_lookup(c, req->tc->params.tflush.oldtag);
345 if (other_req->flush_tag != req->tc->tag) /* stale flush */
346 spin_unlock_irqrestore(&c->lock, flags);
347 else {
348 BUG_ON(other_req->status != REQ_STATUS_FLSH);
349 other_req->status = REQ_STATUS_FLSHD;
350 spin_unlock_irqrestore(&c->lock, flags);
351 wake_up(other_req->wq);
352 }
353 p9_free_req(c, req);
354 } else { /* normal receive path */
355 P9_DPRINTK(P9_DEBUG_MUX, "normal: %d\n", req->tc->tag);
356 spin_lock_irqsave(&c->lock, flags);
357 if (req->status != REQ_STATUS_FLSHD)
358 req->status = REQ_STATUS_RCVD;
359 req->flush_tag = P9_NOTAG;
360 spin_unlock_irqrestore(&c->lock, flags);
361 wake_up(req->wq);
362 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
363 }
364}
365EXPORT_SYMBOL(p9_client_cb);
366
367/**
368 * p9_client_rpc - issue a request and wait for a response
369 * @c: client session
370 * @tc: &p9_fcall request to transmit
371 * @rc: &p9_fcall to put reponse into
372 *
373 * Returns 0 on success, error code on failure
374 */
375
376static int
377p9_client_rpc(struct p9_client *c, struct p9_fcall *tc, struct p9_fcall **rc)
378{
379 int tag, err, size;
380 char *rdata;
381 struct p9_req_t *req;
382 unsigned long flags;
383 int sigpending;
384 int flushed = 0;
385
386 P9_DPRINTK(P9_DEBUG_9P, "client %p tc %p rc %p\n", c, tc, rc);
387
388 if (c->status != Connected)
389 return -EIO;
390
391 if (signal_pending(current)) {
392 sigpending = 1;
393 clear_thread_flag(TIF_SIGPENDING);
394 } else
395 sigpending = 0;
396
397 tag = P9_NOTAG;
398 if (tc->id != P9_TVERSION) {
399 tag = p9_idpool_get(c->tagpool);
400 if (tag < 0)
401 return -ENOMEM;
402 }
403
404 req = p9_tag_alloc(c, tag);
405
406 /* if this is a flush request, backlink flush request now to
407 * avoid race conditions later. */
408 if (tc->id == P9_TFLUSH) {
409 struct p9_req_t *other_req =
410 p9_tag_lookup(c, tc->params.tflush.oldtag);
411 if (other_req->status == REQ_STATUS_FLSH)
412 other_req->flush_tag = tag;
413 }
414
415 p9_set_tag(tc, tag);
416
417 /*
418 * if client passed in a pre-allocated response fcall struct
419 * then we just use that, otherwise we allocate one.
420 */
421
422 if (rc == NULL)
423 req->rc = NULL;
424 else
425 req->rc = *rc;
426 if (req->rc == NULL) {
427 req->rc = kmalloc(sizeof(struct p9_fcall) + c->msize,
428 GFP_KERNEL);
429 if (!req->rc) {
430 err = -ENOMEM;
431 p9_idpool_put(tag, c->tagpool);
432 p9_free_req(c, req);
433 goto reterr;
434 }
435 *rc = req->rc;
436 }
437
438 rdata = (char *)req->rc+sizeof(struct p9_fcall);
439
440 req->tc = tc;
441 P9_DPRINTK(P9_DEBUG_9P, "request: tc: %p rc: %p\n", req->tc, req->rc);
442
443 err = c->trans_mod->request(c, req);
444 if (err < 0) {
445 c->status = Disconnected;
446 goto reterr;
447 }
448
449 /* if it was a flush we just transmitted, return our tag */
450 if (tc->id == P9_TFLUSH)
451 return 0;
452again:
453 P9_DPRINTK(P9_DEBUG_9P, "wait %p tag: %d\n", req->wq, tag);
454 err = wait_event_interruptible(*req->wq,
455 req->status >= REQ_STATUS_RCVD);
456 P9_DPRINTK(P9_DEBUG_9P, "wait %p tag: %d returned %d (flushed=%d)\n",
457 req->wq, tag, err, flushed);
458
459 if (req->status == REQ_STATUS_ERROR) {
460 P9_DPRINTK(P9_DEBUG_9P, "req_status error %d\n", req->t_err);
461 err = req->t_err;
462 } else if (err == -ERESTARTSYS && flushed) {
463 P9_DPRINTK(P9_DEBUG_9P, "flushed - going again\n");
464 goto again;
465 } else if (req->status == REQ_STATUS_FLSHD) {
466 P9_DPRINTK(P9_DEBUG_9P, "flushed - erestartsys\n");
467 err = -ERESTARTSYS;
468 }
469
470 if ((err == -ERESTARTSYS) && (c->status == Connected) && (!flushed)) {
471 P9_DPRINTK(P9_DEBUG_9P, "flushing\n");
472 spin_lock_irqsave(&c->lock, flags);
473 if (req->status == REQ_STATUS_SENT)
474 req->status = REQ_STATUS_FLSH;
475 spin_unlock_irqrestore(&c->lock, flags);
476 sigpending = 1;
477 flushed = 1;
478 clear_thread_flag(TIF_SIGPENDING);
479
480 if (c->trans_mod->cancel(c, req)) {
481 err = p9_client_flush(c, req);
482 if (err == 0)
483 goto again;
484 }
485 }
486
487 if (sigpending) {
488 spin_lock_irqsave(&current->sighand->siglock, flags);
489 recalc_sigpending();
490 spin_unlock_irqrestore(&current->sighand->siglock, flags);
491 }
492
493 if (err < 0)
494 goto reterr;
495
496 size = le32_to_cpu(*(__le32 *) rdata);
497
498 err = p9_deserialize_fcall(rdata, size, req->rc, c->dotu);
499 if (err < 0) {
500 P9_DPRINTK(P9_DEBUG_9P,
501 "9p debug: client rpc deserialize returned %d\n", err);
502 goto reterr;
503 }
504
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500505 if (req->rc->id == P9_RERROR) {
506 int ecode = req->rc->params.rerror.errno;
507 struct p9_str *ename = &req->rc->params.rerror.error;
508
509 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
510 ename->str);
511
512 if (c->dotu)
513 err = -ecode;
514
515 if (!err) {
516 err = p9_errstr2errno(ename->str, ename->len);
517
518 /* string match failed */
519 if (!err) {
520 PRINT_FCALL_ERROR("unknown error", req->rc);
521 err = -ESERVERFAULT;
522 }
523 }
524 } else
525 err = 0;
526
527reterr:
528 p9_free_req(c, req);
529
530 P9_DPRINTK(P9_DEBUG_9P, "returning %d\n", err);
531 return err;
532}
533
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500534static struct p9_fid *p9_fid_create(struct p9_client *clnt)
535{
536 int err;
537 struct p9_fid *fid;
538
539 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
540 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
541 if (!fid)
542 return ERR_PTR(-ENOMEM);
543
544 fid->fid = p9_idpool_get(clnt->fidpool);
545 if (fid->fid < 0) {
546 err = -ENOSPC;
547 goto error;
548 }
549
550 memset(&fid->qid, 0, sizeof(struct p9_qid));
551 fid->mode = -1;
552 fid->rdir_fpos = 0;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500553 fid->uid = current->fsuid;
554 fid->clnt = clnt;
555 fid->aux = NULL;
556
557 spin_lock(&clnt->lock);
558 list_add(&fid->flist, &clnt->fidlist);
559 spin_unlock(&clnt->lock);
560
561 return fid;
562
563error:
564 kfree(fid);
565 return ERR_PTR(err);
566}
567
568static void p9_fid_destroy(struct p9_fid *fid)
569{
570 struct p9_client *clnt;
571
572 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
573 clnt = fid->clnt;
574 p9_idpool_put(fid->fid, clnt->fidpool);
575 spin_lock(&clnt->lock);
576 list_del(&fid->flist);
577 spin_unlock(&clnt->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500578 kfree(fid);
579}
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600580
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500581static int p9_client_version(struct p9_client *clnt)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500582{
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500583 int err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500584 struct p9_fcall *tc, *rc;
585 struct p9_str *version;
586
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500587 P9_DPRINTK(P9_DEBUG_9P, "%p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500588 err = 0;
589 tc = NULL;
590 rc = NULL;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500591
592 tc = p9_create_tversion(clnt->msize,
593 clnt->dotu ? "9P2000.u" : "9P2000");
594 if (IS_ERR(tc)) {
595 err = PTR_ERR(tc);
596 tc = NULL;
597 goto error;
598 }
599
600 err = p9_client_rpc(clnt, tc, &rc);
601 if (err)
602 goto error;
603
604 version = &rc->params.rversion.version;
605 if (version->len == 8 && !memcmp(version->str, "9P2000.u", 8))
606 clnt->dotu = 1;
607 else if (version->len == 6 && !memcmp(version->str, "9P2000", 6))
608 clnt->dotu = 0;
609 else {
610 err = -EREMOTEIO;
611 goto error;
612 }
613
614 if (rc->params.rversion.msize < clnt->msize)
615 clnt->msize = rc->params.rversion.msize;
616
617error:
618 kfree(tc);
619 kfree(rc);
620
621 return err;
622}
623EXPORT_SYMBOL(p9_client_auth);
624
625struct p9_client *p9_client_create(const char *dev_name, char *options)
626{
627 int err;
628 struct p9_client *clnt;
629
630 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500631 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
632 if (!clnt)
633 return ERR_PTR(-ENOMEM);
634
Tejun Heo72029fe2008-09-24 16:22:23 -0500635 clnt->trans_mod = NULL;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600636 clnt->trans = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500637 spin_lock_init(&clnt->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500638 INIT_LIST_HEAD(&clnt->fidlist);
639 clnt->fidpool = p9_idpool_create();
Josef 'Jeff' Sipek728fc4e2008-03-07 11:40:33 -0600640 if (IS_ERR(clnt->fidpool)) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500641 err = PTR_ERR(clnt->fidpool);
642 clnt->fidpool = NULL;
643 goto error;
644 }
645
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500646 p9_tag_init(clnt);
647
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600648 err = parse_opts(options, clnt);
649 if (err < 0)
650 goto error;
651
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600652 if (clnt->trans_mod == NULL) {
653 err = -EPROTONOSUPPORT;
654 P9_DPRINTK(P9_DEBUG_ERROR,
655 "No transport defined or default transport\n");
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500656 goto error;
657 }
658
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600659 P9_DPRINTK(P9_DEBUG_9P, "clnt %p trans %p msize %d dotu %d\n",
660 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
661
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500662 err = clnt->trans_mod->create(clnt, dev_name, options);
663 if (err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600664 goto error;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600665
666 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
667 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
668
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500669 err = p9_client_version(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500670 if (err)
671 goto error;
672
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500673 return clnt;
674
675error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500676 p9_client_destroy(clnt);
677 return ERR_PTR(err);
678}
679EXPORT_SYMBOL(p9_client_create);
680
681void p9_client_destroy(struct p9_client *clnt)
682{
683 struct p9_fid *fid, *fidptr;
684
685 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500686
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500687 if (clnt->trans_mod)
688 clnt->trans_mod->close(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500689
Tejun Heo72029fe2008-09-24 16:22:23 -0500690 v9fs_put_trans(clnt->trans_mod);
691
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500692 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
693 p9_fid_destroy(fid);
694
Eric Van Hensbergen0af88872007-07-13 16:47:58 -0500695 if (clnt->fidpool)
696 p9_idpool_destroy(clnt->fidpool);
697
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500698 p9_tag_cleanup(clnt);
699
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500700 kfree(clnt);
701}
702EXPORT_SYMBOL(p9_client_destroy);
703
704void p9_client_disconnect(struct p9_client *clnt)
705{
706 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500707 clnt->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500708}
709EXPORT_SYMBOL(p9_client_disconnect);
710
711struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500712 char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500713{
714 int err;
715 struct p9_fcall *tc, *rc;
716 struct p9_fid *fid;
717
718 P9_DPRINTK(P9_DEBUG_9P, "clnt %p afid %d uname %s aname %s\n",
719 clnt, afid?afid->fid:-1, uname, aname);
720 err = 0;
721 tc = NULL;
722 rc = NULL;
723
724 fid = p9_fid_create(clnt);
725 if (IS_ERR(fid)) {
726 err = PTR_ERR(fid);
727 fid = NULL;
728 goto error;
729 }
730
Latchesar Ionkovba176742007-10-17 14:31:07 -0500731 tc = p9_create_tattach(fid->fid, afid?afid->fid:P9_NOFID, uname, aname,
732 n_uname, clnt->dotu);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500733 if (IS_ERR(tc)) {
734 err = PTR_ERR(tc);
735 tc = NULL;
736 goto error;
737 }
738
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600739 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500740 if (err)
741 goto error;
742
743 memmove(&fid->qid, &rc->params.rattach.qid, sizeof(struct p9_qid));
744 kfree(tc);
745 kfree(rc);
746 return fid;
747
748error:
749 kfree(tc);
750 kfree(rc);
751 if (fid)
752 p9_fid_destroy(fid);
753 return ERR_PTR(err);
754}
755EXPORT_SYMBOL(p9_client_attach);
756
Latchesar Ionkovba176742007-10-17 14:31:07 -0500757struct p9_fid *p9_client_auth(struct p9_client *clnt, char *uname,
758 u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500759{
760 int err;
761 struct p9_fcall *tc, *rc;
762 struct p9_fid *fid;
763
764 P9_DPRINTK(P9_DEBUG_9P, "clnt %p uname %s aname %s\n", clnt, uname,
765 aname);
766 err = 0;
767 tc = NULL;
768 rc = NULL;
769
770 fid = p9_fid_create(clnt);
771 if (IS_ERR(fid)) {
772 err = PTR_ERR(fid);
773 fid = NULL;
774 goto error;
775 }
776
Latchesar Ionkovba176742007-10-17 14:31:07 -0500777 tc = p9_create_tauth(fid->fid, uname, aname, n_uname, clnt->dotu);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500778 if (IS_ERR(tc)) {
779 err = PTR_ERR(tc);
780 tc = NULL;
781 goto error;
782 }
783
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600784 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500785 if (err)
786 goto error;
787
788 memmove(&fid->qid, &rc->params.rauth.qid, sizeof(struct p9_qid));
789 kfree(tc);
790 kfree(rc);
791 return fid;
792
793error:
794 kfree(tc);
795 kfree(rc);
796 if (fid)
797 p9_fid_destroy(fid);
798 return ERR_PTR(err);
799}
800EXPORT_SYMBOL(p9_client_auth);
801
802struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
803 int clone)
804{
805 int err;
806 struct p9_fcall *tc, *rc;
807 struct p9_client *clnt;
808 struct p9_fid *fid;
809
810 P9_DPRINTK(P9_DEBUG_9P, "fid %d nwname %d wname[0] %s\n",
811 oldfid->fid, nwname, wnames?wnames[0]:NULL);
812 err = 0;
813 tc = NULL;
814 rc = NULL;
815 clnt = oldfid->clnt;
816 if (clone) {
817 fid = p9_fid_create(clnt);
818 if (IS_ERR(fid)) {
819 err = PTR_ERR(fid);
820 fid = NULL;
821 goto error;
822 }
823
824 fid->uid = oldfid->uid;
825 } else
826 fid = oldfid;
827
828 tc = p9_create_twalk(oldfid->fid, fid->fid, nwname, wnames);
829 if (IS_ERR(tc)) {
830 err = PTR_ERR(tc);
831 tc = NULL;
832 goto error;
833 }
834
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600835 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500836 if (err) {
837 if (rc && rc->id == P9_RWALK)
838 goto clunk_fid;
839 else
840 goto error;
841 }
842
843 if (rc->params.rwalk.nwqid != nwname) {
844 err = -ENOENT;
845 goto clunk_fid;
846 }
847
848 if (nwname)
849 memmove(&fid->qid,
850 &rc->params.rwalk.wqids[rc->params.rwalk.nwqid - 1],
851 sizeof(struct p9_qid));
852 else
853 fid->qid = oldfid->qid;
854
855 kfree(tc);
856 kfree(rc);
857 return fid;
858
859clunk_fid:
860 kfree(tc);
861 kfree(rc);
862 rc = NULL;
863 tc = p9_create_tclunk(fid->fid);
864 if (IS_ERR(tc)) {
865 err = PTR_ERR(tc);
866 tc = NULL;
867 goto error;
868 }
869
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600870 p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500871
872error:
873 kfree(tc);
874 kfree(rc);
875 if (fid && (fid != oldfid))
876 p9_fid_destroy(fid);
877
878 return ERR_PTR(err);
879}
880EXPORT_SYMBOL(p9_client_walk);
881
882int p9_client_open(struct p9_fid *fid, int mode)
883{
884 int err;
885 struct p9_fcall *tc, *rc;
886 struct p9_client *clnt;
887
888 P9_DPRINTK(P9_DEBUG_9P, "fid %d mode %d\n", fid->fid, mode);
889 err = 0;
890 tc = NULL;
891 rc = NULL;
892 clnt = fid->clnt;
893
894 if (fid->mode != -1)
895 return -EINVAL;
896
897 tc = p9_create_topen(fid->fid, mode);
898 if (IS_ERR(tc)) {
899 err = PTR_ERR(tc);
900 tc = NULL;
901 goto done;
902 }
903
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600904 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500905 if (err)
906 goto done;
907
908 fid->mode = mode;
909 fid->iounit = rc->params.ropen.iounit;
910
911done:
912 kfree(tc);
913 kfree(rc);
914 return err;
915}
916EXPORT_SYMBOL(p9_client_open);
917
918int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
919 char *extension)
920{
921 int err;
922 struct p9_fcall *tc, *rc;
923 struct p9_client *clnt;
924
925 P9_DPRINTK(P9_DEBUG_9P, "fid %d name %s perm %d mode %d\n", fid->fid,
926 name, perm, mode);
927 err = 0;
928 tc = NULL;
929 rc = NULL;
930 clnt = fid->clnt;
931
932 if (fid->mode != -1)
933 return -EINVAL;
934
935 tc = p9_create_tcreate(fid->fid, name, perm, mode, extension,
936 clnt->dotu);
937 if (IS_ERR(tc)) {
938 err = PTR_ERR(tc);
939 tc = NULL;
940 goto done;
941 }
942
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600943 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500944 if (err)
945 goto done;
946
947 fid->mode = mode;
948 fid->iounit = rc->params.ropen.iounit;
949
950done:
951 kfree(tc);
952 kfree(rc);
953 return err;
954}
955EXPORT_SYMBOL(p9_client_fcreate);
956
957int p9_client_clunk(struct p9_fid *fid)
958{
959 int err;
960 struct p9_fcall *tc, *rc;
961 struct p9_client *clnt;
962
963 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
964 err = 0;
965 tc = NULL;
966 rc = NULL;
967 clnt = fid->clnt;
968
969 tc = p9_create_tclunk(fid->fid);
970 if (IS_ERR(tc)) {
971 err = PTR_ERR(tc);
972 tc = NULL;
973 goto done;
974 }
975
Eric Van Hensbergene2735b72008-02-06 19:25:58 -0600976 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500977 if (err)
978 goto done;
979
980 p9_fid_destroy(fid);
981
982done:
983 kfree(tc);
984 kfree(rc);
985 return err;
986}
987EXPORT_SYMBOL(p9_client_clunk);
988
989int p9_client_remove(struct p9_fid *fid)
990{
991 int err;
992 struct p9_fcall *tc, *rc;
993 struct p9_client *clnt;
994
995 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
996 err = 0;
997 tc = NULL;
998 rc = NULL;
999 clnt = fid->clnt;
1000
1001 tc = p9_create_tremove(fid->fid);
1002 if (IS_ERR(tc)) {
1003 err = PTR_ERR(tc);
1004 tc = NULL;
1005 goto done;
1006 }
1007
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001008 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001009 if (err)
1010 goto done;
1011
1012 p9_fid_destroy(fid);
1013
1014done:
1015 kfree(tc);
1016 kfree(rc);
1017 return err;
1018}
1019EXPORT_SYMBOL(p9_client_remove);
1020
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001021int
1022p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1023 u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001024{
1025 int err, n, rsize, total;
1026 struct p9_fcall *tc, *rc;
1027 struct p9_client *clnt;
1028
1029 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu %d\n", fid->fid,
1030 (long long unsigned) offset, count);
1031 err = 0;
1032 tc = NULL;
1033 rc = NULL;
1034 clnt = fid->clnt;
1035 total = 0;
1036
1037 rsize = fid->iounit;
1038 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1039 rsize = clnt->msize - P9_IOHDRSZ;
1040
1041 do {
1042 if (count < rsize)
1043 rsize = count;
1044
1045 tc = p9_create_tread(fid->fid, offset, rsize);
1046 if (IS_ERR(tc)) {
1047 err = PTR_ERR(tc);
1048 tc = NULL;
1049 goto error;
1050 }
1051
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001052 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001053 if (err)
1054 goto error;
1055
1056 n = rc->params.rread.count;
1057 if (n > count)
1058 n = count;
1059
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001060 if (data) {
1061 memmove(data, rc->params.rread.data, n);
1062 data += n;
1063 }
1064
1065 if (udata) {
1066 err = copy_to_user(udata, rc->params.rread.data, n);
1067 if (err) {
1068 err = -EFAULT;
1069 goto error;
1070 }
1071 udata += n;
1072 }
1073
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001074 count -= n;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001075 offset += n;
1076 total += n;
1077 kfree(tc);
1078 tc = NULL;
1079 kfree(rc);
1080 rc = NULL;
1081 } while (count > 0 && n == rsize);
1082
1083 return total;
1084
1085error:
1086 kfree(tc);
1087 kfree(rc);
1088 return err;
1089}
1090EXPORT_SYMBOL(p9_client_read);
1091
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001092int
1093p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1094 u64 offset, u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001095{
1096 int err, n, rsize, total;
1097 struct p9_fcall *tc, *rc;
1098 struct p9_client *clnt;
1099
1100 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
1101 (long long unsigned) offset, count);
1102 err = 0;
1103 tc = NULL;
1104 rc = NULL;
1105 clnt = fid->clnt;
1106 total = 0;
1107
1108 rsize = fid->iounit;
1109 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1110 rsize = clnt->msize - P9_IOHDRSZ;
1111
1112 do {
1113 if (count < rsize)
1114 rsize = count;
1115
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001116 if (data)
1117 tc = p9_create_twrite(fid->fid, offset, rsize, data);
1118 else
1119 tc = p9_create_twrite_u(fid->fid, offset, rsize, udata);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001120 if (IS_ERR(tc)) {
1121 err = PTR_ERR(tc);
1122 tc = NULL;
1123 goto error;
1124 }
1125
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001126 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001127 if (err)
1128 goto error;
1129
1130 n = rc->params.rread.count;
1131 count -= n;
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001132
1133 if (data)
1134 data += n;
1135 else
1136 udata += n;
1137
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001138 offset += n;
1139 total += n;
1140 kfree(tc);
1141 tc = NULL;
1142 kfree(rc);
1143 rc = NULL;
1144 } while (count > 0);
1145
1146 return total;
1147
1148error:
1149 kfree(tc);
1150 kfree(rc);
1151 return err;
1152}
1153EXPORT_SYMBOL(p9_client_write);
1154
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001155static struct p9_stat *p9_clone_stat(struct p9_stat *st, int dotu)
1156{
1157 int n;
1158 char *p;
1159 struct p9_stat *ret;
1160
1161 n = sizeof(struct p9_stat) + st->name.len + st->uid.len + st->gid.len +
1162 st->muid.len;
1163
1164 if (dotu)
1165 n += st->extension.len;
1166
1167 ret = kmalloc(n, GFP_KERNEL);
1168 if (!ret)
1169 return ERR_PTR(-ENOMEM);
1170
1171 memmove(ret, st, sizeof(struct p9_stat));
1172 p = ((char *) ret) + sizeof(struct p9_stat);
1173 memmove(p, st->name.str, st->name.len);
1174 ret->name.str = p;
1175 p += st->name.len;
1176 memmove(p, st->uid.str, st->uid.len);
1177 ret->uid.str = p;
1178 p += st->uid.len;
1179 memmove(p, st->gid.str, st->gid.len);
1180 ret->gid.str = p;
1181 p += st->gid.len;
1182 memmove(p, st->muid.str, st->muid.len);
1183 ret->muid.str = p;
1184 p += st->muid.len;
1185
1186 if (dotu) {
1187 memmove(p, st->extension.str, st->extension.len);
1188 ret->extension.str = p;
1189 p += st->extension.len;
1190 }
1191
1192 return ret;
1193}
1194
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001195struct p9_stat *p9_client_stat(struct p9_fid *fid)
1196{
1197 int err;
1198 struct p9_fcall *tc, *rc;
1199 struct p9_client *clnt;
1200 struct p9_stat *ret;
1201
1202 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1203 err = 0;
1204 tc = NULL;
1205 rc = NULL;
1206 ret = NULL;
1207 clnt = fid->clnt;
1208
1209 tc = p9_create_tstat(fid->fid);
1210 if (IS_ERR(tc)) {
1211 err = PTR_ERR(tc);
1212 tc = NULL;
1213 goto error;
1214 }
1215
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001216 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001217 if (err)
1218 goto error;
1219
1220 ret = p9_clone_stat(&rc->params.rstat.stat, clnt->dotu);
1221 if (IS_ERR(ret)) {
1222 err = PTR_ERR(ret);
1223 ret = NULL;
1224 goto error;
1225 }
1226
1227 kfree(tc);
1228 kfree(rc);
1229 return ret;
1230
1231error:
1232 kfree(tc);
1233 kfree(rc);
1234 kfree(ret);
1235 return ERR_PTR(err);
1236}
1237EXPORT_SYMBOL(p9_client_stat);
1238
1239int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1240{
1241 int err;
1242 struct p9_fcall *tc, *rc;
1243 struct p9_client *clnt;
1244
1245 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1246 err = 0;
1247 tc = NULL;
1248 rc = NULL;
1249 clnt = fid->clnt;
1250
1251 tc = p9_create_twstat(fid->fid, wst, clnt->dotu);
1252 if (IS_ERR(tc)) {
1253 err = PTR_ERR(tc);
1254 tc = NULL;
1255 goto done;
1256 }
1257
Eric Van Hensbergene2735b72008-02-06 19:25:58 -06001258 err = p9_client_rpc(clnt, tc, &rc);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001259
1260done:
1261 kfree(tc);
1262 kfree(rc);
1263 return err;
1264}
1265EXPORT_SYMBOL(p9_client_wstat);