6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
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.
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.
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
26 #include <linux/module.h>
27 #include <linux/errno.h>
29 #include <linux/poll.h>
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>
35 #include <linux/parser.h>
36 #include <net/9p/client.h>
37 #include <net/9p/transport.h>
39 static struct p9_fid *p9_fid_create(struct p9_client *clnt);
40 static void p9_fid_destroy(struct p9_fid *fid);
41 static struct p9_stat *p9_clone_stat(struct p9_stat *st, int dotu);
44 * Client Option Parsing (code inspired by NFS code)
45 * - a little lazy - parse all client options
55 static const match_table_t tokens = {
56 {Opt_msize, "msize=%u"},
57 {Opt_legacy, "noextend"},
58 {Opt_trans, "trans=%s"},
63 * v9fs_parse_options - parse mount options into session structure
64 * @options: options string passed from mount
65 * @v9ses: existing v9fs session information
67 * Return 0 upon success, -ERRNO upon failure
70 static int parse_opts(char *opts, struct p9_client *clnt)
74 substring_t args[MAX_OPT_ARGS];
84 options = kstrdup(opts, GFP_KERNEL);
86 P9_DPRINTK(P9_DEBUG_ERROR,
87 "failed to allocate copy of option string\n");
91 while ((p = strsep(&options, ",")) != NULL) {
95 token = match_token(p, tokens, args);
96 if (token < Opt_trans) {
97 int r = match_int(&args[0], &option);
99 P9_DPRINTK(P9_DEBUG_ERROR,
100 "integer field, but no integer?\n");
107 clnt->msize = option;
110 clnt->trans_mod = v9fs_get_trans_by_name(&args[0]);
120 if (!clnt->trans_mod)
121 clnt->trans_mod = v9fs_get_default_trans();
129 * p9_client_rpc - sends 9P request and waits until a response is available.
130 * The function can be interrupted.
132 * @tc: request to be sent
133 * @rc: pointer where a pointer to the response is stored
136 p9_client_rpc(struct p9_client *c, struct p9_fcall *tc,
137 struct p9_fcall **rc)
139 return c->trans_mod->rpc(c, tc, rc);
142 struct p9_client *p9_client_create(const char *dev_name, char *options)
145 struct p9_client *clnt;
146 struct p9_fcall *tc, *rc;
147 struct p9_str *version;
152 clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
154 return ERR_PTR(-ENOMEM);
156 clnt->trans_mod = NULL;
158 spin_lock_init(&clnt->lock);
159 INIT_LIST_HEAD(&clnt->fidlist);
160 clnt->fidpool = p9_idpool_create();
161 if (IS_ERR(clnt->fidpool)) {
162 err = PTR_ERR(clnt->fidpool);
163 clnt->fidpool = NULL;
167 err = parse_opts(options, clnt);
171 if (clnt->trans_mod == NULL) {
172 err = -EPROTONOSUPPORT;
173 P9_DPRINTK(P9_DEBUG_ERROR,
174 "No transport defined or default transport\n");
178 P9_DPRINTK(P9_DEBUG_9P, "clnt %p trans %p msize %d dotu %d\n",
179 clnt, clnt->trans_mod, clnt->msize, clnt->dotu);
182 err = clnt->trans_mod->create(clnt, dev_name, options);
186 if ((clnt->msize+P9_IOHDRSZ) > clnt->trans_mod->maxsize)
187 clnt->msize = clnt->trans_mod->maxsize-P9_IOHDRSZ;
189 tc = p9_create_tversion(clnt->msize, clnt->dotu?"9P2000.u":"9P2000");
196 err = p9_client_rpc(clnt, tc, &rc);
200 version = &rc->params.rversion.version;
201 if (version->len == 8 && !memcmp(version->str, "9P2000.u", 8))
203 else if (version->len == 6 && !memcmp(version->str, "9P2000", 6))
210 n = rc->params.rversion.msize;
221 p9_client_destroy(clnt);
224 EXPORT_SYMBOL(p9_client_create);
226 void p9_client_destroy(struct p9_client *clnt)
228 struct p9_fid *fid, *fidptr;
230 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
233 clnt->trans_mod->close(clnt);
235 v9fs_put_trans(clnt->trans_mod);
237 list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist)
241 p9_idpool_destroy(clnt->fidpool);
245 EXPORT_SYMBOL(p9_client_destroy);
247 void p9_client_disconnect(struct p9_client *clnt)
249 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
250 clnt->status = Disconnected;
252 EXPORT_SYMBOL(p9_client_disconnect);
254 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
255 char *uname, u32 n_uname, char *aname)
258 struct p9_fcall *tc, *rc;
261 P9_DPRINTK(P9_DEBUG_9P, "clnt %p afid %d uname %s aname %s\n",
262 clnt, afid?afid->fid:-1, uname, aname);
267 fid = p9_fid_create(clnt);
274 tc = p9_create_tattach(fid->fid, afid?afid->fid:P9_NOFID, uname, aname,
275 n_uname, clnt->dotu);
282 err = p9_client_rpc(clnt, tc, &rc);
286 memmove(&fid->qid, &rc->params.rattach.qid, sizeof(struct p9_qid));
298 EXPORT_SYMBOL(p9_client_attach);
300 struct p9_fid *p9_client_auth(struct p9_client *clnt, char *uname,
301 u32 n_uname, char *aname)
304 struct p9_fcall *tc, *rc;
307 P9_DPRINTK(P9_DEBUG_9P, "clnt %p uname %s aname %s\n", clnt, uname,
313 fid = p9_fid_create(clnt);
320 tc = p9_create_tauth(fid->fid, uname, aname, n_uname, clnt->dotu);
327 err = p9_client_rpc(clnt, tc, &rc);
331 memmove(&fid->qid, &rc->params.rauth.qid, sizeof(struct p9_qid));
343 EXPORT_SYMBOL(p9_client_auth);
345 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, int nwname, char **wnames,
349 struct p9_fcall *tc, *rc;
350 struct p9_client *clnt;
353 P9_DPRINTK(P9_DEBUG_9P, "fid %d nwname %d wname[0] %s\n",
354 oldfid->fid, nwname, wnames?wnames[0]:NULL);
360 fid = p9_fid_create(clnt);
367 fid->uid = oldfid->uid;
371 tc = p9_create_twalk(oldfid->fid, fid->fid, nwname, wnames);
378 err = p9_client_rpc(clnt, tc, &rc);
380 if (rc && rc->id == P9_RWALK)
386 if (rc->params.rwalk.nwqid != nwname) {
393 &rc->params.rwalk.wqids[rc->params.rwalk.nwqid - 1],
394 sizeof(struct p9_qid));
396 fid->qid = oldfid->qid;
406 tc = p9_create_tclunk(fid->fid);
413 p9_client_rpc(clnt, tc, &rc);
418 if (fid && (fid != oldfid))
423 EXPORT_SYMBOL(p9_client_walk);
425 int p9_client_open(struct p9_fid *fid, int mode)
428 struct p9_fcall *tc, *rc;
429 struct p9_client *clnt;
431 P9_DPRINTK(P9_DEBUG_9P, "fid %d mode %d\n", fid->fid, mode);
440 tc = p9_create_topen(fid->fid, mode);
447 err = p9_client_rpc(clnt, tc, &rc);
452 fid->iounit = rc->params.ropen.iounit;
459 EXPORT_SYMBOL(p9_client_open);
461 int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
465 struct p9_fcall *tc, *rc;
466 struct p9_client *clnt;
468 P9_DPRINTK(P9_DEBUG_9P, "fid %d name %s perm %d mode %d\n", fid->fid,
478 tc = p9_create_tcreate(fid->fid, name, perm, mode, extension,
486 err = p9_client_rpc(clnt, tc, &rc);
491 fid->iounit = rc->params.ropen.iounit;
498 EXPORT_SYMBOL(p9_client_fcreate);
500 int p9_client_clunk(struct p9_fid *fid)
503 struct p9_fcall *tc, *rc;
504 struct p9_client *clnt;
506 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
512 tc = p9_create_tclunk(fid->fid);
519 err = p9_client_rpc(clnt, tc, &rc);
530 EXPORT_SYMBOL(p9_client_clunk);
532 int p9_client_remove(struct p9_fid *fid)
535 struct p9_fcall *tc, *rc;
536 struct p9_client *clnt;
538 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
544 tc = p9_create_tremove(fid->fid);
551 err = p9_client_rpc(clnt, tc, &rc);
562 EXPORT_SYMBOL(p9_client_remove);
564 int p9_client_read(struct p9_fid *fid, char *data, u64 offset, u32 count)
566 int err, n, rsize, total;
567 struct p9_fcall *tc, *rc;
568 struct p9_client *clnt;
570 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu %d\n", fid->fid,
571 (long long unsigned) offset, count);
579 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
580 rsize = clnt->msize - P9_IOHDRSZ;
586 tc = p9_create_tread(fid->fid, offset, rsize);
593 err = p9_client_rpc(clnt, tc, &rc);
597 n = rc->params.rread.count;
601 memmove(data, rc->params.rread.data, n);
610 } while (count > 0 && n == rsize);
619 EXPORT_SYMBOL(p9_client_read);
621 int p9_client_write(struct p9_fid *fid, char *data, u64 offset, u32 count)
623 int err, n, rsize, total;
624 struct p9_fcall *tc, *rc;
625 struct p9_client *clnt;
627 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
628 (long long unsigned) offset, count);
636 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
637 rsize = clnt->msize - P9_IOHDRSZ;
643 tc = p9_create_twrite(fid->fid, offset, rsize, data);
650 err = p9_client_rpc(clnt, tc, &rc);
654 n = rc->params.rread.count;
672 EXPORT_SYMBOL(p9_client_write);
675 p9_client_uread(struct p9_fid *fid, char __user *data, u64 offset, u32 count)
677 int err, n, rsize, total;
678 struct p9_fcall *tc, *rc;
679 struct p9_client *clnt;
681 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
682 (long long unsigned) offset, count);
690 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
691 rsize = clnt->msize - P9_IOHDRSZ;
697 tc = p9_create_tread(fid->fid, offset, rsize);
704 err = p9_client_rpc(clnt, tc, &rc);
708 n = rc->params.rread.count;
712 err = copy_to_user(data, rc->params.rread.data, n);
726 } while (count > 0 && n == rsize);
735 EXPORT_SYMBOL(p9_client_uread);
738 p9_client_uwrite(struct p9_fid *fid, const char __user *data, u64 offset,
741 int err, n, rsize, total;
742 struct p9_fcall *tc, *rc;
743 struct p9_client *clnt;
745 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
746 (long long unsigned) offset, count);
754 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
755 rsize = clnt->msize - P9_IOHDRSZ;
761 tc = p9_create_twrite_u(fid->fid, offset, rsize, data);
768 err = p9_client_rpc(clnt, tc, &rc);
772 n = rc->params.rread.count;
790 EXPORT_SYMBOL(p9_client_uwrite);
792 int p9_client_readn(struct p9_fid *fid, char *data, u64 offset, u32 count)
796 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu count %d\n", fid->fid,
797 (long long unsigned) offset, count);
801 n = p9_client_read(fid, data, offset, count);
816 EXPORT_SYMBOL(p9_client_readn);
818 struct p9_stat *p9_client_stat(struct p9_fid *fid)
821 struct p9_fcall *tc, *rc;
822 struct p9_client *clnt;
825 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
832 tc = p9_create_tstat(fid->fid);
839 err = p9_client_rpc(clnt, tc, &rc);
843 ret = p9_clone_stat(&rc->params.rstat.stat, clnt->dotu);
860 EXPORT_SYMBOL(p9_client_stat);
862 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
865 struct p9_fcall *tc, *rc;
866 struct p9_client *clnt;
868 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
874 tc = p9_create_twstat(fid->fid, wst, clnt->dotu);
881 err = p9_client_rpc(clnt, tc, &rc);
888 EXPORT_SYMBOL(p9_client_wstat);
890 struct p9_stat *p9_client_dirread(struct p9_fid *fid, u64 offset)
893 struct p9_fcall *tc, *rc;
894 struct p9_client *clnt;
895 struct p9_stat st, *ret;
897 P9_DPRINTK(P9_DEBUG_9P, "fid %d offset %llu\n", fid->fid,
898 (long long unsigned) offset);
905 /* if the offset is below or above the current response, free it */
906 if (offset < fid->rdir_fpos || (fid->rdir_fcall &&
907 offset >= fid->rdir_fpos+fid->rdir_fcall->params.rread.count)) {
910 fid->rdir_fpos += fid->rdir_fcall->params.rread.count;
912 kfree(fid->rdir_fcall);
913 fid->rdir_fcall = NULL;
914 if (offset < fid->rdir_fpos)
918 if (!fid->rdir_fcall) {
920 if (!n || n > clnt->msize-P9_IOHDRSZ)
921 n = clnt->msize - P9_IOHDRSZ;
924 if (fid->rdir_fcall) {
926 fid->rdir_fcall->params.rread.count;
927 kfree(fid->rdir_fcall);
928 fid->rdir_fcall = NULL;
931 tc = p9_create_tread(fid->fid, fid->rdir_fpos, n);
938 err = p9_client_rpc(clnt, tc, &rc);
942 n = rc->params.rread.count;
946 fid->rdir_fcall = rc;
948 if (offset >= fid->rdir_fpos &&
949 offset < fid->rdir_fpos+n)
956 m = offset - fid->rdir_fpos;
960 n = p9_deserialize_stat(fid->rdir_fcall->params.rread.data + m,
961 fid->rdir_fcall->params.rread.count - m, &st, clnt->dotu);
970 ret = p9_clone_stat(&st, clnt->dotu);
988 EXPORT_SYMBOL(p9_client_dirread);
990 static struct p9_stat *p9_clone_stat(struct p9_stat *st, int dotu)
996 n = sizeof(struct p9_stat) + st->name.len + st->uid.len + st->gid.len +
1000 n += st->extension.len;
1002 ret = kmalloc(n, GFP_KERNEL);
1004 return ERR_PTR(-ENOMEM);
1006 memmove(ret, st, sizeof(struct p9_stat));
1007 p = ((char *) ret) + sizeof(struct p9_stat);
1008 memmove(p, st->name.str, st->name.len);
1011 memmove(p, st->uid.str, st->uid.len);
1014 memmove(p, st->gid.str, st->gid.len);
1017 memmove(p, st->muid.str, st->muid.len);
1022 memmove(p, st->extension.str, st->extension.len);
1023 ret->extension.str = p;
1024 p += st->extension.len;
1030 static struct p9_fid *p9_fid_create(struct p9_client *clnt)
1035 P9_DPRINTK(P9_DEBUG_9P, "clnt %p\n", clnt);
1036 fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
1038 return ERR_PTR(-ENOMEM);
1040 fid->fid = p9_idpool_get(clnt->fidpool);
1046 memset(&fid->qid, 0, sizeof(struct p9_qid));
1050 fid->rdir_fcall = NULL;
1051 fid->uid = current->fsuid;
1055 spin_lock(&clnt->lock);
1056 list_add(&fid->flist, &clnt->fidlist);
1057 spin_unlock(&clnt->lock);
1063 return ERR_PTR(err);
1066 static void p9_fid_destroy(struct p9_fid *fid)
1068 struct p9_client *clnt;
1070 P9_DPRINTK(P9_DEBUG_9P, "fid %d\n", fid->fid);
1072 p9_idpool_put(fid->fid, clnt->fidpool);
1073 spin_lock(&clnt->lock);
1074 list_del(&fid->flist);
1075 spin_unlock(&clnt->lock);
1076 kfree(fid->rdir_fcall);