Source

o9fs / o9fs_vnops.c

The default branch has multiple heads

  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
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/dirent.h>
#include <sys/proc.h>
#include <sys/filedesc.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/rwlock.h>
#include <sys/dirent.h>
#include <sys/mount.h>
#include <sys/malloc.h>
#include <sys/namei.h>
#include <sys/syscallargs.h>

#include "o9fs.h"
#include "o9fs_extern.h"

enum{
	Debug = 0,
};

int o9fs_open(void *);
int o9fs_close(void *);
int o9fs_lookup(void *);
int o9fs_create(void *);
int o9fs_access(void *);
int o9fs_getattr(void *);
int o9fs_setattr(void *);
int o9fs_read(void *);
int o9fs_write(void *);
int o9fs_mkdir(void *);
int o9fs_readdir(void *);
int o9fs_remove(void *);
int o9fs_inactive(void *);
int o9fs_reclaim(void *);

int (**o9fs_vnodeop_p)(void *);

struct vops o9fs_vops = {
	.vop_lock = vop_generic_lock,
	.vop_unlock = vop_generic_unlock,
	.vop_islocked = vop_generic_islocked,
	.vop_abortop = vop_generic_abortop,
	.vop_access = o9fs_access,
	.vop_advlock = eopnotsupp,
	.vop_bmap = eopnotsupp,
	.vop_bwrite = eopnotsupp,
	.vop_close = o9fs_close,
	.vop_create = o9fs_create,
	.vop_fsync = nullop,
	.vop_getattr = o9fs_getattr,
	.vop_inactive = o9fs_inactive,
	.vop_ioctl = (int (*)(void *))enoioctl,
	.vop_link = eopnotsupp,
	.vop_lookup = o9fs_lookup,
	.vop_mknod = eopnotsupp, 
	.vop_open = o9fs_open,
	.vop_pathconf = eopnotsupp,
	.vop_poll = eopnotsupp,
	.vop_print = eopnotsupp,
	.vop_read = o9fs_read,
	.vop_readdir = o9fs_readdir,
	.vop_readlink = eopnotsupp,
	.vop_reallocblks = eopnotsupp,
	.vop_reclaim = o9fs_reclaim,
	.vop_remove = o9fs_remove,
	.vop_rename = eopnotsupp,
	.vop_revoke = vop_generic_revoke,
	.vop_mkdir = o9fs_mkdir,
	.vop_rmdir = o9fs_remove,
	.vop_setattr = o9fs_setattr,
	.vop_strategy = eopnotsupp,
	.vop_symlink = eopnotsupp,
	.vop_write = o9fs_write,
	.vop_kqfilter = eopnotsupp,
};
	
	
int 
o9fs_open(void *v)
{
	struct vop_open_args *ap;
	struct vnode *vp;
	struct proc *p;
	struct o9fs *fs;
	struct o9fid *f, *nf;
	DIN();

	ap = v;
	vp = ap->a_vp;
	p = ap->a_p;
	fs = VFSTOO9FS(vp->v_mount);
	f = VTO9(vp);

	printvp(vp);

	/* BUG: old fid leakage */
	nf = o9fs_walk(fs, f, NULL, NULL);
	if (nf == NULL) {
		DRET();
		return -1;
	}

	if (o9fs_opencreate(fs, nf, O9FS_TOPEN, ap->a_mode, 0, 0) < 0) {
		DBG("failed open\n");
		o9fs_putfid(fs, nf);
		DRET();
		return -1;
	}

	nf->parent = f;
	vp->v_data = nf; /* walk has set other properties */

out:
	DRET();
	return 0;
}

int
o9fs_close(void *v)
{
	struct vop_close_args *ap;
	struct vnode *vp;
	struct o9fid *f;
	struct o9fs *fs;
	DIN();

	ap = v;
	vp = ap->a_vp;

	printvp(vp);
	DRET();
	return 0;
}

/* TODO */
int
o9fs_access(void *v)
{
	struct vop_access_args *ap;
	struct vnode *vp;
	mode_t a_mode;
	struct ucred *cred;
	struct proc *p;

	ap = v;
	vp = ap->a_vp;
	a_mode = ap->a_mode;
	cred = ap->a_cred;
	p = ap->a_p;

	return 0;
}

