summaryrefslogtreecommitdiffstats
path: root/sys/fs/hpfs/hpfs_vnops.c
blob: 4c73fe4d396834a88762b3b211f409f1995dd88f (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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
/*-
 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/dirent.h>

#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/vm_page.h>
#include <vm/vm_object.h>
#include <vm/vm_pager.h>
#include <vm/vnode_pager.h>
#include <vm/vm_extern.h>

#include <sys/unistd.h> /* for pathconf(2) constants */

#include <fs/hpfs/hpfs.h>
#include <fs/hpfs/hpfsmount.h>
#include <fs/hpfs/hpfs_subr.h>
#include <fs/hpfs/hpfs_ioctl.h>

static int	hpfs_de_uiomove(struct hpfsmount *, struct hpfsdirent *,
				     struct uio *);
static vop_ioctl_t	hpfs_ioctl;
static vop_read_t	hpfs_read;
static vop_write_t	hpfs_write;
static vop_getattr_t	hpfs_getattr;
static vop_setattr_t	hpfs_setattr;
static vop_inactive_t	hpfs_inactive;
static vop_print_t	hpfs_print;
static vop_reclaim_t	hpfs_reclaim;
static vop_strategy_t	hpfs_strategy;
static vop_access_t	hpfs_access;
static vop_open_t	hpfs_open;
static vop_close_t	hpfs_close;
static vop_readdir_t	hpfs_readdir;
static vop_cachedlookup_t	hpfs_lookup;
static vop_create_t	hpfs_create;
static vop_remove_t	hpfs_remove;
static vop_bmap_t	hpfs_bmap;
static vop_fsync_t	hpfs_fsync;
static vop_pathconf_t	hpfs_pathconf;

static int
hpfs_fsync(ap)
	struct vop_fsync_args /* {
		struct vnode *a_vp;
		struct ucred *a_cred;
		int a_waitfor;
		struct thread *a_td;
	} */ *ap;
{
	/*
	 * Flush our dirty buffers.
	 */
	vop_stdfsync(ap);

	/*
	 * Write out the on-disc version of the vnode.
	 */
	return hpfs_update(VTOHP(ap->a_vp));
}

static int
hpfs_ioctl (
	struct vop_ioctl_args /* {
		struct vnode *a_vp;
		u_long a_command;
		caddr_t a_data;
		int a_fflag;
		struct ucred *a_cred;
		struct thread *a_td;
	} */ *ap)
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);
	int error;

	printf("hpfs_ioctl(0x%x, 0x%lx, 0x%p, 0x%x): ",
		hp->h_no, ap->a_command, ap->a_data, ap->a_fflag);

	switch (ap->a_command) {
	case HPFSIOCGEANUM: {
		u_long eanum;
		u_long passed;
		struct ea *eap;

		eanum = 0;

		if (hp->h_fn.fn_ealen > 0) {
			eap = (struct ea *)&(hp->h_fn.fn_int);
			passed = 0;

			while (passed < hp->h_fn.fn_ealen) {

				printf("EAname: %s\n", EA_NAME(eap));

				eanum++;
				passed += sizeof(struct ea) +
					  eap->ea_namelen + 1 + eap->ea_vallen;
				eap = (struct ea *)((caddr_t)hp->h_fn.fn_int +
						passed);
			}
			error = 0;
		} else {
			error = ENOENT;
		}

		printf("%lu eas\n", eanum);

		*(u_long *)ap->a_data = eanum;

		break;
	}
	case HPFSIOCGEASZ: {
		u_long eanum;
		u_long passed;
		struct ea *eap;

		printf("EA%ld\n", *(u_long *)ap->a_data);

		eanum = 0;
		if (hp->h_fn.fn_ealen > 0) {
			eap = (struct ea *)&(hp->h_fn.fn_int);
			passed = 0;

			error = ENOENT;
			while (passed < hp->h_fn.fn_ealen) {
				printf("EAname: %s\n", EA_NAME(eap));

				if (eanum == *(u_long *)ap->a_data) {
					*(u_long *)ap->a_data =
					  	eap->ea_namelen + 1 +
						eap->ea_vallen;

					error = 0;
					break;
				}

				eanum++;
				passed += sizeof(struct ea) +
					  eap->ea_namelen + 1 + eap->ea_vallen;
				eap = (struct ea *)((caddr_t)hp->h_fn.fn_int +
						passed);
			}
		} else {
			error = ENOENT;
		}

		break;
	}
	case HPFSIOCRDEA: {
		u_long eanum;
		u_long passed;
		struct hpfs_rdea *rdeap;
		struct ea *eap;

		rdeap = (struct hpfs_rdea *)ap->a_data;
		printf("EA%ld\n", rdeap->ea_no);

		eanum = 0;
		if (hp->h_fn.fn_ealen > 0) {
			eap = (struct ea *)&(hp->h_fn.fn_int);
			passed = 0;

			error = ENOENT;
			while (passed < hp->h_fn.fn_ealen) {
				printf("EAname: %s\n", EA_NAME(eap));

				if (eanum == rdeap->ea_no) {
					rdeap->ea_sz = eap->ea_namelen + 1 +
							eap->ea_vallen;
					copyout(EA_NAME(eap),rdeap->ea_data,
						rdeap->ea_sz);
					error = 0;
					break;
				}

				eanum++;
				passed += sizeof(struct ea) +
					  eap->ea_namelen + 1 + eap->ea_vallen;
				eap = (struct ea *)((caddr_t)hp->h_fn.fn_int +
						passed);
			}
		} else {
			error = ENOENT;
		}

		break;
	}
	default:
		error = ENOTTY;
		break;
	}
	return (error);
}

