summaryrefslogtreecommitdiff
path: root/source4/ntvfs/ntvfs_generic.c
blob: 9aa8e04787230165b872c0bbf88448658db90dfd (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
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
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
/* 
   Unix SMB/CIFS implementation.

   NTVFS generic level mapping code

   Copyright (C) Andrew Tridgell 2003-2004

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   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, see <http://www.gnu.org/licenses/>.
*/
/*
  this implements mappings between info levels for NTVFS backend calls

  the idea is that each of these functions implements one of the NTVFS
  backend calls in terms of the 'generic' call. All backends that use
  these functions must supply the generic call, but can if it wants to
  also implement other levels if the need arises

  this allows backend writers to only implement one variant of each
  call unless they need fine grained control of the calls.
*/

#include "includes.h"
#include "ntvfs/ntvfs.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"

/* a second stage function converts from the out parameters of the generic
   call onto the out parameters of the specific call made */
typedef NTSTATUS (*second_stage_t)(struct ntvfs_module_context *,
				   struct ntvfs_request *,
				   void *, void *, NTSTATUS);

/* 
   this structure holds the async state for pending mapped async calls
*/
struct ntvfs_map_async {
	struct ntvfs_module_context *ntvfs;
	void *io, *io2;
	second_stage_t fn;
};

/*
  this is a async wrapper, called from the backend when it has completed
  a function that it has decided to reply to in an async fashion
*/
static void ntvfs_map_async_send(struct ntvfs_request *req)
{
	struct ntvfs_map_async *m = talloc_get_type(req->async_states->private_data,
				    struct ntvfs_map_async);

	ntvfs_async_state_pop(req);

	/* call the _finish function setup in ntvfs_map_async_setup() */
	req->async_states->status = m->fn(m->ntvfs, req, m->io, m->io2, req->async_states->status);

	/* call the send function from the next module up */
	req->async_states->send_fn(req);
}

/*
  prepare for calling a ntvfs backend with async support
  io is the original call structure
  io2 is the new call structure for the mapped call
  fn is a second stage function for processing the out arguments
*/
static NTSTATUS ntvfs_map_async_setup(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req,
				      void *io, void *io2,
				      second_stage_t fn)
{
	struct ntvfs_map_async *m;
	m = talloc(req, struct ntvfs_map_async);
	if (m == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	m->ntvfs = ntvfs;
	m->io = io;
	m->io2 = io2;
	m->fn = fn;
	return ntvfs_async_state_push(ntvfs, req, m, ntvfs_map_async_send);
}

/*
  called when first stage processing is complete. 
*/	
static NTSTATUS ntvfs_map_async_finish(struct ntvfs_request *req, NTSTATUS status)
{
	struct ntvfs_map_async *m;

	/* if the backend has decided to reply in an async fashion then
	   we don't need to do any work here */
	if (req->async_states->state & NTVFS_ASYNC_STATE_ASYNC) {
		return status;
	}

	/* the backend is replying immediately. call the 2nd stage function after popping our local
	   async state */
	m = talloc_get_type(req->async_states->private_data,
			    struct ntvfs_map_async);

	ntvfs_async_state_pop(req);

	return m->fn(m->ntvfs, req, m->io, m->io2, status);
}

/*
  see if a filename ends in EXE COM DLL or SYM. This is needed for the
  DENY_DOS mapping for OpenX
*/
bool is_exe_filename(const char *fname)
{
	char *p;
	p = strrchr(fname, '.');
	if (!p) {
		return false;
	}
	p++;
	if (strcasecmp(p, "EXE") == 0 ||
	    strcasecmp(p, "COM") == 0 ||
	    strcasecmp(p, "DLL") == 0 ||
	    strcasecmp(p, "SYM") == 0) {
		return true;
	}
	return false;
}


/* 
   NTVFS openx to ntcreatex mapper
*/
static NTSTATUS ntvfs_map_open_finish(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req, 
				      union smb_open *io, 
				      union smb_open *io2, 
				      NTSTATUS status)
{
	time_t write_time = 0;
	uint32_t set_size = 0;
	union smb_setfileinfo *sf;
	unsigned int state;

	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	switch (io->generic.level) {
	case RAW_OPEN_OPEN:
		io->openold.out.file.ntvfs = io2->generic.out.file.ntvfs;
		io->openold.out.attrib     = io2->generic.out.attrib;
		io->openold.out.write_time = nt_time_to_unix(io2->generic.out.write_time);
		io->openold.out.size       = io2->generic.out.size;
		io->openold.out.rmode      = io->openold.in.open_mode;
		break;

	case RAW_OPEN_OPENX:
		io->openx.out.file.ntvfs  = io2->generic.out.file.ntvfs;
		io->openx.out.attrib      = io2->generic.out.attrib;
		io->openx.out.write_time  = nt_time_to_unix(io2->generic.out.write_time);
		io->openx.out.size        = io2->generic.out.size;
		io->openx.out.access      = (io->openx.in.open_mode & OPENX_MODE_ACCESS_MASK);
		io->openx.out.ftype       = 0;
		io->openx.out.devstate    = 0;
		io->openx.out.action      = io2->generic.out.create_action;
		io->openx.out.unique_fid  = 0;
		io->openx.out.access_mask = SEC_STD_ALL;
		io->openx.out.unknown     = 0;
		
		/* we need to extend the file to the requested size if
		   it was newly created */
		if (io2->generic.out.create_action == NTCREATEX_ACTION_CREATED) {
			set_size = io->openx.in.size;
		}
		break;

	case RAW_OPEN_T2OPEN:
		io->t2open.out.file.ntvfs  = io2->generic.out.file.ntvfs;
		io->t2open.out.attrib      = io2->generic.out.attrib;
		io->t2open.out.write_time  = nt_time_to_unix(io2->generic.out.write_time);
		io->t2open.out.size        = io2->generic.out.size;
		io->t2open.out.access      = io->t2open.in.open_mode;
		io->t2open.out.ftype       = 0;
		io->t2open.out.devstate    = 0;
		io->t2open.out.action      = io2->generic.out.create_action;
		io->t2open.out.file_id      = 0;
		break;

	case RAW_OPEN_MKNEW:
	case RAW_OPEN_CREATE:
		io->mknew.out.file.ntvfs= io2->generic.out.file.ntvfs;
		write_time		= io->mknew.in.write_time;
		break;

	case RAW_OPEN_CTEMP:
		io->ctemp.out.file.ntvfs= io2->generic.out.file.ntvfs;
		io->ctemp.out.name 	= talloc_strdup(req, io2->generic.in.fname + 
							strlen(io->ctemp.in.directory) + 1);
		NT_STATUS_HAVE_NO_MEMORY(io->ctemp.out.name);
		break;

	case RAW_OPEN_SMB2:
		ZERO_STRUCT(io->smb2.out);
		io->smb2.out.file.ntvfs		= io2->generic.out.file.ntvfs;
		switch (io2->generic.out.oplock_level) {
		case BATCH_OPLOCK_RETURN:
			io->smb2.out.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
			break;
		case EXCLUSIVE_OPLOCK_RETURN:
			io->smb2.out.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
			break;
		case LEVEL_II_OPLOCK_RETURN:
			io->smb2.out.oplock_level = SMB2_OPLOCK_LEVEL_II;
			break;
		default:
			io->smb2.out.oplock_level = SMB2_OPLOCK_LEVEL_NONE;
			break;
		}
		io->smb2.out.reserved		= 0;
		io->smb2.out.create_action	= io2->generic.out.create_action;
		io->smb2.out.create_time	= io2->generic.out.create_time;
		io->smb2.out.access_time	= io2->generic.out.access_time;
		io->smb2.out.write_time		= io2->generic.out.write_time;
		io->smb2.out.change_time	= io2->generic.out.change_time;
		io->smb2.out.alloc_size		= io2->generic.out.alloc_size;
		io->smb2.out.size		= io2->generic.out.size;
		io->smb2.out.file_attr		= io2->generic.out.attrib;
		io->smb2.out.reserved2		= 0;
		io->smb2.out.maximal_access     = io2->generic.out.maximal_access;
		break;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	/* doing a secondary request async is more trouble than its
	   worth */
	state = req->async_states->state;
	req->async_states->state &= ~NTVFS_ASYNC_STATE_MAY_ASYNC;

	if (write_time != 0) {
		sf = talloc(req, union smb_setfileinfo);
		NT_STATUS_HAVE_NO_MEMORY(sf);
		sf->generic.level           = RAW_SFILEINFO_STANDARD;
		sf->generic.in.file.ntvfs   = io2->generic.out.file.ntvfs;
		sf->standard.in.create_time = 0;
		sf->standard.in.write_time  = write_time;
		sf->standard.in.access_time = 0;
		status = ntvfs->ops->setfileinfo(ntvfs, req, sf);
	}

	if (set_size != 0) {
		sf = talloc(req, union smb_setfileinfo);			
		NT_STATUS_HAVE_NO_MEMORY(sf);
		sf->generic.level            = RAW_SFILEINFO_END_OF_FILE_INFORMATION;
		sf->generic.in.file.ntvfs    = io2->generic.out.file.ntvfs;
		sf->end_of_file_info.in.size = set_size;
		status = ntvfs->ops->setfileinfo(ntvfs, req, sf);
		if (NT_STATUS_IS_OK(status)) {
			io->openx.out.size = io->openx.in.size;
		}
	}

	req->async_states->state = state;

	return NT_STATUS_OK;
}

/*
  the core of the mapping between openx style parameters and ntcreatex 
  parameters
*/
static NTSTATUS map_openx_open(uint16_t flags, uint16_t open_mode, 
			       uint16_t open_func, const char *fname,
			       union smb_open *io2)
{
	io2->generic.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
	io2->generic.in.private_flags = 0;

	if (flags & OPENX_FLAGS_REQUEST_OPLOCK) {
		io2->generic.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
	}
	if (flags & OPENX_FLAGS_REQUEST_BATCH_OPLOCK) {
		io2->generic.in.flags |= NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
	}

	switch (open_mode & OPENX_MODE_ACCESS_MASK) {
	case OPENX_MODE_ACCESS_READ:
	case OPENX_MODE_ACCESS_EXEC:
		io2->generic.in.access_mask = SEC_RIGHTS_FILE_READ;
		break;
	case OPENX_MODE_ACCESS_WRITE:
		io2->generic.in.access_mask = SEC_RIGHTS_FILE_WRITE;
		break;
	case OPENX_MODE_ACCESS_RDWR:
	case OPENX_MODE_ACCESS_FCB:
		io2->generic.in.access_mask = 
			SEC_RIGHTS_FILE_READ | 
			SEC_RIGHTS_FILE_WRITE;
		break;
	default:
		return NT_STATUS_DOS(ERRDOS, ERRbadaccess);
	}

	switch (open_mode & OPENX_MODE_DENY_MASK) {
	case OPENX_MODE_DENY_READ:
		io2->generic.in.share_access = NTCREATEX_SHARE_ACCESS_WRITE;
		break;
	case OPENX_MODE_DENY_WRITE:
		io2->generic.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
		break;
	case OPENX_MODE_DENY_ALL:
		io2->generic.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
		break;
	case OPENX_MODE_DENY_NONE:
		io2->generic.in.share_access = 
			NTCREATEX_SHARE_ACCESS_READ | 
			NTCREATEX_SHARE_ACCESS_WRITE;
		break;
	case OPENX_MODE_DENY_DOS:
		/* DENY_DOS is quite strange - it depends on the filename! */
		io2->generic.in.private_flags |=
			NTCREATEX_OPTIONS_PRIVATE_DENY_DOS;
		if (is_exe_filename(fname)) {
			io2->generic.in.share_access = 
				NTCREATEX_SHARE_ACCESS_READ | 
				NTCREATEX_SHARE_ACCESS_WRITE;
		} else {
			if ((open_mode & OPENX_MODE_ACCESS_MASK) == OPENX_MODE_ACCESS_READ) {
				io2->generic.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
			} else {
				io2->generic.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
			}
		}
		break;
	case OPENX_MODE_DENY_FCB:
		io2->generic.in.private_flags |= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB;
		io2->generic.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
		break;
	default:
		return NT_STATUS_DOS(ERRDOS, ERRbadaccess);
	}

	switch (open_func) {
	case (OPENX_OPEN_FUNC_OPEN):
		io2->generic.in.open_disposition = NTCREATEX_DISP_OPEN;
		break;
	case (OPENX_OPEN_FUNC_TRUNC):
		io2->generic.in.open_disposition = NTCREATEX_DISP_OVERWRITE;
		break;
	case (OPENX_OPEN_FUNC_FAIL | OPENX_OPEN_FUNC_CREATE):
		io2->generic.in.open_disposition = NTCREATEX_DISP_CREATE;
		break;
	case (OPENX_OPEN_FUNC_OPEN | OPENX_OPEN_FUNC_CREATE):
		io2->generic.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
		break;
	case (OPENX_OPEN_FUNC_TRUNC | OPENX_OPEN_FUNC_CREATE):
		io2->generic.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
		break;			
	default:
		/* this one is very strange */
		if ((open_mode & OPENX_MODE_ACCESS_MASK) == OPENX_MODE_ACCESS_EXEC) {
			io2->generic.in.open_disposition = NTCREATEX_DISP_CREATE;
			break;
		}
		return NT_STATUS_DOS(ERRDOS, ERRbadaccess);
	}

	return NT_STATUS_OK;
}

/* 
   NTVFS open generic to any mapper
*/
NTSTATUS ntvfs_map_open(struct ntvfs_module_context *ntvfs,
				 struct ntvfs_request *req,
				 union smb_open *io)
{
	NTSTATUS status;
	union smb_open *io2;

	io2 = talloc_zero(req, union smb_open);
	if (io2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = ntvfs_map_async_setup(ntvfs, req,
				       io, io2, 
				       (second_stage_t)ntvfs_map_open_finish);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	io2->generic.level = RAW_OPEN_GENERIC;
		
	switch (io->generic.level) {
	case RAW_OPEN_OPENX:
		status = map_openx_open(io->openx.in.flags,
					io->openx.in.open_mode, 
					io->openx.in.open_func, 
					io->openx.in.fname,
					io2);
		if (!NT_STATUS_IS_OK(status)) {
			goto done;
		}
		
		io2->generic.in.file_attr = io->openx.in.file_attrs;
		io2->generic.in.fname = io->openx.in.fname;
		
		status = ntvfs->ops->open(ntvfs, req, io2);
		break;
		
		
	case RAW_OPEN_OPEN:
		status = map_openx_open(0,
					io->openold.in.open_mode, 
					OPENX_OPEN_FUNC_OPEN, 
					io->openold.in.fname,
					io2);
		if (!NT_STATUS_IS_OK(status)) {
			goto done;
		}

		io2->generic.in.file_attr = io->openold.in.search_attrs;
		io2->generic.in.fname = io->openold.in.fname;

		status = ntvfs->ops->open(ntvfs, req, io2);
		break;

	case RAW_OPEN_T2OPEN:
		io2->generic.level         = RAW_OPEN_NTTRANS_CREATE;

		if (io->t2open.in.open_func == 0) {
			status = NT_STATUS_OBJECT_NAME_COLLISION;
			goto done;
		}

		status = map_openx_open(io->t2open.in.flags,
					io->t2open.in.open_mode, 
					io->t2open.in.open_func, 
					io->t2open.in.fname,
					io2);
		if (!NT_STATUS_IS_OK(status)) {
			goto done;
		}

		io2->generic.in.file_attr        = io->t2open.in.file_attrs;
		io2->generic.in.fname            = io->t2open.in.fname;
		io2->generic.in.ea_list          = talloc(io2, struct smb_ea_list);
		io2->generic.in.ea_list->num_eas = io->t2open.in.num_eas;
		io2->generic.in.ea_list->eas     = io->t2open.in.eas;

		status = ntvfs->ops->open(ntvfs, req, io2);
		break;

	case RAW_OPEN_MKNEW:
		io2->generic.in.file_attr = io->mknew.in.attrib;
		io2->generic.in.fname = io->mknew.in.fname;
		io2->generic.in.open_disposition = NTCREATEX_DISP_CREATE;
		io2->generic.in.access_mask = 
			SEC_RIGHTS_FILE_READ |
			SEC_RIGHTS_FILE_WRITE;
		io2->generic.in.share_access = 
			NTCREATEX_SHARE_ACCESS_READ | 
			NTCREATEX_SHARE_ACCESS_WRITE;
		status = ntvfs->ops->open(ntvfs, req, io2);
		break;

	case RAW_OPEN_CREATE:
		io2->generic.in.file_attr = io->mknew.in.attrib;
		io2->generic.in.fname = io->mknew.in.fname;
		io2->generic.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
		io2->generic.in.access_mask = 
			SEC_RIGHTS_FILE_READ |
			SEC_RIGHTS_FILE_WRITE;
		io2->generic.in.share_access = 
			NTCREATEX_SHARE_ACCESS_READ | 
			NTCREATEX_SHARE_ACCESS_WRITE;
		status = ntvfs->ops->open(ntvfs, req, io2);
		break;

	case RAW_OPEN_CTEMP:
		io2->generic.in.file_attr = io->ctemp.in.attrib;
		io2->generic.in.fname = 
			talloc_asprintf(io2, "%s\\SRV%s", 
					io->ctemp.in.directory,
					generate_random_str_list(io2, 5, "0123456789"));
		io2->generic.in.open_disposition = NTCREATEX_DISP_CREATE;
		io2->generic.in.access_mask = 
			SEC_RIGHTS_FILE_READ |
			SEC_RIGHTS_FILE_WRITE;
		io2->generic.in.share_access = 
			NTCREATEX_SHARE_ACCESS_READ | 
			NTCREATEX_SHARE_ACCESS_WRITE;
		status = ntvfs->ops->open(ntvfs, req, io2);
		break;
	case RAW_OPEN_SMB2:
		switch (io->smb2.in.oplock_level) {
		case SMB2_OPLOCK_LEVEL_BATCH:
			io2->generic.in.flags = NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK |
						NTCREATEX_FLAGS_REQUEST_OPLOCK;
			break;
		case SMB2_OPLOCK_LEVEL_EXCLUSIVE:
			io2->generic.in.flags = NTCREATEX_FLAGS_REQUEST_OPLOCK;
			break;
		default:
			io2->generic.in.flags = 0;
			break;
		}
		io2->generic.in.root_fid.fnum	= 0;
		io2->generic.in.access_mask	= io->smb2.in.desired_access;
		io2->generic.in.alloc_size	= io->smb2.in.alloc_size;
		io2->generic.in.file_attr	= io->smb2.in.file_attributes;
		io2->generic.in.share_access	= io->smb2.in.share_access;
		io2->generic.in.open_disposition= io->smb2.in.create_disposition;
		io2->generic.in.create_options	= io->smb2.in.create_options;
		io2->generic.in.impersonation	= io->smb2.in.impersonation_level;
		io2->generic.in.security_flags	= 0;
		io2->generic.in.fname		= io->smb2.in.fname;
		io2->generic.in.sec_desc	= io->smb2.in.sec_desc;
		io2->generic.in.ea_list		= &io->smb2.in.eas;
		io2->generic.in.query_maximal_access = io->smb2.in.query_maximal_access; 
		io2->generic.in.private_flags	= 0;

		/* we don't support timewarp yet */
		if (io->smb2.in.timewarp != 0) {
			status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
			break;
		}

		/* we need to check these bits before we check the private mask */
		if (io2->generic.in.create_options & SMB2_CREATE_OPTIONS_NOT_SUPPORTED_MASK) {
			DEBUG(2,(__location__ " create_options 0x%x not supported\n",
				 io2->generic.in.create_options));
			status = NT_STATUS_NOT_SUPPORTED;
			break;
		}

		/* TODO: find out why only SMB2 ignores these */
		io2->generic.in.create_options &= ~NTCREATEX_OPTIONS_SYNC_ALERT;
		io2->generic.in.create_options &= ~NTCREATEX_OPTIONS_ASYNC_ALERT;

		status = ntvfs->ops->open(ntvfs, req, io2);		
		break;

	default:
		status = NT_STATUS_INVALID_LEVEL;
		break;
	}
done:
	return ntvfs_map_async_finish(req, status);
}


/* 
   NTVFS any to fsinfo mapper
*/
static NTSTATUS ntvfs_map_fsinfo_finish(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req,
				      union smb_fsinfo *fs,
				      union smb_fsinfo *fs2,
				      NTSTATUS status)
{
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* and convert it to the required level */
	switch (fs->generic.level) {
	case RAW_QFS_DSKATTR: {
		/* map from generic to DSKATTR */
		unsigned int bpunit = 64;

		/* we need to scale the sizes to fit */
		for (bpunit=64; bpunit<0x10000; bpunit *= 2) {
			if (fs2->generic.out.blocks_total * (double)fs2->generic.out.block_size < bpunit * 512 * 65535.0) {
				break;
			}
		}

		fs->dskattr.out.blocks_per_unit = bpunit;
		fs->dskattr.out.block_size = 512;
		fs->dskattr.out.units_total = 
			(fs2->generic.out.blocks_total * (double)fs2->generic.out.block_size) / (bpunit * 512);
		fs->dskattr.out.units_free  = 
			(fs2->generic.out.blocks_free  * (double)fs2->generic.out.block_size) / (bpunit * 512);

		/* we must return a maximum of 2G to old DOS systems, or they get very confused */
		if (bpunit > 64 && req->ctx->protocol <= PROTOCOL_LANMAN2) {
			fs->dskattr.out.blocks_per_unit = 64;
			fs->dskattr.out.units_total = 0xFFFF;
			fs->dskattr.out.units_free = 0xFFFF;
		}
		return NT_STATUS_OK;
	}

	case RAW_QFS_ALLOCATION:
		fs->allocation.out.fs_id = fs2->generic.out.fs_id;
		fs->allocation.out.total_alloc_units = fs2->generic.out.blocks_total;
		fs->allocation.out.avail_alloc_units = fs2->generic.out.blocks_free;
		fs->allocation.out.sectors_per_unit = 1;
		fs->allocation.out.bytes_per_sector = fs2->generic.out.block_size;
		return NT_STATUS_OK;

	case RAW_QFS_VOLUME:
		fs->volume.out.serial_number = fs2->generic.out.serial_number;
		fs->volume.out.volume_name.s = fs2->generic.out.volume_name;
		return NT_STATUS_OK;

	case RAW_QFS_VOLUME_INFO:
	case RAW_QFS_VOLUME_INFORMATION:
		fs->volume_info.out.create_time = fs2->generic.out.create_time;
		fs->volume_info.out.serial_number = fs2->generic.out.serial_number;
		fs->volume_info.out.volume_name.s = fs2->generic.out.volume_name;
		return NT_STATUS_OK;

	case RAW_QFS_SIZE_INFO:
	case RAW_QFS_SIZE_INFORMATION:
		fs->size_info.out.total_alloc_units = fs2->generic.out.blocks_total;
		fs->size_info.out.avail_alloc_units = fs2->generic.out.blocks_free;
		fs->size_info.out.sectors_per_unit = 1;
		fs->size_info.out.bytes_per_sector = fs2->generic.out.block_size;
		return NT_STATUS_OK;

	case RAW_QFS_DEVICE_INFO:
	case RAW_QFS_DEVICE_INFORMATION:
		fs->device_info.out.device_type = fs2->generic.out.device_type;
		fs->device_info.out.characteristics = fs2->generic.out.device_characteristics;
		return NT_STATUS_OK;

	case RAW_QFS_ATTRIBUTE_INFO:
	case RAW_QFS_ATTRIBUTE_INFORMATION:
		fs->attribute_info.out.fs_attr = fs2->generic.out.fs_attr;
		fs->attribute_info.out.max_file_component_length = fs2->generic.out.max_file_component_length;
		fs->attribute_info.out.fs_type.s = fs2->generic.out.fs_type;
		return NT_STATUS_OK;

	case RAW_QFS_QUOTA_INFORMATION:
		ZERO_STRUCT(fs->quota_information.out.unknown);
		fs->quota_information.out.quota_soft = fs2->generic.out.quota_soft;
		fs->quota_information.out.quota_hard = fs2->generic.out.quota_hard;
		fs->quota_information.out.quota_flags = fs2->generic.out.quota_flags;
		return NT_STATUS_OK;

	case RAW_QFS_FULL_SIZE_INFORMATION:
		fs->full_size_information.out.total_alloc_units = fs2->generic.out.blocks_total;
		fs->full_size_information.out.call_avail_alloc_units = fs2->generic.out.blocks_free;
		fs->full_size_information.out.actual_avail_alloc_units = fs2->generic.out.blocks_free;
		fs->full_size_information.out.sectors_per_unit = 1;
		fs->full_size_information.out.bytes_per_sector = fs2->generic.out.block_size;
		return NT_STATUS_OK;

	case RAW_QFS_OBJECTID_INFORMATION:
		fs->objectid_information.out.guid = fs2->generic.out.guid;
		ZERO_STRUCT(fs->objectid_information.out.unknown);
		return NT_STATUS_OK;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}
}

/*
   NTVFS fsinfo any to generic mapper
*/
NTSTATUS ntvfs_map_fsinfo(struct ntvfs_module_context *ntvfs,
			  struct ntvfs_request *req,
			  union smb_fsinfo *fs)
{
	NTSTATUS status;
	union smb_fsinfo *fs2;

	fs2 = talloc(req, union smb_fsinfo);
	if (fs2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	if (fs->generic.level == RAW_QFS_GENERIC) {
		return NT_STATUS_INVALID_LEVEL;
	}

	status = ntvfs_map_async_setup(ntvfs, req, fs, fs2,
				       (second_stage_t)ntvfs_map_fsinfo_finish);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* ask the backend for the generic info */
	fs2->generic.level = RAW_QFS_GENERIC;

	status = ntvfs->ops->fsinfo(ntvfs, req, fs2);
	return ntvfs_map_async_finish(req, status);
}


/* 
   NTVFS fileinfo generic to any mapper
*/
NTSTATUS ntvfs_map_fileinfo(TALLOC_CTX *mem_ctx,
				     union smb_fileinfo *info, 
				     union smb_fileinfo *info2)
{
	int i;
	/* and convert it to the required level using results in info2 */
	switch (info->generic.level) {
	case RAW_FILEINFO_GETATTR:
		info->getattr.out.attrib = info2->generic.out.attrib & 0xff;
		info->getattr.out.size = info2->generic.out.size;
		info->getattr.out.write_time = nt_time_to_unix(info2->generic.out.write_time);
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_GETATTRE:
		info->getattre.out.attrib = info2->generic.out.attrib;
		info->getattre.out.size = info2->generic.out.size;
		info->getattre.out.write_time = nt_time_to_unix(info2->generic.out.write_time);
		info->getattre.out.create_time = nt_time_to_unix(info2->generic.out.create_time);
		info->getattre.out.access_time = nt_time_to_unix(info2->generic.out.access_time);
		info->getattre.out.alloc_size = info2->generic.out.alloc_size;
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
		info->network_open_information.out.create_time = info2->generic.out.create_time;
		info->network_open_information.out.access_time = info2->generic.out.access_time;
		info->network_open_information.out.write_time =  info2->generic.out.write_time;
		info->network_open_information.out.change_time = info2->generic.out.change_time;
		info->network_open_information.out.alloc_size = info2->generic.out.alloc_size;
		info->network_open_information.out.size = info2->generic.out.size;
		info->network_open_information.out.attrib = info2->generic.out.attrib;
		return NT_STATUS_OK;

	case RAW_FILEINFO_ALL_INFO:
	case RAW_FILEINFO_ALL_INFORMATION:
		info->all_info.out.create_time = info2->generic.out.create_time;
		info->all_info.out.access_time = info2->generic.out.access_time;
		info->all_info.out.write_time =  info2->generic.out.write_time;
		info->all_info.out.change_time = info2->generic.out.change_time;
		info->all_info.out.attrib = info2->generic.out.attrib;
		info->all_info.out.alloc_size = info2->generic.out.alloc_size;
		info->all_info.out.size = info2->generic.out.size;
		info->all_info.out.nlink = info2->generic.out.nlink;
		info->all_info.out.delete_pending = info2->generic.out.delete_pending;
		info->all_info.out.directory = info2->generic.out.directory;
		info->all_info.out.ea_size = info2->generic.out.ea_size;
		info->all_info.out.fname.s = info2->generic.out.fname.s;
		info->all_info.out.fname.private_length = info2->generic.out.fname.private_length;
		return NT_STATUS_OK;

	case RAW_FILEINFO_BASIC_INFO:
	case RAW_FILEINFO_BASIC_INFORMATION:
		info->basic_info.out.create_time = info2->generic.out.create_time;
		info->basic_info.out.access_time = info2->generic.out.access_time;
		info->basic_info.out.write_time = info2->generic.out.write_time;
		info->basic_info.out.change_time = info2->generic.out.change_time;
		info->basic_info.out.attrib = info2->generic.out.attrib;
		return NT_STATUS_OK;

	case RAW_FILEINFO_STANDARD:
		info->standard.out.create_time = nt_time_to_unix(info2->generic.out.create_time);
		info->standard.out.access_time = nt_time_to_unix(info2->generic.out.access_time);
		info->standard.out.write_time = nt_time_to_unix(info2->generic.out.write_time);
		info->standard.out.size = info2->generic.out.size;
		info->standard.out.alloc_size = info2->generic.out.alloc_size;
		info->standard.out.attrib = info2->generic.out.attrib;
		return NT_STATUS_OK;

	case RAW_FILEINFO_EA_SIZE:
		info->ea_size.out.create_time = nt_time_to_unix(info2->generic.out.create_time);
		info->ea_size.out.access_time = nt_time_to_unix(info2->generic.out.access_time);
		info->ea_size.out.write_time = nt_time_to_unix(info2->generic.out.write_time);
		info->ea_size.out.size = info2->generic.out.size;
		info->ea_size.out.alloc_size = info2->generic.out.alloc_size;
		info->ea_size.out.attrib = info2->generic.out.attrib;
		info->ea_size.out.ea_size = info2->generic.out.ea_size;
		return NT_STATUS_OK;

	case RAW_FILEINFO_STANDARD_INFO:
	case RAW_FILEINFO_STANDARD_INFORMATION:
		info->standard_info.out.alloc_size = info2->generic.out.alloc_size;
		info->standard_info.out.size = info2->generic.out.size;
		info->standard_info.out.nlink = info2->generic.out.nlink;
		info->standard_info.out.delete_pending = info2->generic.out.delete_pending;
		info->standard_info.out.directory = info2->generic.out.directory;
		return NT_STATUS_OK;

	case RAW_FILEINFO_INTERNAL_INFORMATION:
		info->internal_information.out.file_id = info2->generic.out.file_id;
		return NT_STATUS_OK;

	case RAW_FILEINFO_EA_INFO:
	case RAW_FILEINFO_EA_INFORMATION:
		info->ea_info.out.ea_size = info2->generic.out.ea_size;
		return NT_STATUS_OK;

	case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
		info->attribute_tag_information.out.attrib = info2->generic.out.attrib;
		info->attribute_tag_information.out.reparse_tag = info2->generic.out.reparse_tag;
		return NT_STATUS_OK;

	case RAW_FILEINFO_STREAM_INFO:
	case RAW_FILEINFO_STREAM_INFORMATION:
		info->stream_info.out.num_streams = info2->generic.out.num_streams;
		if (info->stream_info.out.num_streams > 0) {
			info->stream_info.out.streams = 
				talloc_array(mem_ctx, 
					       struct stream_struct,
					       info->stream_info.out.num_streams);
			if (!info->stream_info.out.streams) {
				DEBUG(2,("ntvfs_map_fileinfo: no memory for %d streams\n",
					info->stream_info.out.num_streams));
				return NT_STATUS_NO_MEMORY;
			}
			for (i=0; i < info->stream_info.out.num_streams; i++) {
				info->stream_info.out.streams[i] = info2->generic.out.streams[i];
				info->stream_info.out.streams[i].stream_name.s = 
					talloc_strdup(info->stream_info.out.streams,
						      info2->generic.out.streams[i].stream_name.s);
				if (!info->stream_info.out.streams[i].stream_name.s) {
					DEBUG(2,("ntvfs_map_fileinfo: no memory for stream_name\n"));
					return NT_STATUS_NO_MEMORY;
				}
			}
		}
		return NT_STATUS_OK;

	case RAW_FILEINFO_NAME_INFO:
	case RAW_FILEINFO_NAME_INFORMATION:
		info->name_info.out.fname.s = talloc_strdup(mem_ctx, info2->generic.out.fname.s);
		NT_STATUS_HAVE_NO_MEMORY(info->name_info.out.fname.s);
		info->name_info.out.fname.private_length = info2->generic.out.fname.private_length;
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_ALT_NAME_INFO:
	case RAW_FILEINFO_ALT_NAME_INFORMATION:
		info->alt_name_info.out.fname.s = talloc_strdup(mem_ctx, info2->generic.out.alt_fname.s);
		NT_STATUS_HAVE_NO_MEMORY(info->alt_name_info.out.fname.s);
		info->alt_name_info.out.fname.private_length = info2->generic.out.alt_fname.private_length;
		return NT_STATUS_OK;
	
	case RAW_FILEINFO_POSITION_INFORMATION:
		info->position_information.out.position = info2->generic.out.position;
		return NT_STATUS_OK;
	
	case RAW_FILEINFO_ALL_EAS:
		info->all_eas.out.num_eas = info2->generic.out.num_eas;
		if (info->all_eas.out.num_eas > 0) {
			info->all_eas.out.eas = talloc_array(mem_ctx, 
							       struct ea_struct,
							       info->all_eas.out.num_eas);
			if (!info->all_eas.out.eas) {
				DEBUG(2,("ntvfs_map_fileinfo: no memory for %d eas\n",
					info->all_eas.out.num_eas));
				return NT_STATUS_NO_MEMORY;
			}
			for (i = 0; i < info->all_eas.out.num_eas; i++) {
				info->all_eas.out.eas[i] = info2->generic.out.eas[i];
				info->all_eas.out.eas[i].name.s = 
					talloc_strdup(info->all_eas.out.eas,
						      info2->generic.out.eas[i].name.s);
				if (!info->all_eas.out.eas[i].name.s) {
					DEBUG(2,("ntvfs_map_fileinfo: no memory for stream_name\n"));
					return NT_STATUS_NO_MEMORY;
				}
				info->all_eas.out.eas[i].value.data = 
					(uint8_t *)talloc_memdup(info->all_eas.out.eas,
						info2->generic.out.eas[i].value.data,
						info2->generic.out.eas[i].value.length);
				if (!info->all_eas.out.eas[i].value.data) {
					DEBUG(2,("ntvfs_map_fileinfo: no memory for stream_name\n"));
					return NT_STATUS_NO_MEMORY;
				}
			}
		}
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_IS_NAME_VALID:
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_COMPRESSION_INFO:
	case RAW_FILEINFO_COMPRESSION_INFORMATION:
		info->compression_info.out.compressed_size = info2->generic.out.compressed_size;
		info->compression_info.out.format = info2->generic.out.format;
		info->compression_info.out.unit_shift = info2->generic.out.unit_shift;
		info->compression_info.out.chunk_shift = info2->generic.out.chunk_shift;
		info->compression_info.out.cluster_shift = info2->generic.out.cluster_shift;
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_ACCESS_INFORMATION:
		info->access_information.out.access_flags = info2->generic.out.access_flags;
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_MODE_INFORMATION:
		info->mode_information.out.mode = info2->generic.out.mode;
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_ALIGNMENT_INFORMATION:
		info->alignment_information.out.alignment_requirement =
			info2->generic.out.alignment_requirement;
		return NT_STATUS_OK;
#if 0	
	case RAW_FILEINFO_UNIX_BASIC:
		info->unix_basic_info.out.end_of_file = info2->generic.out.end_of_file;
		info->unix_basic_info.out.num_bytes = info2->generic.out.size;
		info->unix_basic_info.out.status_change_time = info2->generic.out.change_time;
		info->unix_basic_info.out.access_time = info2->generic.out.access_time;
		info->unix_basic_info.out.change_time = info2->generic.out.change_time;
		info->unix_basic_info.out.uid = info2->generic.out.uid;
		info->unix_basic_info.out.gid = info2->generic.out.gid;
		info->unix_basic_info.out.file_type = info2->generic.out.file_type;
		info->unix_basic_info.out.dev_major = info2->generic.out.device;
		info->unix_basic_info.out.dev_minor = info2->generic.out.device;
		info->unix_basic_info.out.unique_id = info2->generic.out.inode;
		info->unix_basic_info.out.permissions = info2->generic.out.permissions;
		info->unix_basic_info.out.nlink = info2->generic.out.nlink;
		return NT_STATUS_OK;
		
	case RAW_FILEINFO_UNIX_LINK:
		info->unix_link_info.out.link_dest = info2->generic.out.link_dest;
		return NT_STATUS_OK;
#endif
	default:
		return NT_STATUS_INVALID_LEVEL;
	}
}

/* 
   NTVFS any to fileinfo mapper
*/
static NTSTATUS ntvfs_map_qfileinfo_finish(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req,
				      union smb_fileinfo *info,
				      union smb_fileinfo *info2,
				      NTSTATUS status)
{
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	return ntvfs_map_fileinfo(req, info, info2);
}

/*
   NTVFS fileinfo generic to any mapper
*/
NTSTATUS ntvfs_map_qfileinfo(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req,
				      union smb_fileinfo *info)
{
	NTSTATUS status;
	union smb_fileinfo *info2;

	info2 = talloc(req, union smb_fileinfo);
	if (info2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	if (info->generic.level == RAW_FILEINFO_GENERIC) {
		return NT_STATUS_INVALID_LEVEL;
	}

	status = ntvfs_map_async_setup(ntvfs, req, info, info2,
				       (second_stage_t)ntvfs_map_qfileinfo_finish);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* ask the backend for the generic info */
	info2->generic.level = RAW_FILEINFO_GENERIC;
	info2->generic.in.file.ntvfs= info->generic.in.file.ntvfs;

	status = ntvfs->ops->qfileinfo(ntvfs, req, info2);
	return ntvfs_map_async_finish(req, status);
}

/*
   NTVFS any to fileinfo mapper
*/
static NTSTATUS ntvfs_map_qpathinfo_finish(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req,
				      union smb_fileinfo *info,
				      union smb_fileinfo *info2,
				      NTSTATUS status)
{
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	return ntvfs_map_fileinfo(req, info, info2);
}

/* 
   NTVFS pathinfo generic to any mapper
*/
NTSTATUS ntvfs_map_qpathinfo(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req,
				      union smb_fileinfo *info)
{
	NTSTATUS status;
	union smb_fileinfo *info2;

	info2 = talloc(req, union smb_fileinfo);
	if (info2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	if (info->generic.level == RAW_FILEINFO_GENERIC) {
		return NT_STATUS_INVALID_LEVEL;
	}

	status = ntvfs_map_async_setup(ntvfs, req, info, info2,
				       (second_stage_t)ntvfs_map_qpathinfo_finish);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* ask the backend for the generic info */
	info2->generic.level		= RAW_FILEINFO_GENERIC;
	info2->generic.in.file.path	= info->generic.in.file.path;

	status = ntvfs->ops->qpathinfo(ntvfs, req, info2);
	return ntvfs_map_async_finish(req, status);
}


/* 
   NTVFS lock generic to any mapper
*/
NTSTATUS ntvfs_map_lock(struct ntvfs_module_context *ntvfs,
			struct ntvfs_request *req,
			union smb_lock *lck)
{
	union smb_lock *lck2;
	struct smb_lock_entry *locks;

	lck2 = talloc(req, union smb_lock);
	if (lck2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	locks = talloc_array(lck2, struct smb_lock_entry, 1);
	if (locks == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	switch (lck->generic.level) {
	case RAW_LOCK_LOCKX:
		return NT_STATUS_INVALID_LEVEL;

	case RAW_LOCK_LOCK:
		lck2->generic.level = RAW_LOCK_GENERIC;
		lck2->generic.in.file.ntvfs= lck->lock.in.file.ntvfs;
		lck2->generic.in.mode = 0;
		lck2->generic.in.timeout = 0;
		lck2->generic.in.ulock_cnt = 0;
		lck2->generic.in.lock_cnt = 1;
		lck2->generic.in.locks = locks;
		locks->pid = req->smbpid;
		locks->offset = lck->lock.in.offset;
		locks->count = lck->lock.in.count;
		break;

	case RAW_LOCK_UNLOCK:
		lck2->generic.level = RAW_LOCK_GENERIC;
		lck2->generic.in.file.ntvfs= lck->unlock.in.file.ntvfs;
		lck2->generic.in.mode = 0;
		lck2->generic.in.timeout = 0;
		lck2->generic.in.ulock_cnt = 1;
		lck2->generic.in.lock_cnt = 0;
		lck2->generic.in.locks = locks;
		locks->pid = req->smbpid;
		locks->offset = lck->unlock.in.offset;
		locks->count = lck->unlock.in.count;
		break;

	case RAW_LOCK_SMB2: {
		/* this is only approximate! We need to change the
		   generic structure to fix this properly */
		int i;
		bool isunlock;
		if (lck->smb2.in.lock_count < 1) {
			return NT_STATUS_INVALID_PARAMETER;
		}

		lck2->generic.level = RAW_LOCK_GENERIC;
		lck2->generic.in.file.ntvfs= lck->smb2.in.file.ntvfs;
		lck2->generic.in.timeout = UINT32_MAX;
		lck2->generic.in.mode = 0;
		lck2->generic.in.lock_cnt = 0;
		lck2->generic.in.ulock_cnt = 0;
		lck2->generic.in.locks = talloc_zero_array(lck2, struct smb_lock_entry, 
							   lck->smb2.in.lock_count);
		if (lck2->generic.in.locks == NULL) {
			return NT_STATUS_NO_MEMORY;
		}
		/* only the first lock gives the UNLOCK bit - see
		   MS-SMB2 3.3.5.14 */
		if (lck->smb2.in.locks[0].flags & SMB2_LOCK_FLAG_UNLOCK) {
			if (lck->smb2.in.locks[0].flags & SMB2_LOCK_FLAG_FAIL_IMMEDIATELY) {
				return NT_STATUS_INVALID_PARAMETER;
			}
			lck2->generic.in.ulock_cnt = lck->smb2.in.lock_count;
			isunlock = true;
		} else {
			lck2->generic.in.lock_cnt = lck->smb2.in.lock_count;
			isunlock = false;
		}
		for (i=0;i<lck->smb2.in.lock_count;i++) {
			if (!isunlock &&
			    lck->smb2.in.locks[i].flags == SMB2_LOCK_FLAG_NONE) {
				return NT_STATUS_INVALID_PARAMETER;
			}

			if (lck->smb2.in.locks[i].flags & ~SMB2_LOCK_FLAG_ALL_MASK) {
				return NT_STATUS_INVALID_PARAMETER;
			}

			if (isunlock && 
			    (lck->smb2.in.locks[i].flags & 
			     (SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_EXCLUSIVE))) {
				return NT_STATUS_INVALID_PARAMETER;
			}
			if (!isunlock && 
			    (lck->smb2.in.locks[i].flags & SMB2_LOCK_FLAG_UNLOCK)) {
				return NT_STATUS_INVALID_PARAMETER;
			}
			lck2->generic.in.locks[i].pid    = req->smbpid;
			lck2->generic.in.locks[i].offset = lck->smb2.in.locks[i].offset;
			lck2->generic.in.locks[i].count  = lck->smb2.in.locks[i].length;
			if (!(lck->smb2.in.locks[i].flags & SMB2_LOCK_FLAG_EXCLUSIVE)) {
				lck2->generic.in.mode = LOCKING_ANDX_SHARED_LOCK;
			}
			if (lck->smb2.in.locks[i].flags & SMB2_LOCK_FLAG_FAIL_IMMEDIATELY) {
				lck2->generic.in.timeout = 0;
			}
		}
		/* initialize output value */
		lck->smb2.out.reserved = 0;
		break;
	}

	case RAW_LOCK_SMB2_BREAK:
		lck2->generic.level		= RAW_LOCK_GENERIC;
		lck2->generic.in.file.ntvfs	= lck->smb2_break.in.file.ntvfs;
		lck2->generic.in.mode		= LOCKING_ANDX_OPLOCK_RELEASE |
						  ((lck->smb2_break.in.oplock_level << 8) & 0xFF00);
		lck2->generic.in.timeout	= 0;
		lck2->generic.in.ulock_cnt	= 0;
		lck2->generic.in.lock_cnt	= 0;
		lck2->generic.in.locks		= NULL;

		/* initialize output value */
		lck->smb2_break.out.oplock_level= lck->smb2_break.in.oplock_level;
		lck->smb2_break.out.reserved	= lck->smb2_break.in.reserved;
		lck->smb2_break.out.reserved2	= lck->smb2_break.in.reserved2;
		lck->smb2_break.out.file	= lck->smb2_break.in.file;
		break;
	}

	/* 
	 * we don't need to call ntvfs_map_async_setup() here,
	 * as lock() doesn't have any output fields
	 */

	return ntvfs->ops->lock(ntvfs, req, lck2);
}


/* 
   NTVFS write generic to any mapper
*/
static NTSTATUS ntvfs_map_write_finish(struct ntvfs_module_context *ntvfs,
				       struct ntvfs_request *req,
				       union smb_write *wr, 
				       union smb_write *wr2, 
				       NTSTATUS status)
{
	union smb_lock *lck;
	union smb_close *cl;
	unsigned int state;

	if (NT_STATUS_IS_ERR(status)) {
		return status;
	}

	switch (wr->generic.level) {
	case RAW_WRITE_WRITE:
		wr->write.out.nwritten    = wr2->generic.out.nwritten;
		break;

	case RAW_WRITE_WRITEUNLOCK:
		wr->writeunlock.out.nwritten = wr2->generic.out.nwritten;

		lck = talloc(wr2, union smb_lock);
		if (lck == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		lck->unlock.level		= RAW_LOCK_UNLOCK;
		lck->unlock.in.file.ntvfs	= wr->writeunlock.in.file.ntvfs;
		lck->unlock.in.count		= wr->writeunlock.in.count;
		lck->unlock.in.offset		= wr->writeunlock.in.offset;

		if (lck->unlock.in.count != 0) {
			/* do the lock sync for now */
			state = req->async_states->state;
			req->async_states->state &= ~NTVFS_ASYNC_STATE_MAY_ASYNC;
			status = ntvfs->ops->lock(ntvfs, req, lck);
			req->async_states->state = state;
		}
		break;

	case RAW_WRITE_WRITECLOSE:
		wr->writeclose.out.nwritten    = wr2->generic.out.nwritten;

		cl = talloc(wr2, union smb_close);
		if (cl == NULL) {
			return NT_STATUS_NO_MEMORY;
		}

		cl->close.level		= RAW_CLOSE_CLOSE;
		cl->close.in.file.ntvfs	= wr->writeclose.in.file.ntvfs;
		cl->close.in.write_time	= wr->writeclose.in.mtime;

		if (wr2->generic.in.count != 0) {
			/* do the close sync for now */
			state = req->async_states->state;
			req->async_states->state &= ~NTVFS_ASYNC_STATE_MAY_ASYNC;
			status = ntvfs->ops->close(ntvfs, req, cl);
			req->async_states->state = state;
		}
		break;

	case RAW_WRITE_SPLWRITE:
		break;

	case RAW_WRITE_SMB2:
		wr->smb2.out._pad	= 0;
		wr->smb2.out.nwritten	= wr2->generic.out.nwritten;
		wr->smb2.out.unknown1	= 0;
		break;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	return status;
}


/* 
   NTVFS write generic to any mapper
*/
NTSTATUS ntvfs_map_write(struct ntvfs_module_context *ntvfs,
				  struct ntvfs_request *req,
				  union smb_write *wr)
{
	union smb_write *wr2;
	NTSTATUS status;

	wr2 = talloc(req, union smb_write);
	if (wr2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = ntvfs_map_async_setup(ntvfs, req, wr, wr2, 
				       (second_stage_t)ntvfs_map_write_finish);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	wr2->writex.level = RAW_WRITE_GENERIC;

	switch (wr->generic.level) {
	case RAW_WRITE_WRITEX:
		status = NT_STATUS_INVALID_LEVEL;
		break;

	case RAW_WRITE_WRITE:
		wr2->writex.in.file.ntvfs= wr->write.in.file.ntvfs;
		wr2->writex.in.offset    = wr->write.in.offset;
		wr2->writex.in.wmode     = 0;
		wr2->writex.in.remaining = wr->write.in.remaining;
		wr2->writex.in.count     = wr->write.in.count;
		wr2->writex.in.data      = wr->write.in.data;
		status = ntvfs->ops->write(ntvfs, req, wr2);
		break;

	case RAW_WRITE_WRITEUNLOCK:
		wr2->writex.in.file.ntvfs= wr->writeunlock.in.file.ntvfs;
		wr2->writex.in.offset    = wr->writeunlock.in.offset;
		wr2->writex.in.wmode     = 0;
		wr2->writex.in.remaining = wr->writeunlock.in.remaining;
		wr2->writex.in.count     = wr->writeunlock.in.count;
		wr2->writex.in.data      = wr->writeunlock.in.data;
		status = ntvfs->ops->write(ntvfs, req, wr2);
		break;

	case RAW_WRITE_WRITECLOSE:
		wr2->writex.in.file.ntvfs= wr->writeclose.in.file.ntvfs;
		wr2->writex.in.offset    = wr->writeclose.in.offset;
		wr2->writex.in.wmode     = 0;
		wr2->writex.in.remaining = 0;
		wr2->writex.in.count     = wr->writeclose.in.count;
		wr2->writex.in.data      = wr->writeclose.in.data;
		status = ntvfs->ops->write(ntvfs, req, wr2);
		break;

	case RAW_WRITE_SPLWRITE:
		wr2->writex.in.file.ntvfs= wr->splwrite.in.file.ntvfs;
		wr2->writex.in.offset    = 0;
		wr2->writex.in.wmode     = 0;
		wr2->writex.in.remaining = 0;
		wr2->writex.in.count     = wr->splwrite.in.count;
		wr2->writex.in.data      = wr->splwrite.in.data;
		status = ntvfs->ops->write(ntvfs, req, wr2);
		break;

	case RAW_WRITE_SMB2:
		wr2->writex.in.file.ntvfs= wr->smb2.in.file.ntvfs;
		wr2->writex.in.offset    = wr->smb2.in.offset;
		wr2->writex.in.wmode     = 0;
		wr2->writex.in.remaining = 0;
		wr2->writex.in.count     = wr->smb2.in.data.length;
		wr2->writex.in.data      = wr->smb2.in.data.data;
		status = ntvfs->ops->write(ntvfs, req, wr2);
	}

	return ntvfs_map_async_finish(req, status);
}


/* 
   NTVFS read generic to any mapper - finish the out mapping
*/
static NTSTATUS ntvfs_map_read_finish(struct ntvfs_module_context *ntvfs,
				      struct ntvfs_request *req, 
				      union smb_read *rd, 
				      union smb_read *rd2,
				      NTSTATUS status)
{
	switch (rd->generic.level) {
	case RAW_READ_READ:
		rd->read.out.nread	= rd2->generic.out.nread;
		break;
	case RAW_READ_READBRAW:
		rd->readbraw.out.nread	= rd2->generic.out.nread;
		break;
	case RAW_READ_LOCKREAD:
		rd->lockread.out.nread	= rd2->generic.out.nread;
		break;
	case RAW_READ_SMB2:
		rd->smb2.out.data.length= rd2->generic.out.nread;
		rd->smb2.out.remaining	= 0;
		rd->smb2.out.reserved	= 0;
		break;
	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	return status;
}

/* 
   NTVFS read* to readx mapper
*/
NTSTATUS ntvfs_map_read(struct ntvfs_module_context *ntvfs,
				 struct ntvfs_request *req,
				 union smb_read *rd)
{
	union smb_read *rd2;
	union smb_lock *lck;
	NTSTATUS status;
	unsigned int state;

	rd2 = talloc(req, union smb_read);
	if (rd2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	status = ntvfs_map_async_setup(ntvfs, req, rd, rd2, 
				       (second_stage_t)ntvfs_map_read_finish);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	rd2->readx.level = RAW_READ_READX;
	rd2->readx.in.read_for_execute = false;

	switch (rd->generic.level) {
	case RAW_READ_READX:
		status = NT_STATUS_INVALID_LEVEL;
		break;

	case RAW_READ_READ:
		rd2->readx.in.file.ntvfs= rd->read.in.file.ntvfs;
		rd2->readx.in.offset    = rd->read.in.offset;
		rd2->readx.in.mincnt    = rd->read.in.count;
		rd2->readx.in.maxcnt    = rd->read.in.count;
		rd2->readx.in.remaining = rd->read.in.remaining;
		rd2->readx.out.data     = rd->read.out.data;
		status = ntvfs->ops->read(ntvfs, req, rd2);
		break;

	case RAW_READ_READBRAW:
		rd2->readx.in.file.ntvfs= rd->readbraw.in.file.ntvfs;
		rd2->readx.in.offset    = rd->readbraw.in.offset;
		rd2->readx.in.mincnt    = rd->readbraw.in.mincnt;
		rd2->readx.in.maxcnt    = rd->readbraw.in.maxcnt;
		rd2->readx.in.remaining = 0;
		rd2->readx.out.data     = rd->readbraw.out.data;
		status = ntvfs->ops->read(ntvfs, req, rd2);
		break;

	case RAW_READ_LOCKREAD:
		/* do the initial lock sync for now */
		state = req->async_states->state;
		req->async_states->state &= ~NTVFS_ASYNC_STATE_MAY_ASYNC;

		lck = talloc(rd2, union smb_lock);
		if (lck == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto done;
		}
		lck->lock.level		= RAW_LOCK_LOCK;
		lck->lock.in.file.ntvfs	= rd->lockread.in.file.ntvfs;
		lck->lock.in.count	= rd->lockread.in.count;
		lck->lock.in.offset	= rd->lockread.in.offset;
		status = ntvfs->ops->lock(ntvfs, req, lck);
		req->async_states->state = state;

		rd2->readx.in.file.ntvfs= rd->lockread.in.file.ntvfs;
		rd2->readx.in.offset    = rd->lockread.in.offset;
		rd2->readx.in.mincnt    = rd->lockread.in.count;
		rd2->readx.in.maxcnt    = rd->lockread.in.count;
		rd2->readx.in.remaining = rd->lockread.in.remaining;
		rd2->readx.out.data     = rd->lockread.out.data;

		if (NT_STATUS_IS_OK(status)) {
			status = ntvfs->ops->read(ntvfs, req, rd2);
		}
		break;

	case RAW_READ_SMB2:
		rd2->readx.in.file.ntvfs= rd->smb2.in.file.ntvfs;
		rd2->readx.in.offset    = rd->smb2.in.offset;
		rd2->readx.in.mincnt    = rd->smb2.in.min_count;
		rd2->readx.in.maxcnt    = rd->smb2.in.length;
		rd2->readx.in.remaining = 0;
		rd2->readx.out.data     = rd->smb2.out.data.data;
		status = ntvfs->ops->read(ntvfs, req, rd2);
		break;
	}

done:
	return ntvfs_map_async_finish(req, status);
}


/* 
   NTVFS close generic to any mapper
*/
static NTSTATUS ntvfs_map_close_finish(struct ntvfs_module_context *ntvfs,
					struct ntvfs_request *req,
					union smb_close *cl, 
					union smb_close *cl2, 
					NTSTATUS status)
{
	NT_STATUS_NOT_OK_RETURN(status);

	switch (cl->generic.level) {
	case RAW_CLOSE_SMB2:
		cl->smb2.out.flags        = cl2->generic.out.flags;
		cl->smb2.out._pad         = 0;
		cl->smb2.out.create_time  = cl2->generic.out.create_time;
		cl->smb2.out.access_time  = cl2->generic.out.access_time;
		cl->smb2.out.write_time   = cl2->generic.out.write_time;
		cl->smb2.out.change_time  = cl2->generic.out.change_time;
		cl->smb2.out.alloc_size   = cl2->generic.out.alloc_size;
		cl->smb2.out.size         = cl2->generic.out.size;
		cl->smb2.out.file_attr    = cl2->generic.out.file_attr;
		break;
	default:
		break;
	}

	return status;
}

/* 
   NTVFS close generic to any mapper
*/
NTSTATUS ntvfs_map_close(struct ntvfs_module_context *ntvfs,
				  struct ntvfs_request *req,
				  union smb_close *cl)
{
	union smb_close *cl2;
	NTSTATUS status;

	cl2 = talloc(req, union smb_close);
	if (cl2 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	switch (cl->generic.level) {
	case RAW_CLOSE_GENERIC:
		return NT_STATUS_INVALID_LEVEL;

	case RAW_CLOSE_CLOSE:
		cl2->generic.level		= RAW_CLOSE_GENERIC;
		cl2->generic.in.file		= cl->close.in.file;
		cl2->generic.in.write_time	= cl->close.in.write_time;
		cl2->generic.in.flags		= 0;
		break;

	case RAW_CLOSE_SPLCLOSE:
		cl2->generic.level		= RAW_CLOSE_GENERIC;
		cl2->generic.in.file		= cl->splclose.in.file;
		cl2->generic.in.write_time	= 0;
		cl2->generic.in.flags		= 0;
		break;

	case RAW_CLOSE_SMB2:
		cl2->generic.level		= RAW_CLOSE_GENERIC;
		cl2->generic.in.file		= cl->smb2.in.file;
		cl2->generic.in.write_time	= 0;
		cl2->generic.in.flags		= cl->smb2.in.flags;
		break;
	}

	status = ntvfs_map_async_setup(ntvfs, req, cl, cl2, 
				       (second_stage_t)ntvfs_map_close_finish);
	NT_STATUS_NOT_OK_RETURN(status);

	status = ntvfs->ops->close(ntvfs, req, cl2);

	return ntvfs_map_async_finish(req, status);
}

/* 
   NTVFS notify generic to any mapper
*/
static NTSTATUS ntvfs_map_notify_finish(struct ntvfs_module_context *ntvfs,
					struct ntvfs_request *req,
					union smb_notify *nt, 
					union smb_notify *nt2, 
					NTSTATUS status)
{
	NT_STATUS_NOT_OK_RETURN(status);

	switch (nt->nttrans.level) {
	case RAW_NOTIFY_SMB2:
		if (nt2->nttrans.out.num_changes == 0) {
			return STATUS_NOTIFY_ENUM_DIR;
		}
		nt->smb2.out.num_changes	= nt2->nttrans.out.num_changes;
		nt->smb2.out.changes		= talloc_steal(req, nt2->nttrans.out.changes);
		break;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	return status;
}


/* 
   NTVFS notify generic to any mapper
*/
NTSTATUS ntvfs_map_notify(struct ntvfs_module_context *ntvfs,
				   struct ntvfs_request *req,
				   union smb_notify *nt)
{
	union smb_notify *nt2;
	NTSTATUS status;

	nt2 = talloc(req, union smb_notify);
	NT_STATUS_HAVE_NO_MEMORY(nt2);

	status = ntvfs_map_async_setup(ntvfs, req, nt, nt2, 
				       (second_stage_t)ntvfs_map_notify_finish);
	NT_STATUS_NOT_OK_RETURN(status);

	nt2->nttrans.level = RAW_NOTIFY_NTTRANS;

	switch (nt->nttrans.level) {
	case RAW_NOTIFY_NTTRANS:
		status = NT_STATUS_INVALID_LEVEL;
		break;

	case RAW_NOTIFY_SMB2:
		nt2->nttrans.in.file.ntvfs		= nt->smb2.in.file.ntvfs;
		nt2->nttrans.in.buffer_size		= nt->smb2.in.buffer_size;
		nt2->nttrans.in.completion_filter	= nt->smb2.in.completion_filter;
		nt2->nttrans.in.recursive		= nt->smb2.in.recursive;
		status = ntvfs->ops->notify(ntvfs, req, nt2);
		break;
	}

	return ntvfs_map_async_finish(req, status);
}