Commit | Line | Data |
---|---|---|
bd238fb4 LI |
1 | /* |
2 | * net/9p/clnt.c | |
3 | * | |
4 | * 9P Client | |
5 | * | |
8a0dc95f | 6 | * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com> |
bd238fb4 LI |
7 | * 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> | |
8a0dc95f | 29 | #include <linux/poll.h> |
bd238fb4 LI |
30 | #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> | |
fb0466c3 | 35 | #include <linux/parser.h> |
bd238fb4 | 36 | #include <net/9p/client.h> |
8b81ef58 | 37 | #include <net/9p/transport.h> |
51a87c55 | 38 | #include "protocol.h" |
bd238fb4 | 39 | |
8a0dc95f EVH |
40 | /* |
41 | * Client Option Parsing (code inspired by NFS code) | |
42 | * - a little lazy - parse all client options | |
43 | */ | |
44 | ||
45 | enum { | |
46 | Opt_msize, | |
47 | Opt_trans, | |
48 | Opt_legacy, | |
49 | Opt_err, | |
50 | }; | |
51 | ||
a447c093 | 52 | static const match_table_t tokens = { |
8a0dc95f EVH |
53 | {Opt_msize, "msize=%u"}, |
54 | {Opt_legacy, "noextend"}, | |
55 | {Opt_trans, "trans=%s"}, | |
56 | {Opt_err, NULL}, | |
57 | }; | |
58 | ||
51a87c55 EVH |
59 | static struct p9_req_t * |
60 | p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...); | |
91b8534f | 61 | |
8a0dc95f EVH |
62 | /** |
63 | * v9fs_parse_options - parse mount options into session structure | |
64 | * @options: options string passed from mount | |
65 | * @v9ses: existing v9fs session information | |
66 | * | |
bb8ffdfc | 67 | * Return 0 upon success, -ERRNO upon failure |
8a0dc95f EVH |
68 | */ |
69 | ||
bb8ffdfc | 70 | static int parse_opts(char *opts, struct p9_client *clnt) |
8a0dc95f | 71 | { |
bb8ffdfc | 72 | char *options; |
8a0dc95f EVH |
73 | char *p; |
74 | substring_t args[MAX_OPT_ARGS]; | |
75 | int option; | |
bb8ffdfc | 76 | int ret = 0; |
8a0dc95f | 77 | |
8a0dc95f EVH |
78 | clnt->dotu = 1; |
79 | clnt->msize = 8192; | |
80 | ||
bb8ffdfc EVH |
81 | if (!opts) |
82 | return 0; | |
83 | ||
84 | options = kstrdup(opts, GFP_KERNEL); | |
85 | if (!options) { | |
86 | P9_DPRINTK(P9_DEBUG_ERROR, | |
87 | "failed to allocate copy of option string\n"); | |
88 | return -ENOMEM; | |
89 | } | |
8a0dc95f EVH |
90 | |
91 | while ((p = strsep(&options, ",")) != NULL) { | |
92 | int token; | |
93 | if (!*p) | |
94 | continue; | |
95 | token = match_token(p, tokens, args); | |
96 | if (token < Opt_trans) { | |
bb8ffdfc EVH |
97 | int r = match_int(&args[0], &option); |
98 | if (r < 0) { | |
8a0dc95f EVH |
99 | P9_DPRINTK(P9_DEBUG_ERROR, |
100 | "integer field, but no integer?\n"); | |
bb8ffdfc | 101 | ret = r; |
8a0dc95f EVH |
102 | continue; |
103 | } | |
104 | } | |
105 | switch (token) { | |
106 | case Opt_msize: | |
107 | clnt->msize = option; | |
108 | break; | |
109 | case Opt_trans: | |
72029fe8 | 110 | clnt->trans_mod = v9fs_get_trans_by_name(&args[0]); |
8a0dc95f EVH |
111 | break; |
112 | case Opt_legacy: | |
113 | clnt->dotu = 0; | |
114 | break; | |
115 | default: | |
116 | continue; | |
117 | } | |
118 | } | |
72029fe8 TH |
119 | |
120 | if (!clnt->trans_mod) | |
121 | clnt->trans_mod = v9fs_get_default_trans(); | |
122 | ||
bb8ffdfc EVH |
123 | kfree(options); |
124 | return ret; | |
8a0dc95f EVH |
125 | } |
126 | ||
fea511a6 EVH |
127 | /** |
128 | * p9_tag_alloc - lookup/allocate a request by tag | |
129 | * @c: client session to lookup tag within | |
130 | * @tag: numeric id for transaction | |
131 | * | |
132 | * this is a simple array lookup, but will grow the | |
133 | * request_slots as necessary to accomodate transaction | |
134 | * ids which did not previously have a slot. | |
135 | * | |
136 | * this code relies on the client spinlock to manage locks, its | |
137 | * possible we should switch to something else, but I'd rather | |
138 | * stick with something low-overhead for the common case. | |
139 | * | |
140 | */ | |
141 | ||
51a87c55 | 142 | static struct p9_req_t *p9_tag_alloc(struct p9_client *c, u16 tag) |
fea511a6 EVH |
143 | { |
144 | unsigned long flags; | |
145 | int row, col; | |
51a87c55 | 146 | struct p9_req_t *req; |
fea511a6 EVH |
147 | |
148 | /* This looks up the original request by tag so we know which | |
149 | * buffer to read the data into */ | |
150 | tag++; | |
151 | ||
152 | if (tag >= c->max_tag) { | |
153 | spin_lock_irqsave(&c->lock, flags); | |
154 | /* check again since original check was outside of lock */ | |
155 | while (tag >= c->max_tag) { | |
156 | row = (tag / P9_ROW_MAXTAG); | |
157 | c->reqs[row] = kcalloc(P9_ROW_MAXTAG, | |
158 | sizeof(struct p9_req_t), GFP_ATOMIC); | |
159 | ||
160 | if (!c->reqs[row]) { | |
161 | printk(KERN_ERR "Couldn't grow tag array\n"); | |
51a87c55 | 162 | return ERR_PTR(-ENOMEM); |
fea511a6 EVH |
163 | } |
164 | for (col = 0; col < P9_ROW_MAXTAG; col++) { | |
165 | c->reqs[row][col].status = REQ_STATUS_IDLE; | |
51a87c55 | 166 | c->reqs[row][col].tc = NULL; |
fea511a6 EVH |
167 | } |
168 | c->max_tag += P9_ROW_MAXTAG; | |
169 | } | |
170 | spin_unlock_irqrestore(&c->lock, flags); | |
171 | } | |
172 | row = tag / P9_ROW_MAXTAG; | |
173 | col = tag % P9_ROW_MAXTAG; | |
174 | ||
51a87c55 EVH |
175 | req = &c->reqs[row][col]; |
176 | if (!req->tc) { | |
177 | req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL); | |
178 | if (!req->wq) { | |
179 | printk(KERN_ERR "Couldn't grow tag array\n"); | |
180 | return ERR_PTR(-ENOMEM); | |
181 | } | |
182 | init_waitqueue_head(req->wq); | |
183 | req->tc = kmalloc(sizeof(struct p9_fcall)+c->msize, | |
184 | GFP_KERNEL); | |
185 | req->rc = kmalloc(sizeof(struct p9_fcall)+c->msize, | |
186 | GFP_KERNEL); | |
187 | if ((!req->tc) || (!req->rc)) { | |
188 | printk(KERN_ERR "Couldn't grow tag array\n"); | |
189 | kfree(req->tc); | |
190 | kfree(req->rc); | |
191 | return ERR_PTR(-ENOMEM); | |
192 | } | |
193 | req->tc->sdata = (char *) req->tc + sizeof(struct p9_fcall); | |
194 | req->tc->capacity = c->msize; | |
195 | req->rc->sdata = (char *) req->rc + sizeof(struct p9_fcall); | |
196 | req->rc->capacity = c->msize; | |
197 | } | |
198 | ||
199 | p9pdu_reset(req->tc); | |
200 | p9pdu_reset(req->rc); | |
201 | ||
202 | req->flush_tag = 0; | |
203 | req->tc->tag = tag-1; | |
204 | req->status = REQ_STATUS_ALLOC; | |
fea511a6 EVH |
205 | |
206 | return &c->reqs[row][col]; | |
207 | } | |
fea511a6 EVH |
208 | |
209 | /** | |
210 | * p9_tag_lookup - lookup a request by tag | |
211 | * @c: client session to lookup tag within | |
212 | * @tag: numeric id for transaction | |
213 | * | |
214 | */ | |
215 | ||
216 | struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag) | |
217 | { | |
218 | int row, col; | |
219 | ||
220 | /* This looks up the original request by tag so we know which | |
221 | * buffer to read the data into */ | |
222 | tag++; | |
223 | ||
224 | BUG_ON(tag >= c->max_tag); | |
225 | ||
226 | row = tag / P9_ROW_MAXTAG; | |
227 | col = tag % P9_ROW_MAXTAG; | |
228 | ||
229 | return &c->reqs[row][col]; | |
230 | } | |
231 | EXPORT_SYMBOL(p9_tag_lookup); | |
232 | ||
233 | /** | |
234 | * p9_tag_init - setup tags structure and contents | |
235 | * @tags: tags structure from the client struct | |
236 | * | |
237 | * This initializes the tags structure for each client instance. | |
238 | * | |
239 | */ | |
240 | ||
241 | static int p9_tag_init(struct p9_client *c) | |
242 | { | |
243 | int err = 0; | |
244 | ||
245 | c->tagpool = p9_idpool_create(); | |
246 | if (IS_ERR(c->tagpool)) { | |
247 | err = PTR_ERR(c->tagpool); | |
248 | c->tagpool = NULL; | |
249 | goto error; | |
250 | } | |
251 | ||
252 | p9_idpool_get(c->tagpool); /* reserve tag 0 */ | |
253 | ||
254 | c->max_tag = 0; | |
255 | error: | |
256 | return err; | |
257 | } | |
258 | ||
259 | /** | |
260 | * p9_tag_cleanup - cleans up tags structure and reclaims resources | |
261 | * @tags: tags structure from the client struct | |
262 | * | |
263 | * This frees resources associated with the tags structure | |
264 | * | |
265 | */ | |
266 | static void p9_tag_cleanup(struct p9_client *c) | |
267 | { | |
268 | int row, col; | |
269 | ||
270 | /* check to insure all requests are idle */ | |
271 | for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) { | |
272 | for (col = 0; col < P9_ROW_MAXTAG; col++) { | |
273 | if (c->reqs[row][col].status != REQ_STATUS_IDLE) { | |
274 | P9_DPRINTK(P9_DEBUG_MUX, | |
275 | "Attempting to cleanup non-free tag %d,%d\n", | |
276 | row, col); | |
277 | /* TODO: delay execution of cleanup */ | |
278 | return; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | if (c->tagpool) | |
284 | p9_idpool_destroy(c->tagpool); | |
285 | ||
286 | /* free requests associated with tags */ | |
287 | for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) { | |
51a87c55 | 288 | for (col = 0; col < P9_ROW_MAXTAG; col++) { |
fea511a6 | 289 | kfree(c->reqs[row][col].wq); |
51a87c55 EVH |
290 | kfree(c->reqs[row][col].tc); |
291 | kfree(c->reqs[row][col].rc); | |
292 | } | |
fea511a6 EVH |
293 | kfree(c->reqs[row]); |
294 | } | |
295 | c->max_tag = 0; | |
296 | } | |
297 | ||
673d62cd EVH |
298 | /** |
299 | * p9_free_req - free a request and clean-up as necessary | |
300 | * c: client state | |
301 | * r: request to release | |
302 | * | |
303 | */ | |
304 | ||
51a87c55 | 305 | static void p9_free_req(struct p9_client *c, struct p9_req_t *r) |
673d62cd | 306 | { |
51a87c55 EVH |
307 | int tag = r->tc->tag; |
308 | P9_DPRINTK(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag); | |
309 | ||
673d62cd | 310 | r->status = REQ_STATUS_IDLE; |
51a87c55 EVH |
311 | if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool)) |
312 | p9_idpool_put(tag, c->tagpool); | |
673d62cd EVH |
313 | |
314 | /* if this was a flush request we have to free response fcall */ | |
51a87c55 | 315 | if (r->rc->id == P9_RFLUSH) { |
673d62cd EVH |
316 | kfree(r->tc); |
317 | kfree(r->rc); | |
318 | } | |
319 | } | |
320 | ||
91b8534f EVH |
321 | /** |
322 | * p9_client_cb - call back from transport to client | |
323 | * c: client state | |
324 | * req: request received | |
325 | * | |
326 | */ | |
327 | void p9_client_cb(struct p9_client *c, struct p9_req_t *req) | |
328 | { | |
329 | struct p9_req_t *other_req; | |
330 | unsigned long flags; | |
331 | ||
51a87c55 | 332 | P9_DPRINTK(P9_DEBUG_MUX, " tag %d\n", req->tc->tag); |
91b8534f EVH |
333 | |
334 | if (req->status == REQ_STATUS_ERROR) | |
335 | wake_up(req->wq); | |
336 | ||
51a87c55 EVH |
337 | if (req->flush_tag) { /* flush receive path */ |
338 | P9_DPRINTK(P9_DEBUG_9P, "<<< RFLUSH %d\n", req->tc->tag); | |
91b8534f | 339 | spin_lock_irqsave(&c->lock, flags); |
51a87c55 EVH |
340 | other_req = p9_tag_lookup(c, req->flush_tag); |
341 | if (other_req->status != REQ_STATUS_FLSH) /* stale flush */ | |
91b8534f EVH |
342 | spin_unlock_irqrestore(&c->lock, flags); |
343 | else { | |
91b8534f EVH |
344 | other_req->status = REQ_STATUS_FLSHD; |
345 | spin_unlock_irqrestore(&c->lock, flags); | |
346 | wake_up(other_req->wq); | |
347 | } | |
348 | p9_free_req(c, req); | |
349 | } else { /* normal receive path */ | |
51a87c55 | 350 | P9_DPRINTK(P9_DEBUG_MUX, "normal: tag %d\n", req->tc->tag); |
91b8534f EVH |
351 | spin_lock_irqsave(&c->lock, flags); |
352 | if (req->status != REQ_STATUS_FLSHD) | |
353 | req->status = REQ_STATUS_RCVD; | |
91b8534f EVH |
354 | spin_unlock_irqrestore(&c->lock, flags); |
355 | wake_up(req->wq); | |
356 | P9_DPRINTK(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag); | |
357 | } | |
358 | } | |
359 | EXPORT_SYMBOL(p9_client_cb); | |
360 | ||
51a87c55 EVH |
361 | /** |
362 | * p9_parse_header - parse header arguments out of a packet | |
363 | * @pdu: packet to parse | |
364 | * @size: size of packet | |
365 | * @type: type of request | |
366 | * @tag: tag of packet | |
367 | * @rewind: set if we need to rewind offset afterwards | |
368 | */ | |
369 | ||
370 | int | |
371 | p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag, | |
372 | int rewind) | |
373 | { | |
374 | int8_t r_type; | |
375 | int16_t r_tag; | |
376 | int32_t r_size; | |
377 | int offset = pdu->offset; | |
378 | int err; | |
379 | ||
380 | pdu->offset = 0; | |
381 | if (pdu->size == 0) | |
382 | pdu->size = 7; | |
383 | ||
384 | err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag); | |
385 | if (err) | |
386 | goto rewind_and_exit; | |
387 | ||
388 | pdu->size = r_size; | |
389 | pdu->id = r_type; | |
390 | pdu->tag = r_tag; | |
391 | ||
392 | P9_DPRINTK(P9_DEBUG_MUX, "pdu: type: %d tag: %d size=%d offset=%d\n", | |
393 | pdu->id, pdu->tag, pdu->size, pdu->offset); | |
394 | ||
395 | if (type) | |
396 | *type = r_type; | |
397 | if (tag) | |
398 | *tag = r_tag; | |
399 | if (size) | |
400 | *size = r_size; | |
401 | ||
402 | ||
403 | rewind_and_exit: | |
404 | if (rewind) | |
405 | pdu->offset = offset; | |
406 | return err; | |
407 | } | |
408 | EXPORT_SYMBOL(p9_parse_header); | |
409 | ||
410 | /** | |
411 | * p9_check_errors - check 9p packet for error return and process it | |
412 | * @c: current client instance | |
413 | * @req: request to parse and check for error conditions | |
414 | * | |
415 | * returns error code if one is discovered, otherwise returns 0 | |
416 | * | |
417 | * this will have to be more complicated if we have multiple | |
418 | * error packet types | |
419 | */ | |
420 | ||
421 | static int p9_check_errors(struct p9_client *c, struct p9_req_t *req) | |
422 | { | |
423 | int8_t type; | |
424 | int err; | |
425 | ||
426 | err = p9_parse_header(req->rc, NULL, &type, NULL, 0); | |
427 | if (err) { | |
428 | P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse header %d\n", err); | |
429 | return err; | |
430 | } | |
431 | ||
432 | if (type == P9_RERROR) { | |
433 | int ecode; | |
434 | char *ename; | |
435 | ||
436 | err = p9pdu_readf(req->rc, c->dotu, "s?d", &ename, &ecode); | |
437 | if (err) { | |
438 | P9_DPRINTK(P9_DEBUG_ERROR, "couldn't parse error%d\n", | |
439 | err); | |
440 | return err; | |
441 | } | |
442 | ||
443 | if (c->dotu) | |
444 | err = -ecode; | |
445 | ||
446 | if (!err) { | |
447 | err = p9_errstr2errno(ename, strlen(ename)); | |
448 | ||
449 | /* string match failed */ | |
450 | if (!err) | |
451 | err = -ESERVERFAULT; | |
452 | } | |
453 | ||
454 | 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 | |
465 | * c: client state | |
466 | * req: request to cancel | |
467 | * | |
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 | ||
475 | static 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 | ||
491 | req->flush_tag = oldtag; | |
492 | ||
493 | /* we don't free anything here because RPC isn't complete */ | |
494 | return 0; | |
495 | } | |
496 | ||
91b8534f EVH |
497 | /** |
498 | * p9_client_rpc - issue a request and wait for a response | |
499 | * @c: client session | |
51a87c55 EVH |
500 | * @type: type of request |
501 | * @fmt: protocol format string (see protocol.c) | |
91b8534f | 502 | * |
51a87c55 | 503 | * Returns request structure (which client must free using p9_free_req) |
91b8534f EVH |
504 | */ |
505 | ||
51a87c55 EVH |
506 | static struct p9_req_t * |
507 | p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...) | |
91b8534f | 508 | { |
51a87c55 EVH |
509 | va_list ap; |
510 | int tag, err; | |
91b8534f EVH |
511 | struct p9_req_t *req; |
512 | unsigned long flags; | |
513 | int sigpending; | |
514 | int flushed = 0; | |
515 | ||
51a87c55 | 516 | P9_DPRINTK(P9_DEBUG_MUX, "client %p op %d\n", c, type); |
91b8534f EVH |
517 | |
518 | if (c->status != Connected) | |
51a87c55 | 519 | return ERR_PTR(-EIO); |
91b8534f EVH |
520 | |
521 | if (signal_pending(current)) { | |
522 | sigpending = 1; | |
523 | clear_thread_flag(TIF_SIGPENDING); | |
524 | } else | |
525 | sigpending = 0; | |
526 | ||
527 | tag = P9_NOTAG; | |
51a87c55 | 528 | if (type != P9_TVERSION) { |
91b8534f EVH |
529 | tag = p9_idpool_get(c->tagpool); |
530 | if (tag < 0) | |
51a87c55 | 531 | return ERR_PTR(-ENOMEM); |
91b8534f EVH |
532 | } |
533 | ||
534 | req = p9_tag_alloc(c, tag); | |
51a87c55 EVH |
535 | if (IS_ERR(req)) |
536 | return req; | |
91b8534f | 537 | |
51a87c55 EVH |
538 | /* marshall the data */ |
539 | p9pdu_prepare(req->tc, tag, type); | |
540 | va_start(ap, fmt); | |
541 | err = p9pdu_vwritef(req->tc, c->dotu, fmt, ap); | |
542 | va_end(ap); | |
543 | p9pdu_finalize(req->tc); | |
91b8534f EVH |
544 | |
545 | err = c->trans_mod->request(c, req); | |
546 | if (err < 0) { | |
547 | c->status = Disconnected; | |
548 | goto reterr; | |
549 | } | |
550 | ||
551 | /* if it was a flush we just transmitted, return our tag */ | |
51a87c55 EVH |
552 | if (type == P9_TFLUSH) |
553 | return req; | |
91b8534f | 554 | again: |
51a87c55 | 555 | P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d\n", req->wq, tag); |
91b8534f EVH |
556 | err = wait_event_interruptible(*req->wq, |
557 | req->status >= REQ_STATUS_RCVD); | |
51a87c55 | 558 | P9_DPRINTK(P9_DEBUG_MUX, "wait %p tag: %d returned %d (flushed=%d)\n", |
91b8534f EVH |
559 | req->wq, tag, err, flushed); |
560 | ||
561 | if (req->status == REQ_STATUS_ERROR) { | |
51a87c55 | 562 | P9_DPRINTK(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err); |
91b8534f EVH |
563 | err = req->t_err; |
564 | } else if (err == -ERESTARTSYS && flushed) { | |
51a87c55 | 565 | P9_DPRINTK(P9_DEBUG_MUX, "flushed - going again\n"); |
91b8534f EVH |
566 | goto again; |
567 | } else if (req->status == REQ_STATUS_FLSHD) { | |
51a87c55 | 568 | P9_DPRINTK(P9_DEBUG_MUX, "flushed - erestartsys\n"); |
91b8534f EVH |
569 | err = -ERESTARTSYS; |
570 | } | |
571 | ||
572 | if ((err == -ERESTARTSYS) && (c->status == Connected) && (!flushed)) { | |
51a87c55 | 573 | P9_DPRINTK(P9_DEBUG_MUX, "flushing\n"); |
91b8534f EVH |
574 | spin_lock_irqsave(&c->lock, flags); |
575 | if (req->status == REQ_STATUS_SENT) | |
576 | req->status = REQ_STATUS_FLSH; | |
577 | spin_unlock_irqrestore(&c->lock, flags); | |
578 | sigpending = 1; | |
579 | flushed = 1; | |
580 | clear_thread_flag(TIF_SIGPENDING); | |
581 | ||
582 | if (c->trans_mod->cancel(c, req)) { | |
583 | err = p9_client_flush(c, req); | |
584 | if (err == 0) | |
585 | goto again; | |
586 | } | |
587 | } | |
588 | ||
589 | if (sigpending) { | |
590 | spin_lock_irqsave(¤t->sighand->siglock, flags); | |
591 | recalc_sigpending(); | |
592 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | |
593 | } | |
594 | ||
595 | if (err < 0) | |
596 | goto reterr; | |
597 | ||
51a87c55 EVH |
598 | err = p9_check_errors(c, req); |
599 | if (!err) { | |
600 | P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d\n", c, type); | |
601 | return req; | |
91b8534f EVH |
602 | } |
603 | ||
91b8534f | 604 | reterr: |
51a87c55 EVH |
605 | P9_DPRINTK(P9_DEBUG_MUX, "exit: client %p op %d error: %d\n", c, type, |
606 | err); | |
91b8534f | 607 | p9_free_req(c, req); |
51a87c55 | 608 | return ERR_PTR(err); |
91b8534f EVH |
609 | } |
610 | ||
5503ac56 EVH |
611 | static struct p9_fid *p9_fid_create(struct p9_client *clnt) |
612 | { | |
613 | int err; | |
614 | struct p9_fid *fid; | |
615 | ||
51a87c55 | 616 | P9_DPRINTK(P9_DEBUG_FID, "clnt %p\n", clnt); |
5503ac56 EVH |
617 | fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL); |
618 | if (!fid) | |
619 | return ERR_PTR(-ENOMEM); | |
620 | ||
621 | fid->fid = p9_idpool_get(clnt->fidpool); | |
622 | if (fid->fid < 0) { | |
623 | err = -ENOSPC; | |
624 | goto error; | |
625 | } | |
626 | ||
627 | memset(&fid->qid, 0, sizeof(struct p9_qid)); | |
628 | fid->mode = -1; | |
629 | fid->rdir_fpos = 0; | |
5503ac56 EVH |
630 | fid->uid = current->fsuid; |
631 | fid->clnt = clnt; | |
632 | fid->aux = NULL; | |
633 | ||
634 | spin_lock(&clnt->lock); | |
635 | list_add(&fid->flist, &clnt->fidlist); | |
636 | spin_unlock(&clnt->lock); | |
637 | ||
638 | return fid; | |
639 | ||
640 | error: | |
641 | kfree(fid); | |
642 | return ERR_PTR(err); | |
643 | } | |
644 | ||
645 | static void p9_fid_destroy(struct p9_fid *fid) | |
646 | { | |
647 | struct p9_client *clnt; | |
648 | ||
51a87c55 | 649 | P9_DPRINTK(P9_DEBUG_FID, "fid %d\n", fid->fid); |
5503ac56 EVH |
650 | clnt = fid->clnt; |
651 | p9_idpool_put(fid->fid, clnt->fidpool); | |
652 | spin_lock(&clnt->lock); | |
653 | list_del(&fid->flist); | |
654 | spin_unlock(&clnt->lock); | |
5503ac56 EVH |
655 | kfree(fid); |
656 | } | |
8a0dc95f | 657 | |
51a87c55 | 658 | int p9_client_version(struct p9_client *c) |
bd238fb4 | 659 | { |
6936bf60 | 660 | int err = 0; |
51a87c55 EVH |
661 | struct p9_req_t *req; |
662 | char *version; | |
663 | int msize; | |
bd238fb4 | 664 | |
51a87c55 EVH |
665 | P9_DPRINTK(P9_DEBUG_9P, ">>> TVERSION msize %d extended %d\n", |
666 | c->msize, c->dotu); | |
667 | req = p9_client_rpc(c, P9_TVERSION, "ds", c->msize, | |
668 | c->dotu ? "9P2000.u" : "9P2000"); | |
669 | if (IS_ERR(req)) | |
670 | return PTR_ERR(req); | |
6936bf60 | 671 | |
51a87c55 EVH |
672 | err = p9pdu_readf(req->rc, c->dotu, "ds", &msize, &version); |
673 | if (err) { | |
674 | P9_DPRINTK(P9_DEBUG_9P, "version error %d\n", err); | |
6936bf60 | 675 | goto error; |
51a87c55 | 676 | } |
6936bf60 | 677 | |
51a87c55 EVH |
678 | P9_DPRINTK(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version); |
679 | if (!memcmp(version, "9P2000.u", 8)) | |
680 | c->dotu = 1; | |
681 | else if (!memcmp(version, "9P2000", 6)) | |
682 | c->dotu = 0; | |
6936bf60 EVH |
683 | else { |
684 | err = -EREMOTEIO; | |
685 | goto error; | |
686 | } | |
687 | ||
51a87c55 EVH |
688 | if (msize < c->msize) |
689 | c->msize = msize; | |
6936bf60 EVH |
690 | |
691 | error: | |
51a87c55 EVH |
692 | kfree(version); |
693 | p9_free_req(c, req); | |
6936bf60 EVH |
694 | |
695 | return err; | |
696 | } | |
51a87c55 | 697 | EXPORT_SYMBOL(p9_client_version); |
6936bf60 EVH |
698 | |
699 | struct p9_client *p9_client_create(const char *dev_name, char *options) | |
700 | { | |
701 | int err; | |
702 | struct p9_client *clnt; | |
703 | ||
704 | err = 0; | |
bd238fb4 LI |
705 | clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL); |
706 | if (!clnt) | |
707 | return ERR_PTR(-ENOMEM); | |
708 | ||
72029fe8 | 709 | clnt->trans_mod = NULL; |
bb8ffdfc | 710 | clnt->trans = NULL; |
bd238fb4 | 711 | spin_lock_init(&clnt->lock); |
bd238fb4 LI |
712 | INIT_LIST_HEAD(&clnt->fidlist); |
713 | clnt->fidpool = p9_idpool_create(); | |
728fc4ef | 714 | if (IS_ERR(clnt->fidpool)) { |
bd238fb4 LI |
715 | err = PTR_ERR(clnt->fidpool); |
716 | clnt->fidpool = NULL; | |
717 | goto error; | |
718 | } | |
719 | ||
fea511a6 EVH |
720 | p9_tag_init(clnt); |
721 | ||
bb8ffdfc EVH |
722 | err = parse_opts(options, clnt); |
723 | if (err < 0) | |
724 | goto error; | |
725 | ||
8a0dc95f EVH |
726 | if (clnt->trans_mod == NULL) { |
727 | err = -EPROTONOSUPPORT; | |
728 | P9_DPRINTK(P9_DEBUG_ERROR, | |
729 | "No transport defined or default transport\n"); | |
730 | goto error; | |
731 | } | |
732 | ||
51a87c55 | 733 | P9_DPRINTK(P9_DEBUG_MUX, "clnt %p trans %p msize %d dotu %d\n", |
8a0dc95f EVH |
734 | clnt, clnt->trans_mod, clnt->msize, clnt->dotu); |
735 | ||
8b81ef58 EVH |
736 | err = clnt->trans_mod->create(clnt, dev_name, options); |
737 | if (err) | |
bd238fb4 | 738 | goto error; |
bd238fb4 | 739 | |
8a0dc95f EVH |
740 | if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize) |
741 | clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ; | |
742 | ||
6936bf60 | 743 | err = p9_client_version(clnt); |
bd238fb4 LI |
744 | if (err) |
745 | goto error; | |
746 | ||
bd238fb4 LI |
747 | return clnt; |
748 | ||
749 | error: | |
bd238fb4 LI |
750 | p9_client_destroy(clnt); |
751 | return ERR_PTR(err); | |
752 | } | |
753 | EXPORT_SYMBOL(p9_client_create); | |
754 | ||
755 | void p9_client_destroy(struct p9_client *clnt) | |
756 | { | |
757 | struct p9_fid *fid, *fidptr; | |
758 | ||
51a87c55 | 759 | P9_DPRINTK(P9_DEBUG_MUX, "clnt %p\n", clnt); |
bd238fb4 | 760 | |
8b81ef58 EVH |
761 | if (clnt->trans_mod) |
762 | clnt->trans_mod->close(clnt); | |
bd238fb4 | 763 | |
72029fe8 TH |
764 | v9fs_put_trans(clnt->trans_mod); |
765 | ||
bd238fb4 LI |
766 | list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) |
767 | p9_fid_destroy(fid); | |
768 | ||
0af8887e EVH |
769 | if (clnt->fidpool) |
770 | p9_idpool_destroy(clnt->fidpool); | |
771 | ||
fea511a6 EVH |
772 | p9_tag_cleanup(clnt); |
773 | ||
bd238fb4 LI |
774 | kfree(clnt); |
775 | } | |
776 | EXPORT_SYMBOL(p9_client_destroy); | |
777 | ||
778 | void p9_client_disconnect(struct p9_client *clnt) | |
779 | { | |
780 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt); | |
8b81ef58 | 781 | clnt->status = Disconnected; |
bd238fb4 LI |
782 | } |
783 | EXPORT_SYMBOL(p9_client_disconnect); | |
784 | ||
785 | struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, | |
ba17674f | 786 | char *uname, u32 n_uname, char *aname) |
bd238fb4 LI |
787 | { |
788 | int err; | |
51a87c55 | 789 | struct p9_req_t *req; |
bd238fb4 | 790 | struct p9_fid *fid; |
51a87c55 | 791 | struct p9_qid qid; |
bd238fb4 | 792 | |
51a87c55 EVH |
793 | P9_DPRINTK(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n", |
794 | afid ? afid->fid : -1, uname, aname); | |
bd238fb4 | 795 | err = 0; |
bd238fb4 LI |
796 | |
797 | fid = p9_fid_create(clnt); | |
798 | if (IS_ERR(fid)) { | |
799 | err = PTR_ERR(fid); | |
800 | fid = NULL; | |
801 | goto error; | |
802 | } | |
803 | ||
51a87c55 EVH |
804 | req = p9_client_rpc(clnt, P9_TATTACH, "ddss?d", fid->fid, |
805 | afid ? afid->fid : P9_NOFID, uname, aname, n_uname); | |
806 | if (IS_ERR(req)) { | |
807 | err = PTR_ERR(req); | |
bd238fb4 LI |
808 | goto error; |
809 | } | |
810 | ||
51a87c55 EVH |
811 | err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid); |
812 | if (err) { | |
813 | p9_free_req(clnt, req); | |
bd238fb4 | 814 | goto error; |
51a87c55 | 815 | } |
bd238fb4 | 816 | |
51a87c55 EVH |
817 | P9_DPRINTK(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n", |
818 | qid.type, qid.path, qid.version); | |
819 | ||
820 | memmove(&fid->qid, &qid, sizeof(struct p9_qid)); | |
821 | ||
822 | p9_free_req(clnt, req); | |
bd238fb4 LI |
823 | return fid; |
824 | ||
825 | error: | |
bd238fb4 LI |
826 | if (fid) |
827 | p9_fid_destroy(fid); | |
828 | return ERR_PTR(err); | |
829 | } | |
830 | EXPORT_SYMBOL(p9_client_attach); | |
831 | ||
51a87c55 EVH |
832 | struct p9_fid * |
833 | p9_client_auth(struct p9_client *clnt, char *uname, u32 n_uname, char *aname) | |
bd238fb4 LI |
834 | { |
835 | int err; | |
51a87c55 EVH |
836 | struct p9_req_t *req; |
837 | struct p9_qid qid; | |
838 | struct p9_fid *afid; | |
bd238fb4 | 839 | |
51a87c55 | 840 | P9_DPRINTK(P9_DEBUG_9P, ">>> TAUTH uname %s aname %s\n", uname, aname); |
bd238fb4 | 841 | err = 0; |
bd238fb4 | 842 | |
51a87c55 EVH |
843 | afid = p9_fid_create(clnt); |
844 | if (IS_ERR(afid)) { | |
845 | err = PTR_ERR(afid); | |
846 | afid = NULL; | |
bd238fb4 LI |
847 | goto error; |
848 | } | |
849 | ||
51a87c55 EVH |
850 | req = p9_client_rpc(clnt, P9_TAUTH, "dss?d", |
851 | afid ? afid->fid : P9_NOFID, uname, aname, n_uname); | |
852 | if (IS_ERR(req)) { | |
853 | err = PTR_ERR(req); | |
bd238fb4 LI |
854 | goto error; |
855 | } | |
856 | ||
51a87c55 EVH |
857 | err = p9pdu_readf(req->rc, clnt->dotu, "Q", &qid); |
858 | if (err) { | |
859 | p9_free_req(clnt, req); | |
bd238fb4 | 860 | goto error; |
51a87c55 | 861 | } |
bd238fb4 | 862 | |
51a87c55 EVH |
863 | P9_DPRINTK(P9_DEBUG_9P, "<<< RAUTH qid %x.%llx.%x\n", |
864 | qid.type, qid.path, qid.version); | |
865 | ||
866 | memmove(&afid->qid, &qid, sizeof(struct p9_qid)); | |
867 | p9_free_req(clnt, req); | |
868 | return afid; | |
bd238fb4 LI |
869 | |
870 | error: | |
51a87c55 EVH |
871 | if (afid) |
872 | p9_fid_destroy(afid); | |
bd238fb4 LI |
873 | return ERR_PTR(err); |
874 | } | |
875 | EXPORT_SYMBOL(p9_client_auth); | |
876 | ||
877 | struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames, | |
878 | int clone) | |
879 | { | |
880 | int err; | |
bd238fb4 LI |
881 | struct p9_client *clnt; |
882 | struct p9_fid *fid; | |
51a87c55 EVH |
883 | struct p9_qid *wqids; |
884 | struct p9_req_t *req; | |
885 | int16_t nwqids, count; | |
bd238fb4 | 886 | |
bd238fb4 | 887 | err = 0; |
bd238fb4 LI |
888 | clnt = oldfid->clnt; |
889 | if (clone) { | |
890 | fid = p9_fid_create(clnt); | |
891 | if (IS_ERR(fid)) { | |
892 | err = PTR_ERR(fid); | |
893 | fid = NULL; | |
894 | goto error; | |
895 | } | |
896 | ||
897 | fid->uid = oldfid->uid; | |
898 | } else | |
899 | fid = oldfid; | |
900 | ||
51a87c55 EVH |
901 | |
902 | P9_DPRINTK(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %d wname[0] %s\n", | |
903 | oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL); | |
904 | ||
905 | req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid, | |
906 | nwname, wnames); | |
907 | if (IS_ERR(req)) { | |
908 | err = PTR_ERR(req); | |
bd238fb4 LI |
909 | goto error; |
910 | } | |
911 | ||
51a87c55 EVH |
912 | err = p9pdu_readf(req->rc, clnt->dotu, "R", &nwqids, &wqids); |
913 | p9_free_req(clnt, req); | |
914 | if (err) | |
915 | goto clunk_fid; | |
bd238fb4 | 916 | |
51a87c55 EVH |
917 | P9_DPRINTK(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids); |
918 | ||
919 | if (nwqids != nwname) { | |
bd238fb4 LI |
920 | err = -ENOENT; |
921 | goto clunk_fid; | |
922 | } | |
923 | ||
51a87c55 EVH |
924 | for (count = 0; count < nwqids; count++) |
925 | P9_DPRINTK(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n", | |
926 | count, wqids[count].type, wqids[count].path, | |
927 | wqids[count].version); | |
928 | ||
bd238fb4 | 929 | if (nwname) |
51a87c55 | 930 | memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid)); |
bd238fb4 LI |
931 | else |
932 | fid->qid = oldfid->qid; | |
933 | ||
bd238fb4 LI |
934 | return fid; |
935 | ||
936 | clunk_fid: | |
51a87c55 EVH |
937 | p9_client_clunk(fid); |
938 | fid = NULL; | |
bd238fb4 LI |
939 | |
940 | error: | |
bd238fb4 LI |
941 | if (fid && (fid != oldfid)) |
942 | p9_fid_destroy(fid); | |
943 | ||
944 | return ERR_PTR(err); | |
945 | } | |
946 | EXPORT_SYMBOL(p9_client_walk); | |
947 | ||
948 | int p9_client_open(struct p9_fid *fid, int mode) | |
949 | { | |
950 | int err; | |
bd238fb4 | 951 | struct p9_client *clnt; |
51a87c55 EVH |
952 | struct p9_req_t *req; |
953 | struct p9_qid qid; | |
954 | int iounit; | |
bd238fb4 | 955 | |
51a87c55 | 956 | P9_DPRINTK(P9_DEBUG_9P, ">>> TOPEN fid %d mode %d\n", fid->fid, mode); |
bd238fb4 | 957 | err = 0; |
bd238fb4 LI |
958 | clnt = fid->clnt; |
959 | ||
960 | if (fid->mode != -1) | |
961 | return -EINVAL; | |
962 | ||
51a87c55 EVH |
963 | req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode); |
964 | if (IS_ERR(req)) { | |
965 | err = PTR_ERR(req); | |
966 | goto error; | |
bd238fb4 LI |
967 | } |
968 | ||
51a87c55 EVH |
969 | err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit); |
970 | p9_free_req(clnt, req); | |
bd238fb4 | 971 | if (err) |
51a87c55 EVH |
972 | goto error; |
973 | ||
974 | P9_DPRINTK(P9_DEBUG_9P, "<<< ROPEN qid %x.%llx.%x iounit %x\n", | |
975 | qid.type, qid.path, qid.version, iounit); | |
bd238fb4 LI |
976 | |
977 | fid->mode = mode; | |
51a87c55 | 978 | fid->iounit = iounit; |
bd238fb4 | 979 | |
51a87c55 | 980 | error: |
bd238fb4 LI |
981 | return err; |
982 | } | |
983 | EXPORT_SYMBOL(p9_client_open); | |
984 | ||
985 | int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode, | |
986 | char *extension) | |
987 | { | |
988 | int err; | |
bd238fb4 | 989 | struct p9_client *clnt; |
51a87c55 EVH |
990 | struct p9_req_t *req; |
991 | struct p9_qid qid; | |
992 | int iounit; | |
bd238fb4 | 993 | |
51a87c55 EVH |
994 | P9_DPRINTK(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n", |
995 | fid->fid, name, perm, mode); | |
bd238fb4 | 996 | err = 0; |
bd238fb4 LI |
997 | clnt = fid->clnt; |
998 | ||
999 | if (fid->mode != -1) | |
1000 | return -EINVAL; | |
1001 | ||
51a87c55 EVH |
1002 | req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm, |
1003 | mode, extension); | |
1004 | if (IS_ERR(req)) { | |
1005 | err = PTR_ERR(req); | |
1006 | goto error; | |
bd238fb4 LI |
1007 | } |
1008 | ||
51a87c55 EVH |
1009 | err = p9pdu_readf(req->rc, clnt->dotu, "Qd", &qid, &iounit); |
1010 | p9_free_req(clnt, req); | |
bd238fb4 | 1011 | if (err) |
51a87c55 EVH |
1012 | goto error; |
1013 | ||
1014 | P9_DPRINTK(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n", | |
1015 | qid.type, qid.path, qid.version, iounit); | |
bd238fb4 LI |
1016 | |
1017 | fid->mode = mode; | |
51a87c55 | 1018 | fid->iounit = iounit; |
bd238fb4 | 1019 | |
51a87c55 | 1020 | error: |
bd238fb4 LI |
1021 | return err; |
1022 | } | |
1023 | EXPORT_SYMBOL(p9_client_fcreate); | |
1024 | ||
1025 | int p9_client_clunk(struct p9_fid *fid) | |
1026 | { | |
1027 | int err; | |
bd238fb4 | 1028 | struct p9_client *clnt; |
51a87c55 | 1029 | struct p9_req_t *req; |
bd238fb4 | 1030 | |
51a87c55 | 1031 | P9_DPRINTK(P9_DEBUG_9P, ">>> TCLUNK fid %d\n", fid->fid); |
bd238fb4 | 1032 | err = 0; |
bd238fb4 LI |
1033 | clnt = fid->clnt; |
1034 | ||
51a87c55 EVH |
1035 | req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid); |
1036 | if (IS_ERR(req)) { | |
1037 | err = PTR_ERR(req); | |
1038 | goto error; | |
bd238fb4 LI |
1039 | } |
1040 | ||
51a87c55 | 1041 | P9_DPRINTK(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid); |
bd238fb4 | 1042 | |
51a87c55 | 1043 | p9_free_req(clnt, req); |
bd238fb4 LI |
1044 | p9_fid_destroy(fid); |
1045 | ||
51a87c55 | 1046 | error: |
bd238fb4 LI |
1047 | return err; |
1048 | } | |
1049 | EXPORT_SYMBOL(p9_client_clunk); | |
1050 | ||
1051 | int p9_client_remove(struct p9_fid *fid) | |
1052 | { | |
1053 | int err; | |
bd238fb4 | 1054 | struct p9_client *clnt; |
51a87c55 | 1055 | struct p9_req_t *req; |
bd238fb4 | 1056 | |
51a87c55 | 1057 | P9_DPRINTK(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid); |
bd238fb4 | 1058 | err = 0; |
bd238fb4 LI |
1059 | clnt = fid->clnt; |
1060 | ||
51a87c55 EVH |
1061 | req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid); |
1062 | if (IS_ERR(req)) { | |
1063 | err = PTR_ERR(req); | |
1064 | goto error; | |
bd238fb4 LI |
1065 | } |
1066 | ||
51a87c55 | 1067 | P9_DPRINTK(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid); |
bd238fb4 | 1068 | |
51a87c55 | 1069 | p9_free_req(clnt, req); |
bd238fb4 LI |
1070 | p9_fid_destroy(fid); |
1071 | ||
51a87c55 | 1072 | error: |
bd238fb4 LI |
1073 | return err; |
1074 | } | |
1075 | EXPORT_SYMBOL(p9_client_remove); | |
1076 | ||
0fc9655e EVH |
1077 | int |
1078 | p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset, | |
1079 | u32 count) | |
bd238fb4 | 1080 | { |
51a87c55 | 1081 | int err, rsize, total; |
bd238fb4 | 1082 | struct p9_client *clnt; |
51a87c55 EVH |
1083 | struct p9_req_t *req; |
1084 | char *dataptr; | |
bd238fb4 | 1085 | |
51a87c55 | 1086 | P9_DPRINTK(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", fid->fid, |
bd238fb4 LI |
1087 | (long long unsigned) offset, count); |
1088 | err = 0; | |
bd238fb4 LI |
1089 | clnt = fid->clnt; |
1090 | total = 0; | |
1091 | ||
1092 | rsize = fid->iounit; | |
1093 | if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) | |
1094 | rsize = clnt->msize - P9_IOHDRSZ; | |
1095 | ||
51a87c55 EVH |
1096 | if (count < rsize) |
1097 | rsize = count; | |
bd238fb4 | 1098 | |
51a87c55 EVH |
1099 | req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, rsize); |
1100 | if (IS_ERR(req)) { | |
1101 | err = PTR_ERR(req); | |
1102 | goto error; | |
1103 | } | |
bd238fb4 | 1104 | |
51a87c55 EVH |
1105 | err = p9pdu_readf(req->rc, clnt->dotu, "D", &count, &dataptr); |
1106 | if (err) | |
1107 | goto free_and_error; | |
bd238fb4 | 1108 | |
51a87c55 | 1109 | P9_DPRINTK(P9_DEBUG_9P, "<<< RREAD count %d\n", count); |
bd238fb4 | 1110 | |
51a87c55 EVH |
1111 | if (data) { |
1112 | memmove(data, dataptr, count); | |
1113 | data += count; | |
1114 | } | |
bd238fb4 | 1115 | |
51a87c55 EVH |
1116 | if (udata) { |
1117 | err = copy_to_user(udata, dataptr, count); | |
1118 | if (err) { | |
1119 | err = -EFAULT; | |
1120 | goto free_and_error; | |
bd238fb4 | 1121 | } |
51a87c55 | 1122 | } |
bd238fb4 | 1123 | |
51a87c55 EVH |
1124 | p9_free_req(clnt, req); |
1125 | return count; | |
bd238fb4 | 1126 | |
51a87c55 EVH |
1127 | free_and_error: |
1128 | p9_free_req(clnt, req); | |
bd238fb4 | 1129 | error: |
bd238fb4 LI |
1130 | return err; |
1131 | } | |
0fc9655e | 1132 | EXPORT_SYMBOL(p9_client_read); |
bd238fb4 LI |
1133 | |
1134 | int | |
0fc9655e EVH |
1135 | p9_client_write(struct p9_fid *fid, char *data, const char __user *udata, |
1136 | u64 offset, u32 count) | |
bd238fb4 | 1137 | { |
51a87c55 | 1138 | int err, rsize, total; |
bd238fb4 | 1139 | struct p9_client *clnt; |
51a87c55 | 1140 | struct p9_req_t *req; |
bd238fb4 | 1141 | |
51a87c55 EVH |
1142 | P9_DPRINTK(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n", |
1143 | fid->fid, (long long unsigned) offset, count); | |
bd238fb4 | 1144 | err = 0; |
bd238fb4 LI |
1145 | clnt = fid->clnt; |
1146 | total = 0; | |
1147 | ||
1148 | rsize = fid->iounit; | |
1149 | if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) | |
1150 | rsize = clnt->msize - P9_IOHDRSZ; | |
1151 | ||
51a87c55 EVH |
1152 | if (count < rsize) |
1153 | rsize = count; | |
1154 | if (data) | |
1155 | req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid, offset, | |
1156 | rsize, data); | |
1157 | else | |
1158 | req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid, offset, | |
1159 | rsize, udata); | |
1160 | if (IS_ERR(req)) { | |
1161 | err = PTR_ERR(req); | |
1162 | goto error; | |
1163 | } | |
0fc9655e | 1164 | |
51a87c55 EVH |
1165 | err = p9pdu_readf(req->rc, clnt->dotu, "d", &count); |
1166 | if (err) | |
1167 | goto free_and_error; | |
1168 | P9_DPRINTK(P9_DEBUG_9P, "<<< RWRITE count %d\n", count); | |
bd238fb4 | 1169 | |
51a87c55 EVH |
1170 | p9_free_req(clnt, req); |
1171 | return count; | |
bd238fb4 | 1172 | |
51a87c55 EVH |
1173 | free_and_error: |
1174 | p9_free_req(clnt, req); | |
bd238fb4 | 1175 | error: |
bd238fb4 LI |
1176 | return err; |
1177 | } | |
0fc9655e | 1178 | EXPORT_SYMBOL(p9_client_write); |
bd238fb4 | 1179 | |
51a87c55 | 1180 | struct p9_wstat *p9_client_stat(struct p9_fid *fid) |
5503ac56 | 1181 | { |
51a87c55 EVH |
1182 | int err; |
1183 | struct p9_client *clnt; | |
1184 | struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL); | |
1185 | struct p9_req_t *req; | |
1186 | u16 ignored; | |
5503ac56 | 1187 | |
51a87c55 | 1188 | P9_DPRINTK(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid); |
5503ac56 | 1189 | |
5503ac56 EVH |
1190 | if (!ret) |
1191 | return ERR_PTR(-ENOMEM); | |
1192 | ||
bd238fb4 | 1193 | err = 0; |
bd238fb4 LI |
1194 | clnt = fid->clnt; |
1195 | ||
51a87c55 EVH |
1196 | req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid); |
1197 | if (IS_ERR(req)) { | |
1198 | err = PTR_ERR(req); | |
bd238fb4 LI |
1199 | goto error; |
1200 | } | |
1201 | ||
51a87c55 EVH |
1202 | err = p9pdu_readf(req->rc, clnt->dotu, "wS", &ignored, ret); |
1203 | p9_free_req(clnt, req); | |
bd238fb4 LI |
1204 | if (err) |
1205 | goto error; | |
1206 | ||
51a87c55 EVH |
1207 | P9_DPRINTK(P9_DEBUG_9P, |
1208 | "<<< RSTAT sz=%x type=%x dev=%x qid=%2.2x %4.4x %8.8llx" | |
1209 | " mode=%8.8x uid=%d gid=%d size=%lld %s\n", | |
1210 | ret->size, ret->type, ret->dev, ret->qid.type, | |
1211 | ret->qid.version, ret->qid.path, ret->mode, | |
1212 | ret->n_uid, ret->n_gid, ret->length, ret->name); | |
bd238fb4 | 1213 | |
bd238fb4 | 1214 | return ret; |
bd238fb4 | 1215 | error: |
bd238fb4 LI |
1216 | return ERR_PTR(err); |
1217 | } | |
1218 | EXPORT_SYMBOL(p9_client_stat); | |
1219 | ||
1220 | int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst) | |
1221 | { | |
1222 | int err; | |
51a87c55 | 1223 | struct p9_req_t *req; |
bd238fb4 LI |
1224 | struct p9_client *clnt; |
1225 | ||
51a87c55 | 1226 | P9_DPRINTK(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid); |
bd238fb4 | 1227 | err = 0; |
bd238fb4 LI |
1228 | clnt = fid->clnt; |
1229 | ||
51a87c55 EVH |
1230 | req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, 0, wst); |
1231 | if (IS_ERR(req)) { | |
1232 | err = PTR_ERR(req); | |
1233 | goto error; | |
bd238fb4 LI |
1234 | } |
1235 | ||
51a87c55 | 1236 | P9_DPRINTK(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid); |
bd238fb4 | 1237 | |
51a87c55 EVH |
1238 | p9_free_req(clnt, req); |
1239 | error: | |
bd238fb4 LI |
1240 | return err; |
1241 | } | |
1242 | EXPORT_SYMBOL(p9_client_wstat); |