/*
 * Map file offset to disk offset.
 */
int
hpfs_bmap(ap)
	struct vop_bmap_args /* {
		struct vnode *a_vp;
		daddr_t  a_bn;
		struct bufobj **a_bop;
		daddr_t *a_bnp;
		int *a_runp;
		int *a_runb;
	} */ *ap;
{
	register struct hpfsnode *hp = VTOHP(ap->a_vp);
	daddr_t blkno;
	int error;

	if (ap->a_bop != NULL) 
		*ap->a_bop = &hp->h_devvp->v_bufobj;
	if (ap->a_runb != NULL)
		*ap->a_runb = 0;
	if (ap->a_bnp == NULL)
		return (0);

	dprintf(("hpfs_bmap(0x%x, 0x%x): ",hp->h_no, ap->a_bn));

	error = hpfs_hpbmap (hp, ap->a_bn, &blkno, ap->a_runp);
	*ap->a_bnp = blkno;

	return (error);
}

static int
hpfs_read(ap)
	struct vop_read_args /* {
		struct vnode *a_vp;
		struct uio *a_uio;
		int a_ioflag;
		struct ucred *a_cred;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);
	struct uio *uio = ap->a_uio;
	struct buf *bp;
	u_int xfersz, toread;
	u_int off;
	daddr_t lbn, bn;
	int resid;
	int runl;
	int error = 0;

	resid = min (uio->uio_resid, hp->h_fn.fn_size - uio->uio_offset);

	dprintf(("hpfs_read(0x%x, off: %d resid: %d, segflg: %d): [resid: 0x%x]\n",hp->h_no,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg, resid));

	while (resid) {
		lbn = uio->uio_offset >> DEV_BSHIFT;
		off = uio->uio_offset & (DEV_BSIZE - 1);
		dprintf(("hpfs_read: resid: 0x%x lbn: 0x%x off: 0x%x\n",
			uio->uio_resid, lbn, off));
		error = hpfs_hpbmap(hp, lbn, &bn, &runl);
		if (error)
			return (error);

		toread = min(off + resid, min(DFLTPHYS, (runl+1)*DEV_BSIZE));
		xfersz = (toread + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
		dprintf(("hpfs_read: bn: 0x%x (0x%x) toread: 0x%x (0x%x)\n",
			bn, runl, toread, xfersz));

		if (toread == 0) 
			break;

		error = bread(hp->h_devvp, bn, xfersz, NOCRED, &bp);
		if (error) {
			brelse(bp);
			break;
		}

		error = uiomove(bp->b_data + off, toread - off, uio);
		if(error) {
			brelse(bp);
			break;
		}
		brelse(bp);
		resid -= toread;
	}
	dprintf(("hpfs_read: successful\n"));
	return (error);
}

static int
hpfs_write(ap)
	struct vop_write_args /* {
		struct vnode *a_vp;
		struct uio *a_uio;
		int  a_ioflag;
		struct ucred *a_cred;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);
	struct uio *uio = ap->a_uio;
	struct buf *bp;
	u_int xfersz, towrite;
	u_int off;
	daddr_t lbn, bn;
	int runl;
	int error = 0;

	dprintf(("hpfs_write(0x%x, off: %d resid: %d, segflg: %d):\n",hp->h_no,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg));

	if (ap->a_ioflag & IO_APPEND) {
		dprintf(("hpfs_write: APPEND mode\n"));
		uio->uio_offset = hp->h_fn.fn_size;
	}
	if (uio->uio_offset + uio->uio_resid > hp->h_fn.fn_size) {
		error = hpfs_extend (hp, uio->uio_offset + uio->uio_resid);
		if (error) {
			printf("hpfs_write: hpfs_extend FAILED %d\n", error);
			return (error);
		}
	}

	while (uio->uio_resid) {
		lbn = uio->uio_offset >> DEV_BSHIFT;
		off = uio->uio_offset & (DEV_BSIZE - 1);
		dprintf(("hpfs_write: resid: 0x%x lbn: 0x%x off: 0x%x\n",
			uio->uio_resid, lbn, off));
		error = hpfs_hpbmap(hp, lbn, &bn, &runl);
		if (error)
			return (error);

		towrite = min(off + uio->uio_resid, min(DFLTPHYS, (runl+1)*DEV_BSIZE));
		xfersz = (towrite + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
		dprintf(("hpfs_write: bn: 0x%x (0x%x) towrite: 0x%x (0x%x)\n",
			bn, runl, towrite, xfersz));

		if ((off == 0) && (towrite == xfersz)) {
			bp = getblk(hp->h_devvp, bn, xfersz, 0, 0, 0);
			clrbuf(bp);
		} else {
			error = bread(hp->h_devvp, bn, xfersz, NOCRED, &bp);
			if (error) {
				brelse(bp);
				return (error);
			}
		}

		error = uiomove(bp->b_data + off, towrite - off, uio);
		if(error) {
			brelse(bp);
			return (error);
		}

		if (ap->a_ioflag & IO_SYNC)
			bwrite(bp);
		else
			bawrite(bp);
	}

	dprintf(("hpfs_write: successful\n"));
	return (0);
}

/*
 * XXXXX do we need hpfsnode locking inside?
 */
