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
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
|
/*
* Copyright (c) 2000-2001 Boris Popov
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* 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/namei.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/fcntl.h>
#include <sys/mount.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <sys/limits.h>
#include <sys/lockf.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>
#include <netsmb/smb.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_subr.h>
#include <fs/smbfs/smbfs.h>
#include <fs/smbfs/smbfs_node.h>
#include <fs/smbfs/smbfs_subr.h>
/*
* Prototypes for SMBFS vnode operations
*/
static int smbfs_create(struct vop_create_args *);
static int smbfs_mknod(struct vop_mknod_args *);
static int smbfs_open(struct vop_open_args *);
static int smbfs_close(struct vop_close_args *);
static int smbfs_access(struct vop_access_args *);
static int smbfs_getattr(struct vop_getattr_args *);
static int smbfs_setattr(struct vop_setattr_args *);
static int smbfs_read(struct vop_read_args *);
static int smbfs_write(struct vop_write_args *);
static int smbfs_fsync(struct vop_fsync_args *);
static int smbfs_remove(struct vop_remove_args *);
static int smbfs_link(struct vop_link_args *);
static int smbfs_lookup(struct vop_lookup_args *);
static int smbfs_rename(struct vop_rename_args *);
static int smbfs_mkdir(struct vop_mkdir_args *);
static int smbfs_rmdir(struct vop_rmdir_args *);
static int smbfs_symlink(struct vop_symlink_args *);
static int smbfs_readdir(struct vop_readdir_args *);
static int smbfs_strategy(struct vop_strategy_args *);
static int smbfs_print(struct vop_print_args *);
static int smbfs_pathconf(struct vop_pathconf_args *ap);
static int smbfs_advlock(struct vop_advlock_args *);
static int smbfs_getextattr(struct vop_getextattr_args *ap);
vop_t **smbfs_vnodeop_p;
static struct vnodeopv_entry_desc smbfs_vnodeop_entries[] = {
{ &vop_default_desc, (vop_t *) vop_defaultop },
{ &vop_access_desc, (vop_t *) smbfs_access },
{ &vop_advlock_desc, (vop_t *) smbfs_advlock },
{ &vop_close_desc, (vop_t *) smbfs_close },
{ &vop_create_desc, (vop_t *) smbfs_create },
{ &vop_fsync_desc, (vop_t *) smbfs_fsync },
{ &vop_getattr_desc, (vop_t *) smbfs_getattr },
{ &vop_getpages_desc, (vop_t *) smbfs_getpages },
{ &vop_inactive_desc, (vop_t *) smbfs_inactive },
{ &vop_ioctl_desc, (vop_t *) smbfs_ioctl },
{ &vop_link_desc, (vop_t *) smbfs_link },
{ &vop_lookup_desc, (vop_t *) smbfs_lookup },
{ &vop_mkdir_desc, (vop_t *) smbfs_mkdir },
{ &vop_mknod_desc, (vop_t *) smbfs_mknod },
{ &vop_open_desc, (vop_t *) smbfs_open },
{ &vop_pathconf_desc, (vop_t *) smbfs_pathconf },
{ &vop_print_desc, (vop_t *) smbfs_print },
{ &vop_putpages_desc, (vop_t *) smbfs_putpages },
{ &vop_read_desc, (vop_t *) smbfs_read },
{ &vop_readdir_desc, (vop_t *) smbfs_readdir },
{ &vop_reclaim_desc, (vop_t *) smbfs_reclaim },
{ &vop_remove_desc, (vop_t *) smbfs_remove },
{ &vop_rename_desc, (vop_t *) smbfs_rename },
{ &vop_rmdir_desc, (vop_t *) smbfs_rmdir },
{ &vop_setattr_desc, (vop_t *) smbfs_setattr },
{ &vop_strategy_desc, (vop_t *) smbfs_strategy },
{ &vop_symlink_desc, (vop_t *) smbfs_symlink },
{ &vop_write_desc, (vop_t *) smbfs_write },
{ &vop_getextattr_desc, (vop_t *) smbfs_getextattr },
/* { &vop_setextattr_desc, (vop_t *) smbfs_setextattr },*/
{ NULL, NULL }
};
static struct vnodeopv_desc smbfs_vnodeop_opv_desc =
{ &smbfs_vnodeop_p, smbfs_vnodeop_entries };
VNODEOP_SET(smbfs_vnodeop_opv_desc);
static int
smbfs_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;
mode_t mode = ap->a_mode;
mode_t mpmode;
struct smbmount *smp = VTOSMBFS(vp);
SMBVDEBUG("\n");
if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
switch (vp->v_type) {
case VREG: case VDIR: case VLNK:
return EROFS;
default:
break;
}
}
mpmode = vp->v_type == VREG ? smp->sm_args.file_mode :
smp->sm_args.dir_mode;
return (vaccess(vp->v_type, mpmode, smp->sm_args.uid,
smp->sm_args.gid, ap->a_mode, ap->a_cred, NULL));
}
/* ARGSUSED */
static int
smbfs_open(ap)
struct vop_open_args /* {
struct vnode *a_vp;
int a_mode;
struct ucred *a_cred;
struct thread *a_td;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct smbnode *np = VTOSMB(vp);
struct smb_cred scred;
struct vattr vattr;
int mode = ap->a_mode;
int error, accmode;
SMBVDEBUG("%s,%d\n", np->n_name, (np->n_flag & NOPEN) != 0);
if (vp->v_type != VREG && vp->v_type != VDIR) {
SMBFSERR("open eacces vtype=%d\n", vp->v_type);
return EACCES;
}
if (vp->v_type == VDIR) {
np->n_flag |= NOPEN;
return 0;
}
if (np->n_flag & NMODIFIED) {
if ((error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_td, 1)) == EINTR)
return error;
smbfs_attr_cacheremove(vp);
error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
if (error)
return error;
np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
} else {
error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
if (error)
return error;
if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_td, 1);
if (error == EINTR)
return error;
np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
}
}
if ((np->n_flag & NOPEN) != 0)
return 0;
/*
* Use DENYNONE to give unixy semantics of permitting
* everything not forbidden by permissions. Ie denial
* is up to server with clients/openers needing to use
* advisory locks for further control.
*/
accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
accmode = SMB_SM_DENYNONE|SMB_AM_OPENRW;
smb_makescred(&scred, ap->a_td, ap->a_cred);
error = smbfs_smb_open(np, accmode, &scred);
if (error) {
if (mode & FWRITE)
return EACCES;
else if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
error = smbfs_smb_open(np, accmode, &scred);
}
}
if (error == 0)
np->n_flag |= NOPEN;
smbfs_attr_cacheremove(vp);
return error;
}
/*
* XXX: VOP_CLOSE() usually called without lock held which is suck. Here we
* do some heruistic to determine if vnode should be locked.
*/
static int
smbfs_close(ap)
struct vop_close_args /* {
struct vnodeop_desc *a_desc;
struct vnode *a_vp;
int a_fflag;
struct ucred *a_cred;
struct thread *a_td;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct thread *td = ap->a_td;
int dolock;
VI_LOCK(vp);
dolock = (vp->v_iflag & VI_XLOCK) == 0;
if (dolock)
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
else
VI_UNLOCK(vp);
/* Nothing. */
if (dolock)
VOP_UNLOCK(vp, 0, td);
return 0;
}
/*
* smbfs_getattr call from vfs.
*/
static int
smbfs_getattr(ap)
struct vop_getattr_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 smbnode *np = VTOSMB(vp);
struct vattr *va=ap->a_vap;
struct smbfattr fattr;
struct smb_cred scred;
u_int32_t oldsize;
int error;
SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_vflag & VV_ROOT) != 0);
error = smbfs_attr_cachelookup(vp, va);
if (!error)
return 0;
SMBVDEBUG("not in the cache\n");
smb_makescred(&scred, ap->a_td, ap->a_cred);
oldsize = np->n_size;
error = smbfs_smb_lookup(np, NULL, 0, &fattr, &scred);
if (error) {
SMBVDEBUG("error %d\n", error);
return error;
}
smbfs_attr_cacheenter(vp, &fattr);
smbfs_attr_cachelookup(vp, va);
if (np->n_flag & NOPEN)
np->n_size = oldsize;
return 0;
}
static int
smbfs_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 smbnode *np = VTOSMB(vp);
struct vattr *vap = ap->a_vap;
struct timespec *mtime, *atime;
struct smb_cred scred;
struct smb_share *ssp = np->n_mount->sm_share;
struct smb_vc *vcp = SSTOVC(ssp);
u_quad_t tsize = 0;
int isreadonly, doclose, error = 0;
SMBVDEBUG("\n");
if (vap->va_flags != VNOVAL)
return EOPNOTSUPP;
isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
/*
* Disallow write attempts if the filesystem is mounted read-only.
*/
if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
vap->va_mode != (mode_t)VNOVAL) && isreadonly)
return EROFS;
smb_makescred(&scred, ap->a_td, ap->a_cred);
if (vap->va_size != VNOVAL) {
switch (vp->v_type) {
case VDIR:
return EISDIR;
case VREG:
break;
default:
return EINVAL;
};
if (isreadonly)
return EROFS;
doclose = 0;
vnode_pager_setsize(vp, (u_long)vap->va_size);
tsize = np->n_size;
np->n_size = vap->va_size;
if ((np->n_flag & NOPEN) == 0) {
error = smbfs_smb_open(np,
SMB_SM_DENYNONE|SMB_AM_OPENRW,
&scred);
if (error == 0)
doclose = 1;
}
if (error == 0)
error = smbfs_smb_setfsize(np, vap->va_size, &scred);
if (doclose)
smbfs_smb_close(ssp, np->n_fid, NULL, &scred);
if (error) {
np->n_size = tsize;
vnode_pager_setsize(vp, (u_long)tsize);
return error;
}
}
mtime = atime = NULL;
if (vap->va_mtime.tv_sec != VNOVAL)
mtime = &vap->va_mtime;
if (vap->va_atime.tv_sec != VNOVAL)
atime = &vap->va_atime;
if (mtime != atime) {
if (ap->a_cred->cr_uid != VTOSMBFS(vp)->sm_args.uid &&
(error = suser_cred(ap->a_cred, PRISON_ROOT)) &&
((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
(error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td))))
return (error);
#if 0
if (mtime == NULL)
mtime = &np->n_mtime;
if (atime == NULL)
atime = &np->n_atime;
#endif
/*
* If file is opened, then we can use handle based calls.
* If not, use path based ones.
*/
if ((np->n_flag & NOPEN) == 0) {
if (vcp->vc_flags & SMBV_WIN95) {
error = VOP_OPEN(vp, FWRITE, ap->a_cred, ap->a_td);
if (!error) {
/* error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);*/
if (mtime)
np->n_mtime = *mtime;
VOP_CLOSE(vp, FWRITE, ap->a_cred, ap->a_td);
}
} else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
/* error = smbfs_smb_setpattrNT(np, 0, mtime, atime, &scred);*/
} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
} else {
error = smbfs_smb_setpattr(np, 0, mtime, &scred);
}
} else {
if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
error = smbfs_smb_setftime(np, mtime, atime, &scred);
} else {
/*
* I have no idea how to handle this for core
* level servers. The possible solution is to
* update mtime after file is closed.
*/
SMBERROR("can't update times on an opened file\n");
}
}
}
/*
* Invalidate attribute cache in case if server doesn't set
* required attributes.
*/
smbfs_attr_cacheremove(vp); /* invalidate cache */
VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
return error;
}
/*
* smbfs_read call.
*/
static int
smbfs_read(ap)
struct vop_read_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
SMBVDEBUG("\n");
if (vp->v_type != VREG && vp->v_type != VDIR)
return EPERM;
return smbfs_readvnode(vp, uio, ap->a_cred);
}
static int
smbfs_write(ap)
struct vop_write_args /* {
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
SMBVDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
if (vp->v_type != VREG)
return (EPERM);
return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
}
/*
* smbfs_create call
* Create a regular file. On entry the directory to contain the file being
* created is locked. We must release before we return. We must also free
* the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
* only if the SAVESTART bit in cn_flags is clear on success.
*/
static int
smbfs_create(ap)
struct vop_create_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
} */ *ap;
{
struct vnode *dvp = ap->a_dvp;
struct vattr *vap = ap->a_vap;
struct vnode **vpp=ap->a_vpp;
struct componentname *cnp = ap->a_cnp;
struct smbnode *dnp = VTOSMB(dvp);
struct vnode *vp;
struct vattr vattr;
struct smbfattr fattr;
struct smb_cred scred;
char *name = cnp->cn_nameptr;
int nmlen = cnp->cn_namelen;
int error;
SMBVDEBUG("\n");
*vpp = NULL;
if (vap->va_type != VREG)
return EOPNOTSUPP;
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)))
return error;
smb_makescred(&scred, cnp->cn_thread, cnp->cn_cred);
error = smbfs_smb_create(dnp, name, nmlen, &scred);
if (error)
return error;
error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
if (error)
return error;
error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
if (error)
return error;
*vpp = vp;
if (cnp->cn_flags & MAKEENTRY)
cache_enter(dvp, vp, cnp);
return error;
}
static int
smbfs_remove(ap)
struct vop_remove_args /* {
struct vnodeop_desc *a_desc;
struct vnode * a_dvp;
struct vnode * a_vp;
struct componentname * a_cnp;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
/* struct vnode *dvp = ap->a_dvp;*/
struct componentname *cnp = ap->a_cnp;
struct smbnode *np = VTOSMB(vp);
struct smb_cred scred;
int error;
if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0 || vrefcnt(vp) != 1)
return EPERM;
smb_makescred(&scred, cnp->cn_thread, cnp->cn_cred);
error = smbfs_smb_delete(np, &scred);
cache_purge(vp);
return error;
}
/*
* smbfs_file rename call
*/
static int
smbfs_rename(ap)
struct vop_rename_args /* {
struct vnode *a_fdvp;
struct vnode *a_fvp;
struct componentname *a_fcnp;
struct vnode *a_tdvp;
struct vnode *a_tvp;
struct componentname *a_tcnp;
} */ *ap;
{
struct vnode *fvp = ap->a_fvp;
struct vnode *tvp = ap->a_tvp;
struct vnode *fdvp = ap->a_fdvp;
struct vnode *tdvp = ap->a_tdvp;
struct componentname *tcnp = ap->a_tcnp;
/* struct componentname *fcnp = ap->a_fcnp;*/
struct smb_cred scred;
u_int16_t flags = 6;
int error=0;
/* Check for cross-device rename */
if ((fvp->v_mount != tdvp->v_mount) ||
(tvp && (fvp->v_mount != tvp->v_mount))) {
error = EXDEV;
goto out;
}
if (tvp && vrefcnt(tvp) > 1) {
error = EBUSY;
goto out;
}
flags = 0x10; /* verify all writes */
if (fvp->v_type == VDIR) {
flags |= 2;
} else if (fvp->v_type == VREG) {
flags |= 1;
} else {
error = EINVAL;
goto out;
}
smb_makescred(&scred, tcnp->cn_thread, tcnp->cn_cred);
/*
* It seems that Samba doesn't implement SMB_COM_MOVE call...
*/
#ifdef notnow
if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
tcnp->cn_nameptr, tcnp->cn_namelen, flags, &scred);
} else
#endif
{
/*
* We have to do the work atomicaly
*/
if (tvp && tvp != fvp) {
error = smbfs_smb_delete(VTOSMB(tvp), &scred);
if (error)
goto out_cacherem;
}
error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
tcnp->cn_nameptr, tcnp->cn_namelen, &scred);
}
if (fvp->v_type == VDIR) {
if (tvp != NULL && tvp->v_type == VDIR)
cache_purge(tdvp);
cache_purge(fdvp);
}
out_cacherem:
smbfs_attr_cacheremove(fdvp);
smbfs_attr_cacheremove(tdvp);
out:
if (tdvp == tvp)
vrele(tdvp);
else
vput(tdvp);
if (tvp)
vput(tvp);
vrele(fdvp);
vrele(fvp);
#ifdef possible_mistake
vgone(fvp);
if (tvp)
vgone(tvp);
#endif
return error;
}
/*
* somtime it will come true...
*/
static int
smbfs_link(ap)
struct vop_link_args /* {
struct vnode *a_tdvp;
struct vnode *a_vp;
struct componentname *a_cnp;
} */ *ap;
{
return EOPNOTSUPP;
}
/*
* smbfs_symlink link create call.
* Sometime it will be functional...
*/
static int
smbfs_symlink(ap)
struct vop_symlink_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
char *a_target;
} */ *ap;
{
return EOPNOTSUPP;
}
static int
smbfs_mknod(ap)
struct vop_mknod_args /* {
} */ *ap;
{
return EOPNOTSUPP;
}
static int
smbfs_mkdir(ap)
struct vop_mkdir_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
} */ *ap;
{
struct vnode *dvp = ap->a_dvp;
/* struct vattr *vap = ap->a_vap;*/
struct vnode *vp;
struct componentname *cnp = ap->a_cnp;
struct smbnode *dnp = VTOSMB(dvp);
struct vattr vattr;
struct smb_cred scred;
struct smbfattr fattr;
char *name = cnp->cn_nameptr;
int len = cnp->cn_namelen;
int error;
if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread))) {
return error;
}
if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
return EEXIST;
smb_makescred(&scred, cnp->cn_thread, cnp->cn_cred);
error = smbfs_smb_mkdir(dnp, name, len, &scred);
if (error)
return error;
error = smbfs_smb_lookup(dnp, name, len, &fattr, &scred);
if (error)
return error;
error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
if (error)
return error;
*ap->a_vpp = vp;
return 0;
}
/*
* smbfs_remove directory call
*/
static int
smbfs_rmdir(ap)
struct vop_rmdir_args /* {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct vnode *dvp = ap->a_dvp;
struct componentname *cnp = ap->a_cnp;
/* struct smbmount *smp = VTOSMBFS(vp);*/
struct smbnode *dnp = VTOSMB(dvp);
struct smbnode *np = VTOSMB(vp);
struct smb_cred scred;
int error;
if (dvp == vp)
return EINVAL;
smb_makescred(&scred, cnp->cn_thread, cnp->cn_cred);
error = smbfs_smb_rmdir(np, &scred);
dnp->n_flag |= NMODIFIED;
smbfs_attr_cacheremove(dvp);
/* cache_purge(dvp);*/
cache_purge(vp);
return error;
}
/*
* smbfs_readdir call
*/
static int
smbfs_readdir(ap)
struct vop_readdir_args /* {
struct vnode *a_vp;
struct uio *a_uio;
struct ucred *a_cred;
int *a_eofflag;
u_long *a_cookies;
int a_ncookies;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
int error;
if (vp->v_type != VDIR)
return (EPERM);
#ifdef notnow
if (ap->a_ncookies) {
printf("smbfs_readdir: no support for cookies now...");
return (EOPNOTSUPP);
}
#endif
error = smbfs_readvnode(vp, uio, ap->a_cred);
return error;
}
/* ARGSUSED */
static int
smbfs_fsync(ap)
struct vop_fsync_args /* {
struct vnodeop_desc *a_desc;
struct vnode * a_vp;
struct ucred * a_cred;
int a_waitfor;
struct thread * a_td;
} */ *ap;
{
/* return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
return (0);
}
static
int smbfs_print (ap)
struct vop_print_args /* {
struct vnode *a_vp;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct smbnode *np = VTOSMB(vp);
if (np == NULL) {
printf("no smbnode data\n");
return (0);
}
printf("\tname = %s, parent = %p, open = %d\n", np->n_name,
np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0);
return (0);
}
static int
smbfs_pathconf (ap)
struct vop_pathconf_args /* {
struct vnode *vp;
int name;
register_t *retval;
} */ *ap;
{
struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
struct smb_vc *vcp = SSTOVC(smp->sm_share);
register_t *retval = ap->a_retval;
int error = 0;
switch (ap->a_name) {
case _PC_LINK_MAX:
*retval = 0;
break;
case _PC_NAME_MAX:
*retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
break;
case _PC_PATH_MAX:
*retval = 800; /* XXX: a correct one ? */
break;
default:
error = EINVAL;
}
return error;
}
static int
smbfs_strategy (ap)
struct vop_strategy_args /* {
struct buf *a_bp
} */ *ap;
{
struct buf *bp=ap->a_bp;
struct ucred *cr;
struct thread *td;
int error = 0;
KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)",
__func__, ap->a_vp, ap->a_bp->b_vp));
SMBVDEBUG("\n");
if (bp->b_flags & B_PHYS)
panic("smbfs physio");
if (bp->b_flags & B_ASYNC)
td = (struct thread *)0;
else
td = curthread; /* XXX */
if (bp->b_iocmd == BIO_READ)
cr = bp->b_rcred;
else
cr = bp->b_wcred;
if ((bp->b_flags & B_ASYNC) == 0 )
error = smbfs_doio(bp, cr, td);
return error;
}
int
smbfs_ioctl(ap)
struct vop_ioctl_args /* {
struct vnode *a_vp;
u_long a_command;
caddr_t a_data;
int fflag;
struct ucred *cred;
struct thread *td;
} */ *ap;
{
return ENOTTY;
}
static char smbfs_atl[] = "rhsvda";
static int
smbfs_getextattr(struct vop_getextattr_args *ap)
/* {
IN struct vnode *a_vp;
IN char *a_name;
INOUT struct uio *a_uio;
IN struct ucred *a_cred;
IN struct thread *a_td;
};
*/
{
struct vnode *vp = ap->a_vp;
struct thread *td = ap->a_td;
struct ucred *cred = ap->a_cred;
struct uio *uio = ap->a_uio;
const char *name = ap->a_name;
struct smbnode *np = VTOSMB(vp);
struct vattr vattr;
char buf[10];
int i, attr, error;
error = VOP_ACCESS(vp, VREAD, cred, td);
if (error)
return error;
error = VOP_GETATTR(vp, &vattr, cred, td);
if (error)
return error;
if (strcmp(name, "dosattr") == 0) {
attr = np->n_dosattr;
for (i = 0; i < 6; i++, attr >>= 1)
buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
buf[i] = 0;
error = uiomove(buf, i, uio);
} else
error = EINVAL;
return error;
}
/*
* Since we expected to support F_GETLK (and SMB protocol has no such function),
* it is necessary to use lf_advlock(). It would be nice if this function had
* a callback mechanism because it will help to improve a level of consistency.
*/
int
smbfs_advlock(ap)
struct vop_advlock_args /* {
struct vnode *a_vp;
caddr_t a_id;
int a_op;
struct flock *a_fl;
int a_flags;
} */ *ap;
{
struct vnode *vp = ap->a_vp;
struct smbnode *np = VTOSMB(vp);
struct flock *fl = ap->a_fl;
caddr_t id = (caddr_t)1 /* ap->a_id */;
/* int flags = ap->a_flags;*/
struct thread *td = curthread;
struct smb_cred scred;
u_quad_t size;
off_t start, end, oadd;
int error, lkop;
if (vp->v_type == VDIR) {
/*
* SMB protocol have no support for directory locking.
* Although locks can be processed on local machine, I don't
* think that this is a good idea, because some programs
* can work wrong assuming directory is locked. So, we just
* return 'operation not supported
*/
return EOPNOTSUPP;
}
size = np->n_size;
switch (fl->l_whence) {
case SEEK_SET:
case SEEK_CUR:
start = fl->l_start;
break;
case SEEK_END:
if (size > OFF_MAX ||
(fl->l_start > 0 && size > OFF_MAX - fl->l_start))
return EOVERFLOW;
start = size + fl->l_start;
break;
default:
return EINVAL;
}
if (start < 0)
return EINVAL;
if (fl->l_len < 0) {
if (start == 0)
return EINVAL;
end = start - 1;
start += fl->l_len;
if (start < 0)
return EINVAL;
} else if (fl->l_len == 0)
end = -1;
else {
oadd = fl->l_len - 1;
if (oadd > OFF_MAX - start)
return EOVERFLOW;
end = start + oadd;
}
smb_makescred(&scred, td, td->td_ucred);
switch (ap->a_op) {
case F_SETLK:
switch (fl->l_type) {
case F_WRLCK:
lkop = SMB_LOCK_EXCL;
break;
case F_RDLCK:
lkop = SMB_LOCK_SHARED;
break;
case F_UNLCK:
lkop = SMB_LOCK_RELEASE;
break;
default:
return EINVAL;
}
error = lf_advlock(ap, &np->n_lockf, size);
if (error)
break;
lkop = SMB_LOCK_EXCL;
error = smbfs_smb_lock(np, lkop, id, start, end, &scred);
if (error) {
ap->a_op = F_UNLCK;
lf_advlock(ap, &np->n_lockf, size);
}
break;
case F_UNLCK:
lf_advlock(ap, &np->n_lockf, size);
error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, &scred);
break;
case F_GETLK:
error = lf_advlock(ap, &np->n_lockf, size);
break;
default:
return EINVAL;
}
return error;
}
static int
smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
{
static const char *badchars = "*/\\[]:<>=;?";
static const char *badchars83 = " +|,";
const char *cp;
int i, error;
if (nameiop == LOOKUP)
return 0;
error = ENOENT;
if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
/*
* Name should conform 8.3 format
*/
if (nmlen > 12)
return ENAMETOOLONG;
cp = index(name, '.');
if (cp == NULL)
return error;
if (cp == name || (cp - name) > 8)
return error;
cp = index(cp + 1, '.');
if (cp != NULL)
return error;
for (cp = name, i = 0; i < nmlen; i++, cp++)
if (index(badchars83, *cp) != NULL)
return error;
}
for (cp = name, i = 0; i < nmlen; i++, cp++)
if (index(badchars, *cp) != NULL)
return error;
return 0;
}
#ifndef PDIRUNLOCK
#define PDIRUNLOCK 0
#endif
/*
* Things go even weird without fixed inode numbers...
*/
int
smbfs_lookup(ap)
struct vop_lookup_args /* {
struct vnodeop_desc *a_desc;
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
} */ *ap;
{
struct componentname *cnp = ap->a_cnp;
struct thread *td = cnp->cn_thread;
struct vnode *dvp = ap->a_dvp;
struct vnode **vpp = ap->a_vpp;
struct vnode *vp;
struct smbmount *smp;
struct mount *mp = dvp->v_mount;
struct smbnode *dnp;
struct smbfattr fattr, *fap;
struct smb_cred scred;
char *name = cnp->cn_nameptr;
int flags = cnp->cn_flags;
int nameiop = cnp->cn_nameiop;
int nmlen = cnp->cn_namelen;
int lockparent, wantparent, error, islastcn, isdot;
SMBVDEBUG("\n");
cnp->cn_flags &= ~PDIRUNLOCK;
if (dvp->v_type != VDIR)
return ENOTDIR;
if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) {
SMBFSERR("invalid '..'\n");
return EIO;
}
#ifdef SMB_VNODE_DEBUG
{
char *cp, c;
cp = name + nmlen;
c = *cp;
*cp = 0;
SMBVDEBUG("%d '%s' in '%s' id=d\n", nameiop, name,
VTOSMB(dvp)->n_name);
*cp = c;
}
#endif
islastcn = flags & ISLASTCN;
if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
return EROFS;
if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
return error;
lockparent = flags & LOCKPARENT;
wantparent = flags & (LOCKPARENT|WANTPARENT);
smp = VFSTOSMBFS(mp);
dnp = VTOSMB(dvp);
isdot = (nmlen == 1 && name[0] == '.');
error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
if (error)
return ENOENT;
error = cache_lookup(dvp, vpp, cnp);
SMBVDEBUG("cache_lookup returned %d\n", error);
if (error > 0)
return error;
if (error) { /* name was found */
struct vattr vattr;
int vpid;
vp = *vpp;
mp_fixme("Unlocked v_id access.");
vpid = vp->v_id;
if (dvp == vp) { /* lookup on current */
vref(vp);
error = 0;
SMBVDEBUG("cached '.'\n");
} else if (flags & ISDOTDOT) {
VOP_UNLOCK(dvp, 0, td); /* unlock parent */
cnp->cn_flags |= PDIRUNLOCK;
error = vget(vp, LK_EXCLUSIVE, td);
if (!error && lockparent && islastcn) {
error = vn_lock(dvp, LK_EXCLUSIVE, td);
if (error == 0)
cnp->cn_flags &= ~PDIRUNLOCK;
}
} else {
error = vget(vp, LK_EXCLUSIVE, td);
if (!lockparent || error || !islastcn) {
VOP_UNLOCK(dvp, 0, td);
cnp->cn_flags |= PDIRUNLOCK;
}
}
if (!error) {
if (vpid == vp->v_id) {
if (!VOP_GETATTR(vp, &vattr, cnp->cn_cred, td)
/* && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) {
if (nameiop != LOOKUP && islastcn)
cnp->cn_flags |= SAVENAME;
SMBVDEBUG("use cached vnode\n");
return (0);
}
cache_purge(vp);
}
vput(vp);
if (lockparent && dvp != vp && islastcn)
VOP_UNLOCK(dvp, 0, td);
}
error = vn_lock(dvp, LK_EXCLUSIVE, td);
*vpp = NULLVP;
if (error) {
cnp->cn_flags |= PDIRUNLOCK;
return (error);
}
cnp->cn_flags &= ~PDIRUNLOCK;
}
/*
* entry is not in the cache or has been expired
*/
error = 0;
*vpp = NULLVP;
smb_makescred(&scred, td, cnp->cn_cred);
fap = &fattr;
if (flags & ISDOTDOT) {
error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
&scred);
SMBVDEBUG("result of dotdot lookup: %d\n", error);
} else {
fap = &fattr;
error = smbfs_smb_lookup(dnp, name, nmlen, fap, &scred);
/* if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')*/
SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
}
if (error && error != ENOENT)
return error;
if (error) { /* entry not found */
/*
* Handle RENAME or CREATE case...
*/
if ((nameiop == CREATE || nameiop == RENAME) && wantparent && islastcn) {
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
if (error)
return error;
cnp->cn_flags |= SAVENAME;
if (!lockparent) {
VOP_UNLOCK(dvp, 0, td);
cnp->cn_flags |= PDIRUNLOCK;
}
return (EJUSTRETURN);
}
return ENOENT;
}/* else {
SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
}*/
/*
* handle DELETE case ...
*/
if (nameiop == DELETE && islastcn) { /* delete last component */
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
if (error)
return error;
if (isdot) {
VREF(dvp);
*vpp = dvp;
return 0;
}
error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
if (error)
return error;
*vpp = vp;
cnp->cn_flags |= SAVENAME;
if (!lockparent) {
VOP_UNLOCK(dvp, 0, td);
cnp->cn_flags |= PDIRUNLOCK;
}
return 0;
}
if (nameiop == RENAME && islastcn && wantparent) {
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
if (error)
return error;
if (isdot)
return EISDIR;
error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
if (error)
return error;
*vpp = vp;
cnp->cn_flags |= SAVENAME;
if (!lockparent) {
VOP_UNLOCK(dvp, 0, td);
cnp->cn_flags |= PDIRUNLOCK;
}
return 0;
}
if (flags & ISDOTDOT) {
VOP_UNLOCK(dvp, 0, td);
error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
if (error) {
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
return error;
}
if (lockparent && islastcn) {
error = vn_lock(dvp, LK_EXCLUSIVE, td);
if (error) {
cnp->cn_flags |= PDIRUNLOCK;
vput(vp);
return error;
}
}
*vpp = vp;
} else if (isdot) {
vref(dvp);
*vpp = dvp;
} else {
error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
if (error)
return error;
*vpp = vp;
SMBVDEBUG("lookup: getnewvp!\n");
if (!lockparent || !islastcn) {
VOP_UNLOCK(dvp, 0, td);
cnp->cn_flags |= PDIRUNLOCK;
}
}
if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
/* VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/
cache_enter(dvp, *vpp, cnp);
}
return 0;
}
|