forked from openzfs/zfs
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathzfs_vnops_windows.c
9147 lines (7736 loc) · 239 KB
/
zfs_vnops_windows.c
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2017 Jorgen Lundman <[email protected]>
* Portions Copyright 2022 Andrew Innes <[email protected]>
*/
#undef _NTDDK_
#include <ntifs.h>
#include <ntddk.h>
#include <ntddscsi.h>
#include <scsi.h>
#include <ntddcdrm.h>
#include <ntdddisk.h>
#include <ntddstor.h>
#include <ntintsafe.h>
#include <mountmgr.h>
#include <Mountdev.h>
#include <ntddvol.h>
#include <os/windows/zfs/sys/zfs_ioctl_compat.h>
#include <sys/fs/zfsdi.h>
#include <sys/driver_extension.h>
// I have no idea what black magic is needed to get ntifs.h to define these
#ifndef FsRtlEnterFileSystem
#define FsRtlEnterFileSystem() { \
KeEnterCriticalRegion(); \
}
#endif
#ifndef FsRtlExitFileSystem
#define FsRtlExitFileSystem() { \
KeLeaveCriticalRegion(); \
}
#endif
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/zfs_dir.h>
#include <sys/zfs_ioctl.h>
#include <sys/zfs_ioctl_compat.h>
#include <sys/fs/zfs.h>
#include <sys/dmu.h>
#include <sys/dmu_objset.h>
#include <sys/spa.h>
#include <sys/spa_impl.h>
#include <sys/txg.h>
#include <sys/dbuf.h>
#include <sys/zap.h>
#include <sys/sa.h>
#include <sys/zfs_vnops.h>
#include <sys/zfs_vnops_os.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/zfs_vfsops.h>
#include <sys/zfs_rlock.h>
#include <sys/zfs_ctldir.h>
#include <sys/callb.h>
#include <sys/unistd.h>
#include <sys/zfs_windows.h>
#include <sys/kstat.h>
#ifdef DEBUG_IOCOUNT
static kmutex_t GIANT_SERIAL_LOCK;
#endif
#ifdef _KERNEL
#ifndef STATUS_VOLUME_NOT_MOUNTED
#define STATUS_VOLUME_NOT_MOUNTED 0xC000001A
#endif
DRIVER_INITIALIZE DriverEntry;
unsigned int debug_vnop_osx_printf = 0;
unsigned int zfs_vnop_ignore_negatives = 0;
unsigned int zfs_vnop_ignore_positives = 0;
unsigned int zfs_vnop_create_negatives = 1;
#endif
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#endif
#define DECLARE_CRED(ap) \
cred_t *cr;
#define DECLARE_CONTEXT(ap) \
caller_context_t *ct
#define DECLARE_CRED_AND_CONTEXT(ap) \
DECLARE_CRED(ap); \
DECLARE_CONTEXT(ap)
#ifdef _KERNEL
uint64_t vnop_num_reclaims = 0;
uint64_t vnop_num_vnodes = 0;
uint64_t zfs_disable_wincache = 0;
ZFS_MODULE_RAW(zfs, disable_wincache, zfs_disable_wincache,
U64, ZMOD_RW, 0, "Disable OS caching.");
#endif
extern void UnlockAndFreeMdl(PMDL);
BOOLEAN
zfs_AcquireForLazyWrite(void *Context, BOOLEAN Wait)
{
FILE_OBJECT *fo = Context;
BOOLEAN result = FALSE;
dprintf("%s:fo %p\n", __func__, fo);
if (fo == NULL)
return (FALSE);
mount_t *zmo = fo->DeviceObject->DeviceExtension;
zfsvfs_t *zfsvfs = vfs_fsprivate(zmo);
struct vnode *vp = fo->FsContext;
if (unlikely(zfsvfs == NULL)) {
dprintf("%s: fo %p already freed zfsvfs\n", __func__, fo);
return (FALSE);
}
/* Confirm we are mounted, and stop unmounting */
if (vfs_busy(zfsvfs->z_vfs, 0) != 0)
return (FALSE);
if (zfsvfs->z_unmounted ||
zfs_enter(zfsvfs, FTAG) != 0) {
vfs_unbusy(zfsvfs->z_vfs);
return (FALSE);
}
vfs_unbusy(zfsvfs->z_vfs);
if (vp == NULL ||
VTOZ(vp) == NULL ||
VN_HOLD(vp) != 0) {
zfs_exit(zfsvfs, FTAG);
return (FALSE);
}
zfs_exit(zfsvfs, FTAG);
if (!ExAcquireResourceExclusiveLite(
vp->FileHeader.Resource, Wait)) {
dprintf("Failed\n");
goto out;
}
vnode_ref(vp);
result = TRUE;
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
out:
VN_RELE(vp);
return (result);
}
void
zfs_ReleaseFromLazyWrite(void *Context)
{
FILE_OBJECT *fo = Context;
dprintf("%s:\n", __func__);
if (fo == NULL)
return;
mount_t *zmo = fo->DeviceObject->DeviceExtension;
zfsvfs_t *zfsvfs = vfs_fsprivate(zmo);
struct vnode *vp = fo->FsContext;
if (vp != NULL && VN_HOLD(vp) == 0) {
ExReleaseResourceLite(vp->FileHeader.Resource);
vnode_rele(vp);
VN_RELE(vp);
if (IoGetTopLevelIrp() ==
(PIRP)FSRTL_CACHE_TOP_LEVEL_IRP)
IoSetTopLevelIrp(NULL);
return;
}
dprintf("%s WARNING FAILED\n", __func__);
}
BOOLEAN
zfs_AcquireForReadAhead(void *Context, BOOLEAN Wait)
{
FILE_OBJECT *fo = Context;
BOOLEAN result = FALSE;
dprintf("%s:\n", __func__);
if (fo == NULL)
return (FALSE);
mount_t *zmo = fo->DeviceObject->DeviceExtension;
zfsvfs_t *zfsvfs = vfs_fsprivate(zmo);
struct vnode *vp = fo->FsContext;
if (unlikely(zfsvfs == NULL)) {
dprintf("%s: fo %p already freed zfsvfs\n", __func__, fo);
return (FALSE);
}
if (vfs_busy(zfsvfs->z_vfs, 0) != 0)
return (FALSE);
if (zfsvfs->z_unmounted ||
zfs_enter(zfsvfs, FTAG) != 0) {
vfs_unbusy(zfsvfs->z_vfs);
return (FALSE);
}
vfs_unbusy(zfsvfs->z_vfs);
if (vp == NULL ||
VTOZ(vp) == NULL ||
VN_HOLD(vp) != 0) {
zfs_exit(zfsvfs, FTAG);
return (FALSE);
}
zfs_exit(zfsvfs, FTAG);
if (!ExAcquireResourceSharedLite(vp->FileHeader.Resource,
Wait)) {
dprintf("Failed\n");
goto out;
}
vnode_ref(vp);
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
result = TRUE;
out:
VN_RELE(vp);
return (result);
}
void
zfs_ReleaseFromReadAhead(void *Context)
{
FILE_OBJECT *fo = Context;
dprintf("%s:\n", __func__);
if (fo == NULL)
return;
mount_t *zmo = fo->DeviceObject->DeviceExtension;
zfsvfs_t *zfsvfs = vfs_fsprivate(zmo);
struct vnode *vp = fo->FsContext;
if (vp != NULL && VN_HOLD(vp) == 0) {
ExReleaseResourceLite(vp->FileHeader.Resource);
vnode_rele(vp);
VN_RELE(vp);
if (IoGetTopLevelIrp() ==
(PIRP)FSRTL_CACHE_TOP_LEVEL_IRP)
IoSetTopLevelIrp(NULL);
return;
}
dprintf("%s WARNING FAILED\n", __func__);
}
CACHE_MANAGER_CALLBACKS CacheManagerCallbacks =
{
.AcquireForLazyWrite = zfs_AcquireForLazyWrite,
.ReleaseFromLazyWrite = zfs_ReleaseFromLazyWrite,
.AcquireForReadAhead = zfs_AcquireForReadAhead,
.ReleaseFromReadAhead = zfs_ReleaseFromReadAhead
};
int
zfs_init_cache(FILE_OBJECT *fo, struct vnode *vp, CC_FILE_SIZES *ccfs)
{
zfs_ccb_t *zccb = fo->FsContext2;
try {
if (fo->PrivateCacheMap == NULL) {
VERIFY3U(zccb->cacheinit, ==, 0);
atomic_inc_64(&zccb->cacheinit);
CcInitializeCacheMap(fo,
ccfs,
FALSE,
&CacheManagerCallbacks, fo);
dprintf("CcInitializeCacheMap called on vp %p\n", vp);
// CcSetAdditionalCacheAttributes(fo, FALSE, FALSE);
// must be FALSE (Disk IO only)
// CcSetReadAheadGranularity(fo, READ_AHEAD_GRAN);
// fo->Flags |= FO_CACHE_SUPPORTED;
dprintf("%s: CcInitializeCacheMap\n", __func__);
}
} except(EXCEPTION_EXECUTE_HANDLER) {
return (GetExceptionCode());
}
return (0);
}
/*
* zfs vfs operations.
*/
/*
* FileObject->FsContext will point to vnode, many FileObjects can point
* to same vnode.
* FileObject->FsContext2 will point to own "zfs_ccb_t" and be unique
* to each FileObject.
* - which could also be done with TSD data, but this appears to be
* the Windows norm.
*/
static void
zfs_couplefileobject(vnode_t *vp, vnode_t *dvp, FILE_OBJECT *fileobject,
uint64_t size, zfs_ccb_t **ccb, uint64_t alloc, ACCESS_MASK access,
char *stream)
{
zfs_ccb_t *zccb;
if (fileobject->FsContext2 == NULL) {
zccb = kmem_zalloc(sizeof (zfs_ccb_t), KM_SLEEP);
zccb->magic = ZFS_CCB_MAGIC;
fileobject->FsContext2 = zccb;
} else {
zccb = fileobject->FsContext2;
}
zccb->access = access;
if (ccb != NULL)
*ccb = zccb;
vnode_ref(vp);
vnode_couplefileobject(vp, fileobject, size);
if (dvp)
vnode_setparent(vp, dvp);
uint64_t s = 0ULL;
uint64_t a = 0ULL;
if (VTOZ(vp) != NULL) {
znode_t *zp = VTOZ(vp);
s = zp->z_size;
a = P2ROUNDUP(zp->z_size, zp->z_blksz);
}
vp->FileHeader.AllocationSize.QuadPart = alloc ? alloc : a;
vp->FileHeader.FileSize.QuadPart = s;
vp->FileHeader.ValidDataLength.QuadPart = s;
#ifdef ZFS_HAVE_FASTIO
vp->FileHeader.IsFastIoPossible = fast_io_possible(vp);
#endif
zfs_build_path_stream(VTOZ(vp), dvp ? VTOZ(dvp) : NULL,
&zccb->z_name_cache,
&zccb->z_name_len,
&zccb->z_name_offset,
stream);
}
static void
zfs_decouplefileobject(vnode_t *vp, FILE_OBJECT *fileobject,
boolean_t SkipCache)
{
// We release FsContext2 at CLEANUP, but fastfat releases it in
// CLOSE. Does this matter?
zfs_ccb_t *zccb = fileobject->FsContext2;
if (zccb != NULL) {
if (zccb->searchname.Buffer != NULL) {
kmem_free(zccb->searchname.Buffer,
zccb->searchname.MaximumLength);
zccb->searchname.Buffer = NULL;
zccb->searchname.MaximumLength = 0;
}
if (zccb->z_name_cache != NULL)
kmem_free(zccb->z_name_cache, zccb->z_name_len);
zccb->z_name_cache = NULL;
zccb->z_name_len = 0;
kmem_free(zccb, sizeof (zfs_ccb_t));
fileobject->FsContext2 = NULL;
}
if (!SkipCache)
CcUninitializeCacheMap(fileobject, NULL, NULL);
vnode_decouplefileobject(vp, fileobject);
}
static void
allocate_reparse(struct vnode *vp, char *finalname, PIRP Irp)
{
znode_t *zp;
REPARSE_DATA_BUFFER *rpb;
size_t size;
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
PFILE_OBJECT FileObject = IrpSp->FileObject;
zp = VTOZ(vp);
// fix me, direct vp access
size = zfsctl_is_node(zp) ? vp->v_reparse_size :
zp->z_size;
rpb = ExAllocatePoolWithTag(PagedPool,
size, '!FSZ');
get_reparse_point_impl(zp, (char *)rpb, size, NULL);
/*
* Length, in bytes, of the unparsed portion of the
* file name pointed to by the FileName member of the
* associated file object.
* Should include the leading "/", when finalname
* here would be "lower".
*/
ULONG len = 0;
if (finalname && *finalname) {
RtlUTF8ToUnicodeN(NULL, 0, &len,
finalname, strlen(finalname));
len += sizeof (WCHAR);
}
rpb->Reserved = len;
dprintf("%s: returning REPARSE (remainder %d)\n",
__func__, rpb->Reserved);
Irp->IoStatus.Information = rpb->ReparseTag;
Irp->Tail.Overlay.AuxiliaryBuffer = (void *)rpb;
#if 0
/* Unknown why, but btrfs does this */
if (FileObject) {
UNICODE_STRING *fn = &FileObject->FileName;
if (fn->Buffer[(fn->Length / sizeof (WCHAR)) - 1] == '\\')
rpb->Reserved = sizeof (WCHAR);
}
#endif
}
void
check_and_set_stream_parent(char *stream_name, PFILE_OBJECT FileObject,
uint64_t id)
{
if (stream_name != NULL && FileObject != NULL &&
FileObject->FsContext2 != NULL) {
zfs_ccb_t *zccb = FileObject->FsContext2;
zccb->real_file_id = id;
if (FileObject->FsContext != NULL) {
struct vnode *vp = FileObject->FsContext;
znode_t *dzp;
znode_t *zp = VTOZ(vp);
if (zp != NULL && zp->z_zfsvfs != NULL) {
// Fetch gparent (one above xattr dir)
int error = zfs_zget(zp->z_zfsvfs, id, &dzp);
if (!error) {
zfs_build_path_stream(zp, dzp,
&zccb->z_name_cache,
&zccb->z_name_len,
&zccb->z_name_offset, stream_name);
zrele(dzp);
}
}
}
}
}
/*
* Take filename, look for colons ":".
* No colon, return OK.
* if ends with "::$DATA". Terminate on colon, return OK (regular file open).
* if ends with anything not ":$DATA", return error.
* (we don't handle other types)
* if colon, parse name up until next colon. Assign colonname to
* point to stream name.
*/
int
stream_parse(char *filename, char **streamname)
{
char *colon, *second;
// Just a filename, no streams.
colon = strchr(filename, ':');
if (colon == NULL)
return (0);
// Regular file, with "::$DATA" end?
if (strcasecmp(colon, "::$DATA") == 0) {
*colon = 0; // Terminate before colon
return (0);
}
// Look for second colon
second = strchr(&colon[1], ':');
// No second colon, just stream name. Validity check?
if (second == NULL) {
*streamname = &colon[1];
*colon = 0; // Cut off streamname from filename
// We now ADD ":$DATA" to the stream name.
strcat(*streamname, ":$DATA");
goto checkname;
}
// Have second colon, better be ":$DATA".
if (strcasecmp(second, ":$DATA") == 0) {
// Terminate at second colon, set streamname
// We now keep the ":$DATA" extension in the xattr name
// *second = 0;
*streamname = &colon[1];
*colon = 0; // Cut of streamname from filename
goto checkname;
}
// Not $DATA
dprintf("%s: Not handling StreamType '%s'\n", __func__, second);
return (EINVAL);
checkname:
if (strlen(*streamname) >= 512)
return (STATUS_OBJECT_NAME_INVALID);
if (strchr(*streamname, '/') ||
/* strchr(&colon[2], ':') || there is one at ":$DATA" */
!strcasecmp("DOSATTRIB:$DATA", *streamname) ||
!strcasecmp("EA:$DATA", *streamname) ||
!strcasecmp("reparse:$DATA", *streamname) ||
!strcasecmp("casesensitive:$DATA", *streamname))
return (STATUS_OBJECT_NAME_INVALID);
return (0);
}
/*
* Attempt to parse 'filename', descending into filesystem.
* If start "dvp" is passed in, it is expected to have a HOLD
* If successful, function will return with:
* - HOLD on dvp
* - HOLD on vp
* - final parsed filename part in 'lastname' (in the case of creating an entry)
*
* IRP_MJ_CREATE calls
*
* zfsvfs Filename
* --------------------------------------------------------
* IRP_MJ_CREATE(pool, "/lower/today.txt")
* : lookup "lower", return STATUS_REPARSE
* : Set unparsed length rdp->Reserved = 10 ("/today.txt") * 2
* --------------------------------------------------------
* IRP_MJ_CREATE(lower, "/today.txt")
* : lookup "today.txt", return SUCCESS
*/
int
zfs_find_dvp_vp(zfsvfs_t *zfsvfs, char *filename, int finalpartmaynotexist,
int finalpartmustnotexist, char **lastname, struct vnode **dvpp,
struct vnode **vpp, int flags, ULONG options)
{
int error = ENOENT;
znode_t *zp;
struct vnode *dvp = NULL;
struct vnode *vp = NULL;
char *word = NULL;
char *brkt = NULL;
struct componentname cn;
int fullstrlen;
char namebuffer[MAXNAMELEN];
BOOLEAN FileOpenReparsePoint;
BOOLEAN has_trailing_separator = FALSE;
FileOpenReparsePoint =
BooleanFlagOn(options, FILE_OPEN_REPARSE_POINT);
// Iterate from dvp if given, otherwise root
dvp = *dvpp;
if (dvp == NULL) {
// Grab a HOLD
error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp);
if (error != 0)
return (ESRCH); // No such dir
dvp = ZTOV(zp);
} else {
// Passed in dvp is already HELD, but grab one now
// since we release dirs as we descend
dprintf("%s: passed in dvp\n", __func__);
if (VN_HOLD(dvp) != 0)
return (ESRCH);
}
fullstrlen = strlen(filename);
// Sometimes we are given a path like "\Directory\directory\"
// with the final separator, we want to eat that final character.
if ((fullstrlen > 2) &&
(filename[fullstrlen - 1] == '\\')) {
filename[--fullstrlen] = 0;
has_trailing_separator = TRUE;
}
for (word = strtok_r(filename, "/\\", &brkt);
word;
word = strtok_r(NULL, "/\\", &brkt)) {
int direntflags = 0;
// dprintf("..'%s'..", word);
// If a component part name is too long
if (strlen(word) > MAXNAMELEN - 1) {
VN_RELE(dvp);
if (vp)
VN_RELE(vp);
return (STATUS_OBJECT_NAME_INVALID);
}
strlcpy(namebuffer, word, sizeof (namebuffer));
// Dont forget zfs_lookup() modifies
// "cn" here, so size needs to be max, if
// formD is in effect.
cn.cn_nameiop = LOOKUP;
cn.cn_flags = ISLASTCN;
cn.cn_namelen = strlen(namebuffer);
cn.cn_nameptr = namebuffer;
cn.cn_pnlen = MAXNAMELEN;
cn.cn_pnbuf = namebuffer;
error = zfs_lookup(VTOZ(dvp), namebuffer,
&zp, flags, NULL, &direntflags, &cn);
// If snapshot dir and we are pretending it is deleted...
if (error == 0 && zp->z_vnode != NULL && ZTOV(zp)->v_unlink) {
VN_RELE(ZTOV(zp));
error = ENOENT;
}
if (error != 0) {
// If we are creating a file, or looking up parent,
// allow it not to exist
if (finalpartmaynotexist)
break;
dprintf("failing out here\n");
// since we weren't successful, release dvp here
VN_RELE(dvp);
dvp = NULL;
break;
}
// If last lookup hit a non-directory type, we stop
vp = ZTOV(zp);
ASSERT(zp != NULL);
/*
* If we come across a REPARSE, we stop processing here
* and pass the "zp" back for caller to do more processing,
* which might include returning "zp" (FILE_OPEN_REPARSE_POINT)
* and ReparseTag.
* If they requested FileOpenReparsePoint, AND we are at the
* final-part, we open it normally.
* Other cases we need to ask for redriving the query
*/
if (zp->z_pflags & ZFS_REPARSE) {
/*
* Indicate if reparse was final part,
* caller will handle this case
*/
if (lastname)
*lastname = brkt;
if (dvpp != NULL)
*dvpp = dvp;
if (vpp != NULL)
*vpp = vp;
return (STATUS_REPARSE);
}
if (vp->v_type == VDIR) {
// Not reparse
VN_RELE(dvp);
dvp = vp;
vp = NULL;
} else {
// If we aren't the final component, descending dirs,
// and it's a file?
if (brkt != NULL && *brkt != 0) {
dprintf("%s: not a DIR triggered '%s'\n",
__func__, word);
if (vp)
VN_RELE(vp);
VN_RELE(dvp);
return (ENOTDIR);
}
break;
} // is dir or not
} // for word
// dprintf("\n");
if (dvp != NULL) {
// We return with dvp HELD
// VN_RELE(dvp);
} else {
dprintf("%s: failed to find dvp for '%s' word '%s' err %d\n",
__func__, filename, word?word:"(null)", error);
return (error);
}
if (error != 0 && !vp && !finalpartmaynotexist) {
VN_RELE(dvp);
return (ENOENT);
}
/* finalpartmustnotexist and we got a vp? */
if (!word && finalpartmustnotexist && dvp && vp) {
dprintf("CREATE with existing dir exit?\n");
VN_RELE(vp);
VN_RELE(dvp);
if (zp && !S_ISDIR(zp->z_mode))
return (ENOTDIR);
return (EEXIST);
}
// If finalpartmaynotexist is TRUE, make sure we are looking at
// the finalpart, and not in the middle of descending
if (finalpartmaynotexist && brkt != NULL && *brkt != 0) {
dprintf("finalpartmaynotexist, but not at finalpart: %s\n",
brkt);
VN_RELE(dvp);
return (ESRCH);
}
// Check if we got a file, but request had trailing slash
if (vp != NULL && !vnode_isdir(vp) && has_trailing_separator) {
VN_RELE(vp);
VN_RELE(dvp);
// NTFS returns STATUS_OBJECT_NAME_INVALID
return (STATUS_OBJECT_NAME_INVALID); // ENOTDIR
}
if (lastname) {
*lastname = word /* ? word : filename */;
// Skip any leading "\"
while (*lastname != NULL &&
(**lastname == '\\' || **lastname == '/'))
(*lastname)++;
}
if (dvpp != NULL)
*dvpp = dvp;
if (vpp != NULL)
*vpp = vp;
return (0);
}
/*
* In POSIX, the vnop_lookup() would return with iocount still held
* for the caller to issue VN_RELE() on when done.
* The above zfs_find_dvp_vp() behaves a little like that, in that
* if a successful "vp" is returned, it has a iocount lock, and
* is released here when finished.
* zfs_vnop_lookup serves as the bridge between Windows and Unix
* and will assign FileObject->FsContext as appropriate, with usecount set
* when required, but it will not hold iocount.
*/
int
zfs_vnop_lookup_impl(PIRP Irp, PIO_STACK_LOCATION IrpSp, mount_t *zmo,
char *filename, xvattr_t *xvap)
{
int error;
cred_t *cr = NULL;
char *finalname = NULL;
PFILE_OBJECT FileObject;
ULONG outlen;
struct vnode *dvp = NULL;
struct vnode *vp = NULL;
znode_t *zp = NULL;
znode_t *dzp = NULL;
ULONG Options;
BOOLEAN CreateDirectory;
BOOLEAN NoIntermediateBuffering;
BOOLEAN OpenDirectory;
BOOLEAN IsPagingFile;
BOOLEAN OpenTargetDirectory;
BOOLEAN DirectoryFile;
BOOLEAN NonDirectoryFile;
BOOLEAN NoEaKnowledge;
BOOLEAN DeleteOnClose;
BOOLEAN OpenRequiringOplock;
BOOLEAN TemporaryFile;
BOOLEAN OpenRoot;
BOOLEAN CreateFile;
BOOLEAN FileOpenByFileId;
BOOLEAN FileOpenReparsePoint;
ULONG CreateDisposition;
zfsvfs_t *zfsvfs = vfs_fsprivate(zmo);
int flags = 0;
int dvp_no_rele = 0;
char *stream_name = NULL;
boolean_t UndoShareAccess = FALSE;
NTSTATUS Status = STATUS_SUCCESS;
ACCESS_MASK granted_access = 0;
ACCESS_MASK DesiredAccess =
IrpSp->Parameters.Create.SecurityContext->DesiredAccess;
zfs_ccb_t *zccb = NULL;
vattr_t *vap = &xvap->xva_vattr;
if (zfsvfs == NULL)
return (STATUS_OBJECT_PATH_NOT_FOUND);
if (vfs_isunmount(zmo))
return (STATUS_DEVICE_NOT_READY);
FileObject = IrpSp->FileObject;
Options = IrpSp->Parameters.Create.Options;
dprintf("%s: enter\n", __func__);
if (FileObject->RelatedFileObject != NULL) {
FileObject->Vpb = FileObject->RelatedFileObject->Vpb;
// A relative open must be via a relative path.
if (FileObject->FileName.Length != 0 &&
FileObject->FileName.Buffer[0] == L'\\') {
return (STATUS_INVALID_PARAMETER);
}
} else {
FileObject->Vpb = zmo->vpb;
}
DirectoryFile =
BooleanFlagOn(Options, FILE_DIRECTORY_FILE);
NonDirectoryFile =
BooleanFlagOn(Options, FILE_NON_DIRECTORY_FILE);
NoIntermediateBuffering =
BooleanFlagOn(Options, FILE_NO_INTERMEDIATE_BUFFERING);
NoEaKnowledge =
BooleanFlagOn(Options, FILE_NO_EA_KNOWLEDGE);
DeleteOnClose =
BooleanFlagOn(Options, FILE_DELETE_ON_CLOSE);
FileOpenByFileId =
BooleanFlagOn(Options, FILE_OPEN_BY_FILE_ID);
FileOpenReparsePoint =
BooleanFlagOn(Options, FILE_OPEN_REPARSE_POINT);
// Should be passed an 8/16 byte FileId instead.
if (FileOpenByFileId) {
if (FileObject->FileName.Length !=
sizeof (ULONGLONG) &&
FileObject->FileName.Length !=
sizeof (FILE_ID_128))
return (STATUS_INVALID_PARAMETER);
}
TemporaryFile = BooleanFlagOn(IrpSp->Parameters.Create.FileAttributes,
FILE_ATTRIBUTE_TEMPORARY);
CreateDisposition = (Options >> 24) & 0x000000ff;
IsPagingFile = BooleanFlagOn(IrpSp->Flags, SL_OPEN_PAGING_FILE);
// ASSERT(!IsPagingFile);
// ASSERT(!OpenRequiringOplock);
// Open the directory instead of the file
OpenTargetDirectory = BooleanFlagOn(IrpSp->Flags,
SL_OPEN_TARGET_DIRECTORY);
/*
* CreateDisposition value Action if file exists
* Action if file does not exist UNIX Perms
* FILE_SUPERSEDE Replace the file.
* Create the file. * Unlink + O_CREAT | O_TRUNC
* FILE_CREATE Return an error.
* Create the file. * O_CREAT | O_EXCL
* FILE_OPEN Open the file.
* Return an error. * 0
* FILE_OPEN_IF Open the file.
* Create the file. * O_CREAT
* FILE_OVERWRITE Open the file, overwrite it.
* Return an error. * O_TRUNC
* FILE_OVERWRITE_IF Open the file, overwrite it.
* Create the file. * O_CREAT | O_TRUNC
*
* Apparently SUPERSEDE is more or less Unlink entry before recreate,
* so it loses ACLs, XATTRs and NamedStreams.
*
* IoStatus return codes:
* FILE_CREATED
* FILE_OPENED
* FILE_OVERWRITTEN
* FILE_SUPERSEDED
* FILE_EXISTS
* FILE_DOES_NOT_EXIST
*
*/
// Dir create/open is straight forward, do that here
// Files are harder, do that once we know if it exists.
CreateDirectory = (BOOLEAN)(DirectoryFile &&
((CreateDisposition == FILE_CREATE) ||
(CreateDisposition == FILE_OPEN_IF)));
OpenDirectory = (BOOLEAN)(DirectoryFile &&
((CreateDisposition == FILE_OPEN) ||
(CreateDisposition == FILE_OPEN_IF)));
CreateFile = (BOOLEAN)(
((CreateDisposition == FILE_CREATE) ||
(CreateDisposition == FILE_OPEN_IF) ||
(CreateDisposition == FILE_SUPERSEDE) ||
(CreateDisposition == FILE_OVERWRITE_IF)));
// If it is a volumeopen, we just grab rootvp so that directory
// listings work - most Options are ignored with VolumeOpens
if (FileObject->FileName.Length == 0 &&
FileObject->RelatedFileObject == NULL) {
// don't allow root to be opened on unmounted FS
if (!(zmo->vpb->Flags & VPB_MOUNTED))
return (STATUS_DEVICE_NOT_READY);
dprintf("Started NULL open, returning root of mount\n");
error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp);
if (error != 0)
return (FILE_DOES_NOT_EXIST); // No root dir?!
dvp = ZTOV(zp);
zfs_couplefileobject(dvp, NULL, FileObject, 0ULL, &zccb,
Irp->Overlay.AllocationSize.QuadPart, DesiredAccess,
stream_name);
VN_RELE(dvp);
Irp->IoStatus.Information = FILE_OPENED;
return (STATUS_SUCCESS);
}
// No name conversion with FileID
if (!FileOpenByFileId) {
if (FileObject->FileName.Buffer != NULL &&
FileObject->FileName.Length > 0) {
// Convert incoming filename to utf8
error = RtlUnicodeToUTF8N(filename, PATH_MAX - 1,
&outlen,
FileObject->FileName.Buffer,
FileObject->FileName.Length);
if (error != STATUS_SUCCESS &&
error != STATUS_SOME_NOT_MAPPED) {
dprintf("RtlUnicodeToUTF8N returned 0x%x "
"input len %d\n",
error, FileObject->FileName.Length);
return (STATUS_OBJECT_NAME_INVALID);
}
// ASSERT(error != STATUS_SOME_NOT_MAPPED);
// Output string is only null terminated if input is,
// so do so now.
filename[outlen] = 0;
dprintf("%s: converted name is '%s' input len bytes %d "
"(err %d) %s %s\n", __func__, filename,
FileObject->FileName.Length, error,
DeleteOnClose ? "DeleteOnClose" : "",
IrpSp->Flags&SL_CASE_SENSITIVE ? "CaseSensitive" :
"CaseInsensitive");