static int
hpfs_getattr(ap)
	struct vop_getattr_args /* {
		struct vnode *a_vp;
		struct vattr *a_vap;
		struct ucred *a_cred;
		struct thread *a_td;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);
	register struct vattr *vap = ap->a_vap;
	int error;

	dprintf(("hpfs_getattr(0x%x):\n", hp->h_no));

	vap->va_fsid = dev2udev(hp->h_dev);
	vap->va_fileid = hp->h_no;
	vap->va_mode = hp->h_mode;
	vap->va_nlink = 1;
	vap->va_uid = hp->h_uid;
	vap->va_gid = hp->h_gid;
	vap->va_rdev = 0;				/* XXX UNODEV ? */
	vap->va_size = hp->h_fn.fn_size;
	vap->va_bytes = ((hp->h_fn.fn_size + DEV_BSIZE-1) & ~(DEV_BSIZE-1)) +
			DEV_BSIZE;

	if (!(hp->h_flag & H_PARVALID)) {
		error = hpfs_validateparent(hp);
		if (error) 
			return (error);
	}
	vap->va_atime = hpfstimetounix(hp->h_atime);
	vap->va_mtime = hpfstimetounix(hp->h_mtime);
	vap->va_ctime = hpfstimetounix(hp->h_ctime);

	vap->va_flags = 0;
	vap->va_gen = 0;
	vap->va_blocksize = DEV_BSIZE;
	vap->va_type = vp->v_type;
	vap->va_filerev = 0;

	return (0);
}

/*
 * XXXXX do we need hpfsnode locking inside?
 */
