-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfarm.c
2464 lines (2230 loc) · 63.2 KB
/
gfarm.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
/*
* Gfarm Samba VFS module.
* v0.0.1 03 Sep 2010 Hiroki Ohtsuji <ohtsuji at hpcs.cs.tsukuba.ac.jp>
* Copyright (c) 2012 Osamu Tatebe. All Rights Reserved.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* $Id$
*/
#include "includes.h"
#include "smbd/proto.h"
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef PACKAGE_BUGREPORT
#include <gfarm/gfarm.h>
#include "gfarm_acl.h"
#include "gfarm_id.h"
#include "msgno/msgno.h"
/* internal interface */
int gfs_pio_fileno(GFS_File);
gfarm_error_t gfs_statdir(GFS_Dir, struct gfs_stat *);
/* XXX FIXME */
#define GFS_DEV ((dev_t)-1)
#define GFS_BLKSIZE 8192
#define STAT_BLKSIZ 512 /* for st_blocks */
static void
gfvfs_error(int msgno, const char *func_name, gfarm_error_t e)
{
if (e == GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY ||
e == GFARM_ERR_NO_SUCH_OBJECT ||
e == GFARM_ERR_IS_A_DIRECTORY ||
e == GFARM_ERR_ALREADY_EXISTS)
gflog_debug(msgno, "%s: %s", func_name, gfarm_error_string(e));
else
gflog_error(msgno, "%s: %s", func_name, gfarm_error_string(e));
}
static int
open_flags_gfarmize(int open_flags)
{
int gfs_flags;
switch (open_flags & O_ACCMODE) {
case O_RDONLY:
gfs_flags = GFARM_FILE_RDONLY;
break;
case O_WRONLY:
gfs_flags = GFARM_FILE_WRONLY;
break;
case O_RDWR:
gfs_flags = GFARM_FILE_RDWR;
break;
default:
return (-1);
}
if ((open_flags & O_TRUNC) != 0)
gfs_flags |= GFARM_FILE_TRUNC;
return (gfs_flags);
}
static uid_t
get_uid(const char *path, const char *user)
{
gfarm_error_t e;
uid_t uid;
e = gfarm_id_user_to_uid(path, user, &uid);
if (e != GFARM_ERR_NO_ERROR) {
gflog_warning(GFARM_MSG_3000001,
"get_uid(%s): %s", user, gfarm_error_string(e));
return (gfarm_id_nobody_uid());
}
return (uid);
}
static gid_t
get_gid(const char *path, const char *group)
{
gfarm_error_t e;
gid_t gid;
e = gfarm_id_group_to_gid(path, group, &gid);
if (e != GFARM_ERR_NO_ERROR) {
gflog_warning(GFARM_MSG_3000002,
"get_gid(%s): %s", group, gfarm_error_string(e));
return (gfarm_id_nogroup_gid());
}
return (gid);
}
static void
copy_gfs_stat(const char *path, SMB_STRUCT_STAT *dst, struct gfs_stat *src)
{
memset(dst, 0, sizeof(*dst));
dst->st_ex_dev = GFS_DEV;
dst->st_ex_ino = src->st_ino;
dst->st_ex_mode = src->st_mode;
dst->st_ex_nlink = src->st_nlink;
dst->st_ex_uid = get_uid(path, src->st_user);
dst->st_ex_gid = get_gid(path, src->st_group);
dst->st_ex_size = src->st_size;
dst->st_ex_blksize = GFS_BLKSIZE;
dst->st_ex_blocks = (src->st_size + STAT_BLKSIZ - 1) / STAT_BLKSIZ;
dst->st_ex_atime.tv_sec = src->st_atimespec.tv_sec;
dst->st_ex_atime.tv_nsec = src->st_atimespec.tv_nsec;
dst->st_ex_mtime.tv_sec = src->st_mtimespec.tv_sec;
dst->st_ex_mtime.tv_nsec = src->st_mtimespec.tv_nsec;
dst->st_ex_ctime.tv_sec = src->st_ctimespec.tv_sec;
dst->st_ex_ctime.tv_nsec = src->st_ctimespec.tv_nsec;
/* XXX dst->st_ex_btime, dst->st_ex_calculated_birthtime */
}
static int
switch_user(const char *user)
{
struct passwd *pwd = getpwnam(user);
if (pwd != NULL)
return (seteuid(pwd->pw_uid));
errno = ENOENT;
return (-1);
}
struct gfvfs_data {
char tmp_path[PATH_MAX + 1];
char *cwd;
};
static struct gfvfs_data *
gfvfs_data_init()
{
struct gfvfs_data *gdata;
GFARM_MALLOC(gdata);
if (gdata == NULL)
return (NULL);
gdata->cwd = NULL;
return (gdata);
}
static void
gfvfs_data_free(void **data)
{
struct gfvfs_data *gdata = *data;
free(gdata->cwd);
free(gdata);
}
static const char *
gfvfs_fullpath(vfs_handle_struct *handle, const char *path)
{
struct gfvfs_data *gdata = handle->data;
const char *cwd = gdata->cwd;
char *buf = gdata->tmp_path;
if (cwd == NULL || path[0] == '/')
return (path);
if (path[0] == '\0') /* "" */
return (cwd);
if (path[0] == '.') {
if (path[1] == '\0') /* "." */
return (cwd);
else if (path[1] == '/') /* "./NAME..." */
path += 2;
}
if (cwd[0] == '/') {
if (cwd[1] == '\0') /* rootdir */
snprintf(buf, PATH_MAX + 1, "/%s", path);
else
snprintf(buf, PATH_MAX + 1, "%s/%s", cwd, path);
} else
snprintf(buf, PATH_MAX + 1, "/%s/%s", cwd, path);
return (buf);
}
/*
* OPTIONS
* gfarm:config = PATH
*/
static int
gfvfs_connect(vfs_handle_struct *handle, const char *service,
const char *user)
{
gfarm_error_t e;
struct gfvfs_data *gdata;
gfarm_off_t used, avail, files;
uid_t uid = getuid(), saved_euid = geteuid();
const char *config = lp_parm_const_string(SNUM(handle->conn),
"gfarm", "config", NULL);
static const char log_id[] = "gfarm_samba";
gflog_set_identifier(log_id);
if (config != NULL)
setenv("GFARM_CONFIG_FILE", config, 0);
if (uid == 0) {
if (switch_user(user) == -1) {
int save_errno = errno;
gflog_error(GFARM_MSG_3000003,
"connect: unknown user=%s: %s",
user, strerror(errno));
errno = save_errno;
return (-1);
}
}
e = gfarm_initialize(NULL, NULL);
gflog_debug(GFARM_MSG_3000004,
"connect: %p, service=%s, user=%s, uid=%d/%d, config=%s",
handle, service, user, getuid(), geteuid(), config);
if (e != GFARM_ERR_NO_ERROR) {
if (uid == 0) /* must exit as saved_euid */
seteuid(saved_euid);
gflog_error(GFARM_MSG_3000005, "gfarm_initialize: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
/*
* Trying gfmd connection is nessessary here. Because some
* VFS callbacks by euid==0 will be called after this
* SMB_VFS_CONNECT callback. If a Gfarm connection by euid==0
* with GSI authentication succeeds once, the global user is
* recognized to be a user corresponding to the host
* certificate all the time.
*
* The callbacks behavior by euid==0 is:
* (samba_3.6.x)/source3/smbd/service.c#make_connection_snum()
*/
e = gfs_statfs(&used, &avail, &files);
if (uid == 0) /* must exit as saved_euid */
seteuid(saved_euid);
if (e != GFARM_ERR_NO_ERROR) {
(void)gfarm_terminate();
gflog_error(GFARM_MSG_3000006, "initial gfs_statfs: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
gfvfs_acl_id_init();
gdata = gfvfs_data_init();
if (gdata == NULL) {
(void)gfarm_terminate();
gflog_error(GFARM_MSG_3000007, "connect: no memory");
errno = ENOMEM;
return (-1);
}
handle->data = gdata;
handle->free_data = gfvfs_data_free;
return (0);
}
static void
gfvfs_disconnect(vfs_handle_struct *handle)
{
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000008, "disconnect: %p", handle);
e = gfarm_terminate();
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000009, "gfarm_terminate: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
}
}
static uint64_t
gfvfs_disk_free(vfs_handle_struct *handle, const char *path,
bool small_query, uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
{
gfarm_off_t used, avail, files;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000010, "disk_free: path=%s", path);
e = gfs_statfs(&used, &avail, &files);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000011, "gfs_statfs: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
*bsize = 1024;
*dfree = avail;
*dsize = used + avail;
return (0);
}
static int
gfvfs_get_quota(vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype,
unid_t id, SMB_DISK_QUOTA *dq)
{
gflog_debug(GFARM_MSG_3000012, "get_quota(ENOSYS): qtype=%d", qtype);
errno = ENOSYS;
return (-1);
}
static int
gfvfs_set_quota(vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype,
unid_t id, SMB_DISK_QUOTA *dq)
{
gflog_debug(GFARM_MSG_3000013, "set_quota(ENOSYS): qtype=%d", qtype);
errno = ENOSYS;
return (-1);
}
static int
gfvfs_get_shadow_copy_data(vfs_handle_struct *handle, files_struct *fsp,
struct shadow_copy_data *shadow_copy_data, bool labels)
{
gflog_debug(GFARM_MSG_3000014, "get_shadow_copy_data(ENOSYS): fsp=%p",
fsp);
errno = ENOSYS;
return (-1);
}
static int
gfvfs_statvfs(struct vfs_handle_struct *handle, const char *path,
struct vfs_statvfs_struct *statbuf)
{
gfarm_off_t used, avail, files;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000015, "statvfs: path=%s", path);
e = gfs_statfs(&used, &avail, &files);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000016, "gfs_statfs: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
statbuf->BlockSize = 1024;
statbuf->TotalBlocks = used + avail;
statbuf->BlocksAvail = avail;
statbuf->UserBlocksAvail = avail;
statbuf->TotalFileNodes = files;
statbuf->FreeFileNodes = -1;
return (0);
}
static uint32_t
gfvfs_fs_capabilities(struct vfs_handle_struct *handle,
enum timestamp_set_resolution *p_ts_res)
{
gflog_debug(GFARM_MSG_3000017, "fs_capabilities: uid=%d/%d",
getuid(), geteuid());
return (0);
}
struct gfvfs_dir {
GFS_Dir dp;
char *path;
};
static SMB_STRUCT_DIR *
gfvfs_opendir(vfs_handle_struct *handle, const char *fname,
const char *mask, uint32 attr)
{
GFS_Dir dp;
struct gfvfs_dir *gdp;
gfarm_error_t e;
const char *fullpath;
gflog_debug(GFARM_MSG_3000018, "opendir: path=%s, mask=%s",
fname, mask);
fullpath = gfvfs_fullpath(handle, fname);
e = gfs_opendir_caching(fullpath, &dp);
if (e == GFARM_ERR_NO_ERROR) {
GFARM_MALLOC(gdp);
if (gdp != NULL)
gdp->path = strdup(fullpath);
if (gdp == NULL || gdp->path == NULL) {
gflog_error(GFARM_MSG_3000019, "opendir: no memory");
free(gdp->path);
free(gdp);
gfs_closedir(dp);
return (NULL);
}
gdp->dp = dp;
return ((SMB_STRUCT_DIR *)gdp);
}
gflog_error(GFARM_MSG_3000020, "gfs_opendir: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (NULL);
}
static SMB_STRUCT_DIR *
gfvfs_fdopendir(vfs_handle_struct *handle, files_struct *fsp,
const char *mask, uint32 attr)
{
gflog_debug(GFARM_MSG_3000021, "fdopendir: ENOSYS");
errno = ENOSYS; /* use gfvfs_opendir() instead of this */
return (NULL);
}
/* returns static region */
static SMB_STRUCT_DIRENT *
gfvfs_readdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp,
SMB_STRUCT_STAT *sbuf)
{
struct gfvfs_dir *gdp = (struct gfvfs_dir *)dirp;
struct gfs_dirent *de;
static SMB_STRUCT_DIRENT ssd;
struct gfs_stat st;
char *path;
size_t len;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000022, "readdir: dir=%p", dirp);
if (sbuf)
SET_STAT_INVALID(*sbuf);
e = gfs_readdir(gdp->dp, &de);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000023, "gfs_readdir: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (NULL);
}
if (de == NULL)
return (NULL);
gflog_debug(GFARM_MSG_3000024, "readdir: name=%s", de->d_name);
ssd.d_ino = de->d_fileno;
ssd.d_reclen = de->d_reclen;
ssd.d_type = de->d_type;
ssd.d_off = 0;
strncpy(ssd.d_name, de->d_name, sizeof(ssd.d_name));
if (sbuf) {
/* gdp->path is fullpath */
len = strlen(gdp->path) + strlen(de->d_name) + 2;
GFARM_MALLOC_ARRAY(path, len);
if (path == NULL)
return (&ssd);
snprintf(path, len, "%s/%s", gdp->path, de->d_name);
gflog_debug(GFARM_MSG_3000025, "lstat: path=%s", path);
e = gfs_lstat_cached(path, &st);
if (e == GFARM_ERR_NO_ERROR) {
copy_gfs_stat(path, sbuf, &st);
gfs_stat_free(&st);
}
free(path);
}
return (&ssd);
}
static void
gfvfs_seekdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp, long offset)
{
struct gfvfs_dir *gdp = (struct gfvfs_dir *)dirp;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000026, "seekdir: dir=%p, offset=%ld",
dirp, offset);
e = gfs_seekdir(gdp->dp, offset);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000027, "gfs_seekdir: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
}
return;
}
static long
gfvfs_telldir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
{
gfarm_off_t off;
struct gfvfs_dir *gdp = (struct gfvfs_dir *)dirp;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000028, "telldir: dir=%p", dirp);
e = gfs_telldir(gdp->dp, &off);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000029, "gfs_telldir: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
return ((long)off);
}
static void
gfvfs_rewind_dir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
{
struct gfvfs_dir *gdp = (struct gfvfs_dir *)dirp;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000030, "rewind_dir: dir=%p", dirp);
e = gfs_seekdir(gdp->dp, 0);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000031, "gfs_seekdir: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
}
return;
}
static void
uncache_parent(const char *path)
{
char *p = gfarm_url_dir(path);
if (p == NULL) {
gflog_error(GFARM_MSG_3000032, "uncache_parent(%s): no memory",
path);
return;
}
gflog_debug(GFARM_MSG_3000033, "uncache_parent: parent=%s, path=%s",
p, path);
gfs_stat_cache_purge(p);
free(p);
}
static int
gfvfs_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
{
gfarm_error_t e;
const char *fullpath;
gflog_debug(GFARM_MSG_3000034, "mkdir: path=%s, mode=%o", path, mode);
fullpath = gfvfs_fullpath(handle, path);
e = gfs_mkdir(fullpath, mode & GFARM_S_ALLPERM);
if (e == GFARM_ERR_NO_ERROR) {
uncache_parent(fullpath);
return (0);
}
gfvfs_error(GFARM_MSG_3000035, "gfs_mkdir", e);
errno = gfarm_error_to_errno(e);
return (-1);
}
static int
gfvfs_rmdir(vfs_handle_struct *handle, const char *path)
{
gfarm_error_t e;
const char *fullpath;
gflog_debug(GFARM_MSG_3000036, "rmdir: path=%s", path);
fullpath = gfvfs_fullpath(handle, path);
e = gfs_rmdir(fullpath);
if (e == GFARM_ERR_NO_ERROR) {
gfs_stat_cache_purge(fullpath);
uncache_parent(fullpath);
return (0);
}
gflog_error(GFARM_MSG_3000037, "gfs_rmdir: %s", gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static int
gfvfs_closedir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
{
struct gfvfs_dir *gdp = (struct gfvfs_dir *)dirp;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000038, "closedir: dir=%p", dirp);
e = gfs_closedir(gdp->dp);
free(gdp->path);
free(gdp);
if (e == GFARM_ERR_NO_ERROR)
return (0);
gflog_error(GFARM_MSG_3000039, "gfs_closedir: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static void
gfvfs_init_search_op(struct vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
{
gflog_debug(GFARM_MSG_3000040, "init_search_op: dir=%p", dirp);
return;
}
static int
gfvfs_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
files_struct *fsp, int flags, mode_t mode)
{
int g_flags = open_flags_gfarmize(flags);
const char *fname = smb_fname->base_name, *fullpath;
const char *stream = smb_fname->stream_name;
char *msg;
GFS_File gf;
GFS_Dir dp;
struct gfvfs_dir *gdp;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000041,
"open: fsp=%p, path=%s%s, flags=%x, mode=%o, is_dir=%d",
fsp, fname, stream != NULL ? stream : "",
flags, mode, fsp->is_directory);
if (stream != NULL) {
errno = ENOENT;
return (-1);
}
if (g_flags < 0) {
gflog_error(GFARM_MSG_3000042, "open: %s", strerror(EINVAL));
errno = EINVAL;
return (-1);
}
fullpath = gfvfs_fullpath(handle, fname);
if (fsp->is_directory &&
(g_flags & GFARM_FILE_ACCMODE) == GFARM_FILE_RDONLY) {
msg = "gfs_opendir";
e = gfs_opendir(fullpath, &dp);
} else if (flags & O_CREAT) {
msg = "gfs_pio_create";
e = gfs_pio_create(fullpath, g_flags,
mode & GFARM_S_ALLPERM, &gf);
} else {
msg = "gfs_pio_open";
e = gfs_pio_open(fullpath, g_flags, &gf);
}
if (e != GFARM_ERR_NO_ERROR) {
gfvfs_error(GFARM_MSG_3000043, msg, e);
errno = gfarm_error_to_errno(e);
return (-1);
}
if (flags & O_CREAT)
uncache_parent(fullpath);
else
gfs_stat_cache_purge(fullpath);
/*
* XXX - gen_id is assigned in files.c as a unique number
* identifying this fsp over the life of this pid. I guess it
* is safe to use to store GFS_File.
*/
if (fsp->is_directory) {
GFARM_MALLOC(gdp);
if (gdp != NULL)
gdp->path = strdup(fullpath);
if (gdp == NULL || gdp->path == NULL) {
gflog_error(GFARM_MSG_3000044, "open: no memory");
free(gdp->path);
free(gdp);
gfs_closedir(dp);
errno = ENOMEM;
return (-1);
}
gdp->dp = dp;
fsp->fh->gen_id = (unsigned long)gdp;
return (0); /* dummy */
} else {
fsp->fh->gen_id = (unsigned long)gf;
return (gfs_pio_fileno(gf)); /* although do not use this */
}
}
static NTSTATUS
gfvfs_create_file(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,
uint64_t allocation_size, uint32_t private_flags,
struct security_descriptor *sd, struct ea_list *ea_list,
files_struct **result_fsp, int *pinfo)
{
NTSTATUS result;
const char *str_create_disposition;
gflog_debug(GFARM_MSG_3000045, "create_file(VFS_NEXT): %s%s",
smb_fname->base_name,
smb_fname->stream_name != NULL ? smb_fname->stream_name : "");
*result_fsp = NULL;
result = SMB_VFS_NEXT_CREATE_FILE(
handle, req, root_dir_fid, smb_fname, access_mask, share_access,
create_disposition, create_options, file_attributes,
oplock_request, allocation_size, private_flags, sd, ea_list,
result_fsp, pinfo);
switch (create_disposition) {
case FILE_SUPERSEDE:
str_create_disposition = "SUPERSEDE";
break;
case FILE_OVERWRITE_IF:
str_create_disposition = "OVERWRITE_IF";
break;
case FILE_OPEN:
str_create_disposition = "OPEN";
break;
case FILE_OVERWRITE:
str_create_disposition = "OVERWRITE";
break;
case FILE_CREATE:
str_create_disposition = "CREATE";
break;
case FILE_OPEN_IF:
str_create_disposition = "OPEN_IF";
break;
default:
str_create_disposition = "UNKNOWN";
}
gflog_debug(GFARM_MSG_3000046,
"create_file: %p|%s|0x%x|%s|%s|%s%s",
*result_fsp, nt_errstr(result), access_mask,
create_options & FILE_DIRECTORY_FILE ? "DIR" : "FILE",
str_create_disposition, smb_fname->base_name,
smb_fname->stream_name != NULL ? smb_fname->stream_name : "");
return (result);
}
static int
gfvfs_close_fn(vfs_handle_struct *handle, files_struct *fsp)
{
gfarm_error_t e;
char *msg;
gflog_debug(GFARM_MSG_3000047, "close_fn: fsp=%p", fsp);
if (!fsp->is_directory) {
msg = "gfs_pio_close";
e = gfs_pio_close((GFS_File)fsp->fh->gen_id);
} else {
struct gfvfs_dir *gdp = (struct gfvfs_dir *)fsp->fh->gen_id;
msg = "gfs_closedir";
e = gfs_closedir(gdp->dp);
free(gdp->path);
free(gdp);
}
if (e == GFARM_ERR_NO_ERROR)
return (0);
gflog_error(GFARM_MSG_3000048, "%s: %s", msg, gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static ssize_t
gfvfs_vfs_read(vfs_handle_struct *handle, files_struct *fsp,
void *data, size_t n)
{
gfarm_error_t e;
int rv;
gflog_debug(GFARM_MSG_3000049, "read: fsp=%p, size=%lu", fsp,
(unsigned long)n);
e = gfs_pio_read((GFS_File)fsp->fh->gen_id, data, n, &rv);
if (e == GFARM_ERR_NO_ERROR)
return (rv);
gflog_error(GFARM_MSG_3000050, "gfs_pio_read: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static ssize_t
gfvfs_pread(vfs_handle_struct *handle, files_struct *fsp,
void *data, size_t n, SMB_OFF_T offset)
{
gfarm_error_t e;
gfarm_off_t off;
GFS_File gf = (GFS_File)fsp->fh->gen_id;
int rv;
gflog_debug(GFARM_MSG_3000051, "pread: fsp=%p, size=%lu, offset=%lld",
fsp, (unsigned long)n, (long long)offset);
e = gfs_pio_seek(gf, (off_t)offset, GFARM_SEEK_SET, &off);
if (e == GFARM_ERR_NO_ERROR)
e = gfs_pio_read(gf, data, n, &rv);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000052, "gfs_pio_pread: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
return (rv);
}
static ssize_t
gfvfs_write(vfs_handle_struct *handle, files_struct *fsp,
const void *data, size_t n)
{
gfarm_error_t e;
int rv;
gflog_debug(GFARM_MSG_3000053, "write: fsp=%p, size=%lu", fsp,
(unsigned long)n);
e = gfs_pio_write((GFS_File)fsp->fh->gen_id, data, n, &rv);
if (e == GFARM_ERR_NO_ERROR) {
const char *fullpath =
gfvfs_fullpath(handle, fsp->fsp_name->base_name);
gfs_stat_cache_purge(fullpath);
return (rv);
}
gflog_error(GFARM_MSG_3000054, "gfs_pio_write: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static ssize_t
gfvfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
const void *data, size_t n, SMB_OFF_T offset)
{
gfarm_error_t e;
gfarm_off_t off;
int rv;
const char *fullpath;
gflog_debug(GFARM_MSG_3000055, "pwrite: fsp=%p, size=%lu, offset=%lld",
data, (unsigned long)n, (long long)offset);
e = gfs_pio_seek((GFS_File)fsp->fh->gen_id, offset, GFARM_SEEK_SET,
&off);
if (e == GFARM_ERR_NO_ERROR)
e = gfs_pio_write((GFS_File)fsp->fh->gen_id, data, n, &rv);
if (e != GFARM_ERR_NO_ERROR) {
gflog_error(GFARM_MSG_3000056, "gfs_pio_pwrite: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
fullpath = gfvfs_fullpath(handle, fsp->fsp_name->base_name);
gfs_stat_cache_purge(fullpath);
return (rv);
}
static SMB_OFF_T
gfvfs_lseek(vfs_handle_struct *handle, files_struct *fsp,
SMB_OFF_T offset, int whence)
{
gfarm_off_t off;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000057, "lseek: fsp=%p, offset=%ld, whence=%d",
fsp, (long)offset, whence);
e = gfs_pio_seek((GFS_File)fsp->fh->gen_id, offset, GFARM_SEEK_SET,
&off);
if (e == GFARM_ERR_NO_ERROR)
return (off);
gflog_error(GFARM_MSG_3000058, "gfs_pio_seek: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static ssize_t
gfvfs_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fromfsp,
const DATA_BLOB *hdr, SMB_OFF_T offset, size_t n)
{
gflog_debug(GFARM_MSG_3000059,
"sendfile(ENOSYS): to_fd=%d, from_fsp=%p", tofd, fromfsp);
errno = ENOSYS;
return (-1);
}
static ssize_t
gfvfs_recvfile(vfs_handle_struct *handle, int fromfd, files_struct *tofsp,
SMB_OFF_T offset, size_t n)
{
gflog_debug(GFARM_MSG_3000060,
"recvfile(ENOSYS): from_fd=%d, to_fsp=%p", fromfd, tofsp);
errno = ENOSYS;
return (-1);
}
static int
gfvfs_rename(vfs_handle_struct *handle,
const struct smb_filename *smb_fname_src,
const struct smb_filename *smb_fname_dst)
{
const char *oldname = smb_fname_src->base_name;
const char *old_stream = smb_fname_src->stream_name;
const char *newname = smb_fname_dst->base_name;
const char *new_stream = smb_fname_dst->stream_name;
char *fullold, *fullnew;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000061, "rename: old=%s%s, new=%s%s",
oldname, old_stream != NULL ? old_stream : "",
newname, new_stream != NULL ? new_stream : "");
if (old_stream != NULL || new_stream != NULL) {
errno = ENOENT;
return (-1);
}
fullold = strdup(gfvfs_fullpath(handle, oldname));
fullnew = strdup(gfvfs_fullpath(handle, newname));
if (fullold == NULL || fullnew == NULL) {
free(fullold);
free(fullnew);
gflog_error(GFARM_MSG_3000062, "rename: no memory");
errno = ENOMEM;
return (-1);
}
e = gfs_rename(fullold, fullnew);
if (e == GFARM_ERR_NO_ERROR) {
gfs_stat_cache_purge(fullold);
uncache_parent(fullold);
gfs_stat_cache_purge(fullnew);
uncache_parent(fullnew);
free(fullold);
free(fullnew);
return (0);
}
free(fullold);
free(fullnew);
gflog_error(GFARM_MSG_3000063,
"gfs_rename: %s", gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static int
gfvfs_fsync(vfs_handle_struct *handle, files_struct *fsp)
{
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000064, "fsync: fsp=%p", fsp);
e = gfs_pio_sync((GFS_File)fsp->fh->gen_id);
if (e == GFARM_ERR_NO_ERROR)
return (0);
gflog_error(GFARM_MSG_3000065, "gfs_pio_sync: %s",
gfarm_error_string(e));
errno = gfarm_error_to_errno(e);
return (-1);
}
static int
gfvfs_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
{
struct gfs_stat st;
const char *path = smb_fname->base_name, *fullpath;
const char *stream = smb_fname->stream_name;
gfarm_error_t e;
gflog_debug(GFARM_MSG_3000066, "stat: path=%s%s",
path, stream != NULL ? stream : "");
if (stream != NULL) {
errno = ENOENT;
return (-1);
}
fullpath = gfvfs_fullpath(handle, path);
e = gfs_stat_cached(fullpath, &st);
if (e != GFARM_ERR_NO_ERROR) {
gfvfs_error(GFARM_MSG_3000067, "gfs_stat", e);
errno = gfarm_error_to_errno(e);
return (-1);
}
copy_gfs_stat(fullpath, &smb_fname->st, &st);
gfs_stat_free(&st);
return (0);
}
static int
gfvfs_fstat(vfs_handle_struct *handle, files_struct *fsp,
SMB_STRUCT_STAT *sbuf)
{
struct gfs_stat st;
char *msg;
gfarm_error_t e;
const char *fullpath;
gflog_debug(GFARM_MSG_3000068, "fstat: fsp=%p, is_dir=%d",
fsp, fsp->is_directory);
if (!fsp->is_directory) {
msg = "gfs_pio_stat";
e = gfs_pio_stat((GFS_File)fsp->fh->gen_id, &st);
} else {
msg = "gfs_statdir";
e = gfs_statdir(
((struct gfvfs_dir *)fsp->fh->gen_id)->dp, &st);
}
if (e != GFARM_ERR_NO_ERROR) {