blob: f86a28d1d6a6ac08168a891351e059a59f583859 [file] [log] [blame]
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -07001/*
2 * linux/fs/9p/9p.c
3 *
Latchesar Ionkov531b1092006-01-08 01:05:00 -08004 * This file contains functions to perform synchronous 9P calls
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -07005 *
Latchesar Ionkov531b1092006-01-08 01:05:00 -08006 * Copyright (C) 2004 by Latchesar Ionkov <lucho@ionkov.net>
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -07007 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to:
22 * Free Software Foundation
23 * 51 Franklin Street, Fifth Floor
24 * Boston, MA 02111-1301 USA
25 *
26 */
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/errno.h>
31#include <linux/fs.h>
32#include <linux/idr.h>
33
34#include "debug.h"
35#include "v9fs.h"
36#include "9p.h"
Latchesar Ionkov531b1092006-01-08 01:05:00 -080037#include "conv.h"
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070038#include "mux.h"
39
40/**
41 * v9fs_t_version - negotiate protocol parameters with sever
42 * @v9ses: 9P2000 session information
43 * @msize: requested max size packet
44 * @version: requested version.extension string
45 * @fcall: pointer to response fcall pointer
46 *
47 */
48
49int
50v9fs_t_version(struct v9fs_session_info *v9ses, u32 msize,
Latchesar Ionkov531b1092006-01-08 01:05:00 -080051 char *version, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070052{
Latchesar Ionkov531b1092006-01-08 01:05:00 -080053 int ret;
54 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070055
56 dprintk(DEBUG_9P, "msize: %d version: %s\n", msize, version);
Latchesar Ionkov531b1092006-01-08 01:05:00 -080057 tc = v9fs_create_tversion(msize, version);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070058
Latchesar Ionkov531b1092006-01-08 01:05:00 -080059 if (!IS_ERR(tc)) {
60 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
61 kfree(tc);
62 } else
63 ret = PTR_ERR(tc);
64
65 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070066}
67
68/**
69 * v9fs_t_attach - mount the server
70 * @v9ses: 9P2000 session information
71 * @uname: user name doing the attach
72 * @aname: remote name being attached to
73 * @fid: mount fid to attatch to root node
74 * @afid: authentication fid (in this case result key)
75 * @fcall: pointer to response fcall pointer
76 *
77 */
78
79int
80v9fs_t_attach(struct v9fs_session_info *v9ses, char *uname, char *aname,
Latchesar Ionkov531b1092006-01-08 01:05:00 -080081 u32 fid, u32 afid, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070082{
Latchesar Ionkov531b1092006-01-08 01:05:00 -080083 int ret;
84 struct v9fs_fcall* tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070085
86 dprintk(DEBUG_9P, "uname '%s' aname '%s' fid %d afid %d\n", uname,
87 aname, fid, afid);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070088
Latchesar Ionkov531b1092006-01-08 01:05:00 -080089 tc = v9fs_create_tattach(fid, afid, uname, aname);
90 if (!IS_ERR(tc)) {
91 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
92 kfree(tc);
93 } else
94 ret = PTR_ERR(tc);
95
96 return ret;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -080097}
98
99static void v9fs_t_clunk_cb(void *a, struct v9fs_fcall *tc,
100 struct v9fs_fcall *rc, int err)
101{
102 int fid;
103 struct v9fs_session_info *v9ses;
104
105 if (err)
106 return;
107
108 fid = tc->params.tclunk.fid;
109 kfree(tc);
110
111 if (!rc)
112 return;
113
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800114 v9ses = a;
115 if (rc->id == RCLUNK)
116 v9fs_put_idpool(fid, &v9ses->fidpool);
117
118 kfree(rc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700119}
120
121/**
122 * v9fs_t_clunk - release a fid (finish a transaction)
123 * @v9ses: 9P2000 session information
124 * @fid: fid to release
125 * @fcall: pointer to response fcall pointer
126 *
127 */
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800128
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700129int
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800130v9fs_t_clunk(struct v9fs_session_info *v9ses, u32 fid)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700131{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800132 int ret;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800133 struct v9fs_fcall *tc, *rc;
134
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700135 dprintk(DEBUG_9P, "fid %d\n", fid);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700136
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800137 rc = NULL;
138 tc = v9fs_create_tclunk(fid);
139 if (!IS_ERR(tc))
140 ret = v9fs_mux_rpc(v9ses->mux, tc, &rc);
141 else
142 ret = PTR_ERR(tc);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800143
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800144 if (ret)
145 dprintk(DEBUG_ERROR, "failed fid %d err %d\n", fid, ret);
146
147 v9fs_t_clunk_cb(v9ses, tc, rc, ret);
148 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700149}
150
151/**
152 * v9fs_v9fs_t_flush - flush a pending transaction
153 * @v9ses: 9P2000 session information
154 * @tag: tid to release
155 *
156 */
157
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800158int v9fs_t_flush(struct v9fs_session_info *v9ses, u16 oldtag)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700159{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800160 int ret;
161 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700162
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800163 dprintk(DEBUG_9P, "oldtag %d\n", oldtag);
164
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800165 tc = v9fs_create_tflush(oldtag);
166 if (!IS_ERR(tc)) {
167 ret = v9fs_mux_rpc(v9ses->mux, tc, NULL);
168 kfree(tc);
169 } else
170 ret = PTR_ERR(tc);
171
172 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700173}
174
175/**
176 * v9fs_t_stat - read a file's meta-data
177 * @v9ses: 9P2000 session information
178 * @fid: fid pointing to file or directory to get info about
179 * @fcall: pointer to response fcall
180 *
181 */
182
183int
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800184v9fs_t_stat(struct v9fs_session_info *v9ses, u32 fid, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700185{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800186 int ret;
187 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700188
189 dprintk(DEBUG_9P, "fid %d\n", fid);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700190
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800191 ret = -ENOMEM;
192 tc = v9fs_create_tstat(fid);
193 if (!IS_ERR(tc)) {
194 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
195 kfree(tc);
196 } else
197 ret = PTR_ERR(tc);
198
199 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700200}
201
202/**
203 * v9fs_t_wstat - write a file's meta-data
204 * @v9ses: 9P2000 session information
205 * @fid: fid pointing to file or directory to write info about
206 * @stat: metadata
207 * @fcall: pointer to response fcall
208 *
209 */
210
211int
212v9fs_t_wstat(struct v9fs_session_info *v9ses, u32 fid,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800213 struct v9fs_wstat *wstat, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700214{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800215 int ret;
216 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700217
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800218 dprintk(DEBUG_9P, "fid %d\n", fid);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700219
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800220 tc = v9fs_create_twstat(fid, wstat, v9ses->extended);
221 if (!IS_ERR(tc)) {
222 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
223 kfree(tc);
224 } else
225 ret = PTR_ERR(tc);
226
227 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700228}
229
230/**
231 * v9fs_t_walk - walk a fid to a new file or directory
232 * @v9ses: 9P2000 session information
233 * @fid: fid to walk
234 * @newfid: new fid (for clone operations)
235 * @name: path to walk fid to
236 * @fcall: pointer to response fcall
237 *
238 */
239
240/* TODO: support multiple walk */
241
242int
243v9fs_t_walk(struct v9fs_session_info *v9ses, u32 fid, u32 newfid,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800244 char *name, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700245{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800246 int ret;
247 struct v9fs_fcall *tc;
248 int nwname;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700249
250 dprintk(DEBUG_9P, "fid %d newfid %d wname '%s'\n", fid, newfid, name);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700251
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800252 if (name)
253 nwname = 1;
254 else
255 nwname = 0;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700256
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800257 tc = v9fs_create_twalk(fid, newfid, nwname, &name);
258 if (!IS_ERR(tc)) {
259 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
260 kfree(tc);
261 } else
262 ret = PTR_ERR(tc);
263
264 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700265}
266
267/**
268 * v9fs_t_open - open a file
269 *
270 * @v9ses - 9P2000 session information
271 * @fid - fid to open
272 * @mode - mode to open file (R, RW, etc)
273 * @fcall - pointer to response fcall
274 *
275 */
276
277int
278v9fs_t_open(struct v9fs_session_info *v9ses, u32 fid, u8 mode,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800279 struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700280{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800281 int ret;
282 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700283
284 dprintk(DEBUG_9P, "fid %d mode %d\n", fid, mode);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700285
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800286 tc = v9fs_create_topen(fid, mode);
287 if (!IS_ERR(tc)) {
288 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
289 kfree(tc);
290 } else
291 ret = PTR_ERR(tc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700292
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800293 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700294}
295
296/**
297 * v9fs_t_remove - remove a file or directory
298 * @v9ses: 9P2000 session information
299 * @fid: fid to remove
300 * @fcall: pointer to response fcall
301 *
302 */
303
304int
305v9fs_t_remove(struct v9fs_session_info *v9ses, u32 fid,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800306 struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700307{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800308 int ret;
309 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700310
311 dprintk(DEBUG_9P, "fid %d\n", fid);
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800312
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800313 tc = v9fs_create_tremove(fid);
314 if (!IS_ERR(tc)) {
315 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
316 kfree(tc);
317 } else
318 ret = PTR_ERR(tc);
319
320 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700321}
322
323/**
324 * v9fs_t_create - create a file or directory
325 * @v9ses: 9P2000 session information
326 * @fid: fid to create
327 * @name: name of the file or directory to create
328 * @perm: permissions to create with
329 * @mode: mode to open file (R, RW, etc)
330 * @fcall: pointer to response fcall
331 *
332 */
333
334int
335v9fs_t_create(struct v9fs_session_info *v9ses, u32 fid, char *name,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800336 u32 perm, u8 mode, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700337{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800338 int ret;
339 struct v9fs_fcall *tc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700340
341 dprintk(DEBUG_9P, "fid %d name '%s' perm %x mode %d\n",
342 fid, name, perm, mode);
343
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800344 tc = v9fs_create_tcreate(fid, name, perm, mode);
345 if (!IS_ERR(tc)) {
346 ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
347 kfree(tc);
348 } else
349 ret = PTR_ERR(tc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700350
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800351 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700352}
353
354/**
355 * v9fs_t_read - read data
356 * @v9ses: 9P2000 session information
357 * @fid: fid to read from
358 * @offset: offset to start read at
359 * @count: how many bytes to read
360 * @fcall: pointer to response fcall (with data)
361 *
362 */
363
364int
365v9fs_t_read(struct v9fs_session_info *v9ses, u32 fid, u64 offset,
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800366 u32 count, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700367{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800368 int ret;
369 struct v9fs_fcall *tc, *rc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700370
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800371 dprintk(DEBUG_9P, "fid %d offset 0x%llux count 0x%x\n", fid,
372 (long long unsigned) offset, count);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700373
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800374 tc = v9fs_create_tread(fid, offset, count);
375 if (!IS_ERR(tc)) {
376 ret = v9fs_mux_rpc(v9ses->mux, tc, &rc);
377 if (!ret)
378 ret = rc->params.rread.count;
379 if (rcp)
380 *rcp = rc;
381 else
382 kfree(rc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700383
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800384 kfree(tc);
385 } else
386 ret = PTR_ERR(tc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700387
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800388 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700389}
390
391/**
392 * v9fs_t_write - write data
393 * @v9ses: 9P2000 session information
394 * @fid: fid to write to
395 * @offset: offset to start write at
396 * @count: how many bytes to write
397 * @fcall: pointer to response fcall
398 *
399 */
400
401int
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800402v9fs_t_write(struct v9fs_session_info *v9ses, u32 fid, u64 offset, u32 count,
403 const char __user *data, struct v9fs_fcall **rcp)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700404{
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800405 int ret;
406 struct v9fs_fcall *tc, *rc;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700407
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800408 dprintk(DEBUG_9P, "fid %d offset 0x%llux count 0x%x\n", fid,
409 (long long unsigned) offset, count);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700410
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800411 tc = v9fs_create_twrite(fid, offset, count, data);
412 if (!IS_ERR(tc)) {
413 ret = v9fs_mux_rpc(v9ses->mux, tc, &rc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700414
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800415 if (!ret)
416 ret = rc->params.rwrite.count;
417 if (rcp)
418 *rcp = rc;
419 else
420 kfree(rc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700421
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800422 kfree(tc);
423 } else
424 ret = PTR_ERR(tc);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700425
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800426 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700427}
Latchesar Ionkov531b1092006-01-08 01:05:00 -0800428