static int
hpfs_setattr(ap)
	struct vop_setattr_args /* {
		struct vnode *a_vp;
		struct vattr *a_vap;
		struct ucred *a_cred;
		struct thread *a_td;
	} */ *ap;
{
	struct vnode *vp = ap->a_vp;
	struct hpfsnode *hp = VTOHP(vp);
	struct vattr *vap = ap->a_vap;
	struct ucred *cred = ap->a_cred;
	struct thread *td = ap->a_td;
	int error;

	dprintf(("hpfs_setattr(0x%x):\n", hp->h_no));

	/*
	 * Check for unsettable attributes.
	 */
	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
		dprintf(("hpfs_setattr: changing nonsettable attr\n"));
		return (EINVAL);
	}

	/* Can't change flags XXX Could be implemented */
	if (vap->va_flags != VNOVAL) {
		printf("hpfs_setattr: FLAGS CANNOT BE SET\n");
		return (EINVAL);
	}

	/* Can't change uid/gid XXX Could be implemented */
	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
		printf("hpfs_setattr: UID/GID CANNOT BE SET\n");
		return (EINVAL);
	}

	/* Can't change mode XXX Could be implemented */
	if (vap->va_mode != (mode_t)VNOVAL) {
		printf("hpfs_setattr: MODE CANNOT BE SET\n");
		return (EINVAL);
	}

	/* Update times */
	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
		if (vp->v_mount->mnt_flag & MNT_RDONLY)
			return (EROFS);
		if (cred->cr_uid != hp->h_uid &&
		    (error = suser_cred(cred, SUSER_ALLOWJAIL)) &&
		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
		    (error = VOP_ACCESS(vp, VWRITE, cred, td))))
			return (error);
		if (vap->va_atime.tv_sec != VNOVAL)
			hp->h_atime = vap->va_atime.tv_sec;
		if (vap->va_mtime.tv_sec != VNOVAL)
			hp->h_mtime = vap->va_mtime.tv_sec;

		hp->h_flag |= H_PARCHANGE;
	}

	if (vap->va_size != VNOVAL) {
		switch (vp->v_type) {
		case VDIR:
			return (EISDIR);
		case VREG:
			if (vp->v_mount->mnt_flag & MNT_RDONLY)
				return (EROFS);
			break;
		default:
			printf("hpfs_setattr: WRONG v_type\n");
			return (EINVAL);
		}

		if (vap->va_size < hp->h_fn.fn_size) {
			error = vtruncbuf(vp, cred, td, vap->va_size, DEV_BSIZE);
			if (error)
				return (error);
			error = hpfs_truncate(hp, vap->va_size);
			if (error)
				return (error);

		} else if (vap->va_size > hp->h_fn.fn_size) {
			vnode_pager_setsize(vp, vap->va_size);
			error = hpfs_extend(hp, vap->va_size);
			if (error)
				return (error);
		}
	}

	return (0);
}

/*
 * Last reference to a node.  If necessary, write or delete it.
 */
int
hpfs_inactive(ap)
	struct vop_inactive_args /* {
		struct vnode *a_vp;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);
	int error;

	dprintf(("hpfs_inactive(0x%x): \n", hp->h_no));

	if (hp->h_flag & H_CHANGE) {
		dprintf(("hpfs_inactive: node changed, update\n"));
		error = hpfs_update (hp);
		if (error)
			return (error);
	}

	if (hp->h_flag & H_PARCHANGE) {
		dprintf(("hpfs_inactive: parent node changed, update\n"));
		error = hpfs_updateparent (hp);
		if (error)
			return (error);
	}

	if (prtactive && vrefcnt(vp) != 0)
		vprint("hpfs_inactive: pushing active", vp);

	if (hp->h_flag & H_INVAL) {
		vrecycle(vp, ap->a_td);
		return (0);
	}

	return (0);
}

/*
 * Reclaim an inode so that it can be used for other purposes.
 */
int
hpfs_reclaim(ap)
	struct vop_reclaim_args /* {
		struct vnode *a_vp;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);

	dprintf(("hpfs_reclaim(0x%x0): \n", hp->h_no));

	/*
	 * Destroy the vm object and flush associated pages.
	 */
	vnode_destroy_vobject(vp);

	vfs_hash_remove(vp);

	mtx_destroy(&hp->h_interlock);

	vp->v_data = NULL;

	FREE(hp, M_HPFSNO);

	return (0);
}

static int
hpfs_print(ap)
	struct vop_print_args /* {
		struct vnode *a_vp;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);

	printf("\tino 0x%x\n", hp->h_no);
	return (0);
}

/*
 * Calculate the logical to physical mapping if not done already,
 * then call the device strategy routine.
 *
 * In order to be able to swap to a file, the hpfs_hpbmap operation may not
 * deadlock on memory.  See hpfs_bmap() for details. XXXXXXX (not impl)
 */
