5c48d04f1c4105b912db984ad9e3949d996678c6
[metze/samba/wip.git] / source3 / modules / vfs_default.c
1 /*
2    Unix SMB/CIFS implementation.
3    Wrap disk only vfs functions to sidestep dodgy compilers.
4    Copyright (C) Tim Potter 1998
5    Copyright (C) Jeremy Allison 2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "ntioctl.h"
27 #include "smbprofile.h"
28 #include "../libcli/security/security.h"
29 #include "passdb/lookup_sid.h"
30 #include "source3/include/msdfs.h"
31 #include "librpc/gen_ndr/ndr_dfsblobs.h"
32 #include "lib/util/tevent_unix.h"
33 #include "lib/asys/asys.h"
34 #include "lib/util/tevent_ntstatus.h"
35
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_VFS
38
39 /* Check for NULL pointer parameters in vfswrap_* functions */
40
41 /* We don't want to have NULL function pointers lying around.  Someone
42    is sure to try and execute them.  These stubs are used to prevent
43    this possibility. */
44
45 static int vfswrap_connect(vfs_handle_struct *handle, const char *service, const char *user)
46 {
47     return 0;    /* Return >= 0 for success */
48 }
49
50 static void vfswrap_disconnect(vfs_handle_struct *handle)
51 {
52 }
53
54 /* Disk operations */
55
56 static uint64_t vfswrap_disk_free(vfs_handle_struct *handle, const char *path, bool small_query, uint64_t *bsize,
57                                uint64_t *dfree, uint64_t *dsize)
58 {
59         uint64_t result;
60
61         result = sys_disk_free(handle->conn, path, small_query, bsize, dfree, dsize);
62         return result;
63 }
64
65 static int vfswrap_get_quota(struct vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
66 {
67 #ifdef HAVE_SYS_QUOTAS
68         int result;
69
70         START_PROFILE(syscall_get_quota);
71         result = sys_get_quota(handle->conn->connectpath, qtype, id, qt);
72         END_PROFILE(syscall_get_quota);
73         return result;
74 #else
75         errno = ENOSYS;
76         return -1;
77 #endif
78 }
79
80 static int vfswrap_set_quota(struct vfs_handle_struct *handle, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
81 {
82 #ifdef HAVE_SYS_QUOTAS
83         int result;
84
85         START_PROFILE(syscall_set_quota);
86         result = sys_set_quota(handle->conn->connectpath, qtype, id, qt);
87         END_PROFILE(syscall_set_quota);
88         return result;
89 #else
90         errno = ENOSYS;
91         return -1;
92 #endif
93 }
94
95 static int vfswrap_get_shadow_copy_data(struct vfs_handle_struct *handle,
96                                         struct files_struct *fsp,
97                                         struct shadow_copy_data *shadow_copy_data,
98                                         bool labels)
99 {
100         errno = ENOSYS;
101         return -1;  /* Not implemented. */
102 }
103
104 static int vfswrap_statvfs(struct vfs_handle_struct *handle, const char *path, vfs_statvfs_struct *statbuf)
105 {
106         return sys_statvfs(path, statbuf);
107 }
108
109 static uint32_t vfswrap_fs_capabilities(struct vfs_handle_struct *handle,
110                 enum timestamp_set_resolution *p_ts_res)
111 {
112         connection_struct *conn = handle->conn;
113         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
114         struct smb_filename *smb_fname_cpath = NULL;
115         struct vfs_statvfs_struct statbuf;
116         int ret;
117
118         ZERO_STRUCT(statbuf);
119         ret = sys_statvfs(conn->connectpath, &statbuf);
120         if (ret == 0) {
121                 caps = statbuf.FsCapabilities;
122         }
123
124         *p_ts_res = TIMESTAMP_SET_SECONDS;
125
126         /* Work out what timestamp resolution we can
127          * use when setting a timestamp. */
128
129         smb_fname_cpath = synthetic_smb_fname(talloc_tos(), conn->connectpath,
130                                               NULL, NULL);
131         if (smb_fname_cpath == NULL) {
132                 return caps;
133         }
134
135         ret = SMB_VFS_STAT(conn, smb_fname_cpath);
136         if (ret == -1) {
137                 TALLOC_FREE(smb_fname_cpath);
138                 return caps;
139         }
140
141         if (smb_fname_cpath->st.st_ex_mtime.tv_nsec ||
142                         smb_fname_cpath->st.st_ex_atime.tv_nsec ||
143                         smb_fname_cpath->st.st_ex_ctime.tv_nsec) {
144                 /* If any of the normal UNIX directory timestamps
145                  * have a non-zero tv_nsec component assume
146                  * we might be able to set sub-second timestamps.
147                  * See what filetime set primitives we have.
148                  */
149 #if defined(HAVE_UTIMENSAT)
150                 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
151 #elif defined(HAVE_UTIMES)
152                 /* utimes allows msec timestamps to be set. */
153                 *p_ts_res = TIMESTAMP_SET_MSEC;
154 #elif defined(HAVE_UTIME)
155                 /* utime only allows sec timestamps to be set. */
156                 *p_ts_res = TIMESTAMP_SET_SECONDS;
157 #endif
158
159                 DEBUG(10,("vfswrap_fs_capabilities: timestamp "
160                         "resolution of %s "
161                         "available on share %s, directory %s\n",
162                         *p_ts_res == TIMESTAMP_SET_MSEC ? "msec" : "sec",
163                         lp_servicename(talloc_tos(), conn->params->service),
164                         conn->connectpath ));
165         }
166         TALLOC_FREE(smb_fname_cpath);
167         return caps;
168 }
169
170 static NTSTATUS vfswrap_get_dfs_referrals(struct vfs_handle_struct *handle,
171                                           struct dfs_GetDFSReferral *r)
172 {
173         struct junction_map *junction = NULL;
174         int consumedcnt = 0;
175         bool self_referral = false;
176         char *pathnamep = NULL;
177         char *local_dfs_path = NULL;
178         NTSTATUS status;
179         int i;
180         uint16_t max_referral_level = r->in.req.max_referral_level;
181
182         if (DEBUGLVL(10)) {
183                 NDR_PRINT_IN_DEBUG(dfs_GetDFSReferral, r);
184         }
185
186         /* get the junction entry */
187         if (r->in.req.servername == NULL) {
188                 return NT_STATUS_NOT_FOUND;
189         }
190
191         /*
192          * Trim pathname sent by client so it begins with only one backslash.
193          * Two backslashes confuse some dfs clients
194          */
195
196         local_dfs_path = talloc_strdup(r, r->in.req.servername);
197         if (local_dfs_path == NULL) {
198                 return NT_STATUS_NO_MEMORY;
199         }
200         pathnamep = local_dfs_path;
201         while (IS_DIRECTORY_SEP(pathnamep[0]) &&
202                IS_DIRECTORY_SEP(pathnamep[1])) {
203                 pathnamep++;
204         }
205
206         junction = talloc_zero(r, struct junction_map);
207         if (junction == NULL) {
208                 return NT_STATUS_NO_MEMORY;
209         }
210
211         /* The following call can change cwd. */
212         status = get_referred_path(r, pathnamep,
213                                    !handle->conn->sconn->using_smb2,
214                                    junction, &consumedcnt, &self_referral);
215         if (!NT_STATUS_IS_OK(status)) {
216                 vfs_ChDir(handle->conn, handle->conn->connectpath);
217                 return status;
218         }
219         vfs_ChDir(handle->conn, handle->conn->connectpath);
220
221         if (!self_referral) {
222                 pathnamep[consumedcnt] = '\0';
223
224                 if (DEBUGLVL(3)) {
225                         dbgtext("setup_dfs_referral: Path %s to "
226                                 "alternate path(s):",
227                                 pathnamep);
228                         for (i=0; i < junction->referral_count; i++) {
229                                 dbgtext(" %s",
230                                 junction->referral_list[i].alternate_path);
231                         }
232                         dbgtext(".\n");
233                 }
234         }
235
236         if (r->in.req.max_referral_level <= 2) {
237                 max_referral_level = 2;
238         }
239         if (r->in.req.max_referral_level >= 3) {
240                 max_referral_level = 3;
241         }
242
243         r->out.resp = talloc_zero(r, struct dfs_referral_resp);
244         if (r->out.resp == NULL) {
245                 return NT_STATUS_NO_MEMORY;
246         }
247
248         r->out.resp->path_consumed = strlen_m(pathnamep) * 2;
249         r->out.resp->nb_referrals = junction->referral_count;
250
251         r->out.resp->header_flags = DFS_HEADER_FLAG_STORAGE_SVR;
252         if (self_referral) {
253                 r->out.resp->header_flags |= DFS_HEADER_FLAG_REFERAL_SVR;
254         }
255
256         r->out.resp->referral_entries = talloc_zero_array(r,
257                                 struct dfs_referral_type,
258                                 r->out.resp->nb_referrals);
259         if (r->out.resp->referral_entries == NULL) {
260                 return NT_STATUS_NO_MEMORY;
261         }
262
263         switch (max_referral_level) {
264         case 2:
265                 for(i=0; i < junction->referral_count; i++) {
266                         struct referral *ref = &junction->referral_list[i];
267                         TALLOC_CTX *mem_ctx = r->out.resp->referral_entries;
268                         struct dfs_referral_type *t =
269                                 &r->out.resp->referral_entries[i];
270                         struct dfs_referral_v2 *v2 = &t->referral.v2;
271
272                         t->version = 2;
273                         v2->size = VERSION2_REFERRAL_SIZE;
274                         if (self_referral) {
275                                 v2->server_type = DFS_SERVER_ROOT;
276                         } else {
277                                 v2->server_type = DFS_SERVER_NON_ROOT;
278                         }
279                         v2->entry_flags = 0;
280                         v2->proximity = ref->proximity;
281                         v2->ttl = ref->ttl;
282                         v2->DFS_path = talloc_strdup(mem_ctx, pathnamep);
283                         if (v2->DFS_path == NULL) {
284                                 return NT_STATUS_NO_MEMORY;
285                         }
286                         v2->DFS_alt_path = talloc_strdup(mem_ctx, pathnamep);
287                         if (v2->DFS_alt_path == NULL) {
288                                 return NT_STATUS_NO_MEMORY;
289                         }
290                         v2->netw_address = talloc_strdup(mem_ctx,
291                                                          ref->alternate_path);
292                         if (v2->netw_address == NULL) {
293                                 return NT_STATUS_NO_MEMORY;
294                         }
295                 }
296
297                 break;
298         case 3:
299                 for(i=0; i < junction->referral_count; i++) {
300                         struct referral *ref = &junction->referral_list[i];
301                         TALLOC_CTX *mem_ctx = r->out.resp->referral_entries;
302                         struct dfs_referral_type *t =
303                                 &r->out.resp->referral_entries[i];
304                         struct dfs_referral_v3 *v3 = &t->referral.v3;
305                         struct dfs_normal_referral *r1 = &v3->referrals.r1;
306
307                         t->version = 3;
308                         v3->size = VERSION3_REFERRAL_SIZE;
309                         if (self_referral) {
310                                 v3->server_type = DFS_SERVER_ROOT;
311                         } else {
312                                 v3->server_type = DFS_SERVER_NON_ROOT;
313                         }
314                         v3->entry_flags = 0;
315                         v3->ttl = ref->ttl;
316                         r1->DFS_path = talloc_strdup(mem_ctx, pathnamep);
317                         if (r1->DFS_path == NULL) {
318                                 return NT_STATUS_NO_MEMORY;
319                         }
320                         r1->DFS_alt_path = talloc_strdup(mem_ctx, pathnamep);
321                         if (r1->DFS_alt_path == NULL) {
322                                 return NT_STATUS_NO_MEMORY;
323                         }
324                         r1->netw_address = talloc_strdup(mem_ctx,
325                                                          ref->alternate_path);
326                         if (r1->netw_address == NULL) {
327                                 return NT_STATUS_NO_MEMORY;
328                         }
329                 }
330                 break;
331         default:
332                 DEBUG(0,("setup_dfs_referral: Invalid dfs referral "
333                         "version: %d\n",
334                         max_referral_level));
335                 return NT_STATUS_INVALID_LEVEL;
336         }
337
338         if (DEBUGLVL(10)) {
339                 NDR_PRINT_OUT_DEBUG(dfs_GetDFSReferral, r);
340         }
341
342         return NT_STATUS_OK;
343 }
344
345 /* Directory operations */
346
347 static DIR *vfswrap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
348 {
349         DIR *result;
350
351         START_PROFILE(syscall_opendir);
352         result = opendir(fname);
353         END_PROFILE(syscall_opendir);
354         return result;
355 }
356
357 static DIR *vfswrap_fdopendir(vfs_handle_struct *handle,
358                         files_struct *fsp,
359                         const char *mask,
360                         uint32 attr)
361 {
362         DIR *result;
363
364         START_PROFILE(syscall_fdopendir);
365         result = sys_fdopendir(fsp->fh->fd);
366         END_PROFILE(syscall_fdopendir);
367         return result;
368 }
369
370
371 static struct dirent *vfswrap_readdir(vfs_handle_struct *handle,
372                                           DIR *dirp,
373                                           SMB_STRUCT_STAT *sbuf)
374 {
375         struct dirent *result;
376
377         START_PROFILE(syscall_readdir);
378         result = readdir(dirp);
379         END_PROFILE(syscall_readdir);
380         if (sbuf) {
381                 /* Default Posix readdir() does not give us stat info.
382                  * Set to invalid to indicate we didn't return this info. */
383                 SET_STAT_INVALID(*sbuf);
384 #if defined(HAVE_DIRFD) && defined(HAVE_FSTATAT)
385                 if (result != NULL) {
386                         /* See if we can efficiently return this. */
387                         struct stat st;
388                         int flags = (lp_posix_pathnames() ?
389                                 AT_SYMLINK_NOFOLLOW : 0);
390                         int ret = fstatat(dirfd(dirp),
391                                         result->d_name,
392                                         &st,
393                                         flags);
394                         if (ret == 0) {
395                                 init_stat_ex_from_stat(sbuf,
396                                         &st,
397                                         lp_fake_directory_create_times(
398                                                 SNUM(handle->conn)));
399                         }
400                 }
401 #endif
402         }
403         return result;
404 }
405
406 static void vfswrap_seekdir(vfs_handle_struct *handle, DIR *dirp, long offset)
407 {
408         START_PROFILE(syscall_seekdir);
409         seekdir(dirp, offset);
410         END_PROFILE(syscall_seekdir);
411 }
412
413 static long vfswrap_telldir(vfs_handle_struct *handle, DIR *dirp)
414 {
415         long result;
416         START_PROFILE(syscall_telldir);
417         result = telldir(dirp);
418         END_PROFILE(syscall_telldir);
419         return result;
420 }
421
422 static void vfswrap_rewinddir(vfs_handle_struct *handle, DIR *dirp)
423 {
424         START_PROFILE(syscall_rewinddir);
425         rewinddir(dirp);
426         END_PROFILE(syscall_rewinddir);
427 }
428
429 static int vfswrap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
430 {
431         int result;
432         bool has_dacl = False;
433         char *parent = NULL;
434
435         START_PROFILE(syscall_mkdir);
436
437         if (lp_inherit_acls(SNUM(handle->conn))
438             && parent_dirname(talloc_tos(), path, &parent, NULL)
439             && (has_dacl = directory_has_default_acl(handle->conn, parent)))
440                 mode = (0777 & lp_directory_mask(SNUM(handle->conn)));
441
442         TALLOC_FREE(parent);
443
444         result = mkdir(path, mode);
445
446         if (result == 0 && !has_dacl) {
447                 /*
448                  * We need to do this as the default behavior of POSIX ACLs
449                  * is to set the mask to be the requested group permission
450                  * bits, not the group permission bits to be the requested
451                  * group permission bits. This is not what we want, as it will
452                  * mess up any inherited ACL bits that were set. JRA.
453                  */
454                 int saved_errno = errno; /* We may get ENOSYS */
455                 if ((SMB_VFS_CHMOD_ACL(handle->conn, path, mode) == -1) && (errno == ENOSYS))
456                         errno = saved_errno;
457         }
458
459         END_PROFILE(syscall_mkdir);
460         return result;
461 }
462
463 static int vfswrap_rmdir(vfs_handle_struct *handle, const char *path)
464 {
465         int result;
466
467         START_PROFILE(syscall_rmdir);
468         result = rmdir(path);
469         END_PROFILE(syscall_rmdir);
470         return result;
471 }
472
473 static int vfswrap_closedir(vfs_handle_struct *handle, DIR *dirp)
474 {
475         int result;
476
477         START_PROFILE(syscall_closedir);
478         result = closedir(dirp);
479         END_PROFILE(syscall_closedir);
480         return result;
481 }
482
483 static void vfswrap_init_search_op(vfs_handle_struct *handle,
484                                    DIR *dirp)
485 {
486         /* Default behavior is a NOOP */
487 }
488
489 /* File operations */
490
491 static int vfswrap_open(vfs_handle_struct *handle,
492                         struct smb_filename *smb_fname,
493                         files_struct *fsp, int flags, mode_t mode)
494 {
495         int result = -1;
496
497         START_PROFILE(syscall_open);
498
499         if (smb_fname->stream_name) {
500                 errno = ENOENT;
501                 goto out;
502         }
503
504         result = open(smb_fname->base_name, flags, mode);
505  out:
506         END_PROFILE(syscall_open);
507         return result;
508 }
509
510 static NTSTATUS vfswrap_create_file(vfs_handle_struct *handle,
511                                     struct smb_request *req,
512                                     uint16_t root_dir_fid,
513                                     struct smb_filename *smb_fname,
514                                     uint32_t access_mask,
515                                     uint32_t share_access,
516                                     uint32_t create_disposition,
517                                     uint32_t create_options,
518                                     uint32_t file_attributes,
519                                     uint32_t oplock_request,
520                                     uint64_t allocation_size,
521                                     uint32_t private_flags,
522                                     struct security_descriptor *sd,
523                                     struct ea_list *ea_list,
524                                     files_struct **result,
525                                     int *pinfo)
526 {
527         return create_file_default(handle->conn, req, root_dir_fid, smb_fname,
528                                    access_mask, share_access,
529                                    create_disposition, create_options,
530                                    file_attributes, oplock_request,
531                                    allocation_size, private_flags,
532                                    sd, ea_list, result,
533                                    pinfo);
534 }
535
536 static int vfswrap_close(vfs_handle_struct *handle, files_struct *fsp)
537 {
538         int result;
539
540         START_PROFILE(syscall_close);
541         result = fd_close_posix(fsp);
542         END_PROFILE(syscall_close);
543         return result;
544 }
545
546 static ssize_t vfswrap_read(vfs_handle_struct *handle, files_struct *fsp, void *data, size_t n)
547 {
548         ssize_t result;
549
550         START_PROFILE_BYTES(syscall_read, n);
551         result = sys_read(fsp->fh->fd, data, n);
552         END_PROFILE(syscall_read);
553         return result;
554 }
555
556 static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, void *data,
557                         size_t n, off_t offset)
558 {
559         ssize_t result;
560
561 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
562         START_PROFILE_BYTES(syscall_pread, n);
563         result = sys_pread(fsp->fh->fd, data, n, offset);
564         END_PROFILE(syscall_pread);
565
566         if (result == -1 && errno == ESPIPE) {
567                 /* Maintain the fiction that pipes can be seeked (sought?) on. */
568                 result = SMB_VFS_READ(fsp, data, n);
569                 fsp->fh->pos = 0;
570         }
571
572 #else /* HAVE_PREAD */
573         off_t   curr;
574         int lerrno;
575
576         curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
577         if (curr == -1 && errno == ESPIPE) {
578                 /* Maintain the fiction that pipes can be seeked (sought?) on. */
579                 result = SMB_VFS_READ(fsp, data, n);
580                 fsp->fh->pos = 0;
581                 return result;
582         }
583
584         if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
585                 return -1;
586         }
587
588         errno = 0;
589         result = SMB_VFS_READ(fsp, data, n);
590         lerrno = errno;
591
592         SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
593         errno = lerrno;
594
595 #endif /* HAVE_PREAD */
596
597         return result;
598 }
599
600 static ssize_t vfswrap_write(vfs_handle_struct *handle, files_struct *fsp, const void *data, size_t n)
601 {
602         ssize_t result;
603
604         START_PROFILE_BYTES(syscall_write, n);
605         result = sys_write(fsp->fh->fd, data, n);
606         END_PROFILE(syscall_write);
607         return result;
608 }
609
610 static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, const void *data,
611                         size_t n, off_t offset)
612 {
613         ssize_t result;
614
615 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
616         START_PROFILE_BYTES(syscall_pwrite, n);
617         result = sys_pwrite(fsp->fh->fd, data, n, offset);
618         END_PROFILE(syscall_pwrite);
619
620         if (result == -1 && errno == ESPIPE) {
621                 /* Maintain the fiction that pipes can be sought on. */
622                 result = SMB_VFS_WRITE(fsp, data, n);
623         }
624
625 #else /* HAVE_PWRITE */
626         off_t   curr;
627         int         lerrno;
628
629         curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
630         if (curr == -1) {
631                 return -1;
632         }
633
634         if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
635                 return -1;
636         }
637
638         result = SMB_VFS_WRITE(fsp, data, n);
639         lerrno = errno;
640
641         SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
642         errno = lerrno;
643
644 #endif /* HAVE_PWRITE */
645
646         return result;
647 }
648
649 static void vfswrap_asys_finished(struct tevent_context *ev,
650                                   struct tevent_fd *fde,
651                                   uint16_t flags, void *p);
652
653 static bool vfswrap_init_asys_ctx(struct smbXsrv_connection *conn)
654 {
655         int ret;
656         int fd;
657
658         if (conn->asys_ctx != NULL) {
659                 return true;
660         }
661         ret = asys_context_init(&conn->asys_ctx, aio_pending_size);
662         if (ret != 0) {
663                 DEBUG(1, ("asys_context_init failed: %s\n", strerror(ret)));
664                 return false;
665         }
666
667         fd = asys_signalfd(conn->asys_ctx);
668
669         set_blocking(fd, false);
670
671         conn->asys_fde = tevent_add_fd(conn->ev_ctx, conn, fd,
672                                        TEVENT_FD_READ,
673                                        vfswrap_asys_finished,
674                                        conn->asys_ctx);
675         if (conn->asys_fde == NULL) {
676                 DEBUG(1, ("tevent_add_fd failed\n"));
677                 asys_context_destroy(conn->asys_ctx);
678                 conn->asys_ctx = NULL;
679                 return false;
680         }
681         return true;
682 }
683
684 struct vfswrap_asys_state {
685         struct asys_context *asys_ctx;
686         struct tevent_req *req;
687         ssize_t ret;
688         int err;
689 };
690
691 static int vfswrap_asys_state_destructor(struct vfswrap_asys_state *s)
692 {
693         asys_cancel(s->asys_ctx, s->req);
694         return 0;
695 }
696
697 static struct tevent_req *vfswrap_pread_send(struct vfs_handle_struct *handle,
698                                              TALLOC_CTX *mem_ctx,
699                                              struct tevent_context *ev,
700                                              struct files_struct *fsp,
701                                              void *data,
702                                              size_t n, off_t offset)
703 {
704         struct tevent_req *req;
705         struct vfswrap_asys_state *state;
706         int ret;
707
708         req = tevent_req_create(mem_ctx, &state, struct vfswrap_asys_state);
709         if (req == NULL) {
710                 return NULL;
711         }
712         if (!vfswrap_init_asys_ctx(handle->conn->sconn->conn)) {
713                 tevent_req_oom(req);
714                 return tevent_req_post(req, ev);
715         }
716         state->asys_ctx = handle->conn->sconn->conn->asys_ctx;
717         state->req = req;
718
719         ret = asys_pread(state->asys_ctx, fsp->fh->fd, data, n, offset, req);
720         if (ret != 0) {
721                 tevent_req_error(req, ret);
722                 return tevent_req_post(req, ev);
723         }
724         talloc_set_destructor(state, vfswrap_asys_state_destructor);
725
726         return req;
727 }
728
729 static struct tevent_req *vfswrap_pwrite_send(struct vfs_handle_struct *handle,
730                                               TALLOC_CTX *mem_ctx,
731                                               struct tevent_context *ev,
732                                               struct files_struct *fsp,
733                                               const void *data,
734                                               size_t n, off_t offset)
735 {
736         struct tevent_req *req;
737         struct vfswrap_asys_state *state;
738         int ret;
739
740         req = tevent_req_create(mem_ctx, &state, struct vfswrap_asys_state);
741         if (req == NULL) {
742                 return NULL;
743         }
744         if (!vfswrap_init_asys_ctx(handle->conn->sconn->conn)) {
745                 tevent_req_oom(req);
746                 return tevent_req_post(req, ev);
747         }
748         state->asys_ctx = handle->conn->sconn->conn->asys_ctx;
749         state->req = req;
750
751         ret = asys_pwrite(state->asys_ctx, fsp->fh->fd, data, n, offset, req);
752         if (ret != 0) {
753                 tevent_req_error(req, ret);
754                 return tevent_req_post(req, ev);
755         }
756         talloc_set_destructor(state, vfswrap_asys_state_destructor);
757
758         return req;
759 }
760
761 static struct tevent_req *vfswrap_fsync_send(struct vfs_handle_struct *handle,
762                                              TALLOC_CTX *mem_ctx,
763                                              struct tevent_context *ev,
764                                              struct files_struct *fsp)
765 {
766         struct tevent_req *req;
767         struct vfswrap_asys_state *state;
768         int ret;
769
770         req = tevent_req_create(mem_ctx, &state, struct vfswrap_asys_state);
771         if (req == NULL) {
772                 return NULL;
773         }
774         if (!vfswrap_init_asys_ctx(handle->conn->sconn->conn)) {
775                 tevent_req_oom(req);
776                 return tevent_req_post(req, ev);
777         }
778         state->asys_ctx = handle->conn->sconn->conn->asys_ctx;
779         state->req = req;
780
781         ret = asys_fsync(state->asys_ctx, fsp->fh->fd, req);
782         if (ret != 0) {
783                 tevent_req_error(req, ret);
784                 return tevent_req_post(req, ev);
785         }
786         talloc_set_destructor(state, vfswrap_asys_state_destructor);
787
788         return req;
789 }
790
791 static void vfswrap_asys_finished(struct tevent_context *ev,
792                                         struct tevent_fd *fde,
793                                         uint16_t flags, void *p)
794 {
795         struct asys_context *asys_ctx = (struct asys_context *)p;
796         struct tevent_req *req;
797         struct vfswrap_asys_state *state;
798         int res;
799         ssize_t ret;
800         int err;
801         void *private_data;
802
803         if ((flags & TEVENT_FD_READ) == 0) {
804                 return;
805         }
806
807         while (true) {
808                 res = asys_result(asys_ctx, &ret, &err, &private_data);
809                 if (res == EINTR || res == EAGAIN) {
810                         return;
811                 }
812 #ifdef EWOULDBLOCK
813                 if (res == EWOULDBLOCK) {
814                         return;
815                 }
816 #endif
817
818                 if (res == ECANCELED) {
819                         return;
820                 }
821
822                 if (res != 0) {
823                         DEBUG(1, ("asys_result returned %s\n", strerror(res)));
824                         return;
825                 }
826
827                 req = talloc_get_type_abort(private_data, struct tevent_req);
828                 state = tevent_req_data(req, struct vfswrap_asys_state);
829
830                 talloc_set_destructor(state, NULL);
831
832                 state->ret = ret;
833                 state->err = err;
834                 tevent_req_defer_callback(req, ev);
835                 tevent_req_done(req);
836         }
837 }
838
839 static ssize_t vfswrap_asys_ssize_t_recv(struct tevent_req *req, int *err)
840 {
841         struct vfswrap_asys_state *state = tevent_req_data(
842                 req, struct vfswrap_asys_state);
843
844         if (tevent_req_is_unix_error(req, err)) {
845                 return -1;
846         }
847         *err = state->err;
848         return state->ret;
849 }
850
851 static int vfswrap_asys_int_recv(struct tevent_req *req, int *err)
852 {
853         struct vfswrap_asys_state *state = tevent_req_data(
854                 req, struct vfswrap_asys_state);
855
856         if (tevent_req_is_unix_error(req, err)) {
857                 return -1;
858         }
859         *err = state->err;
860         return state->ret;
861 }
862
863 static off_t vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, off_t offset, int whence)
864 {
865         off_t result = 0;
866
867         START_PROFILE(syscall_lseek);
868
869         /* Cope with 'stat' file opens. */
870         if (fsp->fh->fd != -1)
871                 result = lseek(fsp->fh->fd, offset, whence);
872
873         /*
874          * We want to maintain the fiction that we can seek
875          * on a fifo for file system purposes. This allows
876          * people to set up UNIX fifo's that feed data to Windows
877          * applications. JRA.
878          */
879
880         if((result == -1) && (errno == ESPIPE)) {
881                 result = 0;
882                 errno = 0;
883         }
884
885         END_PROFILE(syscall_lseek);
886         return result;
887 }
888
889 static ssize_t vfswrap_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
890                         off_t offset, size_t n)
891 {
892         ssize_t result;
893
894         START_PROFILE_BYTES(syscall_sendfile, n);
895         result = sys_sendfile(tofd, fromfsp->fh->fd, hdr, offset, n);
896         END_PROFILE(syscall_sendfile);
897         return result;
898 }
899
900 static ssize_t vfswrap_recvfile(vfs_handle_struct *handle,
901                         int fromfd,
902                         files_struct *tofsp,
903                         off_t offset,
904                         size_t n)
905 {
906         ssize_t result;
907
908         START_PROFILE_BYTES(syscall_recvfile, n);
909         result = sys_recvfile(fromfd, tofsp->fh->fd, offset, n);
910         END_PROFILE(syscall_recvfile);
911         return result;
912 }
913
914 static int vfswrap_rename(vfs_handle_struct *handle,
915                           const struct smb_filename *smb_fname_src,
916                           const struct smb_filename *smb_fname_dst)
917 {
918         int result = -1;
919
920         START_PROFILE(syscall_rename);
921
922         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
923                 errno = ENOENT;
924                 goto out;
925         }
926
927         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
928
929  out:
930         END_PROFILE(syscall_rename);
931         return result;
932 }
933
934 static int vfswrap_fsync(vfs_handle_struct *handle, files_struct *fsp)
935 {
936 #ifdef HAVE_FSYNC
937         int result;
938
939         START_PROFILE(syscall_fsync);
940         result = fsync(fsp->fh->fd);
941         END_PROFILE(syscall_fsync);
942         return result;
943 #else
944         return 0;
945 #endif
946 }
947
948 static int vfswrap_stat(vfs_handle_struct *handle,
949                         struct smb_filename *smb_fname)
950 {
951         int result = -1;
952
953         START_PROFILE(syscall_stat);
954
955         if (smb_fname->stream_name) {
956                 errno = ENOENT;
957                 goto out;
958         }
959
960         result = sys_stat(smb_fname->base_name, &smb_fname->st,
961                           lp_fake_directory_create_times(SNUM(handle->conn)));
962  out:
963         END_PROFILE(syscall_stat);
964         return result;
965 }
966
967 static int vfswrap_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
968 {
969         int result;
970
971         START_PROFILE(syscall_fstat);
972         result = sys_fstat(fsp->fh->fd,
973                            sbuf, lp_fake_directory_create_times(SNUM(handle->conn)));
974         END_PROFILE(syscall_fstat);
975         return result;
976 }
977
978 static int vfswrap_lstat(vfs_handle_struct *handle,
979                          struct smb_filename *smb_fname)
980 {
981         int result = -1;
982
983         START_PROFILE(syscall_lstat);
984
985         if (smb_fname->stream_name) {
986                 errno = ENOENT;
987                 goto out;
988         }
989
990         result = sys_lstat(smb_fname->base_name, &smb_fname->st,
991                            lp_fake_directory_create_times(SNUM(handle->conn)));
992  out:
993         END_PROFILE(syscall_lstat);
994         return result;
995 }
996
997 static NTSTATUS vfswrap_translate_name(struct vfs_handle_struct *handle,
998                                        const char *name,
999                                        enum vfs_translate_direction direction,
1000                                        TALLOC_CTX *mem_ctx,
1001                                        char **mapped_name)
1002 {
1003         return NT_STATUS_NONE_MAPPED;
1004 }
1005
1006 /*
1007  * Implement the default fsctl operation.
1008  */
1009 static bool vfswrap_logged_ioctl_message = false;
1010
1011 static NTSTATUS vfswrap_fsctl(struct vfs_handle_struct *handle,
1012                               struct files_struct *fsp,
1013                               TALLOC_CTX *ctx,
1014                               uint32_t function,
1015                               uint16_t req_flags, /* Needed for UNICODE ... */
1016                               const uint8_t *_in_data,
1017                               uint32_t in_len,
1018                               uint8_t **_out_data,
1019                               uint32_t max_out_len,
1020                               uint32_t *out_len)
1021 {
1022         const char *in_data = (const char *)_in_data;
1023         char **out_data = (char **)_out_data;
1024
1025         switch (function) {
1026         case FSCTL_SET_SPARSE:
1027         {
1028                 bool set_sparse = true;
1029                 NTSTATUS status;
1030
1031                 if (in_len >= 1 && in_data[0] == 0) {
1032                         set_sparse = false;
1033                 }
1034
1035                 status = file_set_sparse(handle->conn, fsp, set_sparse);
1036
1037                 DEBUG(NT_STATUS_IS_OK(status) ? 10 : 9,
1038                       ("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
1039                        smb_fname_str_dbg(fsp->fsp_name), set_sparse,
1040                        nt_errstr(status)));
1041
1042                 return status;
1043         }
1044
1045         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1046         {
1047                 unsigned char objid[16];
1048                 char *return_data = NULL;
1049
1050                 /* This should return the object-id on this file.
1051                  * I think I'll make this be the inode+dev. JRA.
1052                  */
1053
1054                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on %s\n",
1055                           fsp_fnum_dbg(fsp)));
1056
1057                 *out_len = (max_out_len >= 64) ? 64 : max_out_len;
1058                 /* Hmmm, will this cause problems if less data asked for? */
1059                 return_data = talloc_array(ctx, char, 64);
1060                 if (return_data == NULL) {
1061                         return NT_STATUS_NO_MEMORY;
1062                 }
1063
1064                 /* For backwards compatibility only store the dev/inode. */
1065                 push_file_id_16(return_data, &fsp->file_id);
1066                 memcpy(return_data+16,create_volume_objectid(fsp->conn,objid),16);
1067                 push_file_id_16(return_data+32, &fsp->file_id);
1068                 memset(return_data+48, 0, 16);
1069                 *out_data = return_data;
1070                 return NT_STATUS_OK;
1071         }
1072
1073         case FSCTL_GET_REPARSE_POINT:
1074         {
1075                 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
1076                 DEBUG(10, ("FSCTL_GET_REPARSE_POINT: called on %s. "
1077                            "Status: NOT_IMPLEMENTED\n", fsp_fnum_dbg(fsp)));
1078                 return NT_STATUS_NOT_A_REPARSE_POINT;
1079         }
1080
1081         case FSCTL_SET_REPARSE_POINT:
1082         {
1083                 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
1084                 DEBUG(10, ("FSCTL_SET_REPARSE_POINT: called on %s. "
1085                            "Status: NOT_IMPLEMENTED\n", fsp_fnum_dbg(fsp)));
1086                 return NT_STATUS_NOT_A_REPARSE_POINT;
1087         }
1088
1089         case FSCTL_GET_SHADOW_COPY_DATA:
1090         {
1091                 /*
1092                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1093                  * and return their volume names.  If max_data_count is 16, then it is just
1094                  * asking for the number of volumes and length of the combined names.
1095                  *
1096                  * pdata is the data allocated by our caller, but that uses
1097                  * total_data_count (which is 0 in our case) rather than max_data_count.
1098                  * Allocate the correct amount and return the pointer to let
1099                  * it be deallocated when we return.
1100                  */
1101                 struct shadow_copy_data *shadow_data = NULL;
1102                 bool labels = False;
1103                 uint32 labels_data_count = 0;
1104                 uint32 i;
1105                 char *cur_pdata = NULL;
1106
1107                 if (max_out_len < 16) {
1108                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1109                                 max_out_len));
1110                         return NT_STATUS_INVALID_PARAMETER;
1111                 }
1112
1113                 if (max_out_len > 16) {
1114                         labels = True;
1115                 }
1116
1117                 shadow_data = talloc_zero(ctx, struct shadow_copy_data);
1118                 if (shadow_data == NULL) {
1119                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
1120                         return NT_STATUS_NO_MEMORY;
1121                 }
1122
1123                 /*
1124                  * Call the VFS routine to actually do the work.
1125                  */
1126                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1127                         TALLOC_FREE(shadow_data);
1128                         if (errno == ENOSYS) {
1129                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1130                                         fsp->conn->connectpath));
1131                                 return NT_STATUS_NOT_SUPPORTED;
1132                         } else {
1133                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1134                                         fsp->conn->connectpath));
1135                                 return NT_STATUS_UNSUCCESSFUL;
1136                         }
1137                 }
1138
1139                 labels_data_count = (shadow_data->num_volumes * 2 *
1140                                         sizeof(SHADOW_COPY_LABEL)) + 2;
1141
1142                 if (!labels) {
1143                         *out_len = 16;
1144                 } else {
1145                         *out_len = 12 + labels_data_count;
1146                 }
1147
1148                 if (max_out_len < *out_len) {
1149                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1150                                 max_out_len, *out_len));
1151                         TALLOC_FREE(shadow_data);
1152                         return NT_STATUS_BUFFER_TOO_SMALL;
1153                 }
1154
1155                 cur_pdata = talloc_zero_array(ctx, char, *out_len);
1156                 if (cur_pdata == NULL) {
1157                         TALLOC_FREE(shadow_data);
1158                         return NT_STATUS_NO_MEMORY;
1159                 }
1160
1161                 *out_data = cur_pdata;
1162
1163                 /* num_volumes 4 bytes */
1164                 SIVAL(cur_pdata, 0, shadow_data->num_volumes);
1165
1166                 if (labels) {
1167                         /* num_labels 4 bytes */
1168                         SIVAL(cur_pdata, 4, shadow_data->num_volumes);
1169                 }
1170
1171                 /* needed_data_count 4 bytes */
1172                 SIVAL(cur_pdata, 8, labels_data_count);
1173
1174                 cur_pdata += 12;
1175
1176                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
1177                           shadow_data->num_volumes, fsp_str_dbg(fsp)));
1178                 if (labels && shadow_data->labels) {
1179                         for (i=0; i<shadow_data->num_volumes; i++) {
1180                                 srvstr_push(cur_pdata, req_flags,
1181                                             cur_pdata, shadow_data->labels[i],
1182                                             2 * sizeof(SHADOW_COPY_LABEL),
1183                                             STR_UNICODE|STR_TERMINATE);
1184                                 cur_pdata += 2 * sizeof(SHADOW_COPY_LABEL);
1185                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
1186                         }
1187                 }
1188
1189                 TALLOC_FREE(shadow_data);
1190
1191                 return NT_STATUS_OK;
1192         }
1193
1194         case FSCTL_FIND_FILES_BY_SID:
1195         {
1196                 /* pretend this succeeded -
1197                  *
1198                  * we have to send back a list with all files owned by this SID
1199                  *
1200                  * but I have to check that --metze
1201                  */
1202                 struct dom_sid sid;
1203                 uid_t uid;
1204                 size_t sid_len;
1205
1206                 DEBUG(10, ("FSCTL_FIND_FILES_BY_SID: called on %s\n",
1207                            fsp_fnum_dbg(fsp)));
1208
1209                 if (in_len < 8) {
1210                         /* NT_STATUS_BUFFER_TOO_SMALL maybe? */
1211                         return NT_STATUS_INVALID_PARAMETER;
1212                 }
1213
1214                 sid_len = MIN(in_len - 4,SID_MAX_SIZE);
1215
1216                 /* unknown 4 bytes: this is not the length of the sid :-(  */
1217                 /*unknown = IVAL(pdata,0);*/
1218
1219                 if (!sid_parse(in_data + 4, sid_len, &sid)) {
1220                         return NT_STATUS_INVALID_PARAMETER;
1221                 }
1222                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
1223
1224                 if (!sid_to_uid(&sid, &uid)) {
1225                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
1226                                  sid_string_dbg(&sid),
1227                                  (unsigned long)sid_len));
1228                         uid = (-1);
1229                 }
1230
1231                 /* we can take a look at the find source :-)
1232                  *
1233                  * find ./ -uid $uid  -name '*'   is what we need here
1234                  *
1235                  *
1236                  * and send 4bytes len and then NULL terminated unicode strings
1237                  * for each file
1238                  *
1239                  * but I don't know how to deal with the paged results
1240                  * (maybe we can hang the result anywhere in the fsp struct)
1241                  *
1242                  * but I don't know how to deal with the paged results
1243                  * (maybe we can hang the result anywhere in the fsp struct)
1244                  *
1245                  * we don't send all files at once
1246                  * and at the next we should *not* start from the beginning,
1247                  * so we have to cache the result
1248                  *
1249                  * --metze
1250                  */
1251
1252                 /* this works for now... */
1253                 return NT_STATUS_OK;
1254         }
1255
1256         case FSCTL_QUERY_ALLOCATED_RANGES:
1257         {
1258                 /* FIXME: This is just a dummy reply, telling that all of the
1259                  * file is allocated. MKS cp needs that.
1260                  * Adding the real allocated ranges via FIEMAP on Linux
1261                  * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
1262                  * this FSCTL correct for sparse files.
1263                  */
1264                 NTSTATUS status;
1265                 uint64_t offset, length;
1266                 char *out_data_tmp = NULL;
1267
1268                 if (in_len != 16) {
1269                         DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
1270                                 in_len));
1271                         return NT_STATUS_INVALID_PARAMETER;
1272                 }
1273
1274                 if (max_out_len < 16) {
1275                         DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_out_len (%u) < 16 is invalid!\n",
1276                                 max_out_len));
1277                         return NT_STATUS_INVALID_PARAMETER;
1278                 }
1279
1280                 offset = BVAL(in_data,0);
1281                 length = BVAL(in_data,8);
1282
1283                 if (offset + length < offset) {
1284                         /* No 64-bit integer wrap. */
1285                         return NT_STATUS_INVALID_PARAMETER;
1286                 }
1287
1288                 /* Shouldn't this be SMB_VFS_STAT ... ? */
1289                 status = vfs_stat_fsp(fsp);
1290                 if (!NT_STATUS_IS_OK(status)) {
1291                         return status;
1292                 }
1293
1294                 *out_len = 16;
1295                 out_data_tmp = talloc_array(ctx, char, *out_len);
1296                 if (out_data_tmp == NULL) {
1297                         DEBUG(10, ("unable to allocate memory for response\n"));
1298                         return NT_STATUS_NO_MEMORY;
1299                 }
1300
1301                 if (offset > fsp->fsp_name->st.st_ex_size ||
1302                                 fsp->fsp_name->st.st_ex_size == 0 ||
1303                                 length == 0) {
1304                         memset(out_data_tmp, 0, *out_len);
1305                 } else {
1306                         uint64_t end = offset + length;
1307                         end = MIN(end, fsp->fsp_name->st.st_ex_size);
1308                         SBVAL(out_data_tmp, 0, 0);
1309                         SBVAL(out_data_tmp, 8, end);
1310                 }
1311
1312                 *out_data = out_data_tmp;
1313
1314                 return NT_STATUS_OK;
1315         }
1316
1317         case FSCTL_IS_VOLUME_DIRTY:
1318         {
1319                 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on %s "
1320                           "(but remotely not supported)\n", fsp_fnum_dbg(fsp)));
1321                 /*
1322                  * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
1323                  * says we have to respond with NT_STATUS_INVALID_PARAMETER
1324                  */
1325                 return NT_STATUS_INVALID_PARAMETER;
1326         }
1327
1328         default:
1329                 /*
1330                  * Only print once ... unfortunately there could be lots of
1331                  * different FSCTLs that are called.
1332                  */
1333                 if (!vfswrap_logged_ioctl_message) {
1334                         vfswrap_logged_ioctl_message = true;
1335                         DEBUG(2, ("%s (0x%x): Currently not implemented.\n",
1336                         __func__, function));
1337                 }
1338         }
1339
1340         return NT_STATUS_NOT_SUPPORTED;
1341 }
1342
1343 struct vfs_cc_state {
1344         off_t copied;
1345         uint8_t buf[65536];
1346 };
1347
1348 static struct tevent_req *vfswrap_copy_chunk_send(struct vfs_handle_struct *handle,
1349                                                   TALLOC_CTX *mem_ctx,
1350                                                   struct tevent_context *ev,
1351                                                   struct files_struct *src_fsp,
1352                                                   off_t src_off,
1353                                                   struct files_struct *dest_fsp,
1354                                                   off_t dest_off,
1355                                                   off_t num)
1356 {
1357         struct tevent_req *req;
1358         struct vfs_cc_state *vfs_cc_state;
1359         NTSTATUS status;
1360
1361         DEBUG(10, ("performing server side copy chunk of length %lu\n",
1362                    (unsigned long)num));
1363
1364         req = tevent_req_create(mem_ctx, &vfs_cc_state, struct vfs_cc_state);
1365         if (req == NULL) {
1366                 return NULL;
1367         }
1368
1369         status = vfs_stat_fsp(src_fsp);
1370         if (tevent_req_nterror(req, status)) {
1371                 return tevent_req_post(req, ev);
1372         }
1373
1374         if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
1375                 /*
1376                  * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
1377                  *   If the SourceOffset or SourceOffset + Length extends beyond
1378                  *   the end of file, the server SHOULD<240> treat this as a
1379                  *   STATUS_END_OF_FILE error.
1380                  * ...
1381                  *   <240> Section 3.3.5.15.6: Windows servers will return
1382                  *   STATUS_INVALID_VIEW_SIZE instead of STATUS_END_OF_FILE.
1383                  */
1384                 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
1385                 return tevent_req_post(req, ev);
1386         }
1387
1388         /* could use 2.6.33+ sendfile here to do this in kernel */
1389         while (vfs_cc_state->copied < num) {
1390                 ssize_t ret;
1391                 struct lock_struct lck;
1392                 int saved_errno;
1393
1394                 off_t this_num = MIN(sizeof(vfs_cc_state->buf),
1395                                      num - vfs_cc_state->copied);
1396
1397                 init_strict_lock_struct(src_fsp,
1398                                         src_fsp->op->global->open_persistent_id,
1399                                         src_off,
1400                                         this_num,
1401                                         READ_LOCK,
1402                                         &lck);
1403
1404                 if (!SMB_VFS_STRICT_LOCK(src_fsp->conn, src_fsp, &lck)) {
1405                         tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
1406                         return tevent_req_post(req, ev);
1407                 }
1408
1409                 ret = SMB_VFS_PREAD(src_fsp, vfs_cc_state->buf,
1410                                     this_num, src_off);
1411                 if (ret == -1) {
1412                         saved_errno = errno;
1413                 }
1414
1415                 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &lck);
1416
1417                 if (ret == -1) {
1418                         errno = saved_errno;
1419                         tevent_req_nterror(req, map_nt_error_from_unix(errno));
1420                         return tevent_req_post(req, ev);
1421                 }
1422                 if (ret != this_num) {
1423                         /* zero tolerance for short reads */
1424                         tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
1425                         return tevent_req_post(req, ev);
1426                 }
1427
1428                 src_off += ret;
1429
1430                 init_strict_lock_struct(dest_fsp,
1431                                         dest_fsp->op->global->open_persistent_id,
1432                                         dest_off,
1433                                         this_num,
1434                                         WRITE_LOCK,
1435                                         &lck);
1436
1437                 if (!SMB_VFS_STRICT_LOCK(dest_fsp->conn, dest_fsp, &lck)) {
1438                         tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
1439                         return tevent_req_post(req, ev);
1440                 }
1441
1442                 ret = SMB_VFS_PWRITE(dest_fsp, vfs_cc_state->buf,
1443                                      this_num, dest_off);
1444                 if (ret == -1) {
1445                         saved_errno = errno;
1446                 }
1447
1448                 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &lck);
1449
1450                 if (ret == -1) {
1451                         errno = saved_errno;
1452                         tevent_req_nterror(req, map_nt_error_from_unix(errno));
1453                         return tevent_req_post(req, ev);
1454                 }
1455                 if (ret != this_num) {
1456                         /* zero tolerance for short writes */
1457                         tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
1458                         return tevent_req_post(req, ev);
1459                 }
1460                 dest_off += ret;
1461
1462                 vfs_cc_state->copied += this_num;
1463         }
1464
1465         tevent_req_done(req);
1466         return tevent_req_post(req, ev);
1467 }
1468
1469 static NTSTATUS vfswrap_copy_chunk_recv(struct vfs_handle_struct *handle,
1470                                         struct tevent_req *req,
1471                                         off_t *copied)
1472 {
1473         struct vfs_cc_state *vfs_cc_state = tevent_req_data(req,
1474                                                         struct vfs_cc_state);
1475         NTSTATUS status;
1476
1477         if (tevent_req_is_nterror(req, &status)) {
1478                 DEBUG(2, ("server side copy chunk failed: %s\n",
1479                           nt_errstr(status)));
1480                 *copied = 0;
1481                 tevent_req_received(req);
1482                 return status;
1483         }
1484
1485         *copied = vfs_cc_state->copied;
1486         DEBUG(10, ("server side copy chunk copied %lu\n",
1487                    (unsigned long)*copied));
1488         tevent_req_received(req);
1489
1490         return NT_STATUS_OK;
1491 }
1492
1493 static NTSTATUS vfswrap_get_compression(struct vfs_handle_struct *handle,
1494                                         TALLOC_CTX *mem_ctx,
1495                                         struct files_struct *fsp,
1496                                         struct smb_filename *smb_fname,
1497                                         uint16_t *_compression_fmt)
1498 {
1499         return NT_STATUS_INVALID_DEVICE_REQUEST;
1500 }
1501
1502 static NTSTATUS vfswrap_set_compression(struct vfs_handle_struct *handle,
1503                                         TALLOC_CTX *mem_ctx,
1504                                         struct files_struct *fsp,
1505                                         uint16_t compression_fmt)
1506 {
1507         return NT_STATUS_INVALID_DEVICE_REQUEST;
1508 }
1509
1510 /********************************************************************
1511  Given a stat buffer return the allocated size on disk, taking into
1512  account sparse files.
1513 ********************************************************************/
1514 static uint64_t vfswrap_get_alloc_size(vfs_handle_struct *handle,
1515                                        struct files_struct *fsp,
1516                                        const SMB_STRUCT_STAT *sbuf)
1517 {
1518         uint64_t result;
1519
1520         START_PROFILE(syscall_get_alloc_size);
1521
1522         if(S_ISDIR(sbuf->st_ex_mode)) {
1523                 result = 0;
1524                 goto out;
1525         }
1526
1527 #if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
1528         /* The type of st_blocksize is blkcnt_t which *MUST* be
1529            signed (according to POSIX) and can be less than 64-bits.
1530            Ensure when we're converting to 64 bits wide we don't
1531            sign extend. */
1532 #if defined(SIZEOF_BLKCNT_T_8)
1533         result = (uint64_t)STAT_ST_BLOCKSIZE * (uint64_t)sbuf->st_ex_blocks;
1534 #elif defined(SIZEOF_BLKCNT_T_4)
1535         {
1536                 uint64_t bs = ((uint64_t)sbuf->st_ex_blocks) & 0xFFFFFFFFLL;
1537                 result = (uint64_t)STAT_ST_BLOCKSIZE * bs;
1538         }
1539 #else
1540 #error SIZEOF_BLKCNT_T_NOT_A_SUPPORTED_VALUE
1541 #endif
1542 #else
1543         result = get_file_size_stat(sbuf);
1544 #endif
1545
1546         if (fsp && fsp->initial_allocation_size)
1547                 result = MAX(result,fsp->initial_allocation_size);
1548
1549         result = smb_roundup(handle->conn, result);
1550
1551  out:
1552         END_PROFILE(syscall_get_alloc_size);
1553         return result;
1554 }
1555
1556 static int vfswrap_unlink(vfs_handle_struct *handle,
1557                           const struct smb_filename *smb_fname)
1558 {
1559         int result = -1;
1560
1561         START_PROFILE(syscall_unlink);
1562
1563         if (smb_fname->stream_name) {
1564                 errno = ENOENT;
1565                 goto out;
1566         }
1567         result = unlink(smb_fname->base_name);
1568
1569  out:
1570         END_PROFILE(syscall_unlink);
1571         return result;
1572 }
1573
1574 static int vfswrap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
1575 {
1576         int result;
1577
1578         START_PROFILE(syscall_chmod);
1579
1580         /*
1581          * We need to do this due to the fact that the default POSIX ACL
1582          * chmod modifies the ACL *mask* for the group owner, not the
1583          * group owner bits directly. JRA.
1584          */
1585
1586
1587         {
1588                 int saved_errno = errno; /* We might get ENOSYS */
1589                 if ((result = SMB_VFS_CHMOD_ACL(handle->conn, path, mode)) == 0) {
1590                         END_PROFILE(syscall_chmod);
1591                         return result;
1592                 }
1593                 /* Error - return the old errno. */
1594                 errno = saved_errno;
1595         }
1596
1597         result = chmod(path, mode);
1598         END_PROFILE(syscall_chmod);
1599         return result;
1600 }
1601
1602 static int vfswrap_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1603 {
1604         int result;
1605
1606         START_PROFILE(syscall_fchmod);
1607
1608         /*
1609          * We need to do this due to the fact that the default POSIX ACL
1610          * chmod modifies the ACL *mask* for the group owner, not the
1611          * group owner bits directly. JRA.
1612          */
1613
1614         {
1615                 int saved_errno = errno; /* We might get ENOSYS */
1616                 if ((result = SMB_VFS_FCHMOD_ACL(fsp, mode)) == 0) {
1617                         END_PROFILE(syscall_fchmod);
1618                         return result;
1619                 }
1620                 /* Error - return the old errno. */
1621                 errno = saved_errno;
1622         }
1623
1624 #if defined(HAVE_FCHMOD)
1625         result = fchmod(fsp->fh->fd, mode);
1626 #else
1627         result = -1;
1628         errno = ENOSYS;
1629 #endif
1630
1631         END_PROFILE(syscall_fchmod);
1632         return result;
1633 }
1634
1635 static int vfswrap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
1636 {
1637         int result;
1638
1639         START_PROFILE(syscall_chown);
1640         result = chown(path, uid, gid);
1641         END_PROFILE(syscall_chown);
1642         return result;
1643 }
1644
1645 static int vfswrap_fchown(vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
1646 {
1647 #ifdef HAVE_FCHOWN
1648         int result;
1649
1650         START_PROFILE(syscall_fchown);
1651         result = fchown(fsp->fh->fd, uid, gid);
1652         END_PROFILE(syscall_fchown);
1653         return result;
1654 #else
1655         errno = ENOSYS;
1656         return -1;
1657 #endif
1658 }
1659
1660 static int vfswrap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
1661 {
1662         int result;
1663
1664         START_PROFILE(syscall_lchown);
1665         result = lchown(path, uid, gid);
1666         END_PROFILE(syscall_lchown);
1667         return result;
1668 }
1669
1670 static int vfswrap_chdir(vfs_handle_struct *handle, const char *path)
1671 {
1672         int result;
1673
1674         START_PROFILE(syscall_chdir);
1675         result = chdir(path);
1676         END_PROFILE(syscall_chdir);
1677         return result;
1678 }
1679
1680 static char *vfswrap_getwd(vfs_handle_struct *handle)
1681 {
1682         char *result;
1683
1684         START_PROFILE(syscall_getwd);
1685         result = sys_getwd();
1686         END_PROFILE(syscall_getwd);
1687         return result;
1688 }
1689
1690 /*********************************************************************
1691  nsec timestamp resolution call. Convert down to whatever the underlying
1692  system will support.
1693 **********************************************************************/
1694
1695 static int vfswrap_ntimes(vfs_handle_struct *handle,
1696                           const struct smb_filename *smb_fname,
1697                           struct smb_file_time *ft)
1698 {
1699         int result = -1;
1700
1701         START_PROFILE(syscall_ntimes);
1702
1703         if (smb_fname->stream_name) {
1704                 errno = ENOENT;
1705                 goto out;
1706         }
1707
1708         if (ft != NULL) {
1709                 if (null_timespec(ft->atime)) {
1710                         ft->atime= smb_fname->st.st_ex_atime;
1711                 }
1712
1713                 if (null_timespec(ft->mtime)) {
1714                         ft->mtime = smb_fname->st.st_ex_mtime;
1715                 }
1716
1717                 if (!null_timespec(ft->create_time)) {
1718                         set_create_timespec_ea(handle->conn,
1719                                                smb_fname,
1720                                                ft->create_time);
1721                 }
1722
1723                 if ((timespec_compare(&ft->atime,
1724                                       &smb_fname->st.st_ex_atime) == 0) &&
1725                     (timespec_compare(&ft->mtime,
1726                                       &smb_fname->st.st_ex_mtime) == 0)) {
1727                         return 0;
1728                 }
1729         }
1730
1731 #if defined(HAVE_UTIMENSAT)
1732         if (ft != NULL) {
1733                 struct timespec ts[2];
1734                 ts[0] = ft->atime;
1735                 ts[1] = ft->mtime;
1736                 result = utimensat(AT_FDCWD, smb_fname->base_name, ts, 0);
1737         } else {
1738                 result = utimensat(AT_FDCWD, smb_fname->base_name, NULL, 0);
1739         }
1740         if (!((result == -1) && (errno == ENOSYS))) {
1741                 goto out;
1742         }
1743 #endif
1744 #if defined(HAVE_UTIMES)
1745         if (ft != NULL) {
1746                 struct timeval tv[2];
1747                 tv[0] = convert_timespec_to_timeval(ft->atime);
1748                 tv[1] = convert_timespec_to_timeval(ft->mtime);
1749                 result = utimes(smb_fname->base_name, tv);
1750         } else {
1751                 result = utimes(smb_fname->base_name, NULL);
1752         }
1753         if (!((result == -1) && (errno == ENOSYS))) {
1754                 goto out;
1755         }
1756 #endif
1757 #if defined(HAVE_UTIME)
1758         if (ft != NULL) {
1759                 struct utimbuf times;
1760                 times.actime = convert_timespec_to_time_t(ft->atime);
1761                 times.modtime = convert_timespec_to_time_t(ft->mtime);
1762                 result = utime(smb_fname->base_name, &times);
1763         } else {
1764                 result = utime(smb_fname->base_name, NULL);
1765         }
1766         if (!((result == -1) && (errno == ENOSYS))) {
1767                 goto out;
1768         }
1769 #endif
1770         errno = ENOSYS;
1771         result = -1;
1772
1773  out:
1774         END_PROFILE(syscall_ntimes);
1775         return result;
1776 }
1777
1778 /*********************************************************************
1779  A version of ftruncate that will write the space on disk if strict
1780  allocate is set.
1781 **********************************************************************/
1782
1783 static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fsp, off_t len)
1784 {
1785         off_t space_to_write;
1786         uint64_t space_avail;
1787         uint64_t bsize,dfree,dsize;
1788         int ret;
1789         NTSTATUS status;
1790         SMB_STRUCT_STAT *pst;
1791
1792         status = vfs_stat_fsp(fsp);
1793         if (!NT_STATUS_IS_OK(status)) {
1794                 return -1;
1795         }
1796         pst = &fsp->fsp_name->st;
1797
1798 #ifdef S_ISFIFO
1799         if (S_ISFIFO(pst->st_ex_mode))
1800                 return 0;
1801 #endif
1802
1803         if (pst->st_ex_size == len)
1804                 return 0;
1805
1806         /* Shrink - just ftruncate. */
1807         if (pst->st_ex_size > len)
1808                 return ftruncate(fsp->fh->fd, len);
1809
1810         space_to_write = len - pst->st_ex_size;
1811
1812         /* for allocation try fallocate first. This can fail on some
1813            platforms e.g. when the filesystem doesn't support it and no
1814            emulation is being done by the libc (like on AIX with JFS1). In that
1815            case we do our own emulation. fallocate implementations can
1816            return ENOTSUP or EINVAL in cases like that. */
1817         ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_EXTEND_SIZE,
1818                                 pst->st_ex_size, space_to_write);
1819         if (ret == ENOSPC) {
1820                 errno = ENOSPC;
1821                 return -1;
1822         }
1823         if (ret == 0) {
1824                 return 0;
1825         }
1826         DEBUG(10,("strict_allocate_ftruncate: SMB_VFS_FALLOCATE failed with "
1827                 "error %d. Falling back to slow manual allocation\n", ret));
1828
1829         /* available disk space is enough or not? */
1830         space_avail = get_dfree_info(fsp->conn,
1831                                      fsp->fsp_name->base_name, false,
1832                                      &bsize,&dfree,&dsize);
1833         /* space_avail is 1k blocks */
1834         if (space_avail == (uint64_t)-1 ||
1835                         ((uint64_t)space_to_write/1024 > space_avail) ) {
1836                 errno = ENOSPC;
1837                 return -1;
1838         }
1839
1840         /* Write out the real space on disk. */
1841         ret = vfs_slow_fallocate(fsp, pst->st_ex_size, space_to_write);
1842         if (ret != 0) {
1843                 errno = ret;
1844                 ret = -1;
1845         }
1846
1847         return 0;
1848 }
1849
1850 static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, off_t len)
1851 {
1852         int result = -1;
1853         SMB_STRUCT_STAT *pst;
1854         NTSTATUS status;
1855         char c = 0;
1856
1857         START_PROFILE(syscall_ftruncate);
1858
1859         if (lp_strict_allocate(SNUM(fsp->conn)) && !fsp->is_sparse) {
1860                 result = strict_allocate_ftruncate(handle, fsp, len);
1861                 END_PROFILE(syscall_ftruncate);
1862                 return result;
1863         }
1864
1865         /* we used to just check HAVE_FTRUNCATE_EXTEND and only use
1866            ftruncate if the system supports it. Then I discovered that
1867            you can have some filesystems that support ftruncate
1868            expansion and some that don't! On Linux fat can't do
1869            ftruncate extend but ext2 can. */
1870
1871         result = ftruncate(fsp->fh->fd, len);
1872         if (result == 0)
1873                 goto done;
1874
1875         /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
1876            extend a file with ftruncate. Provide alternate implementation
1877            for this */
1878
1879         /* Do an fstat to see if the file is longer than the requested
1880            size in which case the ftruncate above should have
1881            succeeded or shorter, in which case seek to len - 1 and
1882            write 1 byte of zero */
1883         status = vfs_stat_fsp(fsp);
1884         if (!NT_STATUS_IS_OK(status)) {
1885                 goto done;
1886         }
1887         pst = &fsp->fsp_name->st;
1888
1889 #ifdef S_ISFIFO
1890         if (S_ISFIFO(pst->st_ex_mode)) {
1891                 result = 0;
1892                 goto done;
1893         }
1894 #endif
1895
1896         if (pst->st_ex_size == len) {
1897                 result = 0;
1898                 goto done;
1899         }
1900
1901         if (pst->st_ex_size > len) {
1902                 /* the ftruncate should have worked */
1903                 goto done;
1904         }
1905
1906         if (SMB_VFS_PWRITE(fsp, &c, 1, len-1)!=1) {
1907                 goto done;
1908         }
1909
1910         result = 0;
1911
1912   done:
1913
1914         END_PROFILE(syscall_ftruncate);
1915         return result;
1916 }
1917
1918 static int vfswrap_fallocate(vfs_handle_struct *handle,
1919                         files_struct *fsp,
1920                         enum vfs_fallocate_mode mode,
1921                         off_t offset,
1922                         off_t len)
1923 {
1924         int result;
1925
1926         START_PROFILE(syscall_fallocate);
1927         if (mode == VFS_FALLOCATE_EXTEND_SIZE) {
1928                 result = sys_posix_fallocate(fsp->fh->fd, offset, len);
1929         } else if (mode == VFS_FALLOCATE_KEEP_SIZE) {
1930                 result = sys_fallocate(fsp->fh->fd, mode, offset, len);
1931         } else {
1932                 errno = EINVAL;
1933                 result = -1;
1934         }
1935         END_PROFILE(syscall_fallocate);
1936         return result;
1937 }
1938
1939 static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, off_t offset, off_t count, int type)
1940 {
1941         bool result;
1942
1943         START_PROFILE(syscall_fcntl_lock);
1944         result =  fcntl_lock(fsp->fh->fd, op, offset, count, type);
1945         END_PROFILE(syscall_fcntl_lock);
1946         return result;
1947 }
1948
1949 static int vfswrap_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
1950                                 uint32 share_mode, uint32 access_mask)
1951 {
1952         START_PROFILE(syscall_kernel_flock);
1953         kernel_flock(fsp->fh->fd, share_mode, access_mask);
1954         END_PROFILE(syscall_kernel_flock);
1955         return 0;
1956 }
1957
1958 static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1959 {
1960         bool result;
1961
1962         START_PROFILE(syscall_fcntl_getlock);
1963         result =  fcntl_getlock(fsp->fh->fd, poffset, pcount, ptype, ppid);
1964         END_PROFILE(syscall_fcntl_getlock);
1965         return result;
1966 }
1967
1968 static int vfswrap_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1969                                 int leasetype)
1970 {
1971         int result = -1;
1972
1973         START_PROFILE(syscall_linux_setlease);
1974
1975 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1976         result = linux_setlease(fsp->fh->fd, leasetype);
1977 #else
1978         errno = ENOSYS;
1979 #endif
1980         END_PROFILE(syscall_linux_setlease);
1981         return result;
1982 }
1983
1984 static int vfswrap_symlink(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
1985 {
1986         int result;
1987
1988         START_PROFILE(syscall_symlink);
1989         result = symlink(oldpath, newpath);
1990         END_PROFILE(syscall_symlink);
1991         return result;
1992 }
1993
1994 static int vfswrap_readlink(vfs_handle_struct *handle, const char *path, char *buf, size_t bufsiz)
1995 {
1996         int result;
1997
1998         START_PROFILE(syscall_readlink);
1999         result = readlink(path, buf, bufsiz);
2000         END_PROFILE(syscall_readlink);
2001         return result;
2002 }
2003
2004 static int vfswrap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
2005 {
2006         int result;
2007
2008         START_PROFILE(syscall_link);
2009         result = link(oldpath, newpath);
2010         END_PROFILE(syscall_link);
2011         return result;
2012 }
2013
2014 static int vfswrap_mknod(vfs_handle_struct *handle, const char *pathname, mode_t mode, SMB_DEV_T dev)
2015 {
2016         int result;
2017
2018         START_PROFILE(syscall_mknod);
2019         result = sys_mknod(pathname, mode, dev);
2020         END_PROFILE(syscall_mknod);
2021         return result;
2022 }
2023
2024 static char *vfswrap_realpath(vfs_handle_struct *handle, const char *path)
2025 {
2026         char *result;
2027
2028         START_PROFILE(syscall_realpath);
2029 #ifdef REALPATH_TAKES_NULL
2030         result = realpath(path, NULL);
2031 #else
2032         result = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
2033         if (result) {
2034                 char *resolved_path = realpath(path, result);
2035                 if (!resolved_path) {
2036                         SAFE_FREE(result);
2037                 } else {
2038                         /* SMB_ASSERT(result == resolved_path) ? */
2039                         result = resolved_path;
2040                 }
2041         }
2042 #endif
2043         END_PROFILE(syscall_realpath);
2044         return result;
2045 }
2046
2047 static NTSTATUS vfswrap_notify_watch(vfs_handle_struct *vfs_handle,
2048                                      struct sys_notify_context *ctx,
2049                                      const char *path,
2050                                      uint32_t *filter,
2051                                      uint32_t *subdir_filter,
2052                                      void (*callback)(struct sys_notify_context *ctx, 
2053                                                       void *private_data,
2054                                                       struct notify_event *ev),
2055                                      void *private_data, void *handle)
2056 {
2057         /*
2058          * So far inotify is the only supported default notify mechanism. If
2059          * another platform like the the BSD's or a proprietary Unix comes
2060          * along and wants another default, we can play the same trick we
2061          * played with Posix ACLs.
2062          *
2063          * Until that is the case, hard-code inotify here.
2064          */
2065 #ifdef HAVE_INOTIFY
2066         if (lp_kernel_change_notify(vfs_handle->conn->params)) {
2067                 return inotify_watch(ctx, path, filter, subdir_filter,
2068                                      callback, private_data, handle);
2069         }
2070 #endif
2071         /*
2072          * Do nothing, leave everything to notify_internal.c
2073          */
2074         return NT_STATUS_OK;
2075 }
2076
2077 static int vfswrap_chflags(vfs_handle_struct *handle, const char *path,
2078                            unsigned int flags)
2079 {
2080 #ifdef HAVE_CHFLAGS
2081         return chflags(path, flags);
2082 #else
2083         errno = ENOSYS;
2084         return -1;
2085 #endif
2086 }
2087
2088 static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle,
2089                                              const SMB_STRUCT_STAT *sbuf)
2090 {
2091         struct file_id key;
2092
2093         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
2094          * blob */
2095         ZERO_STRUCT(key);
2096
2097         key.devid = sbuf->st_ex_dev;
2098         key.inode = sbuf->st_ex_ino;
2099         /* key.extid is unused by default. */
2100
2101         return key;
2102 }
2103
2104 static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
2105                                    struct files_struct *fsp,
2106                                    const char *fname,
2107                                    TALLOC_CTX *mem_ctx,
2108                                    unsigned int *pnum_streams,
2109                                    struct stream_struct **pstreams)
2110 {
2111         SMB_STRUCT_STAT sbuf;
2112         struct stream_struct *tmp_streams = NULL;
2113         int ret;
2114
2115         if ((fsp != NULL) && (fsp->is_directory)) {
2116                 /*
2117                  * No default streams on directories
2118                  */
2119                 goto done;
2120         }
2121
2122         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
2123                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
2124         }
2125         else {
2126                 struct smb_filename smb_fname;
2127
2128                 ZERO_STRUCT(smb_fname);
2129                 smb_fname.base_name = discard_const_p(char, fname);
2130
2131                 if (lp_posix_pathnames()) {
2132                         ret = SMB_VFS_LSTAT(handle->conn, &smb_fname);
2133                 } else {
2134                         ret = SMB_VFS_STAT(handle->conn, &smb_fname);
2135                 }
2136                 sbuf = smb_fname.st;
2137         }
2138
2139         if (ret == -1) {
2140                 return map_nt_error_from_unix(errno);
2141         }
2142
2143         if (S_ISDIR(sbuf.st_ex_mode)) {
2144                 goto done;
2145         }
2146
2147         tmp_streams = talloc_realloc(mem_ctx, *pstreams, struct stream_struct,
2148                                         (*pnum_streams) + 1);
2149         if (tmp_streams == NULL) {
2150                 return NT_STATUS_NO_MEMORY;
2151         }
2152         tmp_streams[*pnum_streams].name = talloc_strdup(tmp_streams, "::$DATA");
2153         if (tmp_streams[*pnum_streams].name == NULL) {
2154                 return NT_STATUS_NO_MEMORY;
2155         }
2156         tmp_streams[*pnum_streams].size = sbuf.st_ex_size;
2157         tmp_streams[*pnum_streams].alloc_size = SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp, &sbuf);
2158
2159         *pnum_streams += 1;
2160         *pstreams = tmp_streams;
2161  done:
2162         return NT_STATUS_OK;
2163 }
2164
2165 static int vfswrap_get_real_filename(struct vfs_handle_struct *handle,
2166                                      const char *path,
2167                                      const char *name,
2168                                      TALLOC_CTX *mem_ctx,
2169                                      char **found_name)
2170 {
2171         /*
2172          * Don't fall back to get_real_filename so callers can differentiate
2173          * between a full directory scan and an actual case-insensitive stat.
2174          */
2175         errno = EOPNOTSUPP;
2176         return -1;
2177 }
2178
2179 static const char *vfswrap_connectpath(struct vfs_handle_struct *handle,
2180                                        const char *fname)
2181 {
2182         return handle->conn->connectpath;
2183 }
2184
2185 static NTSTATUS vfswrap_brl_lock_windows(struct vfs_handle_struct *handle,
2186                                          struct byte_range_lock *br_lck,
2187                                          struct lock_struct *plock,
2188                                          bool blocking_lock,
2189                                          struct blocking_lock_record *blr)
2190 {
2191         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
2192
2193         /* Note: blr is not used in the default implementation. */
2194         return brl_lock_windows_default(br_lck, plock, blocking_lock);
2195 }
2196
2197 static bool vfswrap_brl_unlock_windows(struct vfs_handle_struct *handle,
2198                                        struct messaging_context *msg_ctx,
2199                                        struct byte_range_lock *br_lck,
2200                                        const struct lock_struct *plock)
2201 {
2202         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
2203
2204         return brl_unlock_windows_default(msg_ctx, br_lck, plock);
2205 }
2206
2207 static bool vfswrap_brl_cancel_windows(struct vfs_handle_struct *handle,
2208                                        struct byte_range_lock *br_lck,
2209                                        struct lock_struct *plock,
2210                                        struct blocking_lock_record *blr)
2211 {
2212         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
2213
2214         /* Note: blr is not used in the default implementation. */
2215         return brl_lock_cancel_default(br_lck, plock);
2216 }
2217
2218 static bool vfswrap_strict_lock(struct vfs_handle_struct *handle,
2219                                 files_struct *fsp,
2220                                 struct lock_struct *plock)
2221 {
2222         SMB_ASSERT(plock->lock_type == READ_LOCK ||
2223             plock->lock_type == WRITE_LOCK);
2224
2225         return strict_lock_default(fsp, plock);
2226 }
2227
2228 static void vfswrap_strict_unlock(struct vfs_handle_struct *handle,
2229                                 files_struct *fsp,
2230                                 struct lock_struct *plock)
2231 {
2232         SMB_ASSERT(plock->lock_type == READ_LOCK ||
2233             plock->lock_type == WRITE_LOCK);
2234
2235         strict_unlock_default(fsp, plock);
2236 }
2237
2238 /* NT ACL operations. */
2239
2240 static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
2241                                     files_struct *fsp,
2242                                     uint32 security_info,
2243                                     TALLOC_CTX *mem_ctx,
2244                                     struct security_descriptor **ppdesc)
2245 {
2246         NTSTATUS result;
2247
2248         START_PROFILE(fget_nt_acl);
2249         result = posix_fget_nt_acl(fsp, security_info,
2250                                    mem_ctx, ppdesc);
2251         END_PROFILE(fget_nt_acl);
2252         return result;
2253 }
2254
2255 static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
2256                                    const char *name,
2257                                    uint32 security_info,
2258                                    TALLOC_CTX *mem_ctx,
2259                                    struct security_descriptor **ppdesc)
2260 {
2261         NTSTATUS result;
2262
2263         START_PROFILE(get_nt_acl);
2264         result = posix_get_nt_acl(handle->conn, name, security_info,
2265                                   mem_ctx, ppdesc);
2266         END_PROFILE(get_nt_acl);
2267         return result;
2268 }
2269
2270 static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
2271 {
2272         NTSTATUS result;
2273
2274         START_PROFILE(fset_nt_acl);
2275         result = set_nt_acl(fsp, security_info_sent, psd);
2276         END_PROFILE(fset_nt_acl);
2277         return result;
2278 }
2279
2280 static NTSTATUS vfswrap_audit_file(struct vfs_handle_struct *handle,
2281                                    struct smb_filename *file,
2282                                    struct security_acl *sacl,
2283                                    uint32_t access_requested,
2284                                    uint32_t access_denied)
2285 {
2286         return NT_STATUS_OK; /* Nothing to do here ... */
2287 }
2288
2289 static int vfswrap_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode)
2290 {
2291 #ifdef HAVE_NO_ACL
2292         errno = ENOSYS;
2293         return -1;
2294 #else
2295         int result;
2296
2297         START_PROFILE(chmod_acl);
2298         result = chmod_acl(handle->conn, name, mode);
2299         END_PROFILE(chmod_acl);
2300         return result;
2301 #endif
2302 }
2303
2304 static int vfswrap_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
2305 {
2306 #ifdef HAVE_NO_ACL
2307         errno = ENOSYS;
2308         return -1;
2309 #else
2310         int result;
2311
2312         START_PROFILE(fchmod_acl);
2313         result = fchmod_acl(fsp, mode);
2314         END_PROFILE(fchmod_acl);
2315         return result;
2316 #endif
2317 }
2318
2319 static SMB_ACL_T vfswrap_sys_acl_get_file(vfs_handle_struct *handle,
2320                                           const char *path_p,
2321                                           SMB_ACL_TYPE_T type,
2322                                           TALLOC_CTX *mem_ctx)
2323 {
2324         return sys_acl_get_file(handle, path_p, type, mem_ctx);
2325 }
2326
2327 static SMB_ACL_T vfswrap_sys_acl_get_fd(vfs_handle_struct *handle,
2328                                         files_struct *fsp,
2329                                         TALLOC_CTX *mem_ctx)
2330 {
2331         return sys_acl_get_fd(handle, fsp, mem_ctx);
2332 }
2333
2334 static int vfswrap_sys_acl_set_file(vfs_handle_struct *handle, const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
2335 {
2336         return sys_acl_set_file(handle, name, acltype, theacl);
2337 }
2338
2339 static int vfswrap_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp, SMB_ACL_T theacl)
2340 {
2341         return sys_acl_set_fd(handle, fsp, theacl);
2342 }
2343
2344 static int vfswrap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
2345 {
2346         return sys_acl_delete_def_file(handle, path);
2347 }
2348
2349 /****************************************************************
2350  Extended attribute operations.
2351 *****************************************************************/
2352
2353 static ssize_t vfswrap_getxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
2354 {
2355         return getxattr(path, name, value, size);
2356 }
2357
2358 static ssize_t vfswrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
2359 {
2360         return fgetxattr(fsp->fh->fd, name, value, size);
2361 }
2362
2363 static ssize_t vfswrap_listxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
2364 {
2365         return listxattr(path, list, size);
2366 }
2367
2368 static ssize_t vfswrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
2369 {
2370         return flistxattr(fsp->fh->fd, list, size);
2371 }
2372
2373 static int vfswrap_removexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
2374 {
2375         return removexattr(path, name);
2376 }
2377
2378 static int vfswrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
2379 {
2380         return fremovexattr(fsp->fh->fd, name);
2381 }
2382
2383 static int vfswrap_setxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
2384 {
2385         return setxattr(path, name, value, size, flags);
2386 }
2387
2388 static int vfswrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
2389 {
2390         return fsetxattr(fsp->fh->fd, name, value, size, flags);
2391 }
2392
2393 static bool vfswrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
2394 {
2395         return false;
2396 }
2397
2398 static bool vfswrap_is_offline(struct vfs_handle_struct *handle,
2399                                const struct smb_filename *fname,
2400                                SMB_STRUCT_STAT *sbuf)
2401 {
2402         NTSTATUS status;
2403         char *path;
2404         bool offline = false;
2405
2406         if (ISDOT(fname->base_name) || ISDOTDOT(fname->base_name)) {
2407                 return false;
2408         }
2409
2410         if (!lp_dmapi_support(SNUM(handle->conn)) || !dmapi_have_session()) {
2411 #if defined(ENOTSUP)
2412                 errno = ENOTSUP;
2413 #endif
2414                 return false;
2415         }
2416
2417         status = get_full_smb_filename(talloc_tos(), fname, &path);
2418         if (!NT_STATUS_IS_OK(status)) {
2419                 errno = map_errno_from_nt_status(status);
2420                 return false;
2421         }
2422
2423         offline = (dmapi_file_flags(path) & FILE_ATTRIBUTE_OFFLINE) != 0;
2424
2425         TALLOC_FREE(path);
2426
2427         return offline;
2428 }
2429
2430 static int vfswrap_set_offline(struct vfs_handle_struct *handle,
2431                                const struct smb_filename *fname)
2432 {
2433         /* We don't know how to set offline bit by default, needs to be overriden in the vfs modules */
2434 #if defined(ENOTSUP)
2435         errno = ENOTSUP;
2436 #endif
2437         return -1;
2438 }
2439
2440 static NTSTATUS vfswrap_durable_cookie(struct vfs_handle_struct *handle,
2441                                        struct files_struct *fsp,
2442                                        TALLOC_CTX *mem_ctx,
2443                                        DATA_BLOB *cookie)
2444 {
2445         return vfs_default_durable_cookie(fsp, mem_ctx, cookie);
2446 }
2447
2448 static NTSTATUS vfswrap_durable_disconnect(struct vfs_handle_struct *handle,
2449                                            struct files_struct *fsp,
2450                                            const DATA_BLOB old_cookie,
2451                                            TALLOC_CTX *mem_ctx,
2452                                            DATA_BLOB *new_cookie)
2453 {
2454         return vfs_default_durable_disconnect(fsp, old_cookie, mem_ctx,
2455                                               new_cookie);
2456 }
2457
2458 static NTSTATUS vfswrap_durable_reconnect(struct vfs_handle_struct *handle,
2459                                           struct smb_request *smb1req,
2460                                           struct smbXsrv_open *op,
2461                                           const DATA_BLOB old_cookie,
2462                                           TALLOC_CTX *mem_ctx,
2463                                           struct files_struct **fsp,
2464                                           DATA_BLOB *new_cookie)
2465 {
2466         return vfs_default_durable_reconnect(handle->conn, smb1req, op,
2467                                              old_cookie, mem_ctx,
2468                                              fsp, new_cookie);
2469 }
2470
2471 static struct vfs_fn_pointers vfs_default_fns = {
2472         /* Disk operations */
2473
2474         .connect_fn = vfswrap_connect,
2475         .disconnect_fn = vfswrap_disconnect,
2476         .disk_free_fn = vfswrap_disk_free,
2477         .get_quota_fn = vfswrap_get_quota,
2478         .set_quota_fn = vfswrap_set_quota,
2479         .get_shadow_copy_data_fn = vfswrap_get_shadow_copy_data,
2480         .statvfs_fn = vfswrap_statvfs,
2481         .fs_capabilities_fn = vfswrap_fs_capabilities,
2482         .get_dfs_referrals_fn = vfswrap_get_dfs_referrals,
2483
2484         /* Directory operations */
2485
2486         .opendir_fn = vfswrap_opendir,
2487         .fdopendir_fn = vfswrap_fdopendir,
2488         .readdir_fn = vfswrap_readdir,
2489         .seekdir_fn = vfswrap_seekdir,
2490         .telldir_fn = vfswrap_telldir,
2491         .rewind_dir_fn = vfswrap_rewinddir,
2492         .mkdir_fn = vfswrap_mkdir,
2493         .rmdir_fn = vfswrap_rmdir,
2494         .closedir_fn = vfswrap_closedir,
2495         .init_search_op_fn = vfswrap_init_search_op,
2496
2497         /* File operations */
2498
2499         .open_fn = vfswrap_open,
2500         .create_file_fn = vfswrap_create_file,
2501         .close_fn = vfswrap_close,
2502         .read_fn = vfswrap_read,
2503         .pread_fn = vfswrap_pread,
2504         .pread_send_fn = vfswrap_pread_send,
2505         .pread_recv_fn = vfswrap_asys_ssize_t_recv,
2506         .write_fn = vfswrap_write,
2507         .pwrite_fn = vfswrap_pwrite,
2508         .pwrite_send_fn = vfswrap_pwrite_send,
2509         .pwrite_recv_fn = vfswrap_asys_ssize_t_recv,
2510         .lseek_fn = vfswrap_lseek,
2511         .sendfile_fn = vfswrap_sendfile,
2512         .recvfile_fn = vfswrap_recvfile,
2513         .rename_fn = vfswrap_rename,
2514         .fsync_fn = vfswrap_fsync,
2515         .fsync_send_fn = vfswrap_fsync_send,
2516         .fsync_recv_fn = vfswrap_asys_int_recv,
2517         .stat_fn = vfswrap_stat,
2518         .fstat_fn = vfswrap_fstat,
2519         .lstat_fn = vfswrap_lstat,
2520         .get_alloc_size_fn = vfswrap_get_alloc_size,
2521         .unlink_fn = vfswrap_unlink,
2522         .chmod_fn = vfswrap_chmod,
2523         .fchmod_fn = vfswrap_fchmod,
2524         .chown_fn = vfswrap_chown,
2525         .fchown_fn = vfswrap_fchown,
2526         .lchown_fn = vfswrap_lchown,
2527         .chdir_fn = vfswrap_chdir,
2528         .getwd_fn = vfswrap_getwd,
2529         .ntimes_fn = vfswrap_ntimes,
2530         .ftruncate_fn = vfswrap_ftruncate,
2531         .fallocate_fn = vfswrap_fallocate,
2532         .lock_fn = vfswrap_lock,
2533         .kernel_flock_fn = vfswrap_kernel_flock,
2534         .linux_setlease_fn = vfswrap_linux_setlease,
2535         .getlock_fn = vfswrap_getlock,
2536         .symlink_fn = vfswrap_symlink,
2537         .readlink_fn = vfswrap_readlink,
2538         .link_fn = vfswrap_link,
2539         .mknod_fn = vfswrap_mknod,
2540         .realpath_fn = vfswrap_realpath,
2541         .notify_watch_fn = vfswrap_notify_watch,
2542         .chflags_fn = vfswrap_chflags,
2543         .file_id_create_fn = vfswrap_file_id_create,
2544         .streaminfo_fn = vfswrap_streaminfo,
2545         .get_real_filename_fn = vfswrap_get_real_filename,
2546         .connectpath_fn = vfswrap_connectpath,
2547         .brl_lock_windows_fn = vfswrap_brl_lock_windows,
2548         .brl_unlock_windows_fn = vfswrap_brl_unlock_windows,
2549         .brl_cancel_windows_fn = vfswrap_brl_cancel_windows,
2550         .strict_lock_fn = vfswrap_strict_lock,
2551         .strict_unlock_fn = vfswrap_strict_unlock,
2552         .translate_name_fn = vfswrap_translate_name,
2553         .fsctl_fn = vfswrap_fsctl,
2554         .copy_chunk_send_fn = vfswrap_copy_chunk_send,
2555         .copy_chunk_recv_fn = vfswrap_copy_chunk_recv,
2556         .get_compression_fn = vfswrap_get_compression,
2557         .set_compression_fn = vfswrap_set_compression,
2558
2559         /* NT ACL operations. */
2560
2561         .fget_nt_acl_fn = vfswrap_fget_nt_acl,
2562         .get_nt_acl_fn = vfswrap_get_nt_acl,
2563         .fset_nt_acl_fn = vfswrap_fset_nt_acl,
2564         .audit_file_fn = vfswrap_audit_file,
2565
2566         /* POSIX ACL operations. */
2567
2568         .chmod_acl_fn = vfswrap_chmod_acl,
2569         .fchmod_acl_fn = vfswrap_fchmod_acl,
2570
2571         .sys_acl_get_file_fn = vfswrap_sys_acl_get_file,
2572         .sys_acl_get_fd_fn = vfswrap_sys_acl_get_fd,
2573         .sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
2574         .sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
2575         .sys_acl_set_file_fn = vfswrap_sys_acl_set_file,
2576         .sys_acl_set_fd_fn = vfswrap_sys_acl_set_fd,
2577         .sys_acl_delete_def_file_fn = vfswrap_sys_acl_delete_def_file,
2578
2579         /* EA operations. */
2580         .getxattr_fn = vfswrap_getxattr,
2581         .fgetxattr_fn = vfswrap_fgetxattr,
2582         .listxattr_fn = vfswrap_listxattr,
2583         .flistxattr_fn = vfswrap_flistxattr,
2584         .removexattr_fn = vfswrap_removexattr,
2585         .fremovexattr_fn = vfswrap_fremovexattr,
2586         .setxattr_fn = vfswrap_setxattr,
2587         .fsetxattr_fn = vfswrap_fsetxattr,
2588
2589         /* aio operations */
2590         .aio_force_fn = vfswrap_aio_force,
2591
2592         /* offline operations */
2593         .is_offline_fn = vfswrap_is_offline,
2594         .set_offline_fn = vfswrap_set_offline,
2595
2596         /* durable handle operations */
2597         .durable_cookie_fn = vfswrap_durable_cookie,
2598         .durable_disconnect_fn = vfswrap_durable_disconnect,
2599         .durable_reconnect_fn = vfswrap_durable_reconnect,
2600 };
2601
2602 NTSTATUS vfs_default_init(void);
2603 NTSTATUS vfs_default_init(void)
2604 {
2605         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
2606                                 DEFAULT_VFS_MODULE_NAME, &vfs_default_fns);
2607 }
2608
2609