This repository was archived by the owner on Apr 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvfs_proxyfs.c
More file actions
2938 lines (2426 loc) · 83.1 KB
/
vfs_proxyfs.c
File metadata and controls
2938 lines (2426 loc) · 83.1 KB
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
// Copyright (c) 2015-2021, NVIDIA CORPORATION.
// SPDX-License-Identifier: Apache-2.0
/*
* Unix SMB/CIFS implementation.
*
* VFS module to invoke ProxyFS built on swift object store.
*/
/**
* @file vfs_proxyfs.c
* @author SwiftStack
* @date September 2017
* @brief Samba VFS module for proxyfs
*
* A Samba VFS module for ProxyFS.
* This is a "bottom" vfs module (not something to be stacked on top of
* another module), and translates (most) calls to the closest actions
* available in rpc calls to ProxyFS.
*/
#include <stdlib.h> // TODO: Remove this per below
#include <stdio.h>
#include <libgen.h>
#include <fcntl.h>
// Temporarily including for getpwnam():
#include <sys/types.h>
#include <pwd.h>
#include <includes.h>
#include <smbd/smbd.h>
#include <bin/default/librpc/gen_ndr/ndr_smb_acl.h>
#include "vfs_proxyfs.h"
#ifdef HAVE_LINUX_FALLOC_H
#include <linux/falloc.h>
#endif
/*
* Proxyfs VFS module is written for SAMBA Major version 4 and supports minor versions 2, 3, 4, 5 and 6.
* The default is 4.3 and exceptions/deviations in the interface are handled for 2, 4, 5 and 6 with #if conditions.
*/
#if !defined(SAMBA_VERSION_MAJOR) || !defined(SAMBA_VERSION_MINOR) || SAMBA_VERSION_MAJOR != 4 || SAMBA_VERSION_MINOR < 2 || SAMBA_VERSION_MINOR > 6
#error Samba Major version should be 4 and minor should be between 2 and 6 (inclusive).
#endif
#define vfs_proxyfs_init samba_init_module
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_VFS
const char*vfs_proxyfs_PrivateIPAddr; // [global]proxyfs:PrivateIPAddr
int vfs_proxyfs_TCPPort; // [global]proxyfs:TCPPort
int vfs_proxyfs_FastTCPPort; // [global]proxyfs:FastTCPPort
char *resolve_path(struct vfs_handle_struct *handle,
const char *path)
{
char *path_to_resolve;
if (path[0] == '/') {
path_to_resolve = strdup((char *)path);
if (strcmp(path_to_resolve, "/") == 0) {
return path_to_resolve;
}
} else {
if (CWD(handle) == NULL) {
path_to_resolve = strdup("/");
} else {
path_to_resolve = strdup(CWD(handle));
}
int buf_len = strlen(path_to_resolve) + strlen("/") + strlen((char *)path) + 1;
char *tmp_path = malloc(buf_len);
bzero(tmp_path, buf_len);
snprintf(tmp_path, buf_len, "%s/%s", path_to_resolve, path);
free(path_to_resolve);
path_to_resolve = tmp_path;
}
// Convert now the path to canonical path:
char **path_stack = (char **)malloc((strlen(path_to_resolve) / 2 + 1) * sizeof(char *));
int i = 0;
int path_count = 0;
char *token, *saveptr;
char *firstptr = path_to_resolve;
do {
token = strtok_r(firstptr, "/", &saveptr);
firstptr = NULL;
if (token == NULL) {
break;
}
if (strcmp(token, ".") == 0) {
continue;
}
if (strcmp(token, "..") == 0) {
if (i == 0) {
continue;
}
path_count -= strlen(path_stack[i - 1]);
i--;
continue;
}
path_stack[i] = token;
path_count += strlen(token);
i++;
} while (token != NULL);
int buf_len = path_count + i + 2;
char *rpath = (char *)malloc(buf_len);
bzero(rpath, buf_len);
snprintf(rpath, buf_len, "/"); // Initialize it to /, will be over written if there are other path components.
int j, idx;
for (idx = 0, j = 0; j < i; j++) {
snprintf(&rpath[idx], buf_len - idx, "/%s", path_stack[j]);
idx += strlen(path_stack[j]) + 1;
}
free(path_stack);
free(path_to_resolve);
return rpath;
}
int get_parent_ino(struct vfs_handle_struct *handle,
char *base_name,
uint64_t *parent_inop)
{
char *path, *parent, *name;
uint64_t parent_ino, file_ino;
int err = 0;
path = resolve_path(handle, base_name);
parent = dirname(path);
if (strcmp(CWD(handle), parent) == 0) {
*parent_inop = CWD_INUM(handle);
} else {
err = proxyfs_lookup_path(MOUNT_HANDLE(handle), parent, parent_inop);
}
free(path);
if (err != 0) {
errno = err;
return -1;
}
return 0;
}
/**
* Helper to convert struct stat to struct stat_ex.
*/
static void smb_stat_ex_from_stat(struct stat_ex *dst,
const proxyfs_stat_t *src)
{
bzero(dst, sizeof(struct stat_ex));
dst->st_ex_dev = src->dev;
dst->st_ex_ino = src->ino;
dst->st_ex_mode = src->mode;
dst->st_ex_nlink = src->nlink;
dst->st_ex_uid = src->uid;
dst->st_ex_gid = src->gid;
dst->st_ex_rdev = src->dev;
dst->st_ex_size = src->size;
dst->st_ex_atime.tv_sec = src->atim.sec;
dst->st_ex_mtime.tv_sec = src->mtim.sec;
dst->st_ex_ctime.tv_sec = src->ctim.sec;
dst->st_ex_btime.tv_sec = src->crtim.sec;
dst->st_ex_blksize = 65536;
dst->st_ex_blocks = src->size / 512;
#ifdef STAT_HAVE_NSEC
dst->st_ex_atime.tv_nsec = src->atim.nsec;
dst->st_ex_mtime.tv_nsec = src->mtim.nsec;
dst->st_ex_ctime.tv_nsec = src->ctim.nsec;
dst->st_ex_btime.tv_nsec = src->crtim.nsec;
#endif
}
static void free_data(void **handle_data)
{
fs_ctx_t *ctx = (fs_ctx_t *)*handle_data;
if (ctx == NULL) {
return;
}
if (ctx->cwd) {
free(ctx->cwd);
}
free(ctx);
*handle_data = NULL;
}
static int vfs_proxyfs_connect(struct vfs_handle_struct *handle,
const char *service,
const char *user)
{
int err;
fs_ctx_t *ctx = (fs_ctx_t *)malloc(sizeof(fs_ctx_t));
if (ctx == NULL) {
errno = ENOMEM;
return -1;
}
bzero(ctx, sizeof(fs_ctx_t));
ctx->volume = (char *)lp_parm_const_string(SNUM(handle->conn), "proxyfs", "volume", service);
ctx->readonly = handle->conn->read_only;
uint64_t mount_option = ctx->readonly ? 1 : 0;
DEBUG(10, ("vfs_proxyfs_connect: Volume : %s Connection_path %s Service %s user %s\n", ctx->volume, handle->conn->connectpath, service, user));
err = proxyfs_mount((char *)ctx->volume, mount_option, geteuid(), getegid(), &ctx->mnt_handle);
if (err != 0) {
errno = err;
DEBUG(1, ("proxyfs_mount_failed: Volume : %s Connection_path %s Service %s user %s errno %d\n", ctx->volume, handle->conn->connectpath, service, user, errno));
free(ctx);
return -1;
}
ctx->cwd = strdup("/");
ctx->cwd_inum = ctx->mnt_handle->root_dir_inode_num;
ctx->open_count = 0;
handle->data = ctx;
handle->free_data = free_data;
return 0;
}
static void vfs_proxyfs_disconnect(struct vfs_handle_struct *handle)
{
fs_ctx_t *ctx = (fs_ctx_t *)handle->data;
if (ctx == NULL) {
DEBUG(1, ("vfs_proxyfs_disconnect: null ctx done\n"));
return;
}
DEBUG(10, ("vfs_proxyfs_disconnect: %s\n", ctx->volume));
proxyfs_unmount(MOUNT_HANDLE(handle));
free_data(&handle->data);
}
static uint64_t vfs_proxyfs_disk_free(struct vfs_handle_struct *handle,
const char *path,
#if SAMBA_VERSION_MINOR == 2
bool small_query,
#endif
uint64_t *bsize_p,
uint64_t *bavail_p,
uint64_t *btotal_p)
{
uint64_t ret = 0;
struct statvfs* stat_vfs = NULL;
int err = proxyfs_statvfs(MOUNT_HANDLE(handle), &stat_vfs);
if (err != 0) {
errno = err;
return -1;
}
if (bsize_p != NULL) {
*bsize_p = (uint64_t)stat_vfs->f_bsize; /* Block size */
}
if (bavail_p != NULL) {
*bavail_p = (uint64_t)stat_vfs->f_bavail; /* Available Block units */
ret = *bavail_p;
}
if (btotal_p != NULL) {
*btotal_p = (uint64_t)stat_vfs->f_blocks; /* Total Block units */
}
free(stat_vfs);
DEBUG(10, ("vfs_proxyfs_disk_free: %s, returning %ld\n", handle->conn->connectpath, ret));
return ret;
}
static int vfs_proxyfs_get_quota(struct vfs_handle_struct *handle,
enum SMB_QUOTA_TYPE qtype,
unid_t id,
SMB_DISK_QUOTA *qt)
{
DEBUG(10, ("vfs_proxyfs_get_quota: %s\n", handle->conn->connectpath));
errno = ENOSYS;
return -1;
}
#if SAMBA_VERSION_MINOR >= 4
static int vfs_proxyfs_get_quota_4_4(struct vfs_handle_struct *handle,
const char *path,
enum SMB_QUOTA_TYPE qtype,
unid_t id,
SMB_DISK_QUOTA *qt)
{
return vfs_proxyfs_get_quota(handle, qtype, id, qt);
}
#endif
static int
vfs_proxyfs_set_quota(struct vfs_handle_struct *handle,
enum SMB_QUOTA_TYPE qtype,
unid_t id,
SMB_DISK_QUOTA *qt)
{
DEBUG(10, ("vfs_proxyfs_set_quota: %s\n", handle->conn->connectpath));
errno = ENOSYS;
return -1;
}
static int vfs_get_shadow_copy_data(struct vfs_handle_struct *handle,
struct files_struct *fsp,
struct shadow_copy_data *shadow_copy_data,
bool labels)
{
DEBUG(10, ("vfs_get_shadow_copy_data: %s\n", handle->conn->connectpath));
errno = ENOTSUP;
return -1;
}
static int vfs_proxyfs_statvfs(struct vfs_handle_struct *handle,
const char *path,
struct vfs_statvfs_struct *vfs_statvfs)
{
struct statvfs* stat_vfs = NULL;
int err = proxyfs_statvfs(MOUNT_HANDLE(handle), &stat_vfs);
if (err != 0) {
errno = err;
return -1;
}
bzero(vfs_statvfs, sizeof(struct vfs_statvfs_struct));
vfs_statvfs->OptimalTransferSize = stat_vfs->f_frsize;
vfs_statvfs->BlockSize = stat_vfs->f_bsize;
vfs_statvfs->TotalBlocks = stat_vfs->f_blocks;
vfs_statvfs->BlocksAvail = stat_vfs->f_bfree;
vfs_statvfs->UserBlocksAvail = stat_vfs->f_bavail;
vfs_statvfs->TotalFileNodes = stat_vfs->f_files;
vfs_statvfs->FreeFileNodes = stat_vfs->f_ffree;
vfs_statvfs->FsIdentifier = stat_vfs->f_fsid;
vfs_statvfs->FsCapabilities = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
free(stat_vfs);
DEBUG(10, ("vfs_proxyfs_statvfs: returning statvfs for %s : block size %d\n", handle->conn->connectpath, vfs_statvfs->BlockSize));
return err;
}
static uint32_t vfs_proxyfs_fs_capabilities(struct vfs_handle_struct *handle,
enum timestamp_set_resolution *p_ts_res)
{
DEBUG(10, ("vfs_proxyfs_fs_capabilities: %s\n", handle->conn->connectpath));
uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
#ifdef STAT_HAVE_NSEC
*p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
#endif
return caps;
}
static NTSTATUS vfs_proxyfs_get_dfs_referrals(struct vfs_handle_struct *handle,
struct dfs_GetDFSReferral *r)
{
DEBUG(10, ("vfs_proxyfs_get_dfs_referrals: %s\n", handle->conn->connectpath));
errno = ENOTSUP;
return NT_STATUS_NOT_IMPLEMENTED; // TBD: need to send NTSTATUS.
}
static DIR *vfs_proxyfs_opendir(struct vfs_handle_struct *handle,
const char *fname,
const char *mask,
uint32_t attributes)
{
DEBUG(10, ("vfs_proxyfs_opendir: %s\n", handle->conn->connectpath));
file_handle_t *dir;
int err;
dir = (file_handle_t *)malloc(sizeof(file_handle_t));
if (dir == NULL) {
errno = ENOMEM;
return NULL;
}
bzero(dir, sizeof(file_handle_t));
char *path = resolve_path(handle, fname);
err = proxyfs_lookup_path(MOUNT_HANDLE(handle), path, &dir->inum);
if (err != 0) {
errno = err;
free(path);
free(dir);
return NULL;
}
free(path);
return (DIR *) dir;
}
#if SAMBA_VERSION_MINOR >= 5
static DIR *vfs_proxyfs_opendir_4_4(struct vfs_handle_struct *handle,
const struct smb_filename *smb_fname,
const char *mask,
uint32_t attributes) {
return vfs_proxyfs_opendir(handle, smb_fname->base_name, mask, attributes);
}
#endif
static DIR *vfs_proxyfs_fdopendir(struct vfs_handle_struct *handle,
files_struct *fsp,
const char *mask,
uint32_t attributes)
{
DEBUG(10, ("vfs_proxyfs_fdopendir: %s\n", handle->conn->connectpath));
return (DIR *) *(file_handle_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
}
/*
* The readdir() function returns a pointer to a dirent structure representing the next directory entry
* in the directory stream pointed to by dirp.
*
* Note on next direntry pointer:
* In proxyfs we use last read name as the marker instead of the corresponding dirent.d_off. Because, the internal
* directory btree key is name (offset is just the location of the entry in the directory at the time of making the call).
* Therefore, if files are deleted then offet corresponding to a name could change, leading to inconsistency if we use it
* as a marker.
*/
static struct dirent *vfs_proxyfs_readdir(struct vfs_handle_struct *handle,
DIR *dirp,
SMB_STRUCT_STAT *sbuf)
{
int ret;
struct dirent *dir_ent = NULL;
file_handle_t *dir = (file_handle_t *)dirp;
uint64_t num_ents, size;
bool are_more_entries;
proxyfs_stat_t *stats;
char *prev_name_marker = NULL;
if (sbuf != NULL) {
DEBUG(10, ("Invoking proxyfs_readdir_plus for inum = %ld\n", dir->inum));
if (dir->use_name) {
ret = proxyfs_readdir_plus(MOUNT_HANDLE(handle), dir->inum, (char *)dir->prev_readdir_name, &dir_ent, &stats);
} else {
// TODO: ProxyFS wants the offset to be previous to the one returned to work correctly. This needs to be fixed!!
ret = proxyfs_readdir_plus_by_loc(MOUNT_HANDLE(handle), dir->inum, dir->offset - 1, &dir_ent, &stats);
}
} else {
DEBUG(10, ("Invoking proxyfs_readdir for inum = %ld\n", dir->inum));
if (dir->use_name) {
ret = proxyfs_readdir(MOUNT_HANDLE(handle), dir->inum, (char *)dir->prev_readdir_name, &dir_ent);
} else {
// TODO: ProxyFS wants the offset to be previous to the one returned to work correctly. This needs to be fixed!!
ret = proxyfs_readdir_by_loc(MOUNT_HANDLE(handle), dir->inum, dir->offset - 1, &dir_ent);
}
}
if ((ret != 0) || (dir_ent == NULL)) {
errno = ret;
return NULL;
}
dir->use_name = true; // Since we have the name in hand, we will use it as the marker.
dir->prev_readdir_name = (char *)dir->dir_ent.d_name;
memcpy(&dir->dir_ent, dir_ent, sizeof(struct dirent));
free(dir_ent);
if (sbuf != NULL) {
smb_stat_ex_from_stat(sbuf, &stats[0]);
free(stats);
}
dir->offset = dir->dir_ent.d_off;
return &dir->dir_ent;
}
/*
* The seekdir() function sets the location in the directory stream from which the next readdir call will start.
* The loc argument should be a value returned by a previous call to telldir.
*
*/
static void vfs_proxyfs_seekdir(struct vfs_handle_struct *handle,
DIR *dirp,
long offset)
{
DEBUG(10, ("vfs_proxyfs_seekdir: %s offset : %ld \n", handle->conn->connectpath, offset));
file_handle_t *dir = (file_handle_t *)dirp;
if (offset == dir->telldir_info[0].offset) {
dir->offset = offset;
dir->use_name = dir->telldir_info[0].use_name;
dir->prev_readdir_name = dir->telldir_info[0].name;
} else if (offset == dir->telldir_info[1].offset) {
dir->offset = offset;
dir->use_name = dir->telldir_info[1].use_name;
dir->prev_readdir_name = dir->telldir_info[1].name;
} else {
// We are seeking to a location for which we don't have corresponding name, we fall back to
// using offset as the marker, this could lead to inconsistency if files were deleted in the directory
// after getting the offset from telldir().
// This should not happen in case of samba requests!
DEBUG(3, ("vfs_proxyfs_seekdir: %s offset : %ld seeking to a location we are not tracking by name, could lead to readdir inconsistency\n", handle->conn->connectpath, offset));
dir->offset = offset;
dir->use_name = false;
}
}
/*
* The telldir() function returns the current location associated with the directory stream dirp.
*
* To follow posix description properly, we are supposed to maintain the readdir marker associated
* with every telldir offset we replied. So that a seekdir() to any of the previously returned telldir()
* location can properly be handled.
*
* Samba gets directory query info with a buffer to fill. Samba continuously reads on entry at a time and
* adds it to the buffer. When it encounters an entry that overflows the buffer, then samba will reply back
* to the query request and seekdir() back one entry so that the last read entry which could not be filled
* into the buffer will be re-read again for the subsequent request.
*/
static long vfs_proxyfs_telldir(struct vfs_handle_struct *handle, DIR *dirp)
{
DEBUG(10, ("vfs_proxyfs_telldir: %s\n", handle->conn->connectpath));
file_handle_t *dir = (file_handle_t *)dirp;
dir->telldir_info[0] = dir->telldir_info[1];
strncpy(dir->telldir_info[1].name, dir->dir_ent.d_name, NAME_MAX);
dir->telldir_info[1].offset = dir->offset;
dir->telldir_info[1].use_name = dir->use_name;
return dir->offset;
}
static void vfs_proxyfs_rewinddir(struct vfs_handle_struct *handle,
DIR *dirp)
{
DEBUG(10, ("vfs_proxyfs_rewinddir: %s\n", handle->conn->connectpath));
file_handle_t *dir = (file_handle_t *)dirp;
dir->offset = 0;
dir->use_name = false; // Subsequent readdir should use offset instead of name as the next entry marker.
}
static int vfs_proxyfs_mkdir(struct vfs_handle_struct *handle,
const char *smb_path,
mode_t mode)
{
char *path = resolve_path(handle, smb_path);
uid_t uid = get_current_uid(handle->conn);
gid_t gid = get_current_gid(handle->conn);
int err = proxyfs_mkdir_path(MOUNT_HANDLE(handle), path, uid, gid, mode);
DEBUG(10, ("vfs_proxyfs_mkdir: %s mode 0%o errno = %d\n", path, mode, err));
free(path);
if (err != 0) {
errno = err;
return -1;
}
return 0;
}
#if SAMBA_VERSION_MINOR >= 5
static int vfs_proxyfs_mkdir_4_4(struct vfs_handle_struct *handle,
const struct smb_filename *smb_fname,
mode_t mode)
{
return vfs_proxyfs_mkdir(handle, smb_fname->base_name, mode);
}
#endif
static int vfs_proxyfs_rmdir(struct vfs_handle_struct *handle,
const char *smb_fname)
{
char *path = resolve_path(handle, smb_fname);
int err = proxyfs_rmdir_path(MOUNT_HANDLE(handle), path);
DEBUG(10, ("vfs_proxyfs_rmdir: %s errno: %d\n", path, err));
free(path);
if (err != 0) {
errno = err;
}
return err;
}
#if SAMBA_VERSION_MINOR >= 5
static int vfs_proxyfs_rmdir_4_4(struct vfs_handle_struct *handle,
const struct smb_filename *smb_fname)
{
return vfs_proxyfs_rmdir(handle, smb_fname->base_name);
}
#endif
static int vfs_proxyfs_closedir(struct vfs_handle_struct *handle,
DIR *dirp)
{
DEBUG(10, ("vfs_proxyfs_closedir: %s\n", handle->conn->connectpath));
file_handle_t *dir = (file_handle_t *)dirp;
free(dir);
return 0;
}
static void vfs_proxyfs_init_search_op(struct vfs_handle_struct *handle,
DIR *dirp)
{
DEBUG(10, ("vfs_proxyfs_init_search_op: %s\n", handle->conn->connectpath));
return;
}
static int vfs_proxyfs_open(struct vfs_handle_struct *handle,
struct smb_filename *smb_fname,
files_struct *fsp,
int flags,
mode_t mode)
{
DEBUG(10, ("vfs_proxyfs_open: %s %s\n", handle->conn->connectpath, smb_fname->base_name));
file_handle_t *fd;
int err;
char *path;
fd = (file_handle_t *)malloc(sizeof(file_handle_t));
if (fd == NULL) {
errno = ENOMEM;
return -1;
}
bzero(fd, sizeof(file_handle_t));
if (flags & O_DIRECTORY) {
path = resolve_path(handle, smb_fname->base_name);
err = proxyfs_lookup_path(MOUNT_HANDLE(handle), path, &fd->inum);
free(path);
if (err != 0) {
free(fd);
errno = err;
return -1;
}
fd->offset = 0;
} else if (flags & O_CREAT) {
path = resolve_path(handle, smb_fname->base_name);
char *tmp_path = strdup(path);
char *parent = dirname(tmp_path);
uid_t uid = get_current_uid(handle->conn);
gid_t gid = get_current_gid(handle->conn);
if (strcmp(parent, CWD(handle)) == 0) {
char *name = basename(path);
err = proxyfs_create(MOUNT_HANDLE(handle), CWD_INUM(handle), name, uid, gid, mode, &fd->inum);
} else {
err = proxyfs_create_path(MOUNT_HANDLE(handle), path, uid, gid, mode, &fd->inum);
}
free(tmp_path);
free(path);
if (err != 0) {
free(fd);
errno = err;
return -1;
}
fd->offset = 0;
fd->flags = flags;
fd->mode = mode;
} else {
path = resolve_path(handle, smb_fname->base_name);
err = proxyfs_lookup_path(MOUNT_HANDLE(handle), path, &fd->inum);
free(path);
if (err != 0) {
free(fd);
errno = err;
return -1;
}
fd->offset = 0;
fd->flags = flags;
fd->mode = mode;
}
file_handle_t **p_tmp = (file_handle_t **)VFS_ADD_FSP_EXTENSION(handle, fsp, file_handle_t *, NULL);
*p_tmp = fd;
DEBUG(10, ("vfs_proxyfs_open - success: %s %s\n", handle->conn->connectpath, smb_fname->base_name));
return 1;
}
static NTSTATUS vfs_proxyfs_create(struct vfs_handle_struct *handle, struct smb_request *req,
uint16_t root_dir_fid, struct smb_filename *smb_fname,
uint32_t access_mask, uint32_t share_access,
uint32_t create_disposition, uint32_t create_options,
uint32_t file_attributes, uint32_t oplock_request,
struct smb2_lease *lease, uint64_t allocation_size,
uint32_t private_flags, struct security_descriptor *sd,
struct ea_list *ea_list, files_struct **result,
int *pinfo, const struct smb2_create_blobs *in_context_blobs,
struct smb2_create_blobs *out_context_blobs) {
DEBUG(10,("create_file: access_mask = 0x%x "
"file_attributes = 0x%x, share_access = 0x%x, "
"create_disposition = 0x%x create_options = 0x%x "
"oplock_request = 0x%x "
"private_flags = 0x%x "
"root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
"fname = %s\n",
(unsigned int)access_mask,
(unsigned int)file_attributes,
(unsigned int)share_access,
(unsigned int)create_disposition,
(unsigned int)create_options,
(unsigned int)oplock_request,
(unsigned int)private_flags,
(unsigned int)root_dir_fid,
ea_list, sd, smb_fname_str_dbg(smb_fname)));
// Samba creates files via - open, since we have set .create_file_fn = NULL
// This function won't be inovked. Just a place holder.
errno = ENOTSUP;
return NT_STATUS_NOT_IMPLEMENTED;
}
static bool vfs_proxyfs_lock(struct vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type);
static int vfs_proxyfs_close(struct vfs_handle_struct *handle,
files_struct *fsp)
{
DEBUG(10, ("vfs_proxyfs_close: %s\n", fsp->fsp_name->base_name));
file_handle_t *fd;
fd = *(file_handle_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
//proxyfs_flush(MOUNT_HANDLE(handle), fd->inum);
// TBD: This should be handled inside proxyfs layer - unlock all the acquired locks.
vfs_proxyfs_lock(handle, fsp, F_SETLK, 0, 0, F_UNLCK);
VFS_REMOVE_FSP_EXTENSION(handle, fsp);
free(fd);
}
static ssize_t vfs_proxyfs_pread(struct vfs_handle_struct *handle,
files_struct *fsp,
void *data,
size_t n,
off_t offset)
{
file_handle_t *fd;
fd = *(file_handle_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
if (fd == NULL) {
errno = EINVAL;
return -1;
}
if (n < 0) {
errno = EINVAL;
return -1;
}
proxyfs_io_request_t *req = (proxyfs_io_request_t *)malloc(sizeof(proxyfs_io_request_t));
if (req == NULL) {
errno = ENOMEM;
return -1;
}
req->op = IO_READ;
req->mount_handle = MOUNT_HANDLE(handle);
req->inode_number = fd->inum;
req->offset = offset;
req->length = n;
req->data = data;
req->error = 0;
req->out_size = 0;
req->done_cb = NULL;
req->done_cb_arg = NULL;
req->done_cb_fd = -1;
int ret = proxyfs_sync_io(req);
if (ret != 0) {
free(req);
errno = EIO;
return -1;
}
if (req->error != 0) {
errno = req->error;
free(req);
return -1;
}
ssize_t size = req->out_size;
free(req);
DEBUG(10, ("vfs_proxyfs_pread: %s inum=%ld offset=%ld size=%ld\n", fsp->fsp_name->base_name, fd->inum, offset, size));
return size;
}
static ssize_t vfs_proxyfs_read(struct vfs_handle_struct *handle,
files_struct *fsp,
void *data,
size_t n)
{
file_handle_t *fd;
fd = *(file_handle_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
ssize_t size = vfs_proxyfs_pread(handle, fsp, data, n, fd->offset);
if (size > 0) {
fd->offset += size;
}
return size;
}
static int read_fd = -1;
static int write_fd = -1;
static struct tevent_fd *aio_read_event = NULL;
typedef enum vfs_proxyfs_aio_state_e {
VFS_PROXYFS_AIO_DONE = 1,
VFS_PROXYFS_AIO_SENDING = 2,
VFS_PROXYFS_AIO_DONE_BEFORE_SEND_REPLY = 3,
VFS_PROXYFS_AIO_WAITING = 4,
} vfs_proxyfs_aio_state_t;
typedef struct vfs_proxyfs_tevent_status_s {
pthread_mutex_t mutex;
vfs_proxyfs_aio_state_t io_state;
void *req_id;
uint64_t ino;
off_t offset;
size_t length;
} vfs_proxyfs_tevent_status_t;
static void proxyfs_done_callback(proxyfs_io_request_t *pfs_io)
{
struct tevent_req *req = (struct tevent_req *)pfs_io->done_cb_arg;
int err = sys_write(pfs_io->done_cb_fd, &req, sizeof(struct tevent_req *));
if (err < 0) {
DEBUG(1, ("Write to AIO pipe failed: err %s\n", strerror(errno)));
}
}
static void vfs_proxyfs_aio_done_callback(struct tevent_context *event_ctx,
struct tevent_fd *fde,
uint16_t flags,
void *data)
{
struct tevent_req *req = NULL;
int err = sys_read(read_fd, &req, sizeof(struct tevent_req *));
if (err < 0) {
DEBUG(1, ("AIO READ from pipe failed %s\n", strerror(errno)));
return;
}
tevent_req_done(req);
}
static bool init_proxyfs_aio(struct vfs_handle_struct *handle)
{
int fds[2];
int ret = -1;
if (read_fd != -1) {
return true;
}
DEBUG(10, ("Starting proxyfs aio callback init..\n"));
ret = pipe(fds);
if (ret == -1) {
return false;
}
read_fd = fds[0];
write_fd = fds[1];
aio_read_event = tevent_add_fd(server_event_context(), NULL, read_fd, TEVENT_FD_READ, vfs_proxyfs_aio_done_callback, NULL);
if (aio_read_event == NULL) {
DEBUG(10, ("Failed to setup pipe for aio event handling\n"));
TALLOC_FREE(aio_read_event);
if (read_fd != -1) {
close(read_fd);
close(write_fd);
read_fd = -1;
write_fd = -1;
}
return false;
}
return true;
}
static struct tevent_req *vfs_proxyfs_pread_send(struct vfs_handle_struct *handle,
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct files_struct *fsp,
void *data,
size_t n, off_t offset)
{
proxyfs_io_request_t *pfs_io = NULL;
file_handle_t *fd = *(file_handle_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
if (fd == NULL) {
errno = EINVAL;
return NULL;
}
DEBUG(10, ("vfs_proxyfs_pread_send: %s inum=%ld offset=%ld size=%ld\n", fsp->fsp_name->base_name, fd->inum, offset, n));
struct tevent_req *req = tevent_req_create(mem_ctx, &pfs_io, proxyfs_io_request_t);
if (req == NULL) {
return NULL;
}
if (!init_proxyfs_aio(handle)) {
tevent_req_error(req, EIO);
return tevent_req_post(req, ev);
}
pfs_io->op = IO_READ;
pfs_io->mount_handle = MOUNT_HANDLE(handle);
pfs_io->inode_number = fd->inum;
pfs_io->offset = offset;
pfs_io->length = n;
pfs_io->data = data;
pfs_io->done_cb = proxyfs_done_callback;
pfs_io->done_cb_arg = (void *)req;
pfs_io->done_cb_fd = write_fd;
int ret = proxyfs_async_send(pfs_io);
if (ret < 0) {
talloc_free(req);
errno = ret;
return NULL;
}
return req;
}
static ssize_t vfs_proxyfs_read_recv(struct tevent_req *req,
int *err)
{
proxyfs_io_request_t *pfs_io = tevent_req_data(req, proxyfs_io_request_t);
if (pfs_io == NULL) {
return -1;
}
DEBUG(10, ("vfs_proxyfs_read_recv: inum=%lu offset=%ld size=%ld\n", pfs_io->inode_number, pfs_io->offset, pfs_io->length));
*err = pfs_io->error;
return pfs_io->out_size;
}
#if SAMBA_VERSION_MINOR >= 5
static ssize_t vfs_proxyfs_read_recv_4_4(struct tevent_req *req,
struct vfs_aio_state *state)
{
return vfs_proxyfs_read_recv(req, &state->error);
}
#endif
static ssize_t vfs_proxyfs_pwrite(struct vfs_handle_struct *handle,
files_struct *fsp,
const void *data,
size_t n, off_t offset)
{
file_handle_t *fd;
fd = *(file_handle_t **)VFS_FETCH_FSP_EXTENSION(handle, fsp);
if (fd == NULL) {