blob: 3b5f3c94a6ebe11f9d4482a85d55ee10679bbb30 [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 Kodi0fb80ab2010-03-05 18:49:11 +000061/* Interpret mount option for protocol version */
62static unsigned char get_protocol_version(const substring_t *name)
63{
64 unsigned char version = -EINVAL;
65 if (!strncmp("9p2000", name->from, name->to-name->from)) {
66 version = p9_proto_legacy;
67 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: Legacy\n");
68 } else if (!strncmp("9p2000.u", name->from, name->to-name->from)) {
69 version = p9_proto_2000u;
70 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
71 } else if (!strncmp("9p2010.L", name->from, name->to-name->from)) {
72 version = p9_proto_2010L;
73 P9_DPRINTK(P9_DEBUG_9P, "Protocol version: 9P2010.L\n");
74 } else {
75 P9_DPRINTK(P9_DEBUG_ERROR, "Unknown protocol version %s. ",
76 name->from);
77 }
78 return version;
79}
80
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050081static struct p9_req_t *
82p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -050083
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060084/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -060085 * parse_options - parse mount options into client structure
86 * @opts: options string passed from mount
87 * @clnt: existing v9fs client information
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060088 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060089 * Return 0 upon success, -ERRNO upon failure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060090 */
91
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060092static int parse_opts(char *opts, struct p9_client *clnt)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060093{
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -060094 char *options, *tmp_options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060095 char *p;
96 substring_t args[MAX_OPT_ARGS];
97 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -060098 int ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060099
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600100 clnt->dotu = 1;
101 clnt->msize = 8192;
102
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600103 if (!opts)
104 return 0;
105
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600106 tmp_options = kstrdup(opts, GFP_KERNEL);
107 if (!tmp_options) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600108 P9_DPRINTK(P9_DEBUG_ERROR,
109 "failed to allocate copy of option string\n");
110 return -ENOMEM;
111 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600112 options = tmp_options;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600113
114 while ((p = strsep(&options, ",")) != NULL) {
115 int token;
116 if (!*p)
117 continue;
118 token = match_token(p, tokens, args);
119 if (token < Opt_trans) {
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600120 int r = match_int(&args[0], &option);
121 if (r < 0) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600122 P9_DPRINTK(P9_DEBUG_ERROR,
123 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600124 ret = r;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600125 continue;
126 }
127 }
128 switch (token) {
129 case Opt_msize:
130 clnt->msize = option;
131 break;
132 case Opt_trans:
Tejun Heo72029fe2008-09-24 16:22:23 -0500133 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600134 if(clnt->trans_mod == NULL) {
135 P9_DPRINTK(P9_DEBUG_ERROR,
136 "Could not find request transport: %s\n",
137 (char *) &args[0]);
138 ret = -EINVAL;
139 goto free_and_return;
140 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600141 break;
142 case Opt_legacy:
143 clnt->dotu = 0;
144 break;
Sripathi Kodi0fb80ab2010-03-05 18:49:11 +0000145 case Opt_version:
146 ret = get_protocol_version(&args[0]);
147 if (ret == -EINVAL)
148 goto free_and_return;
149 clnt->proto_version = ret;
150 break;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600151 default:
152 continue;
153 }
154 }
Tejun Heo72029fe2008-09-24 16:22:23 -0500155
Eric Van Hensbergen349d3bb2010-01-15 19:01:10 -0600156free_and_return:
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600157 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600158 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600159}
160
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500161/**
162 * p9_tag_alloc - lookup/allocate a request by tag
163 * @c: client session to lookup tag within
164 * @tag: numeric id for transaction
165 *
166 * this is a simple array lookup, but will grow the
167 * request_slots as necessary to accomodate transaction
168 * ids which did not previously have a slot.
169 *
170 * this code relies on the client spinlock to manage locks, its
171 * possible we should switch to something else, but I'd rather
172 * stick with something low-overhead for the common case.
173 *
174 */
175
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500176static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag)
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500177{
178 unsigned long flags;
179 int row, col;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500180 struct p9_req_t *req;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500181
182 /* This looks up the original request by tag so we know which
183 * buffer to read the data into */
184 tag++;
185
186 if (tag >= c->max_tag) {
187 spin_lock_irqsave(&c->lock, flags);
188 /* check again since original check was outside of lock */
189 while (tag >= c->max_tag) {
190 row = (tag / P9_ROW_MAXTAG);
191 c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
192 sizeof(struct p9_req_t), GFP_ATOMIC);
193
194 if (!c->reqs[row]) {
195 printk(KERN_ERR "Couldn't grow tag array\n");
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500196 spin_unlock_irqrestore(&c->lock, flags);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500197 return ERR_PTR(-ENOMEM);
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500198 }
199 for (col = 0; col < P9_ROW_MAXTAG; col++) {
200 c->reqs[row][col].status = REQ_STATUS_IDLE;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500201 c->reqs[row][col].tc = NULL;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500202 }
203 c->max_tag += P9_ROW_MAXTAG;
204 }
205 spin_unlock_irqrestore(&c->lock, flags);
206 }
207 row = tag / P9_ROW_MAXTAG;
208 col = tag % P9_ROW_MAXTAG;
209
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500210 req = &c->reqs[row][col];
211 if (!req->tc) {
212 req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
213 if (!req->wq) {
214 printk(KERN_ERR "Couldn't grow tag array\n");
215 return ERR_PTR(-ENOMEM);
216 }
217 init_waitqueue_head(req->wq);
218 req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize,
219 GFP_KERNEL);
220 req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize,
221 GFP_KERNEL);
222 if ((!req->tc) || (!req->rc)) {
223 printk(KERN_ERR "Couldn't grow tag array\n");
224 kfree(req->tc);
225 kfree(req->rc);
Tom Tucker45abdf12008-10-23 16:33:25 -0500226 kfree(req->wq);
227 req->tc = req->rc = NULL;
228 req->wq = NULL;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500229 return ERR_PTR(-ENOMEM);
230 }
231 req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall);
232 req->tc->capacity = c->msize;
233 req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall);
234 req->rc->capacity = c->msize;
235 }
236
237 p9pdu_reset(req->tc);
238 p9pdu_reset(req->rc);
239
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500240 req->tc->tag = tag-1;
241 req->status = REQ_STATUS_ALLOC;
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500242
243 return &c->reqs[row][col];
244}
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500245
246/**
247 * p9_tag_lookup - lookup a request by tag
248 * @c: client session to lookup tag within
249 * @tag: numeric id for transaction
250 *
251 */
252
253struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
254{
255 int row, col;
256
257 /* This looks up the original request by tag so we know which
258 * buffer to read the data into */
259 tag++;
260
261 BUG_ON(tag >= c->max_tag);
262
263 row = tag / P9_ROW_MAXTAG;
264 col = tag % P9_ROW_MAXTAG;
265
266 return &c->reqs[row][col];
267}
268EXPORT_SYMBOL(p9_tag_lookup);
269
270/**
271 * p9_tag_init - setup tags structure and contents
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600272 * @c: v9fs client struct
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500273 *
274 * This initializes the tags structure for each client instance.
275 *
276 */
277
278static int p9_tag_init(struct p9_client *c)
279{
280 int err = 0;
281
282 c->tagpool = p9_idpool_create();
283 if (IS_ERR(c->tagpool)) {
284 err = PTR_ERR(c->tagpool);
285 c->tagpool = NULL;
286 goto error;
287 }
288
289 p9_idpool_get(c->tagpool); /* reserve tag 0 */
290
291 c->max_tag = 0;
292error:
293 return err;
294}
295
296/**
297 * p9_tag_cleanup - cleans up tags structure and reclaims resources
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600298 * @c: v9fs client struct
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500299 *
300 * This frees resources associated with the tags structure
301 *
302 */
303static void p9_tag_cleanup(struct p9_client *c)
304{
305 int row, col;
306
307 /* check to insure all requests are idle */
308 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
309 for (col = 0; col < P9_ROW_MAXTAG; col++) {
310 if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
311 P9_DPRINTK(P9_DEBUG_MUX,
312 "Attempting to cleanup non-free tag %d,%d\n",
313 row, col);
314 /* TODO: delay execution of cleanup */
315 return;
316 }
317 }
318 }
319
320 if (c->tagpool)
321 p9_idpool_destroy(c->tagpool);
322
323 /* free requests associated with tags */
324 for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500325 for (col = 0; col < P9_ROW_MAXTAG; col++) {
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500326 kfree(c->reqs[row][col].wq);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500327 kfree(c->reqs[row][col].tc);
328 kfree(c->reqs[row][col].rc);
329 }
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500330 kfree(c->reqs[row]);
331 }
332 c->max_tag = 0;
333}
334
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500335/**
336 * p9_free_req - free a request and clean-up as necessary
337 * c: client state
338 * r: request to release
339 *
340 */
341
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500342static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500343{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500344 int tag = r->tc->tag;
345 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
346
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500347 r->status = REQ_STATUS_IDLE;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500348 if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
349 p9_idpool_put(tag, c->tagpool);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500350}
351
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500352/**
353 * p9_client_cb - call back from transport to client
354 * c: client state
355 * req: request received
356 *
357 */
358void p9_client_cb(struct p9_client *c, struct p9_req_t *req)
359{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500360 P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500361 wake_up(req->wq);
362 P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500363}
364EXPORT_SYMBOL(p9_client_cb);
365
366/**
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500367 * p9_parse_header - parse header arguments out of a packet
368 * @pdu: packet to parse
369 * @size: size of packet
370 * @type: type of request
371 * @tag: tag of packet
372 * @rewind: set if we need to rewind offset afterwards
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500373 */
374
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500375int
376p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
377 int rewind)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500378{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500379 int8_t r_type;
380 int16_t r_tag;
381 int32_t r_size;
382 int offset = pdu->offset;
383 int err;
384
385 pdu->offset = 0;
386 if (pdu->size == 0)
387 pdu->size = 7;
388
389 err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
390 if (err)
391 goto rewind_and_exit;
392
393 pdu->size = r_size;
394 pdu->id = r_type;
395 pdu->tag = r_tag;
396
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500397 P9_DPRINTK(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n", pdu->size,
398 pdu->id, pdu->tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500399
400 if (type)
401 *type = r_type;
402 if (tag)
403 *tag = r_tag;
404 if (size)
405 *size = r_size;
406
407
408rewind_and_exit:
409 if (rewind)
410 pdu->offset = offset;
411 return err;
412}
413EXPORT_SYMBOL(p9_parse_header);
414
415/**
416 * p9_check_errors - check 9p packet for error return and process it
417 * @c: current client instance
418 * @req: request to parse and check for error conditions
419 *
420 * returns error code if one is discovered, otherwise returns 0
421 *
422 * this will have to be more complicated if we have multiple
423 * error packet types
424 */
425
426static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
427{
428 int8_t type;
429 int err;
430
431 err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
432 if (err) {
433 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
434 return err;
435 }
436
437 if (type == P9_RERROR) {
438 int ecode;
439 char *ename;
440
441 err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode);
442 if (err) {
443 P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n",
444 err);
445 return err;
446 }
447
448 if (c->dotu)
449 err = -ecode;
450
Abhishek Kulkarni0aad37e2009-08-17 16:38:45 -0500451 if (!err || !IS_ERR_VALUE(err))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500452 err = p9_errstr2errno(ename, strlen(ename));
453
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500454 P9_DPRINTK(P9_DEBUG_9P, "<<< RERROR (%d) %s\n", -ecode, ename);
455
456 kfree(ename);
457 } else
458 err = 0;
459
460 return err;
461}
462
463/**
464 * p9_client_flush - flush (cancel) a request
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600465 * @c: client state
466 * @oldreq: request to cancel
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500467 *
468 * This sents a flush for a particular requests and links
469 * the flush request to the original request. The current
470 * code only supports a single flush request although the protocol
471 * allows for multiple flush requests to be sent for a single request.
472 *
473 */
474
475static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
476{
477 struct p9_req_t *req;
478 int16_t oldtag;
479 int err;
480
481 err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
482 if (err)
483 return err;
484
485 P9_DPRINTK(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
486
487 req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
488 if (IS_ERR(req))
489 return PTR_ERR(req);
490
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500491
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500492 /* if we haven't received a response for oldreq,
493 remove it from the list. */
494 spin_lock(&c->lock);
495 if (oldreq->status == REQ_STATUS_FLSH)
496 list_del(&oldreq->req_list);
497 spin_unlock(&c->lock);
498
499 p9_free_req(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500500 return 0;
501}
502
503/**
504 * p9_client_rpc - issue a request and wait for a response
505 * @c: client session
506 * @type: type of request
507 * @fmt: protocol format string (see protocol.c)
508 *
509 * Returns request structure (which client must free using p9_free_req)
510 */
511
512static struct p9_req_t *
513p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
514{
515 va_list ap;
516 int tag, err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500517 struct p9_req_t *req;
518 unsigned long flags;
519 int sigpending;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500520
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500521 P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500522
523 if (c->status != Connected)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500524 return ERR_PTR(-EIO);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500525
526 if (signal_pending(current)) {
527 sigpending = 1;
528 clear_thread_flag(TIF_SIGPENDING);
529 } else
530 sigpending = 0;
531
532 tag = P9_NOTAG;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500533 if (type != P9_TVERSION) {
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500534 tag = p9_idpool_get(c->tagpool);
535 if (tag < 0)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500536 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500537 }
538
539 req = p9_tag_alloc(c, tag);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500540 if (IS_ERR(req))
541 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500542
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500543 /* marshall the data */
544 p9pdu_prepare(req->tc, tag, type);
545 va_start(ap, fmt);
546 err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap);
547 va_end(ap);
548 p9pdu_finalize(req->tc);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500549
550 err = c->trans_mod->request(c, req);
551 if (err < 0) {
552 c->status = Disconnected;
553 goto reterr;
554 }
555
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500556 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500557 err = wait_event_interruptible(*req->wq,
558 req->status >= REQ_STATUS_RCVD);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500559 P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d\n",
560 req->wq, tag, err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500561
562 if (req->status == REQ_STATUS_ERROR) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500563 P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500564 err = req->t_err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500565 }
566
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500567 if ((err == -ERESTARTSYS) && (c->status == Connected)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500568 P9_DPRINTK(P9_DEBUG_MUX, "flushing\n");
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500569 sigpending = 1;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500570 clear_thread_flag(TIF_SIGPENDING);
571
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500572 if (c->trans_mod->cancel(c, req))
573 p9_client_flush(c, req);
574
575 /* if we received the response anyway, don't signal error */
576 if (req->status == REQ_STATUS_RCVD)
577 err = 0;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500578 }
579
580 if (sigpending) {
581 spin_lock_irqsave(&current->sighand->siglock, flags);
582 recalc_sigpending();
583 spin_unlock_irqrestore(&current->sighand->siglock, flags);
584 }
585
586 if (err < 0)
587 goto reterr;
588
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500589 err = p9_check_errors(c, req);
590 if (!err) {
591 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type);
592 return req;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500593 }
594
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500595reterr:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500596 P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type,
597 err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500598 p9_free_req(c, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500599 return ERR_PTR(err);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500600}
601
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500602static struct p9_fid *p9_fid_create(struct p9_client *clnt)
603{
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500604 int ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500605 struct p9_fid *fid;
Tom Tuckercac23d62008-10-23 16:31:02 -0500606 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500607
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500608 P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500609 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
610 if (!fid)
611 return ERR_PTR(-ENOMEM);
612
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500613 ret = p9_idpool_get(clnt->fidpool);
Roel Kluin24e94de2009-01-18 21:32:11 -0800614 if (ret < 0) {
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500615 ret = -ENOSPC;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500616 goto error;
617 }
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500618 fid->fid = ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500619
620 memset(&fid->qid, 0, sizeof(struct p9_qid));
621 fid->mode = -1;
David Howellsf8b9d532008-11-14 10:38:44 +1100622 fid->uid = current_fsuid();
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500623 fid->clnt = clnt;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600624 fid->rdir = NULL;
Tom Tuckercac23d62008-10-23 16:31:02 -0500625 spin_lock_irqsave(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500626 list_add(&fid->flist, &clnt->fidlist);
Tom Tuckercac23d62008-10-23 16:31:02 -0500627 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500628
629 return fid;
630
631error:
632 kfree(fid);
Roel Kluin9f3e9bb2008-10-28 14:22:43 -0500633 return ERR_PTR(ret);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500634}
635
636static void p9_fid_destroy(struct p9_fid *fid)
637{
638 struct p9_client *clnt;
Tom Tuckercac23d62008-10-23 16:31:02 -0500639 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500640
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500641 P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500642 clnt = fid->clnt;
643 p9_idpool_put(fid->fid, clnt->fidpool);
Tom Tuckercac23d62008-10-23 16:31:02 -0500644 spin_lock_irqsave(&clnt->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500645 list_del(&fid->flist);
Tom Tuckercac23d62008-10-23 16:31:02 -0500646 spin_unlock_irqrestore(&clnt->lock, flags);
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600647 kfree(fid->rdir);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500648 kfree(fid);
649}
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600650
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500651int p9_client_version(struct p9_client *c)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500652{
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500653 int err = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500654 struct p9_req_t *req;
655 char *version;
656 int msize;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500657
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500658 P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n",
659 c->msize, c->dotu);
660 req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize,
661 c->dotu ? "9P2000.u" : "9P2000");
662 if (IS_ERR(req))
663 return PTR_ERR(req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500664
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500665 err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version);
666 if (err) {
667 P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500668 p9pdu_dump(1, req->rc);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500669 goto error;
670 }
671
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500672 P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
673 if (!memcmp(version, "9P2000.u", 8))
674 c->dotu = 1;
675 else if (!memcmp(version, "9P2000", 6))
676 c->dotu = 0;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500677 else {
678 err = -EREMOTEIO;
679 goto error;
680 }
681
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500682 if (msize < c->msize)
683 c->msize = msize;
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500684
685error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500686 kfree(version);
687 p9_free_req(c, req);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500688
689 return err;
690}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500691EXPORT_SYMBOL(p9_client_version);
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500692
693struct p9_client *p9_client_create(const char *dev_name, char *options)
694{
695 int err;
696 struct p9_client *clnt;
697
698 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500699 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
700 if (!clnt)
701 return ERR_PTR(-ENOMEM);
702
Tejun Heo72029fe2008-09-24 16:22:23 -0500703 clnt->trans_mod = NULL;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600704 clnt->trans = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500705 spin_lock_init(&clnt->lock);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500706 INIT_LIST_HEAD(&clnt->fidlist);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500707
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500708 p9_tag_init(clnt);
709
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600710 err = parse_opts(options, clnt);
711 if (err < 0)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600712 goto free_client;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600713
Abhishek Kulkarnia17d1722009-07-14 13:24:10 -0500714 if (!clnt->trans_mod)
715 clnt->trans_mod = v9fs_get_default_trans();
716
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600717 if (clnt->trans_mod == NULL) {
718 err = -EPROTONOSUPPORT;
719 P9_DPRINTK(P9_DEBUG_ERROR,
720 "No transport defined or default transport\n");
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600721 goto free_client;
722 }
723
724 clnt->fidpool = p9_idpool_create();
725 if (IS_ERR(clnt->fidpool)) {
726 err = PTR_ERR(clnt->fidpool);
727 clnt->fidpool = NULL;
728 goto put_trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500729 }
730
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500731 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n",
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600732 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
733
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500734 err = clnt->trans_mod->create(clnt, dev_name, options);
735 if (err)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600736 goto destroy_fidpool;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600737
738 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
739 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
740
Eric Van Hensbergen6936bf62008-10-13 20:36:14 -0500741 err = p9_client_version(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500742 if (err)
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600743 goto close_trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500744
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500745 return clnt;
746
Eric Van Hensbergen8781ff92010-02-08 18:18:34 -0600747close_trans:
748 clnt->trans_mod->close(clnt);
749destroy_fidpool:
750 p9_idpool_destroy(clnt->fidpool);
751put_trans:
752 v9fs_put_trans(clnt->trans_mod);
753free_client:
754 kfree(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500755 return ERR_PTR(err);
756}
757EXPORT_SYMBOL(p9_client_create);
758
759void p9_client_destroy(struct p9_client *clnt)
760{
761 struct p9_fid *fid, *fidptr;
762
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500763 P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500764
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500765 if (clnt->trans_mod)
766 clnt->trans_mod->close(clnt);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500767
Tejun Heo72029fe2008-09-24 16:22:23 -0500768 v9fs_put_trans(clnt->trans_mod);
769
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500770 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
771 p9_fid_destroy(fid);
772
Eric Van Hensbergen0af88872007-07-13 16:47:58 -0500773 if (clnt->fidpool)
774 p9_idpool_destroy(clnt->fidpool);
775
Eric Van Hensbergenfea511a2008-10-13 18:45:23 -0500776 p9_tag_cleanup(clnt);
777
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500778 kfree(clnt);
779}
780EXPORT_SYMBOL(p9_client_destroy);
781
782void p9_client_disconnect(struct p9_client *clnt)
783{
784 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500785 clnt->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500786}
787EXPORT_SYMBOL(p9_client_disconnect);
788
789struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
Latchesar Ionkovba176742007-10-17 14:31:07 -0500790 char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500791{
792 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500793 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500794 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500795 struct p9_qid qid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500796
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500797 P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
798 afid ? afid->fid : -1, uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500799 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500800
801 fid = p9_fid_create(clnt);
802 if (IS_ERR(fid)) {
803 err = PTR_ERR(fid);
804 fid = NULL;
805 goto error;
806 }
807
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500808 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid,
809 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
810 if (IS_ERR(req)) {
811 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500812 goto error;
813 }
814
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500815 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
816 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500817 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500818 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500819 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500820 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500821
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500822 P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800823 qid.type,
824 (unsigned long long)qid.path,
825 qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500826
827 memmove(&fid->qid, &qid, sizeof(struct p9_qid));
828
829 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500830 return fid;
831
832error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500833 if (fid)
834 p9_fid_destroy(fid);
835 return ERR_PTR(err);
836}
837EXPORT_SYMBOL(p9_client_attach);
838
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500839struct p9_fid *
840p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500841{
842 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500843 struct p9_req_t *req;
844 struct p9_qid qid;
845 struct p9_fid *afid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500846
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500847 P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500848 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500849
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500850 afid = p9_fid_create(clnt);
851 if (IS_ERR(afid)) {
852 err = PTR_ERR(afid);
853 afid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500854 goto error;
855 }
856
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500857 req = p9_client_rpc(clnt, P9_TAUTH, "dss?d",
858 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
859 if (IS_ERR(req)) {
860 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500861 goto error;
862 }
863
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500864 err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid);
865 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500866 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500867 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500868 goto error;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500869 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500870
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500871 P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800872 qid.type,
873 (unsigned long long)qid.path,
874 qid.version);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500875
876 memmove(&afid->qid, &qid, sizeof(struct p9_qid));
877 p9_free_req(clnt, req);
878 return afid;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500879
880error:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500881 if (afid)
882 p9_fid_destroy(afid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500883 return ERR_PTR(err);
884}
885EXPORT_SYMBOL(p9_client_auth);
886
887struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
888 int clone)
889{
890 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500891 struct p9_client *clnt;
892 struct p9_fid *fid;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500893 struct p9_qid *wqids;
894 struct p9_req_t *req;
895 int16_t nwqids, count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500896
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500897 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500898 clnt = oldfid->clnt;
899 if (clone) {
900 fid = p9_fid_create(clnt);
901 if (IS_ERR(fid)) {
902 err = PTR_ERR(fid);
903 fid = NULL;
904 goto error;
905 }
906
907 fid->uid = oldfid->uid;
908 } else
909 fid = oldfid;
910
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500911
912 P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n",
913 oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
914
915 req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
916 nwname, wnames);
917 if (IS_ERR(req)) {
918 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500919 goto error;
920 }
921
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500922 err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500923 if (err) {
924 p9pdu_dump(1, req->rc);
925 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500926 goto clunk_fid;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500927 }
928 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500929
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500930 P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
931
932 if (nwqids != nwname) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500933 err = -ENOENT;
934 goto clunk_fid;
935 }
936
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500937 for (count = 0; count < nwqids; count++)
938 P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800939 count, wqids[count].type,
940 (unsigned long long)wqids[count].path,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500941 wqids[count].version);
942
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500943 if (nwname)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500944 memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500945 else
946 fid->qid = oldfid->qid;
947
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500948 return fid;
949
950clunk_fid:
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500951 p9_client_clunk(fid);
952 fid = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500953
954error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500955 if (fid && (fid != oldfid))
956 p9_fid_destroy(fid);
957
958 return ERR_PTR(err);
959}
960EXPORT_SYMBOL(p9_client_walk);
961
962int p9_client_open(struct p9_fid *fid, int mode)
963{
964 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500965 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500966 struct p9_req_t *req;
967 struct p9_qid qid;
968 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500969
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500970 P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500971 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500972 clnt = fid->clnt;
973
974 if (fid->mode != -1)
975 return -EINVAL;
976
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500977 req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
978 if (IS_ERR(req)) {
979 err = PTR_ERR(req);
980 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500981 }
982
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500983 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500984 if (err) {
985 p9pdu_dump(1, req->rc);
986 goto free_and_error;
987 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500988
989 P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -0800990 qid.type,
991 (unsigned long long)qid.path,
992 qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500993
994 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500995 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500996
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500997free_and_error:
998 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500999error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001000 return err;
1001}
1002EXPORT_SYMBOL(p9_client_open);
1003
1004int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
1005 char *extension)
1006{
1007 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001008 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001009 struct p9_req_t *req;
1010 struct p9_qid qid;
1011 int iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001012
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001013 P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
1014 fid->fid, name, perm, mode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001015 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001016 clnt = fid->clnt;
1017
1018 if (fid->mode != -1)
1019 return -EINVAL;
1020
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001021 req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
1022 mode, extension);
1023 if (IS_ERR(req)) {
1024 err = PTR_ERR(req);
1025 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001026 }
1027
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001028 err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001029 if (err) {
1030 p9pdu_dump(1, req->rc);
1031 goto free_and_error;
1032 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001033
1034 P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001035 qid.type,
1036 (unsigned long long)qid.path,
1037 qid.version, iounit);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001038
1039 fid->mode = mode;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001040 fid->iounit = iounit;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001041
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001042free_and_error:
1043 p9_free_req(clnt, req);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001044error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001045 return err;
1046}
1047EXPORT_SYMBOL(p9_client_fcreate);
1048
1049int p9_client_clunk(struct p9_fid *fid)
1050{
1051 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001052 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001053 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001054
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001055 P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001056 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001057 clnt = fid->clnt;
1058
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001059 req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
1060 if (IS_ERR(req)) {
1061 err = PTR_ERR(req);
1062 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001063 }
1064
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001065 P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001066
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001067 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001068 p9_fid_destroy(fid);
1069
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001070error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001071 return err;
1072}
1073EXPORT_SYMBOL(p9_client_clunk);
1074
1075int p9_client_remove(struct p9_fid *fid)
1076{
1077 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001078 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001079 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001080
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001081 P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001082 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001083 clnt = fid->clnt;
1084
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001085 req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
1086 if (IS_ERR(req)) {
1087 err = PTR_ERR(req);
1088 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001089 }
1090
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001091 P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001092
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001093 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001094 p9_fid_destroy(fid);
1095
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001096error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001097 return err;
1098}
1099EXPORT_SYMBOL(p9_client_remove);
1100
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001101int
1102p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset,
1103 u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001104{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001105 int err, rsize, total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001106 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001107 struct p9_req_t *req;
1108 char *dataptr;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001109
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001110 P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid,
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001111 (long long unsigned) offset, count);
1112 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001113 clnt = fid->clnt;
1114 total = 0;
1115
1116 rsize = fid->iounit;
1117 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1118 rsize = clnt->msize - P9_IOHDRSZ;
1119
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001120 if (count < rsize)
1121 rsize = count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001122
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001123 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize);
1124 if (IS_ERR(req)) {
1125 err = PTR_ERR(req);
1126 goto error;
1127 }
1128
1129 err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001130 if (err) {
1131 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001132 goto free_and_error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001133 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001134
1135 P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1136
1137 if (data) {
1138 memmove(data, dataptr, count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001139 }
1140
1141 if (udata) {
1142 err = copy_to_user(udata, dataptr, count);
1143 if (err) {
1144 err = -EFAULT;
1145 goto free_and_error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001146 }
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001147 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001148
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001149 p9_free_req(clnt, req);
1150 return count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001151
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001152free_and_error:
1153 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001154error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001155 return err;
1156}
1157EXPORT_SYMBOL(p9_client_read);
1158
Eric Van Hensbergen0fc96552008-10-13 20:36:17 -05001159int
1160p9_client_write(struct p9_fid *fid, char *data, const char __user *udata,
1161 u64 offset, u32 count)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001162{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001163 int err, rsize, total;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001164 struct p9_client *clnt;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001165 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001166
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001167 P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n",
1168 fid->fid, (long long unsigned) offset, count);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001169 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001170 clnt = fid->clnt;
1171 total = 0;
1172
1173 rsize = fid->iounit;
1174 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1175 rsize = clnt->msize - P9_IOHDRSZ;
1176
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001177 if (count < rsize)
1178 rsize = count;
1179 if (data)
1180 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset,
1181 rsize, data);
1182 else
1183 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset,
1184 rsize, udata);
1185 if (IS_ERR(req)) {
1186 err = PTR_ERR(req);
1187 goto error;
1188 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001189
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001190 err = p9pdu_readf(req->rc, clnt->dotu, "d", &count);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001191 if (err) {
1192 p9pdu_dump(1, req->rc);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001193 goto free_and_error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001194 }
1195
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001196 P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001197
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001198 p9_free_req(clnt, req);
1199 return count;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001200
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001201free_and_error:
1202 p9_free_req(clnt, req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001203error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001204 return err;
1205}
1206EXPORT_SYMBOL(p9_client_write);
1207
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001208struct p9_wstat *p9_client_stat(struct p9_fid *fid)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001209{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001210 int err;
1211 struct p9_client *clnt;
1212 struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
1213 struct p9_req_t *req;
1214 u16 ignored;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001215
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001216 P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001217
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001218 if (!ret)
1219 return ERR_PTR(-ENOMEM);
1220
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001221 err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001222 clnt = fid->clnt;
1223
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001224 req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
1225 if (IS_ERR(req)) {
1226 err = PTR_ERR(req);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001227 goto error;
1228 }
1229
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001230 err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001231 if (err) {
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001232 p9pdu_dump(1, req->rc);
Abhishek Kulkarnieedfe1c2009-07-14 13:25:41 -05001233 p9_free_req(clnt, req);
1234 goto error;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001235 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001236
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001237 P9_DPRINTK(P9_DEBUG_9P,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001238 "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1239 "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1240 "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1241 "<<< uid=%d gid=%d n_muid=%d\n",
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001242 ret->size, ret->type, ret->dev, ret->qid.type,
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001243 (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
1244 ret->atime, ret->mtime, (unsigned long long)ret->length,
1245 ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001246 ret->n_uid, ret->n_gid, ret->n_muid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001247
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001248 p9_free_req(clnt, req);
1249 return ret;
1250
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001251error:
Latchesar Ionkov742b11a2009-04-05 16:26:41 -05001252 kfree(ret);
1253 return ERR_PTR(err);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001254}
1255EXPORT_SYMBOL(p9_client_stat);
1256
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001257static int p9_client_statsize(struct p9_wstat *wst, int optional)
1258{
1259 int ret;
1260
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001261 /* NOTE: size shouldn't include its own length */
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001262 /* size[2] type[2] dev[4] qid[13] */
1263 /* mode[4] atime[4] mtime[4] length[8]*/
1264 /* name[s] uid[s] gid[s] muid[s] */
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001265 ret = 2+4+13+4+4+4+8+2+2+2+2;
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001266
1267 if (wst->name)
1268 ret += strlen(wst->name);
1269 if (wst->uid)
1270 ret += strlen(wst->uid);
1271 if (wst->gid)
1272 ret += strlen(wst->gid);
1273 if (wst->muid)
1274 ret += strlen(wst->muid);
1275
1276 if (optional) {
1277 ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
1278 if (wst->extension)
1279 ret += strlen(wst->extension);
1280 }
1281
1282 return ret;
1283}
1284
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001285int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
1286{
1287 int err;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001288 struct p9_req_t *req;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001289 struct p9_client *clnt;
1290
Latchesar Ionkov453ed902009-04-05 16:22:16 -05001291 err = 0;
1292 clnt = fid->clnt;
1293 wst->size = p9_client_statsize(wst, clnt->dotu);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001294 P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001295 P9_DPRINTK(P9_DEBUG_9P,
1296 " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
1297 " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
1298 " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
1299 " uid=%d gid=%d n_muid=%d\n",
1300 wst->size, wst->type, wst->dev, wst->qid.type,
Randy Dunlapb0d5fde2008-11-04 20:46:46 -08001301 (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
1302 wst->atime, wst->mtime, (unsigned long long)wst->length,
1303 wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -05001304 wst->n_uid, wst->n_gid, wst->n_muid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001305
Eric Van Hensbergen9d6939d2010-01-15 19:01:56 -06001306 req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001307 if (IS_ERR(req)) {
1308 err = PTR_ERR(req);
1309 goto error;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001310 }
1311
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001312 P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001313
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001314 p9_free_req(clnt, req);
1315error:
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001316 return err;
1317}
1318EXPORT_SYMBOL(p9_client_wstat);