int
o9fs_create(void *v)
{
	struct vop_create_args *ap;
	struct vnode *dvp, **vpp;
	struct componentname *cnp;
	struct vattr *vap;
	struct o9fid *f, *nf;
	struct o9fs *fs;
	int error;
	DIN();

	ap = v;
	dvp = ap->a_dvp;
	cnp = ap->a_cnp;
	vap = ap->a_vap;
	vpp = ap->a_vpp;

	f = VTO9(dvp);
	*vpp = NULL;

	fs = VFSTOO9FS(dvp->v_mount);
	if (f == NULL) {
		DRET();
		return -1;
	}

	/* BUG: old fid leakage */
	nf = o9fs_walk(fs, f, NULL, NULL);
	if (nf == NULL) {
		o9fs_putfid(fs, nf);
		DRET();
		return -1;
	}

	if (o9fs_opencreate(fs, nf, O9FS_TCREATE, 0, vap->va_mode, cnp->cn_nameptr) < 0) {
		o9fs_putfid(fs, nf);
		DRET();
		return -1;
	}
	o9fs_clunkremove(fs, nf, O9FS_TCLUNK);

	/* walk from parent dir to get an unopened fid, break create+open atomicity of 9P */
	nf = o9fs_walk(fs, f, NULL, cnp->cn_nameptr);
	if (nf == NULL) {
		o9fs_putfid(fs, nf);
		DRET();
		return -1;
	}
	
	error = o9fs_allocvp(dvp->v_mount, nf, vpp, 0);
	vput(dvp);
	DRET();
	return error;
}

int
o9fs_mkdir(void *v)
{
	struct vop_create_args *ap;
	struct vnode *dvp, **vpp;
	struct componentname *cnp;
	struct vattr *vap;
	int error;

	ap = v;
	dvp = ap->a_dvp;
	cnp = ap->a_cnp;
	vap = ap->a_vap;
	vpp = ap->a_vpp;

	vap->va_mode |= S_IFDIR;
	error = VOP_CREATE(dvp, vpp, cnp, vap);
	return error;
}

int
o9fs_read(void *v)
{
	struct vop_read_args *ap;
	struct vnode *vp;
	struct uio *uio;
	struct o9fid *f;
	struct o9fs *fs;
	uint32_t n;

	ap = v;
	vp = ap->a_vp;
	uio = ap->a_uio;
	f = VTO9(vp);
	fs = VFSTOO9FS(vp->v_mount);

	if (uio->uio_offset < 0)
		return EINVAL;

	if (uio->uio_resid == 0)
		return 0;

	n = o9fs_rdwr(fs, f, O9FS_TREAD, o9fs_sanelen(fs, uio->uio_resid), uio->uio_offset);
	if (n > 0)
		return uiomove(fs->inbuf + Minhd + 4, n, uio);
	return n;
}

static long
dirpackage(u_char *buf, long ts, struct o9stat **d)
{
	char *s;
	long ss, i, n, nn, m;

	*d = NULL;
	if (ts <= 0)
		return 0;

	/*
	 * first find number of all stats, check they look like stats, & size all associated strings
	 */
	ss = 0;
	n = 0;
	for (i = 0; i < ts; i += m) {
		m = O9FS_BIT16SZ + O9FS_GBIT16(&buf[i]);
		if (o9fs_statcheck(&buf[i], m) < 0)
			break;
		ss += m;
		n++;
	}

	if (i != ts)
		return -1;

	*d = malloc(n * sizeof(struct o9stat) + ss, M_O9FS, M_WAITOK);

	/*
	 * then convert all buffers
	 */
	s = (char*)*d + n * sizeof(struct o9stat);
	nn = 0;
	for (i = 0; i < ts; i += m) {
		m = O9FS_BIT16SZ + O9FS_GBIT16((u_char*)&buf[i]);
		if (nn >= n || o9fs_convM2D(&buf[i], m, *d + nn, s) != m) {
			free(*d, M_O9FS);
			*d = NULL;
			return -1;
		}
		nn++;
		s += m;
	}

	return nn;
}