int
hpfs_strategy(ap)
	struct vop_strategy_args /* {
		struct buf *a_bp;
	} */ *ap;
{
	register struct buf *bp = ap->a_bp;
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(ap->a_vp);
	daddr_t blkno;
	struct bufobj *bo;
	int error;

	dprintf(("hpfs_strategy(): \n"));

	if (vp->v_type == VBLK || vp->v_type == VCHR)
		panic("hpfs_strategy: spec");
	if (bp->b_blkno == bp->b_lblkno) {
		error = hpfs_hpbmap (hp, bp->b_lblkno, &blkno, NULL);
		bp->b_blkno = blkno;
		if (error) {
			printf("hpfs_strategy: hpfs_bpbmap FAILED %d\n", error);
			bp->b_error = error;
			bp->b_ioflags |= BIO_ERROR;
			bufdone(bp);
			return (error);
		}
		if ((long)bp->b_blkno == -1)
			vfs_bio_clrbuf(bp);
	}
	if ((long)bp->b_blkno == -1) {
		bufdone(bp);
		return (0);
	}
	bp->b_iooffset = dbtob(bp->b_blkno);
	bo = hp->h_hpmp->hpm_bo;
	BO_STRATEGY(bo, bp);
	return (0);
}

/*
 * XXXXX do we need hpfsnode locking inside?
 */
int
hpfs_access(ap)
	struct vop_access_args /* {
		struct vnode *a_vp;
		int  a_mode;
		struct ucred *a_cred;
		struct thread *a_td;
	} */ *ap;
{
	struct vnode *vp = ap->a_vp;
	struct hpfsnode *hp = VTOHP(vp);
	mode_t mode = ap->a_mode;

	dprintf(("hpfs_access(0x%x):\n", hp->h_no));

	/*
	 * Disallow write attempts on read-only filesystems;
	 * unless the file is a socket, fifo, or a block or
	 * character device resident on the filesystem.
	 */
	if (mode & VWRITE) {
		switch ((int)vp->v_type) {
		case VDIR:
		case VLNK:
		case VREG:
			if (vp->v_mount->mnt_flag & MNT_RDONLY)
				return (EROFS);
			break;
		}
	}

	return (vaccess(vp->v_type, hp->h_mode, hp->h_uid, hp->h_gid,
	    ap->a_mode, ap->a_cred, NULL));
}

/*
 * Open called.
 *
 * Nothing to do.
 */
/* ARGSUSED */
static int
hpfs_open(ap)
	struct vop_open_args /* {
		struct vnode *a_vp;
		int  a_mode;
		struct ucred *a_cred;
		struct thread *a_td;
	} */ *ap;
{
#ifdef HPFS_DEBUG
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);

	printf("hpfs_open(0x%x):\n",hp->h_no);
#endif

	/*
	 * Files marked append-only must be opened for appending.
	 */

	return (0);
}

/*
 * Close called.
 *
 * Update the times on the inode.
 */
/* ARGSUSED */
static int
hpfs_close(ap)
	struct vop_close_args /* {
		struct vnode *a_vp;
		int  a_fflag;
		struct ucred *a_cred;
		struct thread *a_td;
	} */ *ap;
{
#ifdef HPFS_DEBUG
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);

	printf("hpfs_close: %d\n",hp->h_no);
#endif

	return (0);
}

static int
hpfs_de_uiomove (
	struct hpfsmount *hpmp,
	struct hpfsdirent *dep,
	struct uio *uio)
{
	struct dirent cde;
	int i, error;

	dprintf(("[no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x] ",
		dep->de_fnode, dep->de_size, dep->de_namelen,
		dep->de_namelen, dep->de_name, dep->de_flag));

	/*strncpy(cde.d_name, dep->de_name, dep->de_namelen);*/
	for (i=0; i<dep->de_namelen; i++) 
		cde.d_name[i] = hpfs_d2u(hpmp, dep->de_name[i]);

	cde.d_name[dep->de_namelen] = '\0';
	cde.d_namlen = dep->de_namelen;
	cde.d_fileno = dep->de_fnode;
	cde.d_type = (dep->de_flag & DE_DIR) ? DT_DIR : DT_REG;
	cde.d_reclen = sizeof(struct dirent);

	error = uiomove((char *)&cde, sizeof(struct dirent), uio);
	if (error)
		return (error);
	
	dprintf(("[0x%x] ", uio->uio_resid));
	return (error);
}


static struct dirent hpfs_de_dot =
	{ 0, sizeof(struct dirent), DT_DIR, 1, "." };
static struct dirent hpfs_de_dotdot =
	{ 0, sizeof(struct dirent), DT_DIR, 2, ".." };
