blob: 94f5a8f65e9c3649c8417a62c0c87b1e1ef708cd [file] [log] [blame]
Eric Van Hensbergenace51c42008-10-13 20:40:27 -05001/*
2 * net/9p/protocol.c
3 *
4 * 9P Protocol Support Code
5 *
6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
7 *
8 * Base on code from Anthony Liguori <aliguori@us.ibm.com>
9 * Copyright (C) 2008 by IBM, Corp.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
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/module.h>
29#include <linux/errno.h>
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050030#include <linux/uaccess.h>
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -050031#include <linux/sched.h>
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -080032#include <linux/types.h>
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050033#include <net/9p/9p.h>
34#include <net/9p/client.h>
35#include "protocol.h"
36
37#ifndef MIN
38#define MIN(a, b) (((a) < (b)) ? (a) : (b))
39#endif
40
41#ifndef MAX
42#define MAX(a, b) (((a) > (b)) ? (a) : (b))
43#endif
44
45#ifndef offset_of
46#define offset_of(type, memb) \
47 ((unsigned long)(&((type *)0)->memb))
48#endif
49#ifndef container_of
50#define container_of(obj, type, memb) \
51 ((type *)(((char *)obj) - offset_of(type, memb)))
52#endif
53
54static int
Sripathi Kodi342fee12010-03-05 18:50:14 +000055p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050056
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -050057#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050058void
59p9pdu_dump(int way, struct p9_fcall *pdu)
60{
61 int i, n;
62 u8 *data = pdu->sdata;
63 int datalen = pdu->size;
64 char buf[255];
65 int buflen = 255;
66
67 i = n = 0;
68 if (datalen > (buflen-16))
69 datalen = buflen-16;
70 while (i < datalen) {
71 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
72 if (i%4 == 3)
73 n += scnprintf(buf + n, buflen - n, " ");
74 if (i%32 == 31)
75 n += scnprintf(buf + n, buflen - n, "\n");
76
77 i++;
78 }
79 n += scnprintf(buf + n, buflen - n, "\n");
80
81 if (way)
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -050082 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050083 else
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -050084 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050085}
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -050086#else
87void
88p9pdu_dump(int way, struct p9_fcall *pdu)
89{
90}
91#endif
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050092EXPORT_SYMBOL(p9pdu_dump);
93
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050094void p9stat_free(struct p9_wstat *stbuf)
95{
96 kfree(stbuf->name);
97 kfree(stbuf->uid);
98 kfree(stbuf->gid);
99 kfree(stbuf->muid);
100 kfree(stbuf->extension);
101}
102EXPORT_SYMBOL(p9stat_free);
103
104static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
105{
106 size_t len = MIN(pdu->size - pdu->offset, size);
107 memcpy(data, &pdu->sdata[pdu->offset], len);
108 pdu->offset += len;
109 return size - len;
110}
111
112static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
113{
114 size_t len = MIN(pdu->capacity - pdu->size, size);
115 memcpy(&pdu->sdata[pdu->size], data, len);
116 pdu->size += len;
117 return size - len;
118}
119
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500120static size_t
121pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
122{
123 size_t len = MIN(pdu->capacity - pdu->size, size);
124 int err = copy_from_user(&pdu->sdata[pdu->size], udata, len);
125 if (err)
126 printk(KERN_WARNING "pdu_write_u returning: %d\n", err);
127
128 pdu->size += len;
129 return size - len;
130}
131
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500132/*
133 b - int8_t
134 w - int16_t
135 d - int32_t
136 q - int64_t
137 s - string
138 S - stat
139 Q - qid
140 D - data blob (int32_t size followed by void *, results are not freed)
141 T - array of strings (int16_t count, followed by strings)
142 R - array of qids (int16_t count, followed by qids)
143 ? - if optional = 1, continue parsing
144*/
145
146static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000147p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
148 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500149{
150 const char *ptr;
151 int errcode = 0;
152
153 for (ptr = fmt; *ptr; ptr++) {
154 switch (*ptr) {
155 case 'b':{
156 int8_t *val = va_arg(ap, int8_t *);
157 if (pdu_read(pdu, val, sizeof(*val))) {
158 errcode = -EFAULT;
159 break;
160 }
161 }
162 break;
163 case 'w':{
164 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800165 __le16 le_val;
166 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500167 errcode = -EFAULT;
168 break;
169 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800170 *val = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500171 }
172 break;
173 case 'd':{
174 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800175 __le32 le_val;
176 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500177 errcode = -EFAULT;
178 break;
179 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800180 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500181 }
182 break;
183 case 'q':{
184 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800185 __le64 le_val;
186 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500187 errcode = -EFAULT;
188 break;
189 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800190 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500191 }
192 break;
193 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500194 char **sptr = va_arg(ap, char **);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500195 int16_t len;
196 int size;
197
Sripathi Kodi342fee12010-03-05 18:50:14 +0000198 errcode = p9pdu_readf(pdu, proto_version,
199 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500200 if (errcode)
201 break;
202
203 size = MAX(len, 0);
204
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500205 *sptr = kmalloc(size + 1, GFP_KERNEL);
206 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500207 errcode = -EFAULT;
208 break;
209 }
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500210 if (pdu_read(pdu, *sptr, size)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500211 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500212 kfree(*sptr);
213 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500214 } else
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500215 (*sptr)[size] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500216 }
217 break;
218 case 'Q':{
219 struct p9_qid *qid =
220 va_arg(ap, struct p9_qid *);
221
Sripathi Kodi342fee12010-03-05 18:50:14 +0000222 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500223 &qid->type, &qid->version,
224 &qid->path);
225 }
226 break;
227 case 'S':{
228 struct p9_wstat *stbuf =
229 va_arg(ap, struct p9_wstat *);
230
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500231 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500232 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500233 -1;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500234 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000235 p9pdu_readf(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500236 "wwdQdddqssss?sddd",
237 &stbuf->size, &stbuf->type,
238 &stbuf->dev, &stbuf->qid,
239 &stbuf->mode, &stbuf->atime,
240 &stbuf->mtime, &stbuf->length,
241 &stbuf->name, &stbuf->uid,
242 &stbuf->gid, &stbuf->muid,
243 &stbuf->extension,
244 &stbuf->n_uid, &stbuf->n_gid,
245 &stbuf->n_muid);
246 if (errcode)
247 p9stat_free(stbuf);
248 }
249 break;
250 case 'D':{
251 int32_t *count = va_arg(ap, int32_t *);
252 void **data = va_arg(ap, void **);
253
254 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000255 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500256 if (!errcode) {
257 *count =
258 MIN(*count,
259 pdu->size - pdu->offset);
260 *data = &pdu->sdata[pdu->offset];
261 }
262 }
263 break;
264 case 'T':{
265 int16_t *nwname = va_arg(ap, int16_t *);
266 char ***wnames = va_arg(ap, char ***);
267
Sripathi Kodi342fee12010-03-05 18:50:14 +0000268 errcode = p9pdu_readf(pdu, proto_version,
269 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500270 if (!errcode) {
271 *wnames =
272 kmalloc(sizeof(char *) * *nwname,
273 GFP_KERNEL);
274 if (!*wnames)
275 errcode = -ENOMEM;
276 }
277
278 if (!errcode) {
279 int i;
280
281 for (i = 0; i < *nwname; i++) {
282 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000283 p9pdu_readf(pdu,
284 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500285 "s",
286 &(*wnames)[i]);
287 if (errcode)
288 break;
289 }
290 }
291
292 if (errcode) {
293 if (*wnames) {
294 int i;
295
296 for (i = 0; i < *nwname; i++)
297 kfree((*wnames)[i]);
298 }
299 kfree(*wnames);
300 *wnames = NULL;
301 }
302 }
303 break;
304 case 'R':{
305 int16_t *nwqid = va_arg(ap, int16_t *);
306 struct p9_qid **wqids =
307 va_arg(ap, struct p9_qid **);
308
309 *wqids = NULL;
310
311 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000312 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500313 if (!errcode) {
314 *wqids =
315 kmalloc(*nwqid *
316 sizeof(struct p9_qid),
317 GFP_KERNEL);
318 if (*wqids == NULL)
319 errcode = -ENOMEM;
320 }
321
322 if (!errcode) {
323 int i;
324
325 for (i = 0; i < *nwqid; i++) {
326 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000327 p9pdu_readf(pdu,
328 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500329 "Q",
330 &(*wqids)[i]);
331 if (errcode)
332 break;
333 }
334 }
335
336 if (errcode) {
337 kfree(*wqids);
338 *wqids = NULL;
339 }
340 }
341 break;
342 case '?':
Sripathi Kodi342fee12010-03-05 18:50:14 +0000343 if (proto_version != p9_proto_2000u)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500344 return 0;
345 break;
346 default:
347 BUG();
348 break;
349 }
350
351 if (errcode)
352 break;
353 }
354
355 return errcode;
356}
357
358int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000359p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
360 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500361{
362 const char *ptr;
363 int errcode = 0;
364
365 for (ptr = fmt; *ptr; ptr++) {
366 switch (*ptr) {
367 case 'b':{
368 int8_t val = va_arg(ap, int);
369 if (pdu_write(pdu, &val, sizeof(val)))
370 errcode = -EFAULT;
371 }
372 break;
373 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800374 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500375 if (pdu_write(pdu, &val, sizeof(val)))
376 errcode = -EFAULT;
377 }
378 break;
379 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800380 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500381 if (pdu_write(pdu, &val, sizeof(val)))
382 errcode = -EFAULT;
383 }
384 break;
385 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800386 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500387 if (pdu_write(pdu, &val, sizeof(val)))
388 errcode = -EFAULT;
389 }
390 break;
391 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500392 const char *sptr = va_arg(ap, const char *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500393 int16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500394 if (sptr)
395 len = MIN(strlen(sptr), USHORT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500396
Sripathi Kodi342fee12010-03-05 18:50:14 +0000397 errcode = p9pdu_writef(pdu, proto_version,
398 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500399 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500400 errcode = -EFAULT;
401 }
402 break;
403 case 'Q':{
404 const struct p9_qid *qid =
405 va_arg(ap, const struct p9_qid *);
406 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000407 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500408 qid->type, qid->version,
409 qid->path);
410 } break;
411 case 'S':{
412 const struct p9_wstat *stbuf =
413 va_arg(ap, const struct p9_wstat *);
414 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000415 p9pdu_writef(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500416 "wwdQdddqssss?sddd",
417 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500418 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500419 stbuf->mode, stbuf->atime,
420 stbuf->mtime, stbuf->length,
421 stbuf->name, stbuf->uid,
422 stbuf->gid, stbuf->muid,
423 stbuf->extension, stbuf->n_uid,
424 stbuf->n_gid, stbuf->n_muid);
425 } break;
426 case 'D':{
427 int32_t count = va_arg(ap, int32_t);
428 const void *data = va_arg(ap, const void *);
429
Sripathi Kodi342fee12010-03-05 18:50:14 +0000430 errcode = p9pdu_writef(pdu, proto_version, "d",
431 count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500432 if (!errcode && pdu_write(pdu, data, count))
433 errcode = -EFAULT;
434 }
435 break;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500436 case 'U':{
437 int32_t count = va_arg(ap, int32_t);
438 const char __user *udata =
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500439 va_arg(ap, const void __user *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000440 errcode = p9pdu_writef(pdu, proto_version, "d",
441 count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500442 if (!errcode && pdu_write_u(pdu, udata, count))
443 errcode = -EFAULT;
444 }
445 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500446 case 'T':{
447 int16_t nwname = va_arg(ap, int);
448 const char **wnames = va_arg(ap, const char **);
449
Sripathi Kodi342fee12010-03-05 18:50:14 +0000450 errcode = p9pdu_writef(pdu, proto_version, "w",
451 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500452 if (!errcode) {
453 int i;
454
455 for (i = 0; i < nwname; i++) {
456 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000457 p9pdu_writef(pdu,
458 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500459 "s",
460 wnames[i]);
461 if (errcode)
462 break;
463 }
464 }
465 }
466 break;
467 case 'R':{
468 int16_t nwqid = va_arg(ap, int);
469 struct p9_qid *wqids =
470 va_arg(ap, struct p9_qid *);
471
Sripathi Kodi342fee12010-03-05 18:50:14 +0000472 errcode = p9pdu_writef(pdu, proto_version, "w",
473 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500474 if (!errcode) {
475 int i;
476
477 for (i = 0; i < nwqid; i++) {
478 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000479 p9pdu_writef(pdu,
480 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500481 "Q",
482 &wqids[i]);
483 if (errcode)
484 break;
485 }
486 }
487 }
488 break;
489 case '?':
Sripathi Kodi342fee12010-03-05 18:50:14 +0000490 if (proto_version != p9_proto_2000u)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500491 return 0;
492 break;
493 default:
494 BUG();
495 break;
496 }
497
498 if (errcode)
499 break;
500 }
501
502 return errcode;
503}
504
Sripathi Kodi342fee12010-03-05 18:50:14 +0000505int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500506{
507 va_list ap;
508 int ret;
509
510 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000511 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500512 va_end(ap);
513
514 return ret;
515}
516
517static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000518p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500519{
520 va_list ap;
521 int ret;
522
523 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000524 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500525 va_end(ap);
526
527 return ret;
528}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500529
Sripathi Kodi342fee12010-03-05 18:50:14 +0000530int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500531{
532 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500533 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500534
535 fake_pdu.size = len;
536 fake_pdu.capacity = len;
537 fake_pdu.sdata = buf;
538 fake_pdu.offset = 0;
539
Sripathi Kodi342fee12010-03-05 18:50:14 +0000540 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500541 if (ret) {
542 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
543 p9pdu_dump(1, &fake_pdu);
544 }
545
546 return ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500547}
548EXPORT_SYMBOL(p9stat_read);
549
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500550int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
551{
552 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
553}
554
555int p9pdu_finalize(struct p9_fcall *pdu)
556{
557 int size = pdu->size;
558 int err;
559
560 pdu->size = 0;
561 err = p9pdu_writef(pdu, 0, "d", size);
562 pdu->size = size;
563
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500564#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500565 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500566 p9pdu_dump(0, pdu);
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500567#endif
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500568
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500569 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
570 pdu->id, pdu->tag);
571
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500572 return err;
573}
574
575void p9pdu_reset(struct p9_fcall *pdu)
576{
577 pdu->offset = 0;
578 pdu->size = 0;
579}