int
o9fs_readdir(void *v)
{	
	struct vop_readdir_args *ap;
	struct vnode *vp;
	struct uio *uio;
	struct o9fid *f;
	struct o9fs *fs;
	struct o9stat *stat;
	struct dirent d;
	u_char *buf, *nbuf;
	uint32_t n, ts;
	int error, i;
	int64_t len;
	DIN();

	ap = v;
	vp = ap->a_vp;
	uio = ap->a_uio;
	fs = VFSTOO9FS(vp->v_mount);
	f = VTO9(vp);
	error = 0;

	if (vp->v_type != VDIR) {
		DRET();
		return ENOTDIR;
	}
	if (uio->uio_resid == 0) {
		DRET();
		return 0;
	}

	len = uio->uio_resid;
	ts = n = 0;
	buf = malloc(O9FS_DIRMAX, M_O9FS, M_WAITOK);

	for (;;) {
		len = o9fs_sanelen(fs, len);
		nbuf = o9fsrealloc(buf, ts+O9FS_DIRMAX-n, ts+O9FS_DIRMAX);
		buf = nbuf;
		n = o9fs_rdwr(fs, f, O9FS_TREAD, len, f->offset);
		if (n <= 0)
			break;
		memcpy(buf + ts, fs->inbuf + Minhd + 4, n);
		f->offset += n;
		ts += n;
		len -= n;
	}

	if (ts >= 0) {
		ts = dirpackage(buf, ts, &stat);
		if (ts < 0)
			printf("malformed directory contents\n");
	}
	free(buf, M_O9FS);

	if (ts == 0 && n < 0) {
		DRET();
		return -1;
	}

	for (i = 0; i < ts; i++) {
		d.d_fileno = (uint32_t)stat[i].qid.path;
		d.d_type = vp->v_type == VDIR ? DT_DIR : DT_REG;
		d.d_namlen = strlen(stat[i].name);
		memcpy(d.d_name, stat[i].name, d.d_namlen);
		d.d_name[d.d_namlen] = '\0';
		d.d_reclen = DIRENT_SIZE(&d);
		error = uiomove(&d, d.d_reclen, uio);
		if (error) {
			DBG("uiomove error\n");
			DRET();
			return -1;
		}
	}
	DRET();
	return 0;
}

int
o9fs_remove(void *v)
{
	struct vop_remove_args *ap;
	struct vnode *vp;
	DIN();

	ap = v;
	vp = ap->a_vp;

	o9fs_clunkremove(VFSTOO9FS(vp->v_mount), VTO9(vp), O9FS_TREMOVE);
	DRET();
	return 0;
}

int
o9fs_write(void *v)
{	
	struct vop_read_args *ap;
	struct vnode *vp;
	struct uio *uio;
	struct o9fid *f;
	struct o9fs *fs;
	int ioflag, error;
	uint32_t n;
	off_t offset;
	DIN();

	ap = v;
	vp = ap->a_vp;
	uio = ap->a_uio;
	ioflag = ap->a_ioflag;
	f = VTO9(vp);
	fs = VFSTOO9FS(vp->v_mount);
	error = n = 0;

	if (uio->uio_offset < 0 || vp->v_type != VREG) {
		DRET();
		return EINVAL;
	}

	if (uio->uio_resid == 0) {
		DRET();
		return 0;
	}

	offset = uio->uio_offset;
	if (ioflag & IO_APPEND) {
		struct stat st;
		if(vn_stat(vp, &st, curproc) == 0)
			offset = st.st_size;
	}

	n = o9fs_sanelen(fs, uio->uio_resid);
	error = uiomove(fs->outbuf + Minhd + 4 + 8 + 4, n, uio);
	if (error) {
		DRET();
		return -1;
	}

	n = o9fs_rdwr(fs, f, O9FS_TWRITE, n, offset);
	if (n < 0) {
		DRET();
		return -1;
	}

	f->offset = offset + n;
	DRET();
	return 0;
}