int
hpfs_readdir(ap)
	struct vop_readdir_args /* {
		struct vnode *a_vp;
		struct uio *a_uio;
		struct ucred *a_cred;
		int *a_ncookies;
		u_int **cookies;
	} */ *ap;
{
	register struct vnode *vp = ap->a_vp;
	register struct hpfsnode *hp = VTOHP(vp);
	struct hpfsmount *hpmp = hp->h_hpmp;
	struct uio *uio = ap->a_uio;
	int ncookies = 0, i, num, cnum;
	int error = 0;
	off_t off;
	struct buf *bp;
	struct dirblk *dp;
	struct hpfsdirent *dep;
	lsn_t olsn;
	lsn_t lsn;
	int level;

	dprintf(("hpfs_readdir(0x%x, 0x%x, 0x%x): ",hp->h_no,(u_int32_t)uio->uio_offset,uio->uio_resid));

	off = uio->uio_offset;

	if( uio->uio_offset < sizeof(struct dirent) ) {
		dprintf((". faked, "));
		hpfs_de_dot.d_fileno = hp->h_no;
		error = uiomove((char *)&hpfs_de_dot,sizeof(struct dirent),uio);
		if(error) {
			return (error);
		}

		ncookies ++;
	}

	if( uio->uio_offset < 2 * sizeof(struct dirent) ) {
		dprintf((".. faked, "));
		hpfs_de_dotdot.d_fileno = hp->h_fn.fn_parent;

		error = uiomove((char *)&hpfs_de_dotdot, sizeof(struct dirent),
				uio);
		if(error) {
			return (error);
		}

		ncookies ++;
	}

	num = uio->uio_offset / sizeof(struct dirent) - 2;
	cnum = 0;

	lsn = ((alleaf_t *)hp->h_fn.fn_abd)->al_lsn;

	olsn = 0;
	level = 1;

dive:
	dprintf(("[dive 0x%x] ", lsn));
	error = bread(hp->h_devvp, lsn, D_BSIZE, NOCRED, &bp);
	if (error) {
		brelse(bp);
		return (error);
	}

	dp = (struct dirblk *) bp->b_data;
	if (dp->d_magic != D_MAGIC) {
		printf("hpfs_readdir: MAGIC DOESN'T MATCH\n");
		brelse(bp);
		return (EINVAL);
	}

	dep = D_DIRENT(dp);

	if (olsn) {
		dprintf(("[restore 0x%x] ", olsn));

		while(!(dep->de_flag & DE_END) ) {
			if((dep->de_flag & DE_DOWN) &&
			   (olsn == DE_DOWNLSN(dep)))
					 break;
			dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
		}

		if((dep->de_flag & DE_DOWN) && (olsn == DE_DOWNLSN(dep))) {
			if (dep->de_flag & DE_END)
				goto blockdone;

			if (!(dep->de_flag & DE_SPECIAL)) {
				if (num <= cnum) {
					if (uio->uio_resid < sizeof(struct dirent)) {
						brelse(bp);
						dprintf(("[resid] "));
						goto readdone;
					}

					error = hpfs_de_uiomove(hpmp, dep, uio);
					if (error) {
						brelse (bp);
						return (error);
					}
					ncookies++;

					if (uio->uio_resid < sizeof(struct dirent)) {
						brelse(bp);
						dprintf(("[resid] "));
						goto readdone;
					}
				}
				cnum++;
			}

			dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
		} else {
			printf("hpfs_readdir: ERROR! oLSN not found\n");
			brelse(bp);
			return (EINVAL);
		}
	}

	olsn = 0;

	while(!(dep->de_flag & DE_END)) {
		if(dep->de_flag & DE_DOWN) {
			lsn = DE_DOWNLSN(dep);
			brelse(bp);
			level++;
			goto dive;
		}

		if (!(dep->de_flag & DE_SPECIAL)) {
			if (num <= cnum) {
				if (uio->uio_resid < sizeof(struct dirent)) {
					brelse(bp);
					dprintf(("[resid] "));
					goto readdone;
				}

				error = hpfs_de_uiomove(hpmp, dep, uio);
				if (error) {
					brelse (bp);
					return (error);
				}
				ncookies++;
				
				if (uio->uio_resid < sizeof(struct dirent)) {
					brelse(bp);
					dprintf(("[resid] "));
					goto readdone;
				}
			}
			cnum++;
		}

		dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
	}

	if(dep->de_flag & DE_DOWN) {
		dprintf(("[enddive] "));
		lsn = DE_DOWNLSN(dep);
		brelse(bp);
		level++;
		goto dive;
	}

blockdone:
	dprintf(("[EOB] "));
	olsn = lsn;
	lsn = dp->d_parent;
	brelse(bp);
	level--;

	dprintf(("[level %d] ", level));

	if (level > 0)
		goto dive;	/* undive really */

	if (ap->a_eofflag) {
	    dprintf(("[EOF] "));
	    *ap->a_eofflag = 1;
	}

readdone:
	dprintf(("[readdone]\n"));
	if (!error && ap->a_ncookies != NULL) {
		struct dirent* dpStart;
		struct dirent* dp;
		u_long *cookies;
		u_long *cookiep;

		dprintf(("%d cookies, ",ncookies));
		if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
			panic("hpfs_readdir: unexpected uio from NFS server");
		dpStart = (struct dirent *)
		     ((caddr_t)uio->uio_iov->iov_base -
			 (uio->uio_offset - off));
		MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
		       M_TEMP, M_WAITOK);
		for (dp = dpStart, cookiep = cookies, i=0;
		     i < ncookies;
		     dp = (struct dirent *)((caddr_t) dp + dp->d_reclen), i++) {
			off += dp->d_reclen;
			*cookiep++ = (u_int) off;
		}
		*ap->a_ncookies = ncookies;
		*ap->a_cookies = cookies;
	}

	return (0);
}

