summaryrefslogtreecommitdiffstats
path: root/net/9p/conv.c
blob: 37451178e7618013eb38ab7c2f9fae79308b3391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
 * net/9p/conv.c
 *
 * 9P protocol conversion functions
 *
 *  Copyright (C) 2004, 2005 by Latchesar Ionkov <lucho@ionkov.net>
 *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
 *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to:
 *  Free Software Foundation
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02111-1301  USA
 *
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/idr.h>
#include <linux/uaccess.h>
#include <net/9p/9p.h>

/*
 * Buffer to help with string parsing
 */
struct cbuf {
	unsigned char *sp;
	unsigned char *p;
	unsigned char *ep;
};

static inline void buf_init(struct cbuf *buf, void *data, int datalen)
{
	buf->sp = buf->p = data;
	buf->ep = data + datalen;
}

static inline int buf_check_overflow(struct cbuf *buf)
{
	return buf->p > buf->ep;
}

static int buf_check_size(struct cbuf *buf, int len)
{
	if (buf->p + len > buf->ep) {
		if (buf->p < buf->ep) {
			P9_EPRINTK(KERN_ERR,
				"buffer overflow: want %d has %d\n", len,
				(int)(buf->ep - buf->p));
			dump_stack();
			buf->p = buf->ep + 1;
		}

		return 0;
	}

	return 1;
}

static void *buf_alloc(struct cbuf *buf, int len)
{
	void *ret = NULL;

	if (buf_check_size(buf, len)) {
		ret = buf->p;
		buf->p += len;
	}

	return ret;
}

static void buf_put_int8(struct cbuf *buf, u8 val)
{
	if (buf_check_size(buf, 1)) {
		buf->p[0] = val;
		buf->p++;
	}
}

static void buf_put_int16(struct cbuf *buf, u16 val)
{
	if (buf_check_size(buf, 2)) {
		*(__le16 *) buf->p = cpu_to_le16(val);
		buf->p += 2;
	}
}

static void buf_put_int32(struct cbuf *buf, u32 val)
{
	if (buf_check_size(buf, 4)) {
		*(__le32 *)buf->p = cpu_to_le32(val);
		buf->p += 4;
	}
}

static void buf_put_int64(struct cbuf *buf, u64 val)
{
	if (buf_check_size(buf, 8)) {
		*(__le64 *)buf->p = cpu_to_le64(val);
		buf->p += 8;
	}
}

static char *buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
{
	char *ret;

	ret = NULL;
	if (buf_check_size(buf, slen + 2)) {
		buf_put_int16(buf, slen);
		ret = buf->p;
		memcpy(buf->p, s, slen);
		buf->p += slen;
	}

	return ret;
}

static inline void buf_put_string(struct cbuf *buf, const char *s)
{
	buf_put_stringn(buf, s, strlen(s));
}

static u8 buf_get_int8(struct cbuf *buf)
{
	u8 ret = 0;

	if (buf_check_size(buf, 1)) {
		ret = buf->p[0];
		buf->p++;
	}

	return ret;
}

static u16 buf_get_int16(struct cbuf *buf)
{
	u16 ret = 0;

	if (buf_check_size(buf, 2)) {
		ret = le16_to_cpu(*(__le16 *)buf->p);
		buf->p += 2;
	}

	return ret;
}

static u32 buf_get_int32(struct cbuf *buf)
{
	u32 ret = 0;

	if (buf_check_size(buf, 4)) {
		ret = le32_to_cpu(*(__le32 *)buf->p);
		buf->p += 4;
	}

	return ret;
}

static u64 buf_get_int64(struct cbuf *buf)
{
	u64 ret = 0;

	if (buf_check_size(buf, 8)) {
		ret = le64_to_cpu(*(__le64 *)buf->p);
		buf->p += 8;
	}

	return ret;
}

static void buf_get_str(struct cbuf *buf, struct p9_str *vstr)
{
	vstr->len = buf_get_int16(buf);
	if (!buf_check_overflow(buf) && buf_check_size(buf, vstr->len)) {
		vstr->str = buf->p;
		buf->p += vstr->len;
	} else {
		vstr->len = 0;
		vstr->str = NULL;
	}
}

static void buf_get_qid(struct cbuf *bufp, struct p9_qid *qid)
{
	qid->type = buf_get_int8(bufp);
	qid->version = buf_get_int32(bufp);
	qid->path = buf_get_int64(bufp);
}

