]>
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> |
bd238fb4 | 38 | |
8a0dc95f EVH |
39 | /* |
40 | * Client Option Parsing (code inspired by NFS code) | |
41 | * - a little lazy - parse all client options | |
42 | */ | |
43 | ||
44 | enum { | |
45 | Opt_msize, | |
46 | Opt_trans, | |
47 | Opt_legacy, | |
48 | Opt_err, | |
49 | }; | |
50 | ||
a447c093 | 51 | static const match_table_t tokens = { |
8a0dc95f EVH |
52 | {Opt_msize, "msize=%u"}, |
53 | {Opt_legacy, "noextend"}, | |
54 | {Opt_trans, "trans=%s"}, | |
55 | {Opt_err, NULL}, | |
56 | }; | |
57 | ||
58 | /** | |
59 | * v9fs_parse_options - parse mount options into session structure | |
60 | * @options: options string passed from mount | |
61 | * @v9ses: existing v9fs session information | |
62 | * | |
bb8ffdfc | 63 | * Return 0 upon success, -ERRNO upon failure |
8a0dc95f EVH |
64 | */ |
65 | ||
bb8ffdfc | 66 | static int parse_opts(char *opts, struct p9_client *clnt) |
8a0dc95f | 67 | { |
bb8ffdfc | 68 | char *options; |
8a0dc95f EVH |
69 | char *p; |
70 | substring_t args[MAX_OPT_ARGS]; | |
71 | int option; | |
bb8ffdfc | 72 | int ret = 0; |
8a0dc95f | 73 | |
8a0dc95f EVH |
74 | clnt->dotu = 1; |
75 | clnt->msize = 8192; | |
76 | ||
bb8ffdfc EVH |
77 | if (!opts) |
78 | return 0; | |
79 | ||
80 | options = kstrdup(opts, GFP_KERNEL); | |
81 | if (!options) { | |
82 | P9_DPRINTK(P9_DEBUG_ERROR, | |
83 | "failed to allocate copy of option string\n"); | |
84 | return -ENOMEM; | |
85 | } | |
8a0dc95f EVH |
86 | |
87 | while ((p = strsep(&options, ",")) != NULL) { | |
88 | int token; | |
89 | if (!*p) | |
90 | continue; | |
91 | token = match_token(p, tokens, args); | |
92 | if (token < Opt_trans) { | |
bb8ffdfc EVH |
93 | int r = match_int(&args[0], &option); |
94 | if (r < 0) { | |
8a0dc95f EVH |
95 | P9_DPRINTK(P9_DEBUG_ERROR, |
96 | "integer field, but no integer?\n"); | |
bb8ffdfc | 97 | ret = r; |
8a0dc95f EVH |
98 | continue; |
99 | } | |
100 | } | |
101 | switch (token) { | |
102 | case Opt_msize: | |
103 | clnt->msize = option; | |
104 | break; | |
105 | case Opt_trans: | |
72029fe8 | 106 | clnt->trans_mod = v9fs_get_trans_by_name(&args[0]); |
8a0dc95f EVH |
107 | break; |
108 | case Opt_legacy: | |
109 | clnt->dotu = 0; | |
110 | break; | |
111 | default: | |
112 | continue; | |
113 | } | |
114 | } | |
72029fe8 TH |
115 | |
116 | if (!clnt->trans_mod) | |
117 | clnt->trans_mod = v9fs_get_default_trans(); | |
118 | ||
bb8ffdfc EVH |
119 | kfree(options); |
120 | return ret; | |
8a0dc95f EVH |
121 | } |
122 | ||
5503ac56 EVH |
123 | static struct p9_fid *p9_fid_create(struct p9_client *clnt) |
124 | { | |
125 | int err; | |
126 | struct p9_fid *fid; | |
127 | ||
128 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt); | |
129 | fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL); | |
130 | if (!fid) | |
131 | return ERR_PTR(-ENOMEM); | |
132 | ||
133 | fid->fid = p9_idpool_get(clnt->fidpool); | |
134 | if (fid->fid < 0) { | |
135 | err = -ENOSPC; | |
136 | goto error; | |
137 | } | |
138 | ||
139 | memset(&fid->qid, 0, sizeof(struct p9_qid)); | |
140 | fid->mode = -1; | |
141 | fid->rdir_fpos = 0; | |
142 | fid->rdir_pos = 0; | |
143 | fid->rdir_fcall = NULL; | |
144 | fid->uid = current->fsuid; | |
145 | fid->clnt = clnt; | |
146 | fid->aux = NULL; | |
147 | ||
148 | spin_lock(&clnt->lock); | |
149 | list_add(&fid->flist, &clnt->fidlist); | |
150 | spin_unlock(&clnt->lock); | |
151 | ||
152 | return fid; | |
153 | ||
154 | error: | |
155 | kfree(fid); | |
156 | return ERR_PTR(err); | |
157 | } | |
158 | ||
159 | static void p9_fid_destroy(struct p9_fid *fid) | |
160 | { | |
161 | struct p9_client *clnt; | |
162 | ||
163 | P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid); | |
164 | clnt = fid->clnt; | |
165 | p9_idpool_put(fid->fid, clnt->fidpool); | |
166 | spin_lock(&clnt->lock); | |
167 | list_del(&fid->flist); | |
168 | spin_unlock(&clnt->lock); | |
169 | kfree(fid->rdir_fcall); | |
170 | kfree(fid); | |
171 | } | |
8a0dc95f | 172 | |
043aba40 EVH |
173 | /** |
174 | * p9_client_rpc - sends 9P request and waits until a response is available. | |
175 | * The function can be interrupted. | |
176 | * @c: client data | |
177 | * @tc: request to be sent | |
178 | * @rc: pointer where a pointer to the response is stored | |
179 | */ | |
180 | int | |
181 | p9_client_rpc(struct p9_client *c, struct p9_fcall *tc, | |
182 | struct p9_fcall **rc) | |
183 | { | |
8b81ef58 | 184 | return c->trans_mod->rpc(c, tc, rc); |
043aba40 EVH |
185 | } |
186 | ||
8a0dc95f | 187 | struct p9_client *p9_client_create(const char *dev_name, char *options) |
bd238fb4 LI |
188 | { |
189 | int err, n; | |
190 | struct p9_client *clnt; | |
191 | struct p9_fcall *tc, *rc; | |
192 | struct p9_str *version; | |
193 | ||
194 | err = 0; | |
195 | tc = NULL; | |
196 | rc = NULL; | |
197 | clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL); | |
198 | if (!clnt) | |
199 | return ERR_PTR(-ENOMEM); | |
200 | ||
72029fe8 | 201 | clnt->trans_mod = NULL; |
bb8ffdfc | 202 | clnt->trans = NULL; |
bd238fb4 | 203 | spin_lock_init(&clnt->lock); |
bd238fb4 LI |
204 | INIT_LIST_HEAD(&clnt->fidlist); |
205 | clnt->fidpool = p9_idpool_create(); | |
728fc4ef | 206 | if (IS_ERR(clnt->fidpool)) { |
bd238fb4 LI |
207 | err = PTR_ERR(clnt->fidpool); |
208 | clnt->fidpool = NULL; | |
209 | goto error; | |
210 | } | |
211 | ||
bb8ffdfc EVH |
212 | err = parse_opts(options, clnt); |
213 | if (err < 0) | |
214 | goto error; | |
215 | ||
8a0dc95f EVH |
216 | if (clnt->trans_mod == NULL) { |
217 | err = -EPROTONOSUPPORT; | |
218 | P9_DPRINTK(P9_DEBUG_ERROR, | |
219 | "No transport defined or default transport\n"); | |
220 | goto error; | |
221 | } | |
222 | ||
223 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p trans %p msize %d dotu %d\n", | |
224 | clnt, clnt->trans_mod, clnt->msize, clnt->dotu); | |
225 | ||
226 | ||
8b81ef58 EVH |
227 | err = clnt->trans_mod->create(clnt, dev_name, options); |
228 | if (err) | |
bd238fb4 | 229 | goto error; |
bd238fb4 | 230 | |
8a0dc95f EVH |
231 | if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize) |
232 | clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ; | |
233 | ||
bd238fb4 LI |
234 | tc = p9_create_tversion(clnt->msize, clnt->dotu?"9P2000.u":"9P2000"); |
235 | if (IS_ERR(tc)) { | |
236 | err = PTR_ERR(tc); | |
237 | tc = NULL; | |
238 | goto error; | |
239 | } | |
240 | ||
e2735b77 | 241 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
242 | if (err) |
243 | goto error; | |
244 | ||
245 | version = &rc->params.rversion.version; | |
246 | if (version->len == 8 && !memcmp(version->str, "9P2000.u", 8)) | |
247 | clnt->dotu = 1; | |
248 | else if (version->len == 6 && !memcmp(version->str, "9P2000", 6)) | |
249 | clnt->dotu = 0; | |
250 | else { | |
251 | err = -EREMOTEIO; | |
252 | goto error; | |
253 | } | |
254 | ||
255 | n = rc->params.rversion.msize; | |
256 | if (n < clnt->msize) | |
257 | clnt->msize = n; | |
258 | ||
259 | kfree(tc); | |
260 | kfree(rc); | |
261 | return clnt; | |
262 | ||
263 | error: | |
264 | kfree(tc); | |
265 | kfree(rc); | |
266 | p9_client_destroy(clnt); | |
267 | return ERR_PTR(err); | |
268 | } | |
269 | EXPORT_SYMBOL(p9_client_create); | |
270 | ||
271 | void p9_client_destroy(struct p9_client *clnt) | |
272 | { | |
273 | struct p9_fid *fid, *fidptr; | |
274 | ||
275 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt); | |
bd238fb4 | 276 | |
8b81ef58 EVH |
277 | if (clnt->trans_mod) |
278 | clnt->trans_mod->close(clnt); | |
bd238fb4 | 279 | |
72029fe8 TH |
280 | v9fs_put_trans(clnt->trans_mod); |
281 | ||
bd238fb4 LI |
282 | list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) |
283 | p9_fid_destroy(fid); | |
284 | ||
0af8887e EVH |
285 | if (clnt->fidpool) |
286 | p9_idpool_destroy(clnt->fidpool); | |
287 | ||
bd238fb4 LI |
288 | kfree(clnt); |
289 | } | |
290 | EXPORT_SYMBOL(p9_client_destroy); | |
291 | ||
292 | void p9_client_disconnect(struct p9_client *clnt) | |
293 | { | |
294 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt); | |
8b81ef58 | 295 | clnt->status = Disconnected; |
bd238fb4 LI |
296 | } |
297 | EXPORT_SYMBOL(p9_client_disconnect); | |
298 | ||
299 | struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, | |
ba17674f | 300 | char *uname, u32 n_uname, char *aname) |
bd238fb4 LI |
301 | { |
302 | int err; | |
303 | struct p9_fcall *tc, *rc; | |
304 | struct p9_fid *fid; | |
305 | ||
306 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p afid %d uname %s aname %s\n", | |
307 | clnt, afid?afid->fid:-1, uname, aname); | |
308 | err = 0; | |
309 | tc = NULL; | |
310 | rc = NULL; | |
311 | ||
312 | fid = p9_fid_create(clnt); | |
313 | if (IS_ERR(fid)) { | |
314 | err = PTR_ERR(fid); | |
315 | fid = NULL; | |
316 | goto error; | |
317 | } | |
318 | ||
ba17674f LI |
319 | tc = p9_create_tattach(fid->fid, afid?afid->fid:P9_NOFID, uname, aname, |
320 | n_uname, clnt->dotu); | |
bd238fb4 LI |
321 | if (IS_ERR(tc)) { |
322 | err = PTR_ERR(tc); | |
323 | tc = NULL; | |
324 | goto error; | |
325 | } | |
326 | ||
e2735b77 | 327 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
328 | if (err) |
329 | goto error; | |
330 | ||
331 | memmove(&fid->qid, &rc->params.rattach.qid, sizeof(struct p9_qid)); | |
332 | kfree(tc); | |
333 | kfree(rc); | |
334 | return fid; | |
335 | ||
336 | error: | |
337 | kfree(tc); | |
338 | kfree(rc); | |
339 | if (fid) | |
340 | p9_fid_destroy(fid); | |
341 | return ERR_PTR(err); | |
342 | } | |
343 | EXPORT_SYMBOL(p9_client_attach); | |
344 | ||
ba17674f LI |
345 | struct p9_fid *p9_client_auth(struct p9_client *clnt, char *uname, |
346 | u32 n_uname, char *aname) | |
bd238fb4 LI |
347 | { |
348 | int err; | |
349 | struct p9_fcall *tc, *rc; | |
350 | struct p9_fid *fid; | |
351 | ||
352 | P9_DPRINTK(P9_DEBUG_9P, "clnt %p uname %s aname %s\n", clnt, uname, | |
353 | aname); | |
354 | err = 0; | |
355 | tc = NULL; | |
356 | rc = NULL; | |
357 | ||
358 | fid = p9_fid_create(clnt); | |
359 | if (IS_ERR(fid)) { | |
360 | err = PTR_ERR(fid); | |
361 | fid = NULL; | |
362 | goto error; | |
363 | } | |
364 | ||
ba17674f | 365 | tc = p9_create_tauth(fid->fid, uname, aname, n_uname, clnt->dotu); |
bd238fb4 LI |
366 | if (IS_ERR(tc)) { |
367 | err = PTR_ERR(tc); | |
368 | tc = NULL; | |
369 | goto error; | |
370 | } | |
371 | ||
e2735b77 | 372 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
373 | if (err) |
374 | goto error; | |
375 | ||
376 | memmove(&fid->qid, &rc->params.rauth.qid, sizeof(struct p9_qid)); | |
377 | kfree(tc); | |
378 | kfree(rc); | |
379 | return fid; | |
380 | ||
381 | error: | |
382 | kfree(tc); | |
383 | kfree(rc); | |
384 | if (fid) | |
385 | p9_fid_destroy(fid); | |
386 | return ERR_PTR(err); | |
387 | } | |
388 | EXPORT_SYMBOL(p9_client_auth); | |
389 | ||
390 | struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames, | |
391 | int clone) | |
392 | { | |
393 | int err; | |
394 | struct p9_fcall *tc, *rc; | |
395 | struct p9_client *clnt; | |
396 | struct p9_fid *fid; | |
397 | ||
398 | P9_DPRINTK(P9_DEBUG_9P, "fid %d nwname %d wname[0] %s\n", | |
399 | oldfid->fid, nwname, wnames?wnames[0]:NULL); | |
400 | err = 0; | |
401 | tc = NULL; | |
402 | rc = NULL; | |
403 | clnt = oldfid->clnt; | |
404 | if (clone) { | |
405 | fid = p9_fid_create(clnt); | |
406 | if (IS_ERR(fid)) { | |
407 | err = PTR_ERR(fid); | |
408 | fid = NULL; | |
409 | goto error; | |
410 | } | |
411 | ||
412 | fid->uid = oldfid->uid; | |
413 | } else | |
414 | fid = oldfid; | |
415 | ||
416 | tc = p9_create_twalk(oldfid->fid, fid->fid, nwname, wnames); | |
417 | if (IS_ERR(tc)) { | |
418 | err = PTR_ERR(tc); | |
419 | tc = NULL; | |
420 | goto error; | |
421 | } | |
422 | ||
e2735b77 | 423 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
424 | if (err) { |
425 | if (rc && rc->id == P9_RWALK) | |
426 | goto clunk_fid; | |
427 | else | |
428 | goto error; | |
429 | } | |
430 | ||
431 | if (rc->params.rwalk.nwqid != nwname) { | |
432 | err = -ENOENT; | |
433 | goto clunk_fid; | |
434 | } | |
435 | ||
436 | if (nwname) | |
437 | memmove(&fid->qid, | |
438 | &rc->params.rwalk.wqids[rc->params.rwalk.nwqid - 1], | |
439 | sizeof(struct p9_qid)); | |
440 | else | |
441 | fid->qid = oldfid->qid; | |
442 | ||
443 | kfree(tc); | |
444 | kfree(rc); | |
445 | return fid; | |
446 | ||
447 | clunk_fid: | |
448 | kfree(tc); | |
449 | kfree(rc); | |
450 | rc = NULL; | |
451 | tc = p9_create_tclunk(fid->fid); | |
452 | if (IS_ERR(tc)) { | |
453 | err = PTR_ERR(tc); | |
454 | tc = NULL; | |
455 | goto error; | |
456 | } | |
457 | ||
e2735b77 | 458 | p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
459 | |
460 | error: | |
461 | kfree(tc); | |
462 | kfree(rc); | |
463 | if (fid && (fid != oldfid)) | |
464 | p9_fid_destroy(fid); | |
465 | ||
466 | return ERR_PTR(err); | |
467 | } | |
468 | EXPORT_SYMBOL(p9_client_walk); | |
469 | ||
470 | int p9_client_open(struct p9_fid *fid, int mode) | |
471 | { | |
472 | int err; | |
473 | struct p9_fcall *tc, *rc; | |
474 | struct p9_client *clnt; | |
475 | ||
476 | P9_DPRINTK(P9_DEBUG_9P, "fid %d mode %d\n", fid->fid, mode); | |
477 | err = 0; | |
478 | tc = NULL; | |
479 | rc = NULL; | |
480 | clnt = fid->clnt; | |
481 | ||
482 | if (fid->mode != -1) | |
483 | return -EINVAL; | |
484 | ||
485 | tc = p9_create_topen(fid->fid, mode); | |
486 | if (IS_ERR(tc)) { | |
487 | err = PTR_ERR(tc); | |
488 | tc = NULL; | |
489 | goto done; | |
490 | } | |
491 | ||
e2735b77 | 492 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
493 | if (err) |
494 | goto done; | |
495 | ||
496 | fid->mode = mode; | |
497 | fid->iounit = rc->params.ropen.iounit; | |
498 | ||
499 | done: | |
500 | kfree(tc); | |
501 | kfree(rc); | |
502 | return err; | |
503 | } | |
504 | EXPORT_SYMBOL(p9_client_open); | |
505 | ||
506 | int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode, | |
507 | char *extension) | |
508 | { | |
509 | int err; | |
510 | struct p9_fcall *tc, *rc; | |
511 | struct p9_client *clnt; | |
512 | ||
513 | P9_DPRINTK(P9_DEBUG_9P, "fid %d name %s perm %d mode %d\n", fid->fid, | |
514 | name, perm, mode); | |
515 | err = 0; | |
516 | tc = NULL; | |
517 | rc = NULL; | |
518 | clnt = fid->clnt; | |
519 | ||
520 | if (fid->mode != -1) | |
521 | return -EINVAL; | |
522 | ||
523 | tc = p9_create_tcreate(fid->fid, name, perm, mode, extension, | |
524 | clnt->dotu); | |
525 | if (IS_ERR(tc)) { | |
526 | err = PTR_ERR(tc); | |
527 | tc = NULL; | |
528 | goto done; | |
529 | } | |
530 | ||
e2735b77 | 531 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
532 | if (err) |
533 | goto done; | |
534 | ||
535 | fid->mode = mode; | |
536 | fid->iounit = rc->params.ropen.iounit; | |
537 | ||
538 | done: | |
539 | kfree(tc); | |
540 | kfree(rc); | |
541 | return err; | |
542 | } | |
543 | EXPORT_SYMBOL(p9_client_fcreate); | |
544 | ||
545 | int p9_client_clunk(struct p9_fid *fid) | |
546 | { | |
547 | int err; | |
548 | struct p9_fcall *tc, *rc; | |
549 | struct p9_client *clnt; | |
550 | ||
551 | P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid); | |
552 | err = 0; | |
553 | tc = NULL; | |
554 | rc = NULL; | |
555 | clnt = fid->clnt; | |
556 | ||
557 | tc = p9_create_tclunk(fid->fid); | |
558 | if (IS_ERR(tc)) { | |
559 | err = PTR_ERR(tc); | |
560 | tc = NULL; | |
561 | goto done; | |
562 | } | |
563 | ||
e2735b77 | 564 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
565 | if (err) |
566 | goto done; | |
567 | ||
568 | p9_fid_destroy(fid); | |
569 | ||
570 | done: | |
571 | kfree(tc); | |
572 | kfree(rc); | |
573 | return err; | |
574 | } | |
575 | EXPORT_SYMBOL(p9_client_clunk); | |
576 | ||
577 | int p9_client_remove(struct p9_fid *fid) | |
578 | { | |
579 | int err; | |
580 | struct p9_fcall *tc, *rc; | |
581 | struct p9_client *clnt; | |
582 | ||
583 | P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid); | |
584 | err = 0; | |
585 | tc = NULL; | |
586 | rc = NULL; | |
587 | clnt = fid->clnt; | |
588 | ||
589 | tc = p9_create_tremove(fid->fid); | |
590 | if (IS_ERR(tc)) { | |
591 | err = PTR_ERR(tc); | |
592 | tc = NULL; | |
593 | goto done; | |
594 | } | |
595 | ||
e2735b77 | 596 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
597 | if (err) |
598 | goto done; | |
599 | ||
600 | p9_fid_destroy(fid); | |
601 | ||
602 | done: | |
603 | kfree(tc); | |
604 | kfree(rc); | |
605 | return err; | |
606 | } | |
607 | EXPORT_SYMBOL(p9_client_remove); | |
608 | ||
609 | int p9_client_read(struct p9_fid *fid, char *data, u64 offset, u32 count) | |
610 | { | |
611 | int err, n, rsize, total; | |
612 | struct p9_fcall *tc, *rc; | |
613 | struct p9_client *clnt; | |
614 | ||
615 | P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu %d\n", fid->fid, | |
616 | (long long unsigned) offset, count); | |
617 | err = 0; | |
618 | tc = NULL; | |
619 | rc = NULL; | |
620 | clnt = fid->clnt; | |
621 | total = 0; | |
622 | ||
623 | rsize = fid->iounit; | |
624 | if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) | |
625 | rsize = clnt->msize - P9_IOHDRSZ; | |
626 | ||
627 | do { | |
628 | if (count < rsize) | |
629 | rsize = count; | |
630 | ||
631 | tc = p9_create_tread(fid->fid, offset, rsize); | |
632 | if (IS_ERR(tc)) { | |
633 | err = PTR_ERR(tc); | |
634 | tc = NULL; | |
635 | goto error; | |
636 | } | |
637 | ||
e2735b77 | 638 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
639 | if (err) |
640 | goto error; | |
641 | ||
642 | n = rc->params.rread.count; | |
643 | if (n > count) | |
644 | n = count; | |
645 | ||
646 | memmove(data, rc->params.rread.data, n); | |
647 | count -= n; | |
648 | data += n; | |
649 | offset += n; | |
650 | total += n; | |
651 | kfree(tc); | |
652 | tc = NULL; | |
653 | kfree(rc); | |
654 | rc = NULL; | |
655 | } while (count > 0 && n == rsize); | |
656 | ||
657 | return total; | |
658 | ||
659 | error: | |
660 | kfree(tc); | |
661 | kfree(rc); | |
662 | return err; | |
663 | } | |
664 | EXPORT_SYMBOL(p9_client_read); | |
665 | ||
666 | int p9_client_write(struct p9_fid *fid, char *data, u64 offset, u32 count) | |
667 | { | |
668 | int err, n, rsize, total; | |
669 | struct p9_fcall *tc, *rc; | |
670 | struct p9_client *clnt; | |
671 | ||
672 | P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid, | |
673 | (long long unsigned) offset, count); | |
674 | err = 0; | |
675 | tc = NULL; | |
676 | rc = NULL; | |
677 | clnt = fid->clnt; | |
678 | total = 0; | |
679 | ||
680 | rsize = fid->iounit; | |
681 | if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) | |
682 | rsize = clnt->msize - P9_IOHDRSZ; | |
683 | ||
684 | do { | |
685 | if (count < rsize) | |
686 | rsize = count; | |
687 | ||
688 | tc = p9_create_twrite(fid->fid, offset, rsize, data); | |
689 | if (IS_ERR(tc)) { | |
690 | err = PTR_ERR(tc); | |
691 | tc = NULL; | |
692 | goto error; | |
693 | } | |
694 | ||
e2735b77 | 695 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
696 | if (err) |
697 | goto error; | |
698 | ||
699 | n = rc->params.rread.count; | |
700 | count -= n; | |
701 | data += n; | |
702 | offset += n; | |
703 | total += n; | |
704 | kfree(tc); | |
705 | tc = NULL; | |
706 | kfree(rc); | |
707 | rc = NULL; | |
708 | } while (count > 0); | |
709 | ||
710 | return total; | |
711 | ||
712 | error: | |
713 | kfree(tc); | |
714 | kfree(rc); | |
715 | return err; | |
716 | } | |
717 | EXPORT_SYMBOL(p9_client_write); | |
718 | ||
719 | int | |
720 | p9_client_uread(struct p9_fid *fid, char __user *data, u64 offset, u32 count) | |
721 | { | |
722 | int err, n, rsize, total; | |
723 | struct p9_fcall *tc, *rc; | |
724 | struct p9_client *clnt; | |
725 | ||
726 | P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid, | |
727 | (long long unsigned) offset, count); | |
728 | err = 0; | |
729 | tc = NULL; | |
730 | rc = NULL; | |
731 | clnt = fid->clnt; | |
732 | total = 0; | |
733 | ||
734 | rsize = fid->iounit; | |
735 | if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) | |
736 | rsize = clnt->msize - P9_IOHDRSZ; | |
737 | ||
738 | do { | |
739 | if (count < rsize) | |
740 | rsize = count; | |
741 | ||
742 | tc = p9_create_tread(fid->fid, offset, rsize); | |
743 | if (IS_ERR(tc)) { | |
744 | err = PTR_ERR(tc); | |
745 | tc = NULL; | |
746 | goto error; | |
747 | } | |
748 | ||
e2735b77 | 749 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
750 | if (err) |
751 | goto error; | |
752 | ||
753 | n = rc->params.rread.count; | |
754 | if (n > count) | |
755 | n = count; | |
756 | ||
757 | err = copy_to_user(data, rc->params.rread.data, n); | |
758 | if (err) { | |
759 | err = -EFAULT; | |
760 | goto error; | |
761 | } | |
762 | ||
763 | count -= n; | |
764 | data += n; | |
765 | offset += n; | |
766 | total += n; | |
767 | kfree(tc); | |
768 | tc = NULL; | |
769 | kfree(rc); | |
770 | rc = NULL; | |
771 | } while (count > 0 && n == rsize); | |
772 | ||
773 | return total; | |
774 | ||
775 | error: | |
776 | kfree(tc); | |
777 | kfree(rc); | |
778 | return err; | |
779 | } | |
780 | EXPORT_SYMBOL(p9_client_uread); | |
781 | ||
782 | int | |
783 | p9_client_uwrite(struct p9_fid *fid, const char __user *data, u64 offset, | |
784 | u32 count) | |
785 | { | |
786 | int err, n, rsize, total; | |
787 | struct p9_fcall *tc, *rc; | |
788 | struct p9_client *clnt; | |
789 | ||
790 | P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid, | |
791 | (long long unsigned) offset, count); | |
792 | err = 0; | |
793 | tc = NULL; | |
794 | rc = NULL; | |
795 | clnt = fid->clnt; | |
796 | total = 0; | |
797 | ||
798 | rsize = fid->iounit; | |
799 | if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) | |
800 | rsize = clnt->msize - P9_IOHDRSZ; | |
801 | ||
802 | do { | |
803 | if (count < rsize) | |
804 | rsize = count; | |
805 | ||
806 | tc = p9_create_twrite_u(fid->fid, offset, rsize, data); | |
807 | if (IS_ERR(tc)) { | |
808 | err = PTR_ERR(tc); | |
809 | tc = NULL; | |
810 | goto error; | |
811 | } | |
812 | ||
e2735b77 | 813 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
814 | if (err) |
815 | goto error; | |
816 | ||
817 | n = rc->params.rread.count; | |
818 | count -= n; | |
819 | data += n; | |
820 | offset += n; | |
821 | total += n; | |
822 | kfree(tc); | |
823 | tc = NULL; | |
824 | kfree(rc); | |
825 | rc = NULL; | |
826 | } while (count > 0); | |
827 | ||
828 | return total; | |
829 | ||
830 | error: | |
831 | kfree(tc); | |
832 | kfree(rc); | |
833 | return err; | |
834 | } | |
835 | EXPORT_SYMBOL(p9_client_uwrite); | |
836 | ||
837 | int p9_client_readn(struct p9_fid *fid, char *data, u64 offset, u32 count) | |
838 | { | |
839 | int n, total; | |
840 | ||
841 | P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid, | |
842 | (long long unsigned) offset, count); | |
843 | n = 0; | |
844 | total = 0; | |
845 | while (count) { | |
846 | n = p9_client_read(fid, data, offset, count); | |
847 | if (n <= 0) | |
848 | break; | |
849 | ||
850 | data += n; | |
851 | offset += n; | |
852 | count -= n; | |
853 | total += n; | |
854 | } | |
855 | ||
856 | if (n < 0) | |
857 | total = n; | |
858 | ||
859 | return total; | |
860 | } | |
861 | EXPORT_SYMBOL(p9_client_readn); | |
862 | ||
5503ac56 EVH |
863 | static struct p9_stat *p9_clone_stat(struct p9_stat *st, int dotu) |
864 | { | |
865 | int n; | |
866 | char *p; | |
867 | struct p9_stat *ret; | |
868 | ||
869 | n = sizeof(struct p9_stat) + st->name.len + st->uid.len + st->gid.len + | |
870 | st->muid.len; | |
871 | ||
872 | if (dotu) | |
873 | n += st->extension.len; | |
874 | ||
875 | ret = kmalloc(n, GFP_KERNEL); | |
876 | if (!ret) | |
877 | return ERR_PTR(-ENOMEM); | |
878 | ||
879 | memmove(ret, st, sizeof(struct p9_stat)); | |
880 | p = ((char *) ret) + sizeof(struct p9_stat); | |
881 | memmove(p, st->name.str, st->name.len); | |
882 | ret->name.str = p; | |
883 | p += st->name.len; | |
884 | memmove(p, st->uid.str, st->uid.len); | |
885 | ret->uid.str = p; | |
886 | p += st->uid.len; | |
887 | memmove(p, st->gid.str, st->gid.len); | |
888 | ret->gid.str = p; | |
889 | p += st->gid.len; | |
890 | memmove(p, st->muid.str, st->muid.len); | |
891 | ret->muid.str = p; | |
892 | p += st->muid.len; | |
893 | ||
894 | if (dotu) { | |
895 | memmove(p, st->extension.str, st->extension.len); | |
896 | ret->extension.str = p; | |
897 | p += st->extension.len; | |
898 | } | |
899 | ||
900 | return ret; | |
901 | } | |
902 | ||
bd238fb4 LI |
903 | struct p9_stat *p9_client_stat(struct p9_fid *fid) |
904 | { | |
905 | int err; | |
906 | struct p9_fcall *tc, *rc; | |
907 | struct p9_client *clnt; | |
908 | struct p9_stat *ret; | |
909 | ||
910 | P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid); | |
911 | err = 0; | |
912 | tc = NULL; | |
913 | rc = NULL; | |
914 | ret = NULL; | |
915 | clnt = fid->clnt; | |
916 | ||
917 | tc = p9_create_tstat(fid->fid); | |
918 | if (IS_ERR(tc)) { | |
919 | err = PTR_ERR(tc); | |
920 | tc = NULL; | |
921 | goto error; | |
922 | } | |
923 | ||
e2735b77 | 924 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
925 | if (err) |
926 | goto error; | |
927 | ||
928 | ret = p9_clone_stat(&rc->params.rstat.stat, clnt->dotu); | |
929 | if (IS_ERR(ret)) { | |
930 | err = PTR_ERR(ret); | |
931 | ret = NULL; | |
932 | goto error; | |
933 | } | |
934 | ||
935 | kfree(tc); | |
936 | kfree(rc); | |
937 | return ret; | |
938 | ||
939 | error: | |
940 | kfree(tc); | |
941 | kfree(rc); | |
942 | kfree(ret); | |
943 | return ERR_PTR(err); | |
944 | } | |
945 | EXPORT_SYMBOL(p9_client_stat); | |
946 | ||
947 | int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst) | |
948 | { | |
949 | int err; | |
950 | struct p9_fcall *tc, *rc; | |
951 | struct p9_client *clnt; | |
952 | ||
953 | P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid); | |
954 | err = 0; | |
955 | tc = NULL; | |
956 | rc = NULL; | |
957 | clnt = fid->clnt; | |
958 | ||
959 | tc = p9_create_twstat(fid->fid, wst, clnt->dotu); | |
960 | if (IS_ERR(tc)) { | |
961 | err = PTR_ERR(tc); | |
962 | tc = NULL; | |
963 | goto done; | |
964 | } | |
965 | ||
e2735b77 | 966 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
967 | |
968 | done: | |
969 | kfree(tc); | |
970 | kfree(rc); | |
971 | return err; | |
972 | } | |
973 | EXPORT_SYMBOL(p9_client_wstat); | |
974 | ||
975 | struct p9_stat *p9_client_dirread(struct p9_fid *fid, u64 offset) | |
976 | { | |
977 | int err, n, m; | |
978 | struct p9_fcall *tc, *rc; | |
979 | struct p9_client *clnt; | |
980 | struct p9_stat st, *ret; | |
981 | ||
982 | P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu\n", fid->fid, | |
983 | (long long unsigned) offset); | |
984 | err = 0; | |
985 | tc = NULL; | |
986 | rc = NULL; | |
987 | ret = NULL; | |
988 | clnt = fid->clnt; | |
989 | ||
990 | /* if the offset is below or above the current response, free it */ | |
991 | if (offset < fid->rdir_fpos || (fid->rdir_fcall && | |
992 | offset >= fid->rdir_fpos+fid->rdir_fcall->params.rread.count)) { | |
993 | fid->rdir_pos = 0; | |
994 | if (fid->rdir_fcall) | |
995 | fid->rdir_fpos += fid->rdir_fcall->params.rread.count; | |
996 | ||
997 | kfree(fid->rdir_fcall); | |
998 | fid->rdir_fcall = NULL; | |
999 | if (offset < fid->rdir_fpos) | |
1000 | fid->rdir_fpos = 0; | |
1001 | } | |
1002 | ||
1003 | if (!fid->rdir_fcall) { | |
1004 | n = fid->iounit; | |
1005 | if (!n || n > clnt->msize-P9_IOHDRSZ) | |
1006 | n = clnt->msize - P9_IOHDRSZ; | |
1007 | ||
1008 | while (1) { | |
1009 | if (fid->rdir_fcall) { | |
1010 | fid->rdir_fpos += | |
1011 | fid->rdir_fcall->params.rread.count; | |
1012 | kfree(fid->rdir_fcall); | |
1013 | fid->rdir_fcall = NULL; | |
1014 | } | |
1015 | ||
1016 | tc = p9_create_tread(fid->fid, fid->rdir_fpos, n); | |
1017 | if (IS_ERR(tc)) { | |
1018 | err = PTR_ERR(tc); | |
1019 | tc = NULL; | |
1020 | goto error; | |
1021 | } | |
1022 | ||
e2735b77 | 1023 | err = p9_client_rpc(clnt, tc, &rc); |
bd238fb4 LI |
1024 | if (err) |
1025 | goto error; | |
1026 | ||
1027 | n = rc->params.rread.count; | |
1028 | if (n == 0) | |
1029 | goto done; | |
1030 | ||
1031 | fid->rdir_fcall = rc; | |
1032 | rc = NULL; | |
1033 | if (offset >= fid->rdir_fpos && | |
1034 | offset < fid->rdir_fpos+n) | |
1035 | break; | |
1036 | } | |
1037 | ||
1038 | fid->rdir_pos = 0; | |
1039 | } | |
1040 | ||
1041 | m = offset - fid->rdir_fpos; | |
1042 | if (m < 0) | |
1043 | goto done; | |
1044 | ||
1045 | n = p9_deserialize_stat(fid->rdir_fcall->params.rread.data + m, | |
1046 | fid->rdir_fcall->params.rread.count - m, &st, clnt->dotu); | |
1047 | ||
1048 | if (!n) { | |
1049 | err = -EIO; | |
1050 | goto error; | |
1051 | } | |
1052 | ||
1053 | fid->rdir_pos += n; | |
1054 | st.size = n; | |
1055 | ret = p9_clone_stat(&st, clnt->dotu); | |
1056 | if (IS_ERR(ret)) { | |
1057 | err = PTR_ERR(ret); | |
1058 | ret = NULL; | |
1059 | goto error; | |
1060 | } | |
1061 | ||
1062 | done: | |
1063 | kfree(tc); | |
1064 | kfree(rc); | |
1065 | return ret; | |
1066 | ||
1067 | error: | |
1068 | kfree(tc); | |
1069 | kfree(rc); | |
1070 | kfree(ret); | |
1071 | return ERR_PTR(err); | |
1072 | } | |
1073 | EXPORT_SYMBOL(p9_client_dirread); |