int
hpfs_lookup(ap)
	struct vop_cachedlookup_args /* {
		struct vnode *a_dvp;
		struct vnode **a_vpp;
		struct componentname *a_cnp;
	} */ *ap;
{
	register struct vnode *dvp = ap->a_dvp;
	register struct hpfsnode *dhp = VTOHP(dvp);
	struct hpfsmount *hpmp = dhp->h_hpmp;
	struct componentname *cnp = ap->a_cnp;
	struct ucred *cred = cnp->cn_cred;
	int error;
	int nameiop = cnp->cn_nameiop;
	int flags = cnp->cn_flags;
	dprintf(("hpfs_lookup(0x%x, %s, %ld):\n",
		dhp->h_no, cnp->cn_nameptr, cnp->cn_namelen));

	if (nameiop != CREATE && nameiop != DELETE && nameiop != LOOKUP) {
		printf("hpfs_lookup: LOOKUP, DELETE and CREATE are only supported\n");
		return (EOPNOTSUPP);
	}

	error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_thread);
	if(error)
		return (error);

	if( (cnp->cn_namelen == 1) &&
	    !strncmp(cnp->cn_nameptr,".",1) ) {
		dprintf(("hpfs_lookup(0x%x,...): . faked\n",dhp->h_no));

		VREF(dvp);
		*ap->a_vpp = dvp;

		return (0);
	} else if( (cnp->cn_namelen == 2) &&
	    !strncmp(cnp->cn_nameptr,"..",2) && (flags & ISDOTDOT) ) {
		dprintf(("hpfs_lookup(0x%x,...): .. faked (0x%x)\n",
			dhp->h_no, dhp->h_fn.fn_parent));

		if (VFS_VGET(hpmp->hpm_mp, dhp->h_fn.fn_parent,
		    LK_NOWAIT | LK_EXCLUSIVE, ap->a_vpp)) {
			VOP_UNLOCK(dvp,0,cnp->cn_thread);
			error = VFS_VGET(hpmp->hpm_mp,
				 dhp->h_fn.fn_parent, LK_EXCLUSIVE, ap->a_vpp); 
			vn_lock(dvp, LK_EXCLUSIVE|LK_RETRY, cnp->cn_thread);
			if (error)
				return(error);
		}
		return (0);
	} else {
		struct buf *bp;
		struct hpfsdirent *dep;
		struct hpfsnode *hp;

		error = hpfs_genlookupbyname(dhp,
				cnp->cn_nameptr, cnp->cn_namelen, &bp, &dep);
		if (error) {
			if ((error == ENOENT) && (flags & ISLASTCN) &&
			    (nameiop == CREATE || nameiop == RENAME)) {
				cnp->cn_flags |= SAVENAME;
				return (EJUSTRETURN);
			}

			return (error);
		}

		dprintf(("hpfs_lookup: fnode: 0x%x, CPID: 0x%x\n",
			 dep->de_fnode, dep->de_cpid));

		if (nameiop == DELETE && (flags & ISLASTCN)) {
			error = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_thread);
			if (error) {
				brelse(bp);
				return (error);
			}
		}

		if (dhp->h_no == dep->de_fnode) {
			brelse(bp);
			VREF(dvp);
			*ap->a_vpp = dvp;
			return (0);
		}

		error = VFS_VGET(hpmp->hpm_mp, dep->de_fnode, LK_EXCLUSIVE,
				 ap->a_vpp);
		if (error) {
			printf("hpfs_lookup: VFS_VGET FAILED %d\n", error);
			brelse(bp);
			return(error);
		}

		hp = VTOHP(*ap->a_vpp);

		hp->h_mtime = dep->de_mtime;
		hp->h_ctime = dep->de_ctime;
		hp->h_atime = dep->de_atime;
		bcopy(dep->de_name, hp->h_name, dep->de_namelen);
		hp->h_name[dep->de_namelen] = '\0';
		hp->h_namelen = dep->de_namelen;
		hp->h_flag |= H_PARVALID;

		brelse(bp);

		if ((flags & MAKEENTRY) &&
		    (!(flags & ISLASTCN) || 
		     (nameiop != DELETE && nameiop != CREATE)))
			cache_enter(dvp, *ap->a_vpp, cnp);
	}
	return (error);
}