/**
 * p9_size_wstat - calculate the size of a variable length stat struct
 * @stat: metadata (stat) structure
 * @dotu: non-zero if 9P2000.u
 *
 */

static int p9_size_wstat(struct p9_wstat *wstat, int dotu)
{
	int size = 0;

	if (wstat == NULL) {
		P9_EPRINTK(KERN_ERR, "p9_size_stat: got a NULL stat pointer\n");
		return 0;
	}

	size =			/* 2 + *//* size[2] */
	    2 +			/* type[2] */
	    4 +			/* dev[4] */
	    1 +			/* qid.type[1] */
	    4 +			/* qid.vers[4] */
	    8 +			/* qid.path[8] */
	    4 +			/* mode[4] */
	    4 +			/* atime[4] */
	    4 +			/* mtime[4] */
	    8 +			/* length[8] */
	    8;			/* minimum sum of string lengths */

	if (wstat->name)
		size += strlen(wstat->name);
	if (wstat->uid)
		size += strlen(wstat->uid);
	if (wstat->gid)
		size += strlen(wstat->gid);
	if (wstat->muid)
		size += strlen(wstat->muid);

	if (dotu) {
		size += 4 +	/* n_uid[4] */
		    4 +		/* n_gid[4] */
		    4 +		/* n_muid[4] */
		    2;		/* string length of extension[4] */
		if (wstat->extension)
			size += strlen(wstat->extension);
	}

	return size;
}

/**
 * buf_get_stat - safely decode a recieved metadata (stat) structure
 * @bufp: buffer to deserialize
 * @stat: metadata (stat) structure
 * @dotu: non-zero if 9P2000.u
 *
 */

static void
buf_get_stat(struct cbuf *bufp, struct p9_stat *stat, int dotu)
{
	stat->size = buf_get_int16(bufp);
	stat->type = buf_get_int16(bufp);
	stat->dev = buf_get_int32(bufp);
	stat->qid.type = buf_get_int8(bufp);
	stat->qid.version = buf_get_int32(bufp);
	stat->qid.path = buf_get_int64(bufp);
	stat->mode = buf_get_int32(bufp);
	stat->atime = buf_get_int32(bufp);
	stat->mtime = buf_get_int32(bufp);
	stat->length = buf_get_int64(bufp);
	buf_get_str(bufp, &stat->name);
	buf_get_str(bufp, &stat->uid);
	buf_get_str(bufp, &stat->gid);
	buf_get_str(bufp, &stat->muid);

	if (dotu) {
		buf_get_str(bufp, &stat->extension);
		stat->n_uid = buf_get_int32(bufp);
		stat->n_gid = buf_get_int32(bufp);
		stat->n_muid = buf_get_int32(bufp);
	}
}

/**
 * p9_deserialize_stat - decode a received metadata structure
 * @buf: buffer to deserialize
 * @buflen: length of received buffer
 * @stat: metadata structure to decode into
 * @dotu: non-zero if 9P2000.u
 *
 * Note: stat will point to the buf region.
 */

int
p9_deserialize_stat(void *buf, u32 buflen, struct p9_stat *stat,
		int dotu)
{
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;
	unsigned char *p;

	buf_init(bufp, buf, buflen);
	p = bufp->p;
	buf_get_stat(bufp, stat, dotu);

	if (buf_check_overflow(bufp))
		return 0;
	else
		return bufp->p - p;
}
EXPORT_SYMBOL(p9_deserialize_stat);

/**
 * deserialize_fcall - unmarshal a response
 * @buf: recieved buffer
 * @buflen: length of received buffer
 * @rcall: fcall structure to populate
 * @rcalllen: length of fcall structure to populate
 * @dotu: non-zero if 9P2000.u
 *
 */

