Commit | Line | Data |
---|---|---|
b8cf945b EVH |
1 | /* |
2 | * linux/fs/9p/9p.c | |
3 | * | |
531b1094 | 4 | * This file contains functions to perform synchronous 9P calls |
b8cf945b | 5 | * |
531b1094 | 6 | * Copyright (C) 2004 by Latchesar Ionkov <lucho@ionkov.net> |
b8cf945b EVH |
7 | * 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 | |
42e8c509 EVH |
11 | * it under the terms of the GNU General Public License version 2 |
12 | * as published by the Free Software Foundation. | |
b8cf945b EVH |
13 | * |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to: | |
21 | * Free Software Foundation | |
22 | * 51 Franklin Street, Fifth Floor | |
23 | * Boston, MA 02111-1301 USA | |
24 | * | |
25 | */ | |
26 | ||
27 | #include <linux/config.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/errno.h> | |
30 | #include <linux/fs.h> | |
31 | #include <linux/idr.h> | |
32 | ||
33 | #include "debug.h" | |
34 | #include "v9fs.h" | |
35 | #include "9p.h" | |
531b1094 | 36 | #include "conv.h" |
b8cf945b EVH |
37 | #include "mux.h" |
38 | ||
39 | /** | |
40 | * v9fs_t_version - negotiate protocol parameters with sever | |
41 | * @v9ses: 9P2000 session information | |
42 | * @msize: requested max size packet | |
43 | * @version: requested version.extension string | |
44 | * @fcall: pointer to response fcall pointer | |
45 | * | |
46 | */ | |
47 | ||
48 | int | |
49 | v9fs_t_version(struct v9fs_session_info *v9ses, u32 msize, | |
531b1094 | 50 | char *version, struct v9fs_fcall **rcp) |
b8cf945b | 51 | { |
531b1094 LI |
52 | int ret; |
53 | struct v9fs_fcall *tc; | |
b8cf945b EVH |
54 | |
55 | dprintk(DEBUG_9P, "msize: %d version: %s\n", msize, version); | |
531b1094 | 56 | tc = v9fs_create_tversion(msize, version); |
b8cf945b | 57 | |
531b1094 LI |
58 | if (!IS_ERR(tc)) { |
59 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
60 | kfree(tc); | |
61 | } else | |
62 | ret = PTR_ERR(tc); | |
63 | ||
64 | return ret; | |
b8cf945b EVH |
65 | } |
66 | ||
67 | /** | |
68 | * v9fs_t_attach - mount the server | |
69 | * @v9ses: 9P2000 session information | |
70 | * @uname: user name doing the attach | |
71 | * @aname: remote name being attached to | |
72 | * @fid: mount fid to attatch to root node | |
73 | * @afid: authentication fid (in this case result key) | |
74 | * @fcall: pointer to response fcall pointer | |
75 | * | |
76 | */ | |
77 | ||
78 | int | |
79 | v9fs_t_attach(struct v9fs_session_info *v9ses, char *uname, char *aname, | |
531b1094 | 80 | u32 fid, u32 afid, struct v9fs_fcall **rcp) |
b8cf945b | 81 | { |
531b1094 LI |
82 | int ret; |
83 | struct v9fs_fcall* tc; | |
b8cf945b EVH |
84 | |
85 | dprintk(DEBUG_9P, "uname '%s' aname '%s' fid %d afid %d\n", uname, | |
86 | aname, fid, afid); | |
b8cf945b | 87 | |
531b1094 LI |
88 | tc = v9fs_create_tattach(fid, afid, uname, aname); |
89 | if (!IS_ERR(tc)) { | |
90 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
91 | kfree(tc); | |
92 | } else | |
93 | ret = PTR_ERR(tc); | |
94 | ||
95 | return ret; | |
3cf6429a LI |
96 | } |
97 | ||
98 | static void v9fs_t_clunk_cb(void *a, struct v9fs_fcall *tc, | |
99 | struct v9fs_fcall *rc, int err) | |
100 | { | |
101 | int fid; | |
102 | struct v9fs_session_info *v9ses; | |
103 | ||
104 | if (err) | |
105 | return; | |
106 | ||
107 | fid = tc->params.tclunk.fid; | |
108 | kfree(tc); | |
109 | ||
110 | if (!rc) | |
111 | return; | |
112 | ||
3cf6429a LI |
113 | v9ses = a; |
114 | if (rc->id == RCLUNK) | |
115 | v9fs_put_idpool(fid, &v9ses->fidpool); | |
116 | ||
117 | kfree(rc); | |
b8cf945b EVH |
118 | } |
119 | ||
120 | /** | |
121 | * v9fs_t_clunk - release a fid (finish a transaction) | |
122 | * @v9ses: 9P2000 session information | |
123 | * @fid: fid to release | |
124 | * @fcall: pointer to response fcall pointer | |
125 | * | |
126 | */ | |
531b1094 | 127 | |
b8cf945b | 128 | int |
3cf6429a | 129 | v9fs_t_clunk(struct v9fs_session_info *v9ses, u32 fid) |
b8cf945b | 130 | { |
531b1094 | 131 | int ret; |
3cf6429a LI |
132 | struct v9fs_fcall *tc, *rc; |
133 | ||
b8cf945b | 134 | dprintk(DEBUG_9P, "fid %d\n", fid); |
b8cf945b | 135 | |
531b1094 LI |
136 | rc = NULL; |
137 | tc = v9fs_create_tclunk(fid); | |
138 | if (!IS_ERR(tc)) | |
139 | ret = v9fs_mux_rpc(v9ses->mux, tc, &rc); | |
140 | else | |
141 | ret = PTR_ERR(tc); | |
142 | ||
143 | if (ret) | |
144 | dprintk(DEBUG_ERROR, "failed fid %d err %d\n", fid, ret); | |
3cf6429a | 145 | |
531b1094 LI |
146 | v9fs_t_clunk_cb(v9ses, tc, rc, ret); |
147 | return ret; | |
b8cf945b EVH |
148 | } |
149 | ||
29c6e486 | 150 | #if 0 |
b8cf945b EVH |
151 | /** |
152 | * v9fs_v9fs_t_flush - flush a pending transaction | |
153 | * @v9ses: 9P2000 session information | |
4a26c242 | 154 | * @tag: tag to release |
b8cf945b EVH |
155 | * |
156 | */ | |
157 | ||
531b1094 | 158 | int v9fs_t_flush(struct v9fs_session_info *v9ses, u16 oldtag) |
b8cf945b | 159 | { |
531b1094 LI |
160 | int ret; |
161 | struct v9fs_fcall *tc; | |
b8cf945b | 162 | |
531b1094 LI |
163 | dprintk(DEBUG_9P, "oldtag %d\n", oldtag); |
164 | ||
531b1094 LI |
165 | 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; | |
b8cf945b | 173 | } |
29c6e486 | 174 | #endif /* 0 */ |
b8cf945b EVH |
175 | |
176 | /** | |
177 | * v9fs_t_stat - read a file's meta-data | |
178 | * @v9ses: 9P2000 session information | |
179 | * @fid: fid pointing to file or directory to get info about | |
180 | * @fcall: pointer to response fcall | |
181 | * | |
182 | */ | |
183 | ||
184 | int | |
531b1094 | 185 | v9fs_t_stat(struct v9fs_session_info *v9ses, u32 fid, struct v9fs_fcall **rcp) |
b8cf945b | 186 | { |
531b1094 LI |
187 | int ret; |
188 | struct v9fs_fcall *tc; | |
b8cf945b EVH |
189 | |
190 | dprintk(DEBUG_9P, "fid %d\n", fid); | |
b8cf945b | 191 | |
531b1094 LI |
192 | ret = -ENOMEM; |
193 | tc = v9fs_create_tstat(fid); | |
194 | if (!IS_ERR(tc)) { | |
195 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
196 | kfree(tc); | |
197 | } else | |
198 | ret = PTR_ERR(tc); | |
199 | ||
200 | return ret; | |
b8cf945b EVH |
201 | } |
202 | ||
203 | /** | |
204 | * v9fs_t_wstat - write a file's meta-data | |
205 | * @v9ses: 9P2000 session information | |
206 | * @fid: fid pointing to file or directory to write info about | |
207 | * @stat: metadata | |
208 | * @fcall: pointer to response fcall | |
209 | * | |
210 | */ | |
211 | ||
212 | int | |
213 | v9fs_t_wstat(struct v9fs_session_info *v9ses, u32 fid, | |
531b1094 | 214 | struct v9fs_wstat *wstat, struct v9fs_fcall **rcp) |
b8cf945b | 215 | { |
531b1094 LI |
216 | int ret; |
217 | struct v9fs_fcall *tc; | |
b8cf945b | 218 | |
531b1094 LI |
219 | dprintk(DEBUG_9P, "fid %d\n", fid); |
220 | ||
531b1094 LI |
221 | tc = v9fs_create_twstat(fid, wstat, v9ses->extended); |
222 | if (!IS_ERR(tc)) { | |
223 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
224 | kfree(tc); | |
225 | } else | |
226 | ret = PTR_ERR(tc); | |
b8cf945b | 227 | |
531b1094 | 228 | return ret; |
b8cf945b EVH |
229 | } |
230 | ||
231 | /** | |
232 | * v9fs_t_walk - walk a fid to a new file or directory | |
233 | * @v9ses: 9P2000 session information | |
234 | * @fid: fid to walk | |
235 | * @newfid: new fid (for clone operations) | |
236 | * @name: path to walk fid to | |
237 | * @fcall: pointer to response fcall | |
238 | * | |
239 | */ | |
240 | ||
241 | /* TODO: support multiple walk */ | |
242 | ||
243 | int | |
244 | v9fs_t_walk(struct v9fs_session_info *v9ses, u32 fid, u32 newfid, | |
531b1094 | 245 | char *name, struct v9fs_fcall **rcp) |
b8cf945b | 246 | { |
531b1094 LI |
247 | int ret; |
248 | struct v9fs_fcall *tc; | |
249 | int nwname; | |
b8cf945b EVH |
250 | |
251 | dprintk(DEBUG_9P, "fid %d newfid %d wname '%s'\n", fid, newfid, name); | |
531b1094 LI |
252 | |
253 | if (name) | |
254 | nwname = 1; | |
255 | else | |
256 | nwname = 0; | |
257 | ||
531b1094 LI |
258 | tc = v9fs_create_twalk(fid, newfid, nwname, &name); |
259 | if (!IS_ERR(tc)) { | |
260 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
261 | kfree(tc); | |
262 | } else | |
263 | ret = PTR_ERR(tc); | |
264 | ||
265 | return ret; | |
b8cf945b EVH |
266 | } |
267 | ||
268 | /** | |
269 | * v9fs_t_open - open a file | |
270 | * | |
271 | * @v9ses - 9P2000 session information | |
272 | * @fid - fid to open | |
273 | * @mode - mode to open file (R, RW, etc) | |
274 | * @fcall - pointer to response fcall | |
275 | * | |
276 | */ | |
277 | ||
278 | int | |
279 | v9fs_t_open(struct v9fs_session_info *v9ses, u32 fid, u8 mode, | |
531b1094 | 280 | struct v9fs_fcall **rcp) |
b8cf945b | 281 | { |
531b1094 LI |
282 | int ret; |
283 | struct v9fs_fcall *tc; | |
b8cf945b EVH |
284 | |
285 | dprintk(DEBUG_9P, "fid %d mode %d\n", fid, mode); | |
b8cf945b | 286 | |
531b1094 LI |
287 | tc = v9fs_create_topen(fid, mode); |
288 | if (!IS_ERR(tc)) { | |
289 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
290 | kfree(tc); | |
291 | } else | |
292 | ret = PTR_ERR(tc); | |
b8cf945b | 293 | |
531b1094 | 294 | return ret; |
b8cf945b EVH |
295 | } |
296 | ||
297 | /** | |
298 | * v9fs_t_remove - remove a file or directory | |
299 | * @v9ses: 9P2000 session information | |
300 | * @fid: fid to remove | |
301 | * @fcall: pointer to response fcall | |
302 | * | |
303 | */ | |
304 | ||
305 | int | |
306 | v9fs_t_remove(struct v9fs_session_info *v9ses, u32 fid, | |
531b1094 | 307 | struct v9fs_fcall **rcp) |
b8cf945b | 308 | { |
531b1094 LI |
309 | int ret; |
310 | struct v9fs_fcall *tc; | |
b8cf945b EVH |
311 | |
312 | dprintk(DEBUG_9P, "fid %d\n", fid); | |
531b1094 | 313 | |
531b1094 LI |
314 | tc = v9fs_create_tremove(fid); |
315 | if (!IS_ERR(tc)) { | |
316 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
317 | kfree(tc); | |
318 | } else | |
319 | ret = PTR_ERR(tc); | |
320 | ||
321 | return ret; | |
b8cf945b EVH |
322 | } |
323 | ||
324 | /** | |
325 | * v9fs_t_create - create a file or directory | |
326 | * @v9ses: 9P2000 session information | |
327 | * @fid: fid to create | |
328 | * @name: name of the file or directory to create | |
329 | * @perm: permissions to create with | |
330 | * @mode: mode to open file (R, RW, etc) | |
331 | * @fcall: pointer to response fcall | |
332 | * | |
333 | */ | |
334 | ||
335 | int | |
16cce6d2 LI |
336 | v9fs_t_create(struct v9fs_session_info *v9ses, u32 fid, char *name, u32 perm, |
337 | u8 mode, char *extension, struct v9fs_fcall **rcp) | |
b8cf945b | 338 | { |
531b1094 LI |
339 | int ret; |
340 | struct v9fs_fcall *tc; | |
b8cf945b EVH |
341 | |
342 | dprintk(DEBUG_9P, "fid %d name '%s' perm %x mode %d\n", | |
343 | fid, name, perm, mode); | |
344 | ||
16cce6d2 LI |
345 | tc = v9fs_create_tcreate(fid, name, perm, mode, extension, |
346 | v9ses->extended); | |
347 | ||
531b1094 LI |
348 | if (!IS_ERR(tc)) { |
349 | ret = v9fs_mux_rpc(v9ses->mux, tc, rcp); | |
350 | kfree(tc); | |
351 | } else | |
352 | ret = PTR_ERR(tc); | |
b8cf945b | 353 | |
531b1094 | 354 | return ret; |
b8cf945b EVH |
355 | } |
356 | ||
357 | /** | |
358 | * v9fs_t_read - read data | |
359 | * @v9ses: 9P2000 session information | |
360 | * @fid: fid to read from | |
361 | * @offset: offset to start read at | |
362 | * @count: how many bytes to read | |
363 | * @fcall: pointer to response fcall (with data) | |
364 | * | |
365 | */ | |
366 | ||
367 | int | |
368 | v9fs_t_read(struct v9fs_session_info *v9ses, u32 fid, u64 offset, | |
531b1094 | 369 | u32 count, struct v9fs_fcall **rcp) |
b8cf945b | 370 | { |
531b1094 LI |
371 | int ret; |
372 | struct v9fs_fcall *tc, *rc; | |
b8cf945b | 373 | |
531b1094 LI |
374 | dprintk(DEBUG_9P, "fid %d offset 0x%llux count 0x%x\n", fid, |
375 | (long long unsigned) offset, count); | |
376 | ||
531b1094 LI |
377 | tc = v9fs_create_tread(fid, offset, count); |
378 | if (!IS_ERR(tc)) { | |
379 | ret = v9fs_mux_rpc(v9ses->mux, tc, &rc); | |
380 | if (!ret) | |
381 | ret = rc->params.rread.count; | |
382 | if (rcp) | |
383 | *rcp = rc; | |
384 | else | |
385 | kfree(rc); | |
386 | ||
387 | kfree(tc); | |
388 | } else | |
389 | ret = PTR_ERR(tc); | |
390 | ||
391 | return ret; | |
b8cf945b EVH |
392 | } |
393 | ||
394 | /** | |
395 | * v9fs_t_write - write data | |
396 | * @v9ses: 9P2000 session information | |
397 | * @fid: fid to write to | |
398 | * @offset: offset to start write at | |
399 | * @count: how many bytes to write | |
400 | * @fcall: pointer to response fcall | |
401 | * | |
402 | */ | |
403 | ||
404 | int | |
531b1094 LI |
405 | v9fs_t_write(struct v9fs_session_info *v9ses, u32 fid, u64 offset, u32 count, |
406 | const char __user *data, struct v9fs_fcall **rcp) | |
b8cf945b | 407 | { |
531b1094 LI |
408 | int ret; |
409 | struct v9fs_fcall *tc, *rc; | |
b8cf945b | 410 | |
531b1094 LI |
411 | dprintk(DEBUG_9P, "fid %d offset 0x%llux count 0x%x\n", fid, |
412 | (long long unsigned) offset, count); | |
b8cf945b | 413 | |
531b1094 LI |
414 | tc = v9fs_create_twrite(fid, offset, count, data); |
415 | if (!IS_ERR(tc)) { | |
416 | ret = v9fs_mux_rpc(v9ses->mux, tc, &rc); | |
b8cf945b | 417 | |
531b1094 LI |
418 | if (!ret) |
419 | ret = rc->params.rwrite.count; | |
420 | if (rcp) | |
421 | *rcp = rc; | |
422 | else | |
423 | kfree(rc); | |
b8cf945b | 424 | |
531b1094 LI |
425 | kfree(tc); |
426 | } else | |
427 | ret = PTR_ERR(tc); | |
b8cf945b | 428 | |
531b1094 | 429 | return ret; |
b8cf945b | 430 | } |
531b1094 | 431 |