int
o9fs_lookup(void *v)
{
	struct vop_lookup_args *ap;
	struct componentname *cnp;
	struct vnode **vpp, *dvp;
	struct proc *p;
	struct o9fs *fs;
	struct o9fid *f, *parf, *nf;
	int flags, op, islast, error;
	long n;
	char *path;
	
	DIN();
	ap = v;
	dvp = ap->a_dvp;			/* parent dir where to look */
	vpp = ap->a_vpp;			/* resulting vnode */
	cnp = ap->a_cnp;			/* path component name */
	flags = cnp->cn_flags;		/* lookup options */
	p = curproc;
	op = cnp->cn_nameiop;		/* lookup operation */
	islast = flags & ISLASTCN;

	fs = VFSTOO9FS(dvp->v_mount);
	parf = VTO9(dvp);			/* parent fid */
	error = 0;
	*vpp = NULL;
	path = NULL;

	if (parf->mode != -1) {
		DBG("parent %d is open, moving to %d\n", parf->fid, parf->parent->fid);
		vref(dvp);
		f = parf;
		parf = parf->parent;
	}

	/* On dot, clone */
	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
		nf = NULL;
	else {
		path = malloc(cnp->cn_namelen, M_O9FS, M_WAITOK);
		strlcpy(path, cnp->cn_nameptr, cnp->cn_namelen+1);
		nf = o9fs_getfid(fs);
	}
	printvp(dvp);

	/* BUG: fid and path leakage */
	f = o9fs_walk(fs, parf, nf, path);
	if (f == NULL) {
		DBG("%s not found\n", cnp->cn_nameptr);
		if (islast && (op == CREATE || op == RENAME)) {
			/* save the name. it's gonna be used soon */
			cnp->cn_flags |= SAVENAME;
			DRET();
			return EJUSTRETURN;
		}
		*vpp = NULL;
		DRET();
		return ENOENT;
	}
	
	error = o9fs_allocvp(fs->mp, f, vpp, 0);
	if (error) {
		*vpp = NULL;
		DBG("could not alloc vnode\n");
		DRET();
		return ENOENT;
	}
	
	/* fix locking on parent dir */
	if (islast && !(cnp->cn_flags & LOCKPARENT)) {
		VOP_UNLOCK(dvp, 0, p);
		flags |= PDIRUNLOCK;
	}

	if(f->qid.type == O9FS_QTDIR)
		(*vpp)->v_type = VDIR;
	else
		(*vpp)->v_type = VREG;
	printvp(*vpp);

	DRET();
	return 0;
}

int
o9fs_getattr(void *v)
{
	struct vop_getattr_args *ap;
	struct vnode *vp;
	struct vattr *vap;
	struct o9fid *f;
	struct o9stat *stat;
	struct o9fs *fs;
	DIN();

	ap = v;
	vp = ap->a_vp;
	vap = ap->a_vap;
	f = VTO9(vp);
	fs = VFSTOO9FS(vp->v_mount);

	if (f == NULL) {
		DRET();
		return 0;
	}

	stat = o9fs_stat(fs, f);
	if (stat == NULL) {
		DRET();
		return 0;
	}
	
	bzero(vap, sizeof(*vap));
	vattr_null(vap);
	vap->va_uid = 0;
	vap->va_gid = 0;
	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
	vap->va_size = stat->length;
	vap->va_blocksize = VFSTOO9FS(vp->v_mount)->msize;
	vap->va_atime.tv_sec = stat->atime;
	vap->va_mtime.tv_sec = stat->mtime;
	vap->va_ctime.tv_sec = stat->atime;
	vap->va_gen = 0;
	vap->va_flags = 0;
	vap->va_rdev = 0;
	vap->va_bytes = 0;
	vap->va_type = vp->v_type;
	vap->va_mode = o9fs_permtou(stat->mode);
	vap->va_nlink = 0;
	vap->va_fileid = f->qid.path;	/* qid.path is 64bit, va_fileid 32bit */
	vap->va_filerev = f->qid.vers;

	free(stat, M_O9FS);
	DRET();
	return 0;
}

int
o9fs_setattr(void *v)
{
 	struct vop_setattr_args *ap;
	ap = v;

	if (ap->a_vp->v_flag & VROOT)
		return EACCES;

	if (ap->a_vap->va_flags != VNOVAL)
		return EOPNOTSUPP;
	return 0;
}

int
o9fs_inactive(void *v)
{
	struct vop_inactive_args *ap;
	struct vnode *vp;
	struct o9fid *f;
	DIN();

	ap = v;
	vp = ap->a_vp;
	f = VTO9(vp);
	if(!(vp->v_flag & VXLOCK))
		vgone(vp);
	DRET();
	return 0;
}

int
o9fs_reclaim(void *v)
{
	struct vop_reclaim_args *ap;
	struct vnode *vp;
	struct o9fid *f;
	DIN();
	
	ap = v;
	vp = ap->a_vp;
	f = VTO9(vp);
	printvp(vp);

	/* TODO: Removed fids should not be clunked again */
	o9fs_clunkremove(VFSTOO9FS(vp->v_mount), f, O9FS_TCLUNK);
//	o9fs_putfid(VFSTOO9FS(vp->v_mount), f);
	vp->v_data = NULL;
	DRET();
	return 0;
}