int
p9_deserialize_fcall(void *buf, u32 buflen, struct p9_fcall *rcall,
		       int dotu)
{

	struct cbuf buffer;
	struct cbuf *bufp = &buffer;
	int i = 0;

	buf_init(bufp, buf, buflen);

	rcall->size = buf_get_int32(bufp);
	rcall->id = buf_get_int8(bufp);
	rcall->tag = buf_get_int16(bufp);

	P9_DPRINTK(P9_DEBUG_CONV, "size %d id %d tag %d\n", rcall->size,
							rcall->id, rcall->tag);

	switch (rcall->id) {
	default:
		P9_EPRINTK(KERN_ERR, "unknown message type: %d\n", rcall->id);
		return -EPROTO;
	case P9_RVERSION:
		rcall->params.rversion.msize = buf_get_int32(bufp);
		buf_get_str(bufp, &rcall->params.rversion.version);
		break;
	case P9_RFLUSH:
		break;
	case P9_RATTACH:
		rcall->params.rattach.qid.type = buf_get_int8(bufp);
		rcall->params.rattach.qid.version = buf_get_int32(bufp);
		rcall->params.rattach.qid.path = buf_get_int64(bufp);
		break;
	case P9_RWALK:
		rcall->params.rwalk.nwqid = buf_get_int16(bufp);
		if (rcall->params.rwalk.nwqid > P9_MAXWELEM) {
			P9_EPRINTK(KERN_ERR,
					"Rwalk with more than %d qids: %d\n",
					P9_MAXWELEM, rcall->params.rwalk.nwqid);
			return -EPROTO;
		}

		for (i = 0; i < rcall->params.rwalk.nwqid; i++)
			buf_get_qid(bufp, &rcall->params.rwalk.wqids[i]);
		break;
	case P9_ROPEN:
		buf_get_qid(bufp, &rcall->params.ropen.qid);
		rcall->params.ropen.iounit = buf_get_int32(bufp);
		break;
	case P9_RCREATE:
		buf_get_qid(bufp, &rcall->params.rcreate.qid);
		rcall->params.rcreate.iounit = buf_get_int32(bufp);
		break;
	case P9_RREAD:
		rcall->params.rread.count = buf_get_int32(bufp);
		rcall->params.rread.data = bufp->p;
		buf_check_size(bufp, rcall->params.rread.count);
		break;
	case P9_RWRITE:
		rcall->params.rwrite.count = buf_get_int32(bufp);
		break;
	case P9_RCLUNK:
		break;
	case P9_RREMOVE:
		break;
	case P9_RSTAT:
		buf_get_int16(bufp);
		buf_get_stat(bufp, &rcall->params.rstat.stat, dotu);
		break;
	case P9_RWSTAT:
		break;
	case P9_RERROR:
		buf_get_str(bufp, &rcall->params.rerror.error);
		if (dotu)
			rcall->params.rerror.errno = buf_get_int16(bufp);
		break;
	}

	if (buf_check_overflow(bufp)) {
		P9_DPRINTK(P9_DEBUG_ERROR, "buffer overflow\n");
		return -EIO;
	}

	return bufp->p - bufp->sp;
}
EXPORT_SYMBOL(p9_deserialize_fcall);

static inline void p9_put_int8(struct cbuf *bufp, u8 val, u8 * p)
{
	*p = val;
	buf_put_int8(bufp, val);
}

static inline void p9_put_int16(struct cbuf *bufp, u16 val, u16 * p)
{
	*p = val;
	buf_put_int16(bufp, val);
}

static inline void p9_put_int32(struct cbuf *bufp, u32 val, u32 * p)
{
	*p = val;
	buf_put_int32(bufp, val);
}

static inline void p9_put_int64(struct cbuf *bufp, u64 val, u64 * p)
{
	*p = val;
	buf_put_int64(bufp, val);
}

static void
p9_put_str(struct cbuf *bufp, char *data, struct p9_str *str)
{
	int len;
	char *s;

	if (data)
		len = strlen(data);
	else
		len = 0;

	s = buf_put_stringn(bufp, data, len);
	if (str) {
		str->len = len;
		str->str = s;
	}
}

static int
p9_put_data(struct cbuf *bufp, const char *data, int count,
		   unsigned char **pdata)
{
	*pdata = buf_alloc(bufp, count);
	memmove(*pdata, data, count);
	return count;
}

static int
p9_put_user_data(struct cbuf *bufp, const char __user *data, int count,
		   unsigned char **pdata)
{
	*pdata = buf_alloc(bufp, count);
	return copy_from_user(*pdata, data, count);
}

static void
p9_put_wstat(struct cbuf *bufp, struct p9_wstat *wstat,
	       struct p9_stat *stat, int statsz, int dotu)
{
	p9_put_int16(bufp, statsz, &stat->size);
	p9_put_int16(bufp, wstat->type, &stat->type);
	p9_put_int32(bufp, wstat->dev, &stat->dev);
	p9_put_int8(bufp, wstat->qid.type, &stat->qid.type);
	p9_put_int32(bufp, wstat->qid.version, &stat->qid.version);
	p9_put_int64(bufp, wstat->qid.path, &stat->qid.path);
	p9_put_int32(bufp, wstat->mode, &stat->mode);
	p9_put_int32(bufp, wstat->atime, &stat->atime);
	p9_put_int32(bufp, wstat->mtime, &stat->mtime);
	p9_put_int64(bufp, wstat->length, &stat->length);