int
hpfs_remove(ap)
	struct vop_remove_args /* {
		struct vnode *a_dvp;
		struct vnode *a_vp;
		struct componentname *a_cnp;
	} */ *ap;
{
	int error;

	dprintf(("hpfs_remove(0x%x, %s, %ld): \n", VTOHP(ap->a_vp)->h_no,
		ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen));

	if (ap->a_vp->v_type == VDIR)
		return (EPERM);

	error = hpfs_removefnode (ap->a_dvp, ap->a_vp, ap->a_cnp);
	return (error);
}

int
hpfs_create(ap)
	struct vop_create_args /* {
		struct vnode *a_dvp;
		struct vnode **a_vpp;
		struct componentname *a_cnp;
		struct vattr *a_vap;
	} */ *ap;
{
	int error;

	dprintf(("hpfs_create(0x%x, %s, %ld): \n", VTOHP(ap->a_dvp)->h_no,
		ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen));

	if (!(ap->a_cnp->cn_flags & HASBUF)) 
		panic ("hpfs_create: no name\n");

	error = hpfs_makefnode (ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);

	return (error);
}

/*
 * Return POSIX pathconf information applicable to NTFS filesystem
 */
int
hpfs_pathconf(ap)
	struct vop_pathconf_args /* {
		struct vnode *a_vp;
		int a_name;
		register_t *a_retval;
	} */ *ap;
{
	switch (ap->a_name) {
	case _PC_LINK_MAX:
		*ap->a_retval = 1;
		return (0);
	case _PC_NAME_MAX:
		*ap->a_retval = HPFS_MAXFILENAME;
		return (0);
	case _PC_PATH_MAX:
		*ap->a_retval = PATH_MAX;
		return (0);
	case _PC_CHOWN_RESTRICTED:
		*ap->a_retval = 1;
		return (0);
	case _PC_NO_TRUNC:
		*ap->a_retval = 0;
		return (0);
	default:
		return (EINVAL);
	}
	/* NOTREACHED */
}


/*
 * Global vfs data structures
 */
struct vop_vector hpfs_vnodeops = {
	.vop_default =		&default_vnodeops,

	.vop_access =		hpfs_access,
	.vop_bmap =		hpfs_bmap,
	.vop_cachedlookup =	hpfs_lookup,
	.vop_close =		hpfs_close,
	.vop_create =		hpfs_create,
	.vop_fsync =		hpfs_fsync,
	.vop_getattr =		hpfs_getattr,
	.vop_inactive =		hpfs_inactive,
	.vop_ioctl =		hpfs_ioctl,
	.vop_lookup =		vfs_cache_lookup,
	.vop_open =		hpfs_open,
	.vop_pathconf =		hpfs_pathconf,
	.vop_print =		hpfs_print,
	.vop_read =		hpfs_read,
	.vop_readdir =		hpfs_readdir,
	.vop_reclaim =		hpfs_reclaim,
	.vop_remove =		hpfs_remove,
	.vop_setattr =		hpfs_setattr,
	.vop_strategy =		hpfs_strategy,
	.vop_write =		hpfs_write,
};
OpenPOWER on IntegriCloud