blob: 20a33194d66ed95b5140f1ca2e0641e0dcda6724 [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,
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000049 Opt_version,
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060050 Opt_err,
51};
52
Steven Whitehousea447c092008-10-13 10:46:57 +010053static const match_table_t tokens = {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060054 {Opt_msize, "msize=%u"},
55 {Opt_legacy, "noextend"},
56 {Opt_trans, "trans=%s"},
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000057 {Opt_version, "version=%s"},
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060058 {Opt_err, NULL},
59};
60
Sripathi Kodi342fee12010-03-05 18:50:14 +000061inline int p9_is_proto_dotl(struct p9_client *clnt)
62{
Sripathi Kodi45bc21e2010-03-08 17:33:04 +000063 return (clnt->proto_version == p9_proto_2000L);
Sripathi Kodi342fee12010-03-05 18:50:14 +000064}
65EXPORT_SYMBOL(p9_is_proto_dotl);
66
67inline int p9_is_proto_dotu(struct p9_client *clnt)
68{
69 return (clnt->proto_version == p9_proto_2000u);
70}
71EXPORT_SYMBOL(p9_is_proto_dotu);
72
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000073/* Interpret mount option for protocol version */
Dan Carpenter3dc9fef2010-04-05 14:37:28 -050074static int get_protocol_version(const substring_t *name)
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000075{
Dan Carpenter3dc9fef2010-04-05 14:37:28 -050076 int version = -EINVAL;
77
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000078 if (!strncmp("9p2000", name->from, name->to-name->from)) {
79 version = p9_proto_legacy;
80 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: Legacy\n");
81 } else if (!strncmp("9p2000.u", name->from, name->to-name->from)) {
82 version = p9_proto_2000u;
83 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
Sripathi Kodi45bc21e2010-03-08 17:33:04 +000084 } else if (!strncmp("9p2000.L", name->from, name->to-name->from)) {
85 version = p9_proto_2000L;
86 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.L\n");
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +000087 } else {
88 P9_DPRINTK(P9_DEBUG_ERROR, "Unknown protocol version %s. ",
89 name->from);
90 }
91 return version;
92}
93
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050094static struct p9_req_t *
95p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -050096
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060097/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -060098 * parse_options - parse mount options into client structure
99 * @opts: options string passed from mount
100 * @clnt: existing v9fs client information
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600101 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600102 * Return 0 upon success, -ERRNO upon failure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600103 */
104
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600105static int parse_opts(char *opts, struct p9_client *clnt)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600106{
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600107 char *options, *tmp_options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600108 char *p;
109 substring_t args[MAX_OPT_ARGS];
110 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600111 int ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600112
Sripathi Kodi342fee12010-03-05 18:50:14 +0000113 clnt->proto_version = p9_proto_2000u;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600114 clnt->msize = 8192;
115
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600116 if (!opts)
117 return 0;
118
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600119 tmp_options = kstrdup(opts, GFP_KERNEL);
120 if (!tmp_options) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600121 P9_DPRINTK(P9_DEBUG_ERROR,
122 "failed to allocate copy of option string\n");
123 return -ENOMEM;
124 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600125 options = tmp_options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600126
127 while ((p = strsep(&options, ",")) != NULL) {
128 int token;
129 if (!*p)
130 continue;
131 token = match_token(p, tokens, args);
132 if (token < Opt_trans) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600133 int r = match_int(&args[0], &option);
134 if (r < 0) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600135 P9_DPRINTK(P9_DEBUG_ERROR,
136 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600137 ret = r;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600138 continue;
139 }
140 }
141 switch (token) {
142 case Opt_msize:
143 clnt->msize = option;
144 break;
145 case Opt_trans:
Tejun Heo72029fe2008-09-24 16:22:23 -0500146 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600147 if(clnt->trans_mod == NULL) {
148 P9_DPRINTK(P9_DEBUG_ERROR,
149 "Could not find request transport: %s\n",
150 (char *) &args[0]);
151 ret = -EINVAL;
152 goto free_and_return;
153 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600154 break;
155 case Opt_legacy:
Sripathi Kodi342fee12010-03-05 18:50:14 +0000156 clnt->proto_version = p9_proto_legacy;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600157 break;
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000158 case Opt_version:
159 ret = get_protocol_version(&args[0]);
160 if (ret == -EINVAL)
161 goto free_and_return;
162 clnt->proto_version = ret;
163 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600164 default:
165 continue;
166 }
167 }
Tejun Heo72029fe2008-09-24 16:22:23 -0500168
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600169free_and_return:
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600170 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600171 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600172}
173
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500174/**
175 * p9_tag_alloc - lookup/allocate a request by tag
176 * @c: client session to lookup tag within
177 * @tag: numeric id for transaction
178 *
179 * this is a simple array lookup, but will grow the
180 * request_slots as necessary to accomodate transaction
181 * ids which did not previously have a slot.
182 *
183 * this code relies on the client spinlock to manage locks, its
184 * possible we should switch to something else, but I'd rather
185 * stick with something low-overhead for the common case.
186 *
187 */
188
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500189static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500190{
191 unsigned long flags;
192 int row, col;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500193 struct p9_req_t *req;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500194
195 /* This looks up the original request by tag so we know which
196 * buffer to read the data into */
197 tag++;
198
199 if (tag >= c->max_tag) {
200 spin_lock_irqsave(&c->lock, flags);
201 /* check again since original check was outside of lock */
202 while (tag >= c->max_tag) {
203 row = (tag / P9_ROW_MAXTAG);
204 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
205 sizeof(struct p9_req_t), GFP_ATOMIC);
206
207 if (!c->reqs[row]) {
208 printk(KERN_ERR "Couldn't grow tag array\n");
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500209 spin_unlock_irqrestore(&c->lock, flags);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500210 return ERR_PTR(-ENOMEM);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500211 }
212 for (col = 0; col < P9_ROW_MAXTAG; col++) {
213 c->reqs[row][col].status = REQ_STATUS_IDLE;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500214 c->reqs[row][col].tc = NULL;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500215 }
216 c->max_tag += P9_ROW_MAXTAG;
217 }
218 spin_unlock_irqrestore(&c->lock, flags);
219 }
220 row = tag / P9_ROW_MAXTAG;
221 col = tag % P9_ROW_MAXTAG;
222
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500223 req = &c->reqs[row][col];
224 if (!req->tc) {
225 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
226 if (!req->wq) {
227 printk(KERN_ERR "Couldn't grow tag array\n");
228 return ERR_PTR(-ENOMEM);
229 }
230 init_waitqueue_head(req->wq);
231 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize,
232 GFP_KERNEL);
233 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize,
234 GFP_KERNEL);
235 if ((!req->tc) || (!req->rc)) {
236 printk(KERN_ERR "Couldn't grow tag array\n");
237 kfree(req->tc);
238 kfree(req->rc);
Tom Tucker45abdf12008-10-23 16:33:25 -0500239 kfree(req->wq);
240 req->tc = req->rc = NULL;
241 req->wq = NULL;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500242 return ERR_PTR(-ENOMEM);
243 }
244 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
245 req->tc->capacity = c->msize;
246 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
247 req->rc->capacity = c->msize;
248 }
249
250 p9pdu_reset(req->tc);
251 p9pdu_reset(req->rc);
252
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500253 req->tc->tag = tag-1;
254 req->status = REQ_STATUS_ALLOC;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500255
256 return &c->reqs[row][col];
257}
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500258
259/**
260 * p9_tag_lookup - lookup a request by tag
261 * @c: client session to lookup tag within
262 * @tag: numeric id for transaction
263 *
264 */
265
266struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
267{
268 int row, col;
269
270 /* This looks up the original request by tag so we know which
271 * buffer to read the data into */
272 tag++;
273
274 BUG_ON(tag >= c->max_tag);
275
276 row = tag / P9_ROW_MAXTAG;
277 col = tag % P9_ROW_MAXTAG;
278
279 return &c->reqs[row][col];
280}
281EXPORT_SYMBOL(p9_tag_lookup);
282
283/**
284 * p9_tag_init - setup tags structure and contents
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600285 * @c: v9fs client struct
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500286 *
287 * This initializes the tags structure for each client instance.
288 *
289 */
290
291static int p9_tag_init(struct p9_client *c)
292{
293 int err = 0;
294
295 c->tagpool = p9_idpool_create();
296 if (IS_ERR(c->tagpool)) {
297 err = PTR_ERR(c->tagpool);
298 c->tagpool = NULL;
299 goto error;
300 }
301
302 p9_idpool_get(c->tagpool); /* reserve tag 0 */
303
304 c->max_tag = 0;
305error:
306 return err;
307}
308
309/**
310 * p9_tag_cleanup - cleans up tags structure and reclaims resources
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600311 * @c: v9fs client struct
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500312 *
313 * This frees resources associated with the tags structure
314 *
315 */
316static void p9_tag_cleanup(struct p9_client *c)
317{
318 int row, col;
319
320 /* check to insure all requests are idle */
321 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
322 for (col = 0; col < P9_ROW_MAXTAG; col++) {
323 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
324 P9_DPRINTK(P9_DEBUG_MUX,
325 "Attempting to cleanup non-free tag %d,%d\n",
326 row, col);
327 /* TODO: delay execution of cleanup */
328 return;
329 }
330 }
331 }
332
333 if (c->tagpool)
334 p9_idpool_destroy(c->tagpool);
335
336 /* free requests associated with tags */
337 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500338 for (col = 0; col < P9_ROW_MAXTAG; col++) {
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500339 kfree(c->reqs[row][col].wq);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500340 kfree(c->reqs[row][col].tc);
341 kfree(c->reqs[row][col].rc);
342 }
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500343 kfree(c->reqs[row]);
344 }
345 c->max_tag = 0;
346}
347
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500348/**
349 * p9_free_req - free a request and clean-up as necessary
350 * c: client state
351 * r: request to release
352 *
353 */
354
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500355static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500356{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500357 int tag = r->tc->tag;
358 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
359
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500360 r->status = REQ_STATUS_IDLE;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500361 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
362 p9_idpool_put(tag, c->tagpool);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500363}
364
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500365/**
366 * p9_client_cb - call back from transport to client
367 * c: client state
368 * req: request received
369 *
370 */
371void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
372{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500373 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500374 wake_up(req->wq);
375 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500376}
377EXPORT_SYMBOL(p9_client_cb);
378
379/**
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500380 * p9_parse_header - parse header arguments out of a packet
381 * @pdu: packet to parse
382 * @size: size of packet
383 * @type: type of request
384 * @tag: tag of packet
385 * @rewind: set if we need to rewind offset afterwards
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500386 */
387
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500388int
389p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
390 int rewind)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500391{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500392 int8_t r_type;
393 int16_t r_tag;
394 int32_t r_size;
395 int offset = pdu->offset;
396 int err;
397
398 pdu->offset = 0;
399 if (pdu->size == 0)
400 pdu->size = 7;
401
402 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
403 if (err)
404 goto rewind_and_exit;
405
406 pdu->size = r_size;
407 pdu->id = r_type;
408 pdu->tag = r_tag;
409
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500410 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
411 pdu->id, pdu->tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500412
413 if (type)
414 *type = r_type;
415 if (tag)
416 *tag = r_tag;
417 if (size)
418 *size = r_size;
419
420
421rewind_and_exit:
422 if (rewind)
423 pdu->offset = offset;
424 return err;
425}
426EXPORT_SYMBOL(p9_parse_header);
427
428/**
429 * p9_check_errors - check 9p packet for error return and process it
430 * @c: current client instance
431 * @req: request to parse and check for error conditions
432 *
433 * returns error code if one is discovered, otherwise returns 0
434 *
435 * this will have to be more complicated if we have multiple
436 * error packet types
437 */
438
439static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
440{
441 int8_t type;
442 int err;
443
444 err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
445 if (err) {
446 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
447 return err;
448 }
449
450 if (type == P9_RERROR) {
451 int ecode;
452 char *ename;
453
Sripathi Kodi342fee12010-03-05 18:50:14 +0000454 err = p9pdu_readf(req->rc, c->proto_version, "s?d",
455 &ename, &ecode);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500456 if (err) {
457 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
458 err);
459 return err;
460 }
461
Sripathi Kodi342fee12010-03-05 18:50:14 +0000462 if (p9_is_proto_dotu(c))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500463 err = -ecode;
464
Abhishek Kulkarni0aad37e2009-08-17 16:38:45 -0500465 if (!err || !IS_ERR_VALUE(err))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500466 err = p9_errstr2errno(ename, strlen(ename));
467
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500468 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
469
470 kfree(ename);
471 } else
472 err = 0;
473
474 return err;
475}
476
477/**
478 * p9_client_flush - flush (cancel) a request
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600479 * @c: client state
480 * @oldreq: request to cancel
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500481 *
482 * This sents a flush for a particular requests and links
483 * the flush request to the original request. The current
484 * code only supports a single flush request although the protocol
485 * allows for multiple flush requests to be sent for a single request.
486 *
487 */
488
489static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
490{
491 struct p9_req_t *req;
492 int16_t oldtag;
493 int err;
494
495 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
496 if (err)
497 return err;
498
499 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
500
501 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
502 if (IS_ERR(req))
503 return PTR_ERR(req);
504
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500505
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500506 /* if we haven't received a response for oldreq,
507 remove it from the list. */
508 spin_lock(&c->lock);
509 if (oldreq->status == REQ_STATUS_FLSH)
510 list_del(&oldreq->req_list);
511 spin_unlock(&c->lock);
512
513 p9_free_req(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500514 return 0;
515}
516
517/**
518 * p9_client_rpc - issue a request and wait for a response
519 * @c: client session
520 * @type: type of request
521 * @fmt: protocol format string (see protocol.c)
522 *
523 * Returns request structure (which client must free using p9_free_req)
524 */
525
526static struct p9_req_t *
527p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
528{
529 va_list ap;
530 int tag, err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500531 struct p9_req_t *req;
532 unsigned long flags;
533 int sigpending;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500534
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500535 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500536
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500537 /* we allow for any status other than disconnected */
538 if (c->status == Disconnected)
539 return ERR_PTR(-EIO);
540
541 /* if status is begin_disconnected we allow only clunk request */
542 if ((c->status == BeginDisconnect) && (type != P9_TCLUNK))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500543 return ERR_PTR(-EIO);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500544
545 if (signal_pending(current)) {
546 sigpending = 1;
547 clear_thread_flag(TIF_SIGPENDING);
548 } else
549 sigpending = 0;
550
551 tag = P9_NOTAG;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500552 if (type != P9_TVERSION) {
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500553 tag = p9_idpool_get(c->tagpool);
554 if (tag < 0)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500555 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500556 }
557
558 req = p9_tag_alloc(c, tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500559 if (IS_ERR(req))
560 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500561
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500562 /* marshall the data */
563 p9pdu_prepare(req->tc, tag, type);
564 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000565 err = p9pdu_vwritef(req->tc, c->proto_version, fmt, ap);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500566 va_end(ap);
567 p9pdu_finalize(req->tc);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500568
569 err = c->trans_mod->request(c, req);
570 if (err < 0) {
571 c->status = Disconnected;
572 goto reterr;
573 }
574
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500575 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500576 err = wait_event_interruptible(*req->wq,
577 req->status >= REQ_STATUS_RCVD);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500578 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
579 req->wq, tag, err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500580
581 if (req->status == REQ_STATUS_ERROR) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500582 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500583 err = req->t_err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500584 }
585
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500586 if ((err == -ERESTARTSYS) && (c->status == Connected)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500587 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500588 sigpending = 1;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500589 clear_thread_flag(TIF_SIGPENDING);
590
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500591 if (c->trans_mod->cancel(c, req))
592 p9_client_flush(c, req);
593
594 /* if we received the response anyway, don't signal error */
595 if (req->status == REQ_STATUS_RCVD)
596 err = 0;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500597 }
598
599 if (sigpending) {
600 spin_lock_irqsave(&current->sighand->siglock, flags);
601 recalc_sigpending();
602 spin_unlock_irqrestore(&current->sighand->siglock, flags);
603 }
604
605 if (err < 0)
606 goto reterr;
607
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500608 err = p9_check_errors(c, req);
609 if (!err) {
610 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
611 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500612 }
613
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500614reterr:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500615 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
616 err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500617 p9_free_req(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500618 return ERR_PTR(err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500619}
620
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500621static struct p9_fid *p9_fid_create(struct p9_client *clnt)
622{
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500623 int ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500624 struct p9_fid *fid;
Tom Tuckercac23d62008-10-23 16:31:02 -0500625 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500626
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500627 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500628 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
629 if (!fid)
630 return ERR_PTR(-ENOMEM);
631
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500632 ret = p9_idpool_get(clnt->fidpool);
Roel Kluin24e94de2009-01-18 21:32:11 -0800633 if (ret < 0) {
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500634 ret = -ENOSPC;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500635 goto error;
636 }
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500637 fid->fid = ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500638
639 memset(&fid->qid, 0, sizeof(struct p9_qid));
640 fid->mode = -1;
David Howellsf8b9d532008-11-14 10:38:44 +1100641 fid->uid = current_fsuid();
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500642 fid->clnt = clnt;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600643 fid->rdir = NULL;
Tom Tuckercac23d62008-10-23 16:31:02 -0500644 spin_lock_irqsave(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500645 list_add(&fid->flist, &clnt->fidlist);
Tom Tuckercac23d62008-10-23 16:31:02 -0500646 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500647
648 return fid;
649
650error:
651 kfree(fid);
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500652 return ERR_PTR(ret);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500653}
654
655static void p9_fid_destroy(struct p9_fid *fid)
656{
657 struct p9_client *clnt;
Tom Tuckercac23d62008-10-23 16:31:02 -0500658 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500659
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500660 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500661 clnt = fid->clnt;
662 p9_idpool_put(fid->fid, clnt->fidpool);
Tom Tuckercac23d62008-10-23 16:31:02 -0500663 spin_lock_irqsave(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500664 list_del(&fid->flist);
Tom Tuckercac23d62008-10-23 16:31:02 -0500665 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600666 kfree(fid->rdir);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500667 kfree(fid);
668}
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600669
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500670int p9_client_version(struct p9_client *c)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500671{
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500672 int err = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500673 struct p9_req_t *req;
674 char *version;
675 int msize;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500676
Sripathi Kodi342fee12010-03-05 18:50:14 +0000677 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
678 c->msize, c->proto_version);
Sripathi Kodic5a76972010-03-05 18:51:04 +0000679
680 switch (c->proto_version) {
Sripathi Kodi45bc21e2010-03-08 17:33:04 +0000681 case p9_proto_2000L:
Sripathi Kodic5a76972010-03-05 18:51:04 +0000682 req = p9_client_rpc(c, P9_TVERSION, "ds",
Sripathi Kodi45bc21e2010-03-08 17:33:04 +0000683 c->msize, "9P2000.L");
Sripathi Kodic5a76972010-03-05 18:51:04 +0000684 break;
685 case p9_proto_2000u:
686 req = p9_client_rpc(c, P9_TVERSION, "ds",
687 c->msize, "9P2000.u");
688 break;
689 case p9_proto_legacy:
690 req = p9_client_rpc(c, P9_TVERSION, "ds",
691 c->msize, "9P2000");
692 break;
693 default:
694 return -EINVAL;
695 break;
696 }
697
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500698 if (IS_ERR(req))
699 return PTR_ERR(req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500700
Sripathi Kodi342fee12010-03-05 18:50:14 +0000701 err = p9pdu_readf(req->rc, c->proto_version, "ds", &msize, &version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500702 if (err) {
703 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500704 p9pdu_dump(1, req->rc);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500705 goto error;
706 }
707
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500708 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
Sripathi Kodi45bc21e2010-03-08 17:33:04 +0000709 if (!strncmp(version, "9P2000.L", 8))
710 c->proto_version = p9_proto_2000L;
Sripathi Kodic5a76972010-03-05 18:51:04 +0000711 else if (!strncmp(version, "9P2000.u", 8))
Sripathi Kodi342fee12010-03-05 18:50:14 +0000712 c->proto_version = p9_proto_2000u;
713 else if (!strncmp(version, "9P2000", 6))
714 c->proto_version = p9_proto_legacy;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500715 else {
716 err = -EREMOTEIO;
717 goto error;
718 }
719
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500720 if (msize < c->msize)
721 c->msize = msize;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500722
723error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500724 kfree(version);
725 p9_free_req(c, req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500726
727 return err;
728}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500729EXPORT_SYMBOL(p9_client_version);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500730
731struct p9_client *p9_client_create(const char *dev_name, char *options)
732{
733 int err;
734 struct p9_client *clnt;
735
736 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500737 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
738 if (!clnt)
739 return ERR_PTR(-ENOMEM);
740
Tejun Heo72029fe2008-09-24 16:22:23 -0500741 clnt->trans_mod = NULL;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600742 clnt->trans = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500743 spin_lock_init(&clnt->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500744 INIT_LIST_HEAD(&clnt->fidlist);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500745
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500746 p9_tag_init(clnt);
747
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600748 err = parse_opts(options, clnt);
749 if (err < 0)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600750 goto free_client;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600751
Abhishek Kulkarnia17d1722009-07-14 13:24:10 -0500752 if (!clnt->trans_mod)
753 clnt->trans_mod = v9fs_get_default_trans();
754
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600755 if (clnt->trans_mod == NULL) {
756 err = -EPROTONOSUPPORT;
757 P9_DPRINTK(P9_DEBUG_ERROR,
758 "No transport defined or default transport\n");
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600759 goto free_client;
760 }
761
762 clnt->fidpool = p9_idpool_create();
763 if (IS_ERR(clnt->fidpool)) {
764 err = PTR_ERR(clnt->fidpool);
765 clnt->fidpool = NULL;
766 goto put_trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500767 }
768
Sripathi Kodi342fee12010-03-05 18:50:14 +0000769 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
770 clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600771
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500772 err = clnt->trans_mod->create(clnt, dev_name, options);
773 if (err)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600774 goto destroy_fidpool;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600775
776 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
777 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
778
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500779 err = p9_client_version(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500780 if (err)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600781 goto close_trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500782
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500783 return clnt;
784
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600785close_trans:
786 clnt->trans_mod->close(clnt);
787destroy_fidpool:
788 p9_idpool_destroy(clnt->fidpool);
789put_trans:
790 v9fs_put_trans(clnt->trans_mod);
791free_client:
792 kfree(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500793 return ERR_PTR(err);
794}
795EXPORT_SYMBOL(p9_client_create);
796
797void p9_client_destroy(struct p9_client *clnt)
798{
799 struct p9_fid *fid, *fidptr;
800
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500801 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500802
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500803 if (clnt->trans_mod)
804 clnt->trans_mod->close(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500805
Tejun Heo72029fe2008-09-24 16:22:23 -0500806 v9fs_put_trans(clnt->trans_mod);
807
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500808 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) {
809 printk(KERN_INFO "Found fid %d not clunked\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500810 p9_fid_destroy(fid);
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500811 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500812
Eric Van Hensbergen0af88872007-07-13 16:47:58 -0500813 if (clnt->fidpool)
814 p9_idpool_destroy(clnt->fidpool);
815
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500816 p9_tag_cleanup(clnt);
817
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500818 kfree(clnt);
819}
820EXPORT_SYMBOL(p9_client_destroy);
821
822void p9_client_disconnect(struct p9_client *clnt)
823{
824 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500825 clnt->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500826}
827EXPORT_SYMBOL(p9_client_disconnect);
828
Aneesh Kumar K.V6d96d3a2010-03-29 18:13:59 -0500829void p9_client_begin_disconnect(struct p9_client *clnt)
830{
831 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
832 clnt->status = BeginDisconnect;
833}
834EXPORT_SYMBOL(p9_client_begin_disconnect);
835
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500836struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500837 char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500838{
839 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500840 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500841 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500842 struct p9_qid qid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500843
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500844 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
845 afid ? afid->fid : -1, uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500846 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500847
848 fid = p9_fid_create(clnt);
849 if (IS_ERR(fid)) {
850 err = PTR_ERR(fid);
851 fid = NULL;
852 goto error;
853 }
854
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500855 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
856 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
857 if (IS_ERR(req)) {
858 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500859 goto error;
860 }
861
Sripathi Kodi342fee12010-03-05 18:50:14 +0000862 err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500863 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500864 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500865 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500866 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500867 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500868
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500869 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800870 qid.type,
871 (unsigned long long)qid.path,
872 qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500873
874 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
875
876 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500877 return fid;
878
879error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500880 if (fid)
881 p9_fid_destroy(fid);
882 return ERR_PTR(err);
883}
884EXPORT_SYMBOL(p9_client_attach);
885
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500886struct p9_fid *
887p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500888{
889 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500890 struct p9_req_t *req;
891 struct p9_qid qid;
892 struct p9_fid *afid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500893
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500894 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500895 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500896
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500897 afid = p9_fid_create(clnt);
898 if (IS_ERR(afid)) {
899 err = PTR_ERR(afid);
900 afid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500901 goto error;
902 }
903
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500904 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d",
905 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
906 if (IS_ERR(req)) {
907 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500908 goto error;
909 }
910
Sripathi Kodi342fee12010-03-05 18:50:14 +0000911 err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500912 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500913 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500914 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500915 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500916 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500917
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500918 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800919 qid.type,
920 (unsigned long long)qid.path,
921 qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500922
923 memmove(&afid->qid, &qid, sizeof(struct p9_qid));
924 p9_free_req(clnt, req);
925 return afid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500926
927error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500928 if (afid)
929 p9_fid_destroy(afid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500930 return ERR_PTR(err);
931}
932EXPORT_SYMBOL(p9_client_auth);
933
934struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
935 int clone)
936{
937 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500938 struct p9_client *clnt;
939 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500940 struct p9_qid *wqids;
941 struct p9_req_t *req;
942 int16_t nwqids, count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500943
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500944 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500945 clnt = oldfid->clnt;
946 if (clone) {
947 fid = p9_fid_create(clnt);
948 if (IS_ERR(fid)) {
949 err = PTR_ERR(fid);
950 fid = NULL;
951 goto error;
952 }
953
954 fid->uid = oldfid->uid;
955 } else
956 fid = oldfid;
957
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500958
959 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
960 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
961
962 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
963 nwname, wnames);
964 if (IS_ERR(req)) {
965 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500966 goto error;
967 }
968
Sripathi Kodi342fee12010-03-05 18:50:14 +0000969 err = p9pdu_readf(req->rc, clnt->proto_version, "R", &nwqids, &wqids);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500970 if (err) {
971 p9pdu_dump(1, req->rc);
972 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500973 goto clunk_fid;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500974 }
975 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500976
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500977 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
978
979 if (nwqids != nwname) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500980 err = -ENOENT;
981 goto clunk_fid;
982 }
983
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500984 for (count = 0; count < nwqids; count++)
985 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800986 count, wqids[count].type,
987 (unsigned long long)wqids[count].path,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500988 wqids[count].version);
989
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500990 if (nwname)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500991 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500992 else
993 fid->qid = oldfid->qid;
994
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500995 return fid;
996
997clunk_fid:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500998 p9_client_clunk(fid);
999 fid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001000
1001error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001002 if (fid && (fid != oldfid))
1003 p9_fid_destroy(fid);
1004
1005 return ERR_PTR(err);
1006}
1007EXPORT_SYMBOL(p9_client_walk);
1008
1009int p9_client_open(struct p9_fid *fid, int mode)
1010{
1011 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001012 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001013 struct p9_req_t *req;
1014 struct p9_qid qid;
1015 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001016
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001017 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001018 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001019 clnt = fid->clnt;
1020
1021 if (fid->mode != -1)
1022 return -EINVAL;
1023
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001024 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
1025 if (IS_ERR(req)) {
1026 err = PTR_ERR(req);
1027 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001028 }
1029
Sripathi Kodi342fee12010-03-05 18:50:14 +00001030 err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001031 if (err) {
1032 p9pdu_dump(1, req->rc);
1033 goto free_and_error;
1034 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001035
1036 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001037 qid.type,
1038 (unsigned long long)qid.path,
1039 qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001040
1041 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001042 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001043
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001044free_and_error:
1045 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001046error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001047 return err;
1048}
1049EXPORT_SYMBOL(p9_client_open);
1050
1051int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
1052 char *extension)
1053{
1054 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001055 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001056 struct p9_req_t *req;
1057 struct p9_qid qid;
1058 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001059
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001060 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1061 fid->fid, name, perm, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001062 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001063 clnt = fid->clnt;
1064
1065 if (fid->mode != -1)
1066 return -EINVAL;
1067
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001068 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1069 mode, extension);
1070 if (IS_ERR(req)) {
1071 err = PTR_ERR(req);
1072 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001073 }
1074
Sripathi Kodi342fee12010-03-05 18:50:14 +00001075 err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001076 if (err) {
1077 p9pdu_dump(1, req->rc);
1078 goto free_and_error;
1079 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001080
1081 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001082 qid.type,
1083 (unsigned long long)qid.path,
1084 qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001085
1086 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001087 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001088
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001089free_and_error:
1090 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001091error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001092 return err;
1093}
1094EXPORT_SYMBOL(p9_client_fcreate);
1095
1096int p9_client_clunk(struct p9_fid *fid)
1097{
1098 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001099 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001100 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001101
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001102 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001103 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001104 clnt = fid->clnt;
1105
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001106 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1107 if (IS_ERR(req)) {
1108 err = PTR_ERR(req);
1109 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001110 }
1111
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001112 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001113
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001114 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001115 p9_fid_destroy(fid);
1116
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001117error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001118 return err;
1119}
1120EXPORT_SYMBOL(p9_client_clunk);
1121
1122int p9_client_remove(struct p9_fid *fid)
1123{
1124 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001125 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001126 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001127
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001128 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001129 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001130 clnt = fid->clnt;
1131
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001132 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1133 if (IS_ERR(req)) {
1134 err = PTR_ERR(req);
1135 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001136 }
1137
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001138 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001139
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001140 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001141 p9_fid_destroy(fid);
1142
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001143error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001144 return err;
1145}
1146EXPORT_SYMBOL(p9_client_remove);
1147
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001148int
1149p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1150 u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001151{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001152 int err, rsize, total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001153 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001154 struct p9_req_t *req;
1155 char *dataptr;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001156
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001157 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid,
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001158 (long long unsigned) offset, count);
1159 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001160 clnt = fid->clnt;
1161 total = 0;
1162
1163 rsize = fid->iounit;
1164 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1165 rsize = clnt->msize - P9_IOHDRSZ;
1166
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001167 if (count < rsize)
1168 rsize = count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001169
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001170 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize);
1171 if (IS_ERR(req)) {
1172 err = PTR_ERR(req);
1173 goto error;
1174 }
1175
Sripathi Kodi342fee12010-03-05 18:50:14 +00001176 err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001177 if (err) {
1178 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001179 goto free_and_error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001180 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001181
1182 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1183
1184 if (data) {
1185 memmove(data, dataptr, count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001186 }
1187
1188 if (udata) {
1189 err = copy_to_user(udata, dataptr, count);
1190 if (err) {
1191 err = -EFAULT;
1192 goto free_and_error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001193 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001194 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001195
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001196 p9_free_req(clnt, req);
1197 return count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001198
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001199free_and_error:
1200 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001201error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001202 return err;
1203}
1204EXPORT_SYMBOL(p9_client_read);
1205
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001206int
1207p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1208 u64 offset, u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001209{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001210 int err, rsize, total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001211 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001212 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001213
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001214 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1215 fid->fid, (long long unsigned) offset, count);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001216 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001217 clnt = fid->clnt;
1218 total = 0;
1219
1220 rsize = fid->iounit;
1221 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1222 rsize = clnt->msize - P9_IOHDRSZ;
1223
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001224 if (count < rsize)
1225 rsize = count;
1226 if (data)
1227 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset,
1228 rsize, data);
1229 else
1230 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset,
1231 rsize, udata);
1232 if (IS_ERR(req)) {
1233 err = PTR_ERR(req);
1234 goto error;
1235 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001236
Sripathi Kodi342fee12010-03-05 18:50:14 +00001237 err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001238 if (err) {
1239 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001240 goto free_and_error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001241 }
1242
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001243 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001244
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001245 p9_free_req(clnt, req);
1246 return count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001247
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001248free_and_error:
1249 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001250error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001251 return err;
1252}
1253EXPORT_SYMBOL(p9_client_write);
1254
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001255struct p9_wstat *p9_client_stat(struct p9_fid *fid)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001256{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001257 int err;
1258 struct p9_client *clnt;
1259 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1260 struct p9_req_t *req;
1261 u16 ignored;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001262
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001263 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001264
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001265 if (!ret)
1266 return ERR_PTR(-ENOMEM);
1267
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001268 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001269 clnt = fid->clnt;
1270
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001271 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1272 if (IS_ERR(req)) {
1273 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001274 goto error;
1275 }
1276
Sripathi Kodi342fee12010-03-05 18:50:14 +00001277 err = p9pdu_readf(req->rc, clnt->proto_version, "wS", &ignored, ret);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001278 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001279 p9pdu_dump(1, req->rc);
Abhishek Kulkarnieedfe1c2009-07-14 13:25:41 -05001280 p9_free_req(clnt, req);
1281 goto error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001282 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001283
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001284 P9_DPRINTK(P9_DEBUG_9P,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001285 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1286 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1287 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1288 "<<< uid=%d gid=%d n_muid=%d\n",
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001289 ret->size, ret->type, ret->dev, ret->qid.type,
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001290 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1291 ret->atime, ret->mtime, (unsigned long long)ret->length,
1292 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001293 ret->n_uid, ret->n_gid, ret->n_muid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001294
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001295 p9_free_req(clnt, req);
1296 return ret;
1297
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001298error:
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001299 kfree(ret);
1300 return ERR_PTR(err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001301}
1302EXPORT_SYMBOL(p9_client_stat);
1303
Sripathi Kodi342fee12010-03-05 18:50:14 +00001304static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001305{
1306 int ret;
1307
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001308 /* NOTE: size shouldn't include its own length */
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001309 /* size[2] type[2] dev[4] qid[13] */
1310 /* mode[4] atime[4] mtime[4] length[8]*/
1311 /* name[s] uid[s] gid[s] muid[s] */
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001312 ret = 2+4+13+4+4+4+8+2+2+2+2;
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001313
1314 if (wst->name)
1315 ret += strlen(wst->name);
1316 if (wst->uid)
1317 ret += strlen(wst->uid);
1318 if (wst->gid)
1319 ret += strlen(wst->gid);
1320 if (wst->muid)
1321 ret += strlen(wst->muid);
1322
Sripathi Kodi342fee12010-03-05 18:50:14 +00001323 if (proto_version == p9_proto_2000u) {
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001324 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1325 if (wst->extension)
1326 ret += strlen(wst->extension);
1327 }
1328
1329 return ret;
1330}
1331
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001332int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1333{
1334 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001335 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001336 struct p9_client *clnt;
1337
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001338 err = 0;
1339 clnt = fid->clnt;
Sripathi Kodi342fee12010-03-05 18:50:14 +00001340 wst->size = p9_client_statsize(wst, clnt->proto_version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001341 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001342 P9_DPRINTK(P9_DEBUG_9P,
1343 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1344 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1345 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1346 " uid=%d gid=%d n_muid=%d\n",
1347 wst->size, wst->type, wst->dev, wst->qid.type,
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001348 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1349 wst->atime, wst->mtime, (unsigned long long)wst->length,
1350 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001351 wst->n_uid, wst->n_gid, wst->n_muid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001352
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001353 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001354 if (IS_ERR(req)) {
1355 err = PTR_ERR(req);
1356 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001357 }
1358
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001359 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001360
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001361 p9_free_req(clnt, req);
1362error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001363 return err;
1364}
1365EXPORT_SYMBOL(p9_client_wstat);