	p9_put_str(bufp, wstat->name, &stat->name);
	p9_put_str(bufp, wstat->uid, &stat->uid);
	p9_put_str(bufp, wstat->gid, &stat->gid);
	p9_put_str(bufp, wstat->muid, &stat->muid);

	if (dotu) {
		p9_put_str(bufp, wstat->extension, &stat->extension);
		p9_put_int32(bufp, wstat->n_uid, &stat->n_uid);
		p9_put_int32(bufp, wstat->n_gid, &stat->n_gid);
		p9_put_int32(bufp, wstat->n_muid, &stat->n_muid);
	}
}

static struct p9_fcall *
p9_create_common(struct cbuf *bufp, u32 size, u8 id)
{
	struct p9_fcall *fc;

	size += 4 + 1 + 2;	/* size[4] id[1] tag[2] */
	fc = kmalloc(sizeof(struct p9_fcall) + size, GFP_KERNEL);
	if (!fc)
		return ERR_PTR(-ENOMEM);

	fc->sdata = (char *)fc + sizeof(*fc);

	buf_init(bufp, (char *)fc->sdata, size);
	p9_put_int32(bufp, size, &fc->size);
	p9_put_int8(bufp, id, &fc->id);
	p9_put_int16(bufp, P9_NOTAG, &fc->tag);

	return fc;
}

void p9_set_tag(struct p9_fcall *fc, u16 tag)
{
	fc->tag = tag;
	*(__le16 *) (fc->sdata + 5) = cpu_to_le16(tag);
}
EXPORT_SYMBOL(p9_set_tag);

struct p9_fcall *p9_create_tversion(u32 msize, char *version)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 4 + 2 + strlen(version);	/* msize[4] version[s] */
	fc = p9_create_common(bufp, size, P9_TVERSION);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, msize, &fc->params.tversion.msize);
	p9_put_str(bufp, version, &fc->params.tversion.version);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tversion);

struct p9_fcall *p9_create_tauth(u32 afid, char *uname, char *aname)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	/* afid[4] uname[s] aname[s] */
	size = 4 + 2 + strlen(uname) + 2 + strlen(aname);
	fc = p9_create_common(bufp, size, P9_TAUTH);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, afid, &fc->params.tauth.afid);
	p9_put_str(bufp, uname, &fc->params.tauth.uname);
	p9_put_str(bufp, aname, &fc->params.tauth.aname);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tauth);

struct p9_fcall *
p9_create_tattach(u32 fid, u32 afid, char *uname, char *aname)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	/* fid[4] afid[4] uname[s] aname[s] */
	size = 4 + 4 + 2 + strlen(uname) + 2 + strlen(aname);
	fc = p9_create_common(bufp, size, P9_TATTACH);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.tattach.fid);
	p9_put_int32(bufp, afid, &fc->params.tattach.afid);
	p9_put_str(bufp, uname, &fc->params.tattach.uname);
	p9_put_str(bufp, aname, &fc->params.tattach.aname);

error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tattach);

struct p9_fcall *p9_create_tflush(u16 oldtag)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 2;		/* oldtag[2] */
	fc = p9_create_common(bufp, size, P9_TFLUSH);
	if (IS_ERR(fc))
		goto error;

	p9_put_int16(bufp, oldtag, &fc->params.tflush.oldtag);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tflush);

struct p9_fcall *p9_create_twalk(u32 fid, u32 newfid, u16 nwname,
				     char **wnames)
{
	int i, size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	if (nwname > P9_MAXWELEM) {
		P9_DPRINTK(P9_DEBUG_ERROR, "nwname > %d\n", P9_MAXWELEM);
		return NULL;
	}

	size = 4 + 4 + 2;	/* fid[4] newfid[4] nwname[2] ... */
	for (i = 0; i < nwname; i++) {
		size += 2 + strlen(wnames[i]);	/* wname[s] */
	}

	fc = p9_create_common(bufp, size, P9_TWALK);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.twalk.fid);
	p9_put_int32(bufp, newfid, &fc->params.twalk.newfid);
	p9_put_int16(bufp, nwname, &fc->params.twalk.nwname);
	for (i = 0; i < nwname; i++) {
		p9_put_str(bufp, wnames[i], &fc->params.twalk.wnames[i]);
	}

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_twalk);

struct p9_fcall *p9_create_topen(u32 fid, u8 mode)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 4 + 1;		/* fid[4] mode[1] */
	fc = p9_create_common(bufp, size, P9_TOPEN);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.topen.fid);
	p9_put_int8(bufp, mode, &fc->params.topen.mode);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_topen);

struct p9_fcall *p9_create_tcreate(u32 fid, char *name, u32 perm, u8 mode,
	char *extension, int dotu)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	/* fid[4] name[s] perm[4] mode[1] */
	size = 4 + 2 + strlen(name) + 4 + 1;
	if (dotu) {
		size += 2 +			/* extension[s] */
		    (extension == NULL ? 0 : strlen(extension));
	}

	fc = p9_create_common(bufp, size, P9_TCREATE);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.tcreate.fid);
	p9_put_str(bufp, name, &fc->params.tcreate.name);
	p9_put_int32(bufp, perm, &fc->params.tcreate.perm);
	p9_put_int8(bufp, mode, &fc->params.tcreate.mode);
	if (dotu)
		p9_put_str(bufp, extension, &fc->params.tcreate.extension);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tcreate);

struct p9_fcall *p9_create_tread(u32 fid, u64 offset, u32 count)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 4 + 8 + 4;	/* fid[4] offset[8] count[4] */
	fc = p9_create_common(bufp, size, P9_TREAD);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.tread.fid);
	p9_put_int64(bufp, offset, &fc->params.tread.offset);
	p9_put_int32(bufp, count, &fc->params.tread.count);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tread);

struct p9_fcall *p9_create_twrite(u32 fid, u64 offset, u32 count,
				      const char *data)
{
	int size, err;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	/* fid[4] offset[8] count[4] data[count] */
	size = 4 + 8 + 4 + count;
	fc = p9_create_common(bufp, size, P9_TWRITE);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.twrite.fid);
	p9_put_int64(bufp, offset, &fc->params.twrite.offset);
	p9_put_int32(bufp, count, &fc->params.twrite.count);
	err = p9_put_data(bufp, data, count, &fc->params.twrite.data);
	if (err) {
		kfree(fc);
		fc = ERR_PTR(err);
	}

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_twrite);

struct p9_fcall *p9_create_twrite_u(u32 fid, u64 offset, u32 count,
				      const char __user *data)
{
	int size, err;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	/* fid[4] offset[8] count[4] data[count] */
	size = 4 + 8 + 4 + count;
	fc = p9_create_common(bufp, size, P9_TWRITE);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.twrite.fid);
	p9_put_int64(bufp, offset, &fc->params.twrite.offset);
	p9_put_int32(bufp, count, &fc->params.twrite.count);
	err = p9_put_user_data(bufp, data, count, &fc->params.twrite.data);
	if (err) {
		kfree(fc);
		fc = ERR_PTR(err);
	}

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_twrite_u);

struct p9_fcall *p9_create_tclunk(u32 fid)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 4;		/* fid[4] */
	fc = p9_create_common(bufp, size, P9_TCLUNK);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.tclunk.fid);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tclunk);

struct p9_fcall *p9_create_tremove(u32 fid)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 4;		/* fid[4] */
	fc = p9_create_common(bufp, size, P9_TREMOVE);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.tremove.fid);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tremove);

struct p9_fcall *p9_create_tstat(u32 fid)
{
	int size;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	size = 4;		/* fid[4] */
	fc = p9_create_common(bufp, size, P9_TSTAT);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.tstat.fid);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_tstat);

struct p9_fcall *p9_create_twstat(u32 fid, struct p9_wstat *wstat,
				      int dotu)
{
	int size, statsz;
	struct p9_fcall *fc;
	struct cbuf buffer;
	struct cbuf *bufp = &buffer;

	statsz = p9_size_wstat(wstat, dotu);
	size = 4 + 2 + 2 + statsz;	/* fid[4] stat[n] */
	fc = p9_create_common(bufp, size, P9_TWSTAT);
	if (IS_ERR(fc))
		goto error;

	p9_put_int32(bufp, fid, &fc->params.twstat.fid);
	buf_put_int16(bufp, statsz + 2);
	p9_put_wstat(bufp, wstat, &fc->params.twstat.stat, statsz, dotu);

	if (buf_check_overflow(bufp)) {
		kfree(fc);
		fc = ERR_PTR(-ENOMEM);
	}
error:
	return fc;
}
EXPORT_SYMBOL(p9_create_twstat);
OpenPOWER on IntegriCloud