Keep track of the sparse status of an open file handle. Allows bypass of
[obnox/samba/samba-obnox.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
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
25
26 /* Check for NULL pointer parameters in vfswrap_* functions */
27
28 /* We don't want to have NULL function pointers lying around.  Someone
29    is sure to try and execute them.  These stubs are used to prevent
30    this possibility. */
31
32 static int vfswrap_connect(vfs_handle_struct *handle,  const char *service, const char *user)
33 {
34     return 0;    /* Return >= 0 for success */
35 }
36
37 static void vfswrap_disconnect(vfs_handle_struct *handle)
38 {
39 }
40
41 /* Disk operations */
42
43 static uint64_t vfswrap_disk_free(vfs_handle_struct *handle,  const char *path, bool small_query, uint64_t *bsize,
44                                uint64_t *dfree, uint64_t *dsize)
45 {
46         uint64_t result;
47
48         result = sys_disk_free(handle->conn, path, small_query, bsize, dfree, dsize);
49         return result;
50 }
51
52 static int vfswrap_get_quota(struct vfs_handle_struct *handle,  enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
53 {
54 #ifdef HAVE_SYS_QUOTAS
55         int result;
56
57         START_PROFILE(syscall_get_quota);
58         result = sys_get_quota(handle->conn->connectpath, qtype, id, qt);
59         END_PROFILE(syscall_get_quota);
60         return result;
61 #else
62         errno = ENOSYS;
63         return -1;
64 #endif
65 }
66
67 static int vfswrap_set_quota(struct vfs_handle_struct *handle,  enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
68 {
69 #ifdef HAVE_SYS_QUOTAS
70         int result;
71
72         START_PROFILE(syscall_set_quota);
73         result = sys_set_quota(handle->conn->connectpath, qtype, id, qt);
74         END_PROFILE(syscall_set_quota);
75         return result;
76 #else
77         errno = ENOSYS;
78         return -1;
79 #endif
80 }
81
82 static int vfswrap_get_shadow_copy_data(struct vfs_handle_struct *handle, struct files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, bool labels)
83 {
84         errno = ENOSYS;
85         return -1;  /* Not implemented. */
86 }
87
88 static int vfswrap_statvfs(struct vfs_handle_struct *handle,  const char *path, vfs_statvfs_struct *statbuf)
89 {
90         return sys_statvfs(path, statbuf);
91 }
92
93 static uint32_t vfswrap_fs_capabilities(struct vfs_handle_struct *handle,
94                 enum timestamp_set_resolution *p_ts_res)
95 {
96         connection_struct *conn = handle->conn;
97         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
98         struct smb_filename *smb_fname_cpath = NULL;
99         NTSTATUS status;
100         int ret = -1;
101
102 #if defined(DARWINOS)
103         struct vfs_statvfs_struct statbuf;
104         ZERO_STRUCT(statbuf);
105         sys_statvfs(conn->connectpath, &statbuf);
106         caps = statbuf.FsCapabilities;
107 #endif
108
109         *p_ts_res = TIMESTAMP_SET_SECONDS;
110
111         /* Work out what timestamp resolution we can
112          * use when setting a timestamp. */
113
114         status = create_synthetic_smb_fname(talloc_tos(),
115                                 conn->connectpath,
116                                 NULL,
117                                 NULL,
118                                 &smb_fname_cpath);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return caps;
121         }
122
123         ret = SMB_VFS_STAT(conn, smb_fname_cpath);
124         if (ret == -1) {
125                 TALLOC_FREE(smb_fname_cpath);
126                 return caps;
127         }
128
129         if (smb_fname_cpath->st.st_ex_mtime.tv_nsec ||
130                         smb_fname_cpath->st.st_ex_atime.tv_nsec ||
131                         smb_fname_cpath->st.st_ex_ctime.tv_nsec) {
132                 /* If any of the normal UNIX directory timestamps
133                  * have a non-zero tv_nsec component assume
134                  * we might be able to set sub-second timestamps.
135                  * See what filetime set primitives we have.
136                  */
137 #if defined(HAVE_UTIMENSAT)
138                 *p_ts_res = TIMESTAMP_SET_NT_OR_BETTER;
139 #elif defined(HAVE_UTIMES)
140                 /* utimes allows msec timestamps to be set. */
141                 *p_ts_res = TIMESTAMP_SET_MSEC;
142 #elif defined(HAVE_UTIME)
143                 /* utime only allows sec timestamps to be set. */
144                 *p_ts_res = TIMESTAMP_SET_SECONDS;
145 #endif
146
147                 DEBUG(10,("vfswrap_fs_capabilities: timestamp "
148                         "resolution of %s "
149                         "available on share %s, directory %s\n",
150                         *p_ts_res == TIMESTAMP_SET_MSEC ? "msec" : "sec",
151                         lp_servicename(conn->params->service),
152                         conn->connectpath ));
153         }
154         TALLOC_FREE(smb_fname_cpath);
155         return caps;
156 }
157
158 /* Directory operations */
159
160 static SMB_STRUCT_DIR *vfswrap_opendir(vfs_handle_struct *handle,  const char *fname, const char *mask, uint32 attr)
161 {
162         SMB_STRUCT_DIR *result;
163
164         START_PROFILE(syscall_opendir);
165         result = sys_opendir(fname);
166         END_PROFILE(syscall_opendir);
167         return result;
168 }
169
170 static SMB_STRUCT_DIRENT *vfswrap_readdir(vfs_handle_struct *handle,
171                                           SMB_STRUCT_DIR *dirp,
172                                           SMB_STRUCT_STAT *sbuf)
173 {
174         SMB_STRUCT_DIRENT *result;
175
176         START_PROFILE(syscall_readdir);
177         result = sys_readdir(dirp);
178         /* Default Posix readdir() does not give us stat info.
179          * Set to invalid to indicate we didn't return this info. */
180         if (sbuf)
181                 SET_STAT_INVALID(*sbuf);
182         END_PROFILE(syscall_readdir);
183         return result;
184 }
185
186 static void vfswrap_seekdir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp, long offset)
187 {
188         START_PROFILE(syscall_seekdir);
189         sys_seekdir(dirp, offset);
190         END_PROFILE(syscall_seekdir);
191 }
192
193 static long vfswrap_telldir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
194 {
195         long result;
196         START_PROFILE(syscall_telldir);
197         result = sys_telldir(dirp);
198         END_PROFILE(syscall_telldir);
199         return result;
200 }
201
202 static void vfswrap_rewinddir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
203 {
204         START_PROFILE(syscall_rewinddir);
205         sys_rewinddir(dirp);
206         END_PROFILE(syscall_rewinddir);
207 }
208
209 static int vfswrap_mkdir(vfs_handle_struct *handle,  const char *path, mode_t mode)
210 {
211         int result;
212         bool has_dacl = False;
213         char *parent = NULL;
214
215         START_PROFILE(syscall_mkdir);
216
217         if (lp_inherit_acls(SNUM(handle->conn))
218             && parent_dirname(talloc_tos(), path, &parent, NULL)
219             && (has_dacl = directory_has_default_acl(handle->conn, parent)))
220                 mode = (0777 & lp_dir_mask(SNUM(handle->conn)));
221
222         TALLOC_FREE(parent);
223
224         result = mkdir(path, mode);
225
226         if (result == 0 && !has_dacl) {
227                 /*
228                  * We need to do this as the default behavior of POSIX ACLs
229                  * is to set the mask to be the requested group permission
230                  * bits, not the group permission bits to be the requested
231                  * group permission bits. This is not what we want, as it will
232                  * mess up any inherited ACL bits that were set. JRA.
233                  */
234                 int saved_errno = errno; /* We may get ENOSYS */
235                 if ((SMB_VFS_CHMOD_ACL(handle->conn, path, mode) == -1) && (errno == ENOSYS))
236                         errno = saved_errno;
237         }
238
239         END_PROFILE(syscall_mkdir);
240         return result;
241 }
242
243 static int vfswrap_rmdir(vfs_handle_struct *handle,  const char *path)
244 {
245         int result;
246
247         START_PROFILE(syscall_rmdir);
248         result = rmdir(path);
249         END_PROFILE(syscall_rmdir);
250         return result;
251 }
252
253 static int vfswrap_closedir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
254 {
255         int result;
256
257         START_PROFILE(syscall_closedir);
258         result = sys_closedir(dirp);
259         END_PROFILE(syscall_closedir);
260         return result;
261 }
262
263 static void vfswrap_init_search_op(vfs_handle_struct *handle,
264                                    SMB_STRUCT_DIR *dirp)
265 {
266         /* Default behavior is a NOOP */
267 }
268
269 /* File operations */
270
271 static int vfswrap_open(vfs_handle_struct *handle,
272                         struct smb_filename *smb_fname,
273                         files_struct *fsp, int flags, mode_t mode)
274 {
275         int result = -1;
276
277         START_PROFILE(syscall_open);
278
279         if (smb_fname->stream_name) {
280                 errno = ENOENT;
281                 goto out;
282         }
283
284         result = sys_open(smb_fname->base_name, flags, mode);
285  out:
286         END_PROFILE(syscall_open);
287         return result;
288 }
289
290 static NTSTATUS vfswrap_create_file(vfs_handle_struct *handle,
291                                     struct smb_request *req,
292                                     uint16_t root_dir_fid,
293                                     struct smb_filename *smb_fname,
294                                     uint32_t access_mask,
295                                     uint32_t share_access,
296                                     uint32_t create_disposition,
297                                     uint32_t create_options,
298                                     uint32_t file_attributes,
299                                     uint32_t oplock_request,
300                                     uint64_t allocation_size,
301                                     uint32_t private_flags,
302                                     struct security_descriptor *sd,
303                                     struct ea_list *ea_list,
304                                     files_struct **result,
305                                     int *pinfo)
306 {
307         return create_file_default(handle->conn, req, root_dir_fid, smb_fname,
308                                    access_mask, share_access,
309                                    create_disposition, create_options,
310                                    file_attributes, oplock_request,
311                                    allocation_size, private_flags,
312                                    sd, ea_list, result,
313                                    pinfo);
314 }
315
316 static int vfswrap_close(vfs_handle_struct *handle, files_struct *fsp)
317 {
318         int result;
319
320         START_PROFILE(syscall_close);
321         result = fd_close_posix(fsp);
322         END_PROFILE(syscall_close);
323         return result;
324 }
325
326 static ssize_t vfswrap_read(vfs_handle_struct *handle, files_struct *fsp, void *data, size_t n)
327 {
328         ssize_t result;
329
330         START_PROFILE_BYTES(syscall_read, n);
331         result = sys_read(fsp->fh->fd, data, n);
332         END_PROFILE(syscall_read);
333         return result;
334 }
335
336 static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, void *data,
337                         size_t n, SMB_OFF_T offset)
338 {
339         ssize_t result;
340
341 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
342         START_PROFILE_BYTES(syscall_pread, n);
343         result = sys_pread(fsp->fh->fd, data, n, offset);
344         END_PROFILE(syscall_pread);
345
346         if (result == -1 && errno == ESPIPE) {
347                 /* Maintain the fiction that pipes can be seeked (sought?) on. */
348                 result = SMB_VFS_READ(fsp, data, n);
349                 fsp->fh->pos = 0;
350         }
351
352 #else /* HAVE_PREAD */
353         SMB_OFF_T   curr;
354         int lerrno;
355
356         curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
357         if (curr == -1 && errno == ESPIPE) {
358                 /* Maintain the fiction that pipes can be seeked (sought?) on. */
359                 result = SMB_VFS_READ(fsp, data, n);
360                 fsp->fh->pos = 0;
361                 return result;
362         }
363
364         if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
365                 return -1;
366         }
367
368         errno = 0;
369         result = SMB_VFS_READ(fsp, data, n);
370         lerrno = errno;
371
372         SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
373         errno = lerrno;
374
375 #endif /* HAVE_PREAD */
376
377         return result;
378 }
379
380 static ssize_t vfswrap_write(vfs_handle_struct *handle, files_struct *fsp, const void *data, size_t n)
381 {
382         ssize_t result;
383
384         START_PROFILE_BYTES(syscall_write, n);
385         result = sys_write(fsp->fh->fd, data, n);
386         END_PROFILE(syscall_write);
387         return result;
388 }
389
390 static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, const void *data,
391                         size_t n, SMB_OFF_T offset)
392 {
393         ssize_t result;
394
395 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
396         START_PROFILE_BYTES(syscall_pwrite, n);
397         result = sys_pwrite(fsp->fh->fd, data, n, offset);
398         END_PROFILE(syscall_pwrite);
399
400         if (result == -1 && errno == ESPIPE) {
401                 /* Maintain the fiction that pipes can be sought on. */
402                 result = SMB_VFS_WRITE(fsp, data, n);
403         }
404
405 #else /* HAVE_PWRITE */
406         SMB_OFF_T   curr;
407         int         lerrno;
408
409         curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
410         if (curr == -1) {
411                 return -1;
412         }
413
414         if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
415                 return -1;
416         }
417
418         result = SMB_VFS_WRITE(fsp, data, n);
419         lerrno = errno;
420
421         SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
422         errno = lerrno;
423
424 #endif /* HAVE_PWRITE */
425
426         return result;
427 }
428
429 static SMB_OFF_T vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T offset, int whence)
430 {
431         SMB_OFF_T result = 0;
432
433         START_PROFILE(syscall_lseek);
434
435         /* Cope with 'stat' file opens. */
436         if (fsp->fh->fd != -1)
437                 result = sys_lseek(fsp->fh->fd, offset, whence);
438
439         /*
440          * We want to maintain the fiction that we can seek
441          * on a fifo for file system purposes. This allows
442          * people to set up UNIX fifo's that feed data to Windows
443          * applications. JRA.
444          */
445
446         if((result == -1) && (errno == ESPIPE)) {
447                 result = 0;
448                 errno = 0;
449         }
450
451         END_PROFILE(syscall_lseek);
452         return result;
453 }
454
455 static ssize_t vfswrap_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
456                         SMB_OFF_T offset, size_t n)
457 {
458         ssize_t result;
459
460         START_PROFILE_BYTES(syscall_sendfile, n);
461         result = sys_sendfile(tofd, fromfsp->fh->fd, hdr, offset, n);
462         END_PROFILE(syscall_sendfile);
463         return result;
464 }
465
466 static ssize_t vfswrap_recvfile(vfs_handle_struct *handle,
467                         int fromfd,
468                         files_struct *tofsp,
469                         SMB_OFF_T offset,
470                         size_t n)
471 {
472         ssize_t result;
473
474         START_PROFILE_BYTES(syscall_recvfile, n);
475         result = sys_recvfile(fromfd, tofsp->fh->fd, offset, n);
476         END_PROFILE(syscall_recvfile);
477         return result;
478 }
479
480 static int vfswrap_rename(vfs_handle_struct *handle,
481                           const struct smb_filename *smb_fname_src,
482                           const struct smb_filename *smb_fname_dst)
483 {
484         int result = -1;
485
486         START_PROFILE(syscall_rename);
487
488         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
489                 errno = ENOENT;
490                 goto out;
491         }
492
493         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
494
495  out:
496         END_PROFILE(syscall_rename);
497         return result;
498 }
499
500 static int vfswrap_fsync(vfs_handle_struct *handle, files_struct *fsp)
501 {
502 #ifdef HAVE_FSYNC
503         int result;
504
505         START_PROFILE(syscall_fsync);
506         result = fsync(fsp->fh->fd);
507         END_PROFILE(syscall_fsync);
508         return result;
509 #else
510         return 0;
511 #endif
512 }
513
514 static int vfswrap_stat(vfs_handle_struct *handle,
515                         struct smb_filename *smb_fname)
516 {
517         int result = -1;
518
519         START_PROFILE(syscall_stat);
520
521         if (smb_fname->stream_name) {
522                 errno = ENOENT;
523                 goto out;
524         }
525
526         result = sys_stat(smb_fname->base_name, &smb_fname->st,
527                           lp_fake_dir_create_times(SNUM(handle->conn)));
528  out:
529         END_PROFILE(syscall_stat);
530         return result;
531 }
532
533 static int vfswrap_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
534 {
535         int result;
536
537         START_PROFILE(syscall_fstat);
538         result = sys_fstat(fsp->fh->fd,
539                            sbuf, lp_fake_dir_create_times(SNUM(handle->conn)));
540         END_PROFILE(syscall_fstat);
541         return result;
542 }
543
544 static int vfswrap_lstat(vfs_handle_struct *handle,
545                          struct smb_filename *smb_fname)
546 {
547         int result = -1;
548
549         START_PROFILE(syscall_lstat);
550
551         if (smb_fname->stream_name) {
552                 errno = ENOENT;
553                 goto out;
554         }
555
556         result = sys_lstat(smb_fname->base_name, &smb_fname->st,
557                            lp_fake_dir_create_times(SNUM(handle->conn)));
558  out:
559         END_PROFILE(syscall_lstat);
560         return result;
561 }
562
563 static NTSTATUS vfswrap_translate_name(struct vfs_handle_struct *handle,
564                                        const char *name,
565                                        enum vfs_translate_direction direction,
566                                        TALLOC_CTX *mem_ctx,
567                                        char **mapped_name)
568 {
569         return NT_STATUS_NONE_MAPPED;
570 }
571
572 /********************************************************************
573  Given a stat buffer return the allocated size on disk, taking into
574  account sparse files.
575 ********************************************************************/
576 static uint64_t vfswrap_get_alloc_size(vfs_handle_struct *handle,
577                                        struct files_struct *fsp,
578                                        const SMB_STRUCT_STAT *sbuf)
579 {
580         uint64_t result;
581
582         START_PROFILE(syscall_get_alloc_size);
583
584         if(S_ISDIR(sbuf->st_ex_mode)) {
585                 result = 0;
586                 goto out;
587         }
588
589 #if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
590         result = (uint64_t)STAT_ST_BLOCKSIZE * (uint64_t)sbuf->st_ex_blocks;
591 #else
592         result = get_file_size_stat(sbuf);
593 #endif
594
595         if (fsp && fsp->initial_allocation_size)
596                 result = MAX(result,fsp->initial_allocation_size);
597
598         result = smb_roundup(handle->conn, result);
599
600  out:
601         END_PROFILE(syscall_get_alloc_size);
602         return result;
603 }
604
605 static int vfswrap_unlink(vfs_handle_struct *handle,
606                           const struct smb_filename *smb_fname)
607 {
608         int result = -1;
609
610         START_PROFILE(syscall_unlink);
611
612         if (smb_fname->stream_name) {
613                 errno = ENOENT;
614                 goto out;
615         }
616         result = unlink(smb_fname->base_name);
617
618  out:
619         END_PROFILE(syscall_unlink);
620         return result;
621 }
622
623 static int vfswrap_chmod(vfs_handle_struct *handle,  const char *path, mode_t mode)
624 {
625         int result;
626
627         START_PROFILE(syscall_chmod);
628
629         /*
630          * We need to do this due to the fact that the default POSIX ACL
631          * chmod modifies the ACL *mask* for the group owner, not the
632          * group owner bits directly. JRA.
633          */
634
635
636         {
637                 int saved_errno = errno; /* We might get ENOSYS */
638                 if ((result = SMB_VFS_CHMOD_ACL(handle->conn, path, mode)) == 0) {
639                         END_PROFILE(syscall_chmod);
640                         return result;
641                 }
642                 /* Error - return the old errno. */
643                 errno = saved_errno;
644         }
645
646         result = chmod(path, mode);
647         END_PROFILE(syscall_chmod);
648         return result;
649 }
650
651 static int vfswrap_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
652 {
653         int result;
654
655         START_PROFILE(syscall_fchmod);
656
657         /*
658          * We need to do this due to the fact that the default POSIX ACL
659          * chmod modifies the ACL *mask* for the group owner, not the
660          * group owner bits directly. JRA.
661          */
662
663         {
664                 int saved_errno = errno; /* We might get ENOSYS */
665                 if ((result = SMB_VFS_FCHMOD_ACL(fsp, mode)) == 0) {
666                         END_PROFILE(syscall_fchmod);
667                         return result;
668                 }
669                 /* Error - return the old errno. */
670                 errno = saved_errno;
671         }
672
673 #if defined(HAVE_FCHMOD)
674         result = fchmod(fsp->fh->fd, mode);
675 #else
676         result = -1;
677         errno = ENOSYS;
678 #endif
679
680         END_PROFILE(syscall_fchmod);
681         return result;
682 }
683
684 static int vfswrap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
685 {
686         int result;
687
688         START_PROFILE(syscall_chown);
689         result = chown(path, uid, gid);
690         END_PROFILE(syscall_chown);
691         return result;
692 }
693
694 static int vfswrap_fchown(vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
695 {
696 #ifdef HAVE_FCHOWN
697         int result;
698
699         START_PROFILE(syscall_fchown);
700         result = fchown(fsp->fh->fd, uid, gid);
701         END_PROFILE(syscall_fchown);
702         return result;
703 #else
704         errno = ENOSYS;
705         return -1;
706 #endif
707 }
708
709 static int vfswrap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
710 {
711         int result;
712
713         START_PROFILE(syscall_lchown);
714         result = lchown(path, uid, gid);
715         END_PROFILE(syscall_lchown);
716         return result;
717 }
718
719 static int vfswrap_chdir(vfs_handle_struct *handle,  const char *path)
720 {
721         int result;
722
723         START_PROFILE(syscall_chdir);
724         result = chdir(path);
725         END_PROFILE(syscall_chdir);
726         return result;
727 }
728
729 static char *vfswrap_getwd(vfs_handle_struct *handle,  char *path)
730 {
731         char *result;
732
733         START_PROFILE(syscall_getwd);
734         result = sys_getwd(path);
735         END_PROFILE(syscall_getwd);
736         return result;
737 }
738
739 /*********************************************************************
740  nsec timestamp resolution call. Convert down to whatever the underlying
741  system will support.
742 **********************************************************************/
743
744 static int vfswrap_ntimes(vfs_handle_struct *handle,
745                           const struct smb_filename *smb_fname,
746                           struct smb_file_time *ft)
747 {
748         int result = -1;
749
750         START_PROFILE(syscall_ntimes);
751
752         if (smb_fname->stream_name) {
753                 errno = ENOENT;
754                 goto out;
755         }
756
757         if (ft != NULL) {
758                 if (null_timespec(ft->atime)) {
759                         ft->atime= smb_fname->st.st_ex_atime;
760                 }
761
762                 if (null_timespec(ft->mtime)) {
763                         ft->mtime = smb_fname->st.st_ex_mtime;
764                 }
765
766                 if (!null_timespec(ft->create_time)) {
767                         set_create_timespec_ea(handle->conn,
768                                                smb_fname,
769                                                ft->create_time);
770                 }
771
772                 if ((timespec_compare(&ft->atime,
773                                       &smb_fname->st.st_ex_atime) == 0) &&
774                     (timespec_compare(&ft->mtime,
775                                       &smb_fname->st.st_ex_mtime) == 0)) {
776                         return 0;
777                 }
778         }
779
780 #if defined(HAVE_UTIMENSAT)
781         if (ft != NULL) {
782                 struct timespec ts[2];
783                 ts[0] = ft->atime;
784                 ts[1] = ft->mtime;
785                 result = utimensat(AT_FDCWD, smb_fname->base_name, ts, 0);
786         } else {
787                 result = utimensat(AT_FDCWD, smb_fname->base_name, NULL, 0);
788         }
789 #elif defined(HAVE_UTIMES)
790         if (ft != NULL) {
791                 struct timeval tv[2];
792                 tv[0] = convert_timespec_to_timeval(ft->atime);
793                 tv[1] = convert_timespec_to_timeval(ft->mtime);
794                 result = utimes(smb_fname->base_name, tv);
795         } else {
796                 result = utimes(smb_fname->base_name, NULL);
797         }
798 #elif defined(HAVE_UTIME)
799         if (ft != NULL) {
800                 struct utimbuf times;
801                 times.actime = convert_timespec_to_time_t(ft->atime);
802                 times.modtime = convert_timespec_to_time_t(ft->mtime);
803                 result = utime(smb_fname->base_name, &times);
804         } else {
805                 result = utime(smb_fname->base_name, NULL);
806         }
807 #else
808         errno = ENOSYS;
809         result = -1;
810 #endif
811
812  out:
813         END_PROFILE(syscall_ntimes);
814         return result;
815 }
816
817 /*********************************************************************
818  A version of ftruncate that will write the space on disk if strict
819  allocate is set.
820 **********************************************************************/
821
822 static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T len)
823 {
824         SMB_OFF_T space_to_write;
825         uint64_t space_avail;
826         uint64_t bsize,dfree,dsize;
827         int ret;
828         NTSTATUS status;
829         SMB_STRUCT_STAT *pst;
830
831         status = vfs_stat_fsp(fsp);
832         if (!NT_STATUS_IS_OK(status)) {
833                 return -1;
834         }
835         pst = &fsp->fsp_name->st;
836
837 #ifdef S_ISFIFO
838         if (S_ISFIFO(pst->st_ex_mode))
839                 return 0;
840 #endif
841
842         if (pst->st_ex_size == len)
843                 return 0;
844
845         /* Shrink - just ftruncate. */
846         if (pst->st_ex_size > len)
847                 return sys_ftruncate(fsp->fh->fd, len);
848
849         space_to_write = len - pst->st_ex_size;
850
851         /* for allocation try fallocate first. This can fail on some
852            platforms e.g. when the filesystem doesn't support it and no
853            emulation is being done by the libc (like on AIX with JFS1). In that
854            case we do our own emulation. fallocate implementations can
855            return ENOTSUP or EINVAL in cases like that. */
856         ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_EXTEND_SIZE,
857                                 pst->st_ex_size, space_to_write);
858         if (ret == ENOSPC) {
859                 errno = ENOSPC;
860                 return -1;
861         }
862         if (ret == 0) {
863                 return 0;
864         }
865         DEBUG(10,("strict_allocate_ftruncate: SMB_VFS_FALLOCATE failed with "
866                 "error %d. Falling back to slow manual allocation\n", ret));
867
868         /* available disk space is enough or not? */
869         space_avail = get_dfree_info(fsp->conn,
870                                      fsp->fsp_name->base_name, false,
871                                      &bsize,&dfree,&dsize);
872         /* space_avail is 1k blocks */
873         if (space_avail == (uint64_t)-1 ||
874                         ((uint64_t)space_to_write/1024 > space_avail) ) {
875                 errno = ENOSPC;
876                 return -1;
877         }
878
879         /* Write out the real space on disk. */
880         ret = vfs_slow_fallocate(fsp, pst->st_ex_size, space_to_write);
881         if (ret != 0) {
882                 errno = ret;
883                 ret = -1;
884         }
885
886         return 0;
887 }
888
889 static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T len)
890 {
891         int result = -1;
892         SMB_STRUCT_STAT *pst;
893         NTSTATUS status;
894         char c = 0;
895
896         START_PROFILE(syscall_ftruncate);
897
898         if (lp_strict_allocate(SNUM(fsp->conn)) && !fsp->is_sparse) {
899                 result = strict_allocate_ftruncate(handle, fsp, len);
900                 END_PROFILE(syscall_ftruncate);
901                 return result;
902         }
903
904         /* we used to just check HAVE_FTRUNCATE_EXTEND and only use
905            sys_ftruncate if the system supports it. Then I discovered that
906            you can have some filesystems that support ftruncate
907            expansion and some that don't! On Linux fat can't do
908            ftruncate extend but ext2 can. */
909
910         result = sys_ftruncate(fsp->fh->fd, len);
911         if (result == 0)
912                 goto done;
913
914         /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
915            extend a file with ftruncate. Provide alternate implementation
916            for this */
917
918         /* Do an fstat to see if the file is longer than the requested
919            size in which case the ftruncate above should have
920            succeeded or shorter, in which case seek to len - 1 and
921            write 1 byte of zero */
922         status = vfs_stat_fsp(fsp);
923         if (!NT_STATUS_IS_OK(status)) {
924                 goto done;
925         }
926         pst = &fsp->fsp_name->st;
927
928 #ifdef S_ISFIFO
929         if (S_ISFIFO(pst->st_ex_mode)) {
930                 result = 0;
931                 goto done;
932         }
933 #endif
934
935         if (pst->st_ex_size == len) {
936                 result = 0;
937                 goto done;
938         }
939
940         if (pst->st_ex_size > len) {
941                 /* the sys_ftruncate should have worked */
942                 goto done;
943         }
944
945         if (SMB_VFS_PWRITE(fsp, &c, 1, len-1)!=1) {
946                 goto done;
947         }
948
949         result = 0;
950
951   done:
952
953         END_PROFILE(syscall_ftruncate);
954         return result;
955 }
956
957 static int vfswrap_fallocate(vfs_handle_struct *handle,
958                         files_struct *fsp,
959                         enum vfs_fallocate_mode mode,
960                         SMB_OFF_T offset,
961                         SMB_OFF_T len)
962 {
963         int result;
964
965         START_PROFILE(syscall_fallocate);
966         if (mode == VFS_FALLOCATE_EXTEND_SIZE) {
967                 result = sys_posix_fallocate(fsp->fh->fd, offset, len);
968         } else if (mode == VFS_FALLOCATE_KEEP_SIZE) {
969                 result = sys_fallocate(fsp->fh->fd, mode, offset, len);
970         } else {
971                 errno = EINVAL;
972                 result = -1;
973         }
974         END_PROFILE(syscall_fallocate);
975         return result;
976 }
977
978 static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
979 {
980         bool result;
981
982         START_PROFILE(syscall_fcntl_lock);
983         result =  fcntl_lock(fsp->fh->fd, op, offset, count, type);
984         END_PROFILE(syscall_fcntl_lock);
985         return result;
986 }
987
988 static int vfswrap_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
989                                 uint32 share_mode, uint32 access_mask)
990 {
991         START_PROFILE(syscall_kernel_flock);
992         kernel_flock(fsp->fh->fd, share_mode, access_mask);
993         END_PROFILE(syscall_kernel_flock);
994         return 0;
995 }
996
997 static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
998 {
999         bool result;
1000
1001         START_PROFILE(syscall_fcntl_getlock);
1002         result =  fcntl_getlock(fsp->fh->fd, poffset, pcount, ptype, ppid);
1003         END_PROFILE(syscall_fcntl_getlock);
1004         return result;
1005 }
1006
1007 static int vfswrap_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1008                                 int leasetype)
1009 {
1010         int result = -1;
1011
1012         START_PROFILE(syscall_linux_setlease);
1013
1014 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1015         /* first set the signal handler */
1016         if(linux_set_lease_sighandler(fsp->fh->fd) == -1) {
1017                 return -1;
1018         }
1019
1020         result = linux_setlease(fsp->fh->fd, leasetype);
1021 #else
1022         errno = ENOSYS;
1023 #endif
1024         END_PROFILE(syscall_linux_setlease);
1025         return result;
1026 }
1027
1028 static int vfswrap_symlink(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
1029 {
1030         int result;
1031
1032         START_PROFILE(syscall_symlink);
1033         result = symlink(oldpath, newpath);
1034         END_PROFILE(syscall_symlink);
1035         return result;
1036 }
1037
1038 static int vfswrap_readlink(vfs_handle_struct *handle,  const char *path, char *buf, size_t bufsiz)
1039 {
1040         int result;
1041
1042         START_PROFILE(syscall_readlink);
1043         result = readlink(path, buf, bufsiz);
1044         END_PROFILE(syscall_readlink);
1045         return result;
1046 }
1047
1048 static int vfswrap_link(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
1049 {
1050         int result;
1051
1052         START_PROFILE(syscall_link);
1053         result = link(oldpath, newpath);
1054         END_PROFILE(syscall_link);
1055         return result;
1056 }
1057
1058 static int vfswrap_mknod(vfs_handle_struct *handle,  const char *pathname, mode_t mode, SMB_DEV_T dev)
1059 {
1060         int result;
1061
1062         START_PROFILE(syscall_mknod);
1063         result = sys_mknod(pathname, mode, dev);
1064         END_PROFILE(syscall_mknod);
1065         return result;
1066 }
1067
1068 static char *vfswrap_realpath(vfs_handle_struct *handle,  const char *path)
1069 {
1070         char *result;
1071
1072         START_PROFILE(syscall_realpath);
1073 #ifdef REALPATH_TAKES_NULL
1074         result = realpath(path, NULL);
1075 #else
1076         result = SMB_MALLOC_ARRAY(char, PATH_MAX+1);
1077         if (result) {
1078                 char *resolved_path = realpath(path, result);
1079                 if (!resolved_path) {
1080                         SAFE_FREE(result);
1081                 } else {
1082                         /* SMB_ASSERT(result == resolved_path) ? */
1083                         result = resolved_path;
1084                 }
1085         }
1086 #endif
1087         END_PROFILE(syscall_realpath);
1088         return result;
1089 }
1090
1091 static NTSTATUS vfswrap_notify_watch(vfs_handle_struct *vfs_handle,
1092                                      struct sys_notify_context *ctx,
1093                                      struct notify_entry *e,
1094                                      void (*callback)(struct sys_notify_context *ctx, 
1095                                                       void *private_data,
1096                                                       struct notify_event *ev),
1097                                      void *private_data, void *handle)
1098 {
1099         /*
1100          * So far inotify is the only supported default notify mechanism. If
1101          * another platform like the the BSD's or a proprietary Unix comes
1102          * along and wants another default, we can play the same trick we
1103          * played with Posix ACLs.
1104          *
1105          * Until that is the case, hard-code inotify here.
1106          */
1107 #ifdef HAVE_INOTIFY
1108         if (lp_kernel_change_notify(ctx->conn->params)) {
1109                 return inotify_watch(ctx, e, callback, private_data, handle);
1110         }
1111 #endif
1112         /*
1113          * Do nothing, leave everything to notify_internal.c
1114          */
1115         return NT_STATUS_OK;
1116 }
1117
1118 static int vfswrap_chflags(vfs_handle_struct *handle, const char *path,
1119                            unsigned int flags)
1120 {
1121 #ifdef HAVE_CHFLAGS
1122         return chflags(path, flags);
1123 #else
1124         errno = ENOSYS;
1125         return -1;
1126 #endif
1127 }
1128
1129 static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle,
1130                                              const SMB_STRUCT_STAT *sbuf)
1131 {
1132         struct file_id key;
1133
1134         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
1135          * blob */
1136         ZERO_STRUCT(key);
1137
1138         key.devid = sbuf->st_ex_dev;
1139         key.inode = sbuf->st_ex_ino;
1140         /* key.extid is unused by default. */
1141
1142         return key;
1143 }
1144
1145 static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
1146                                    struct files_struct *fsp,
1147                                    const char *fname,
1148                                    TALLOC_CTX *mem_ctx,
1149                                    unsigned int *pnum_streams,
1150                                    struct stream_struct **pstreams)
1151 {
1152         SMB_STRUCT_STAT sbuf;
1153         unsigned int num_streams = 0;
1154         struct stream_struct *streams = NULL;
1155         int ret;
1156
1157         if ((fsp != NULL) && (fsp->is_directory)) {
1158                 /*
1159                  * No default streams on directories
1160                  */
1161                 goto done;
1162         }
1163
1164         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
1165                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
1166         }
1167         else {
1168                 struct smb_filename smb_fname;
1169
1170                 ZERO_STRUCT(smb_fname);
1171                 smb_fname.base_name = discard_const_p(char, fname);
1172
1173                 if (lp_posix_pathnames()) {
1174                         ret = SMB_VFS_LSTAT(handle->conn, &smb_fname);
1175                 } else {
1176                         ret = SMB_VFS_STAT(handle->conn, &smb_fname);
1177                 }
1178                 sbuf = smb_fname.st;
1179         }
1180
1181         if (ret == -1) {
1182                 return map_nt_error_from_unix(errno);
1183         }
1184
1185         if (S_ISDIR(sbuf.st_ex_mode)) {
1186                 goto done;
1187         }
1188
1189         streams = talloc(mem_ctx, struct stream_struct);
1190
1191         if (streams == NULL) {
1192                 return NT_STATUS_NO_MEMORY;
1193         }
1194
1195         streams->size = sbuf.st_ex_size;
1196         streams->alloc_size = SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp, &sbuf);
1197
1198         streams->name = talloc_strdup(streams, "::$DATA");
1199         if (streams->name == NULL) {
1200                 TALLOC_FREE(streams);
1201                 return NT_STATUS_NO_MEMORY;
1202         }
1203
1204         num_streams = 1;
1205  done:
1206         *pnum_streams = num_streams;
1207         *pstreams = streams;
1208         return NT_STATUS_OK;
1209 }
1210
1211 static int vfswrap_get_real_filename(struct vfs_handle_struct *handle,
1212                                      const char *path,
1213                                      const char *name,
1214                                      TALLOC_CTX *mem_ctx,
1215                                      char **found_name)
1216 {
1217         /*
1218          * Don't fall back to get_real_filename so callers can differentiate
1219          * between a full directory scan and an actual case-insensitive stat.
1220          */
1221         errno = EOPNOTSUPP;
1222         return -1;
1223 }
1224
1225 static const char *vfswrap_connectpath(struct vfs_handle_struct *handle,
1226                                        const char *fname)
1227 {
1228         return handle->conn->connectpath;
1229 }
1230
1231 static NTSTATUS vfswrap_brl_lock_windows(struct vfs_handle_struct *handle,
1232                                          struct byte_range_lock *br_lck,
1233                                          struct lock_struct *plock,
1234                                          bool blocking_lock,
1235                                          struct blocking_lock_record *blr)
1236 {
1237         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1238
1239         /* Note: blr is not used in the default implementation. */
1240         return brl_lock_windows_default(br_lck, plock, blocking_lock);
1241 }
1242
1243 static bool vfswrap_brl_unlock_windows(struct vfs_handle_struct *handle,
1244                                        struct messaging_context *msg_ctx,
1245                                        struct byte_range_lock *br_lck,
1246                                        const struct lock_struct *plock)
1247 {
1248         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1249
1250         return brl_unlock_windows_default(msg_ctx, br_lck, plock);
1251 }
1252
1253 static bool vfswrap_brl_cancel_windows(struct vfs_handle_struct *handle,
1254                                        struct byte_range_lock *br_lck,
1255                                        struct lock_struct *plock,
1256                                        struct blocking_lock_record *blr)
1257 {
1258         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1259
1260         /* Note: blr is not used in the default implementation. */
1261         return brl_lock_cancel_default(br_lck, plock);
1262 }
1263
1264 static bool vfswrap_strict_lock(struct vfs_handle_struct *handle,
1265                                 files_struct *fsp,
1266                                 struct lock_struct *plock)
1267 {
1268         SMB_ASSERT(plock->lock_type == READ_LOCK ||
1269             plock->lock_type == WRITE_LOCK);
1270
1271         return strict_lock_default(fsp, plock);
1272 }
1273
1274 static void vfswrap_strict_unlock(struct vfs_handle_struct *handle,
1275                                 files_struct *fsp,
1276                                 struct lock_struct *plock)
1277 {
1278         SMB_ASSERT(plock->lock_type == READ_LOCK ||
1279             plock->lock_type == WRITE_LOCK);
1280
1281         strict_unlock_default(fsp, plock);
1282 }
1283
1284 /* NT ACL operations. */
1285
1286 static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
1287                                     files_struct *fsp,
1288                                     uint32 security_info,
1289                                     struct security_descriptor **ppdesc)
1290 {
1291         NTSTATUS result;
1292
1293         START_PROFILE(fget_nt_acl);
1294         result = posix_fget_nt_acl(fsp, security_info, ppdesc);
1295         END_PROFILE(fget_nt_acl);
1296         return result;
1297 }
1298
1299 static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
1300                                    const char *name,
1301                                    uint32 security_info,
1302                                    struct security_descriptor **ppdesc)
1303 {
1304         NTSTATUS result;
1305
1306         START_PROFILE(get_nt_acl);
1307         result = posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
1308         END_PROFILE(get_nt_acl);
1309         return result;
1310 }
1311
1312 static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const struct security_descriptor *psd)
1313 {
1314         NTSTATUS result;
1315
1316         START_PROFILE(fset_nt_acl);
1317         result = set_nt_acl(fsp, security_info_sent, psd);
1318         END_PROFILE(fset_nt_acl);
1319         return result;
1320 }
1321
1322 static int vfswrap_chmod_acl(vfs_handle_struct *handle,  const char *name, mode_t mode)
1323 {
1324 #ifdef HAVE_NO_ACL
1325         errno = ENOSYS;
1326         return -1;
1327 #else
1328         int result;
1329
1330         START_PROFILE(chmod_acl);
1331         result = chmod_acl(handle->conn, name, mode);
1332         END_PROFILE(chmod_acl);
1333         return result;
1334 #endif
1335 }
1336
1337 static int vfswrap_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1338 {
1339 #ifdef HAVE_NO_ACL
1340         errno = ENOSYS;
1341         return -1;
1342 #else
1343         int result;
1344
1345         START_PROFILE(fchmod_acl);
1346         result = fchmod_acl(fsp, mode);
1347         END_PROFILE(fchmod_acl);
1348         return result;
1349 #endif
1350 }
1351
1352 static int vfswrap_sys_acl_get_entry(vfs_handle_struct *handle,  SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1353 {
1354         return sys_acl_get_entry(theacl, entry_id, entry_p);
1355 }
1356
1357 static int vfswrap_sys_acl_get_tag_type(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1358 {
1359         return sys_acl_get_tag_type(entry_d, tag_type_p);
1360 }
1361
1362 static int vfswrap_sys_acl_get_permset(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1363 {
1364         return sys_acl_get_permset(entry_d, permset_p);
1365 }
1366
1367 static void * vfswrap_sys_acl_get_qualifier(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d)
1368 {
1369         return sys_acl_get_qualifier(entry_d);
1370 }
1371
1372 static SMB_ACL_T vfswrap_sys_acl_get_file(vfs_handle_struct *handle,  const char *path_p, SMB_ACL_TYPE_T type)
1373 {
1374         return sys_acl_get_file(handle, path_p, type);
1375 }
1376
1377 static SMB_ACL_T vfswrap_sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp)
1378 {
1379         return sys_acl_get_fd(handle, fsp);
1380 }
1381
1382 static int vfswrap_sys_acl_clear_perms(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset)
1383 {
1384         return sys_acl_clear_perms(permset);
1385 }
1386
1387 static int vfswrap_sys_acl_add_perm(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1388 {
1389         return sys_acl_add_perm(permset, perm);
1390 }
1391
1392 static char * vfswrap_sys_acl_to_text(vfs_handle_struct *handle,  SMB_ACL_T theacl, ssize_t *plen)
1393 {
1394         return sys_acl_to_text(theacl, plen);
1395 }
1396
1397 static SMB_ACL_T vfswrap_sys_acl_init(vfs_handle_struct *handle,  int count)
1398 {
1399         return sys_acl_init(count);
1400 }
1401
1402 static int vfswrap_sys_acl_create_entry(vfs_handle_struct *handle,  SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1403 {
1404         return sys_acl_create_entry(pacl, pentry);
1405 }
1406
1407 static int vfswrap_sys_acl_set_tag_type(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1408 {
1409         return sys_acl_set_tag_type(entry, tagtype);
1410 }
1411
1412 static int vfswrap_sys_acl_set_qualifier(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, void *qual)
1413 {
1414         return sys_acl_set_qualifier(entry, qual);
1415 }
1416
1417 static int vfswrap_sys_acl_set_permset(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1418 {
1419         return sys_acl_set_permset(entry, permset);
1420 }
1421
1422 static int vfswrap_sys_acl_valid(vfs_handle_struct *handle,  SMB_ACL_T theacl )
1423 {
1424         return sys_acl_valid(theacl );
1425 }
1426
1427 static int vfswrap_sys_acl_set_file(vfs_handle_struct *handle,  const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1428 {
1429         return sys_acl_set_file(handle, name, acltype, theacl);
1430 }
1431
1432 static int vfswrap_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp, SMB_ACL_T theacl)
1433 {
1434         return sys_acl_set_fd(handle, fsp, theacl);
1435 }
1436
1437 static int vfswrap_sys_acl_delete_def_file(vfs_handle_struct *handle,  const char *path)
1438 {
1439         return sys_acl_delete_def_file(handle, path);
1440 }
1441
1442 static int vfswrap_sys_acl_get_perm(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1443 {
1444         return sys_acl_get_perm(permset, perm);
1445 }
1446
1447 static int vfswrap_sys_acl_free_text(vfs_handle_struct *handle,  char *text)
1448 {
1449         return sys_acl_free_text(text);
1450 }
1451
1452 static int vfswrap_sys_acl_free_acl(vfs_handle_struct *handle,  SMB_ACL_T posix_acl)
1453 {
1454         return sys_acl_free_acl(posix_acl);
1455 }
1456
1457 static int vfswrap_sys_acl_free_qualifier(vfs_handle_struct *handle,  void *qualifier, SMB_ACL_TAG_T tagtype)
1458 {
1459         return sys_acl_free_qualifier(qualifier, tagtype);
1460 }
1461
1462 /****************************************************************
1463  Extended attribute operations.
1464 *****************************************************************/
1465
1466 static ssize_t vfswrap_getxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
1467 {
1468         return sys_getxattr(path, name, value, size);
1469 }
1470
1471 static ssize_t vfswrap_lgetxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
1472 {
1473         return sys_lgetxattr(path, name, value, size);
1474 }
1475
1476 static ssize_t vfswrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1477 {
1478         return sys_fgetxattr(fsp->fh->fd, name, value, size);
1479 }
1480
1481 static ssize_t vfswrap_listxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
1482 {
1483         return sys_listxattr(path, list, size);
1484 }
1485
1486 ssize_t vfswrap_llistxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
1487 {
1488         return sys_llistxattr(path, list, size);
1489 }
1490
1491 ssize_t vfswrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1492 {
1493         return sys_flistxattr(fsp->fh->fd, list, size);
1494 }
1495
1496 static int vfswrap_removexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
1497 {
1498         return sys_removexattr(path, name);
1499 }
1500
1501 static int vfswrap_lremovexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
1502 {
1503         return sys_lremovexattr(path, name);
1504 }
1505
1506 static int vfswrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1507 {
1508         return sys_fremovexattr(fsp->fh->fd, name);
1509 }
1510
1511 static int vfswrap_setxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
1512 {
1513         return sys_setxattr(path, name, value, size, flags);
1514 }
1515
1516 static int vfswrap_lsetxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
1517 {
1518         return sys_lsetxattr(path, name, value, size, flags);
1519 }
1520
1521 static int vfswrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1522 {
1523         return sys_fsetxattr(fsp->fh->fd, name, value, size, flags);
1524 }
1525
1526 static int vfswrap_aio_read(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1527 {
1528         int ret;
1529         /*
1530          * aio_read must be done as root, because in the glibc aio
1531          * implementation the helper thread needs to be able to send a signal
1532          * to the main thread, even when it has done a seteuid() to a
1533          * different user.
1534          */
1535         become_root();
1536         ret = sys_aio_read(aiocb);
1537         unbecome_root();
1538         return ret;
1539 }
1540
1541 static int vfswrap_aio_write(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1542 {
1543         int ret;
1544         /*
1545          * aio_write must be done as root, because in the glibc aio
1546          * implementation the helper thread needs to be able to send a signal
1547          * to the main thread, even when it has done a seteuid() to a
1548          * different user.
1549          */
1550         become_root();
1551         ret = sys_aio_write(aiocb);
1552         unbecome_root();
1553         return ret;
1554 }
1555
1556 static ssize_t vfswrap_aio_return(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1557 {
1558         return sys_aio_return(aiocb);
1559 }
1560
1561 static int vfswrap_aio_cancel(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1562 {
1563         return sys_aio_cancel(fsp->fh->fd, aiocb);
1564 }
1565
1566 static int vfswrap_aio_error(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1567 {
1568         return sys_aio_error(aiocb);
1569 }
1570
1571 static int vfswrap_aio_fsync(struct vfs_handle_struct *handle, struct files_struct *fsp, int op, SMB_STRUCT_AIOCB *aiocb)
1572 {
1573         return sys_aio_fsync(op, aiocb);
1574 }
1575
1576 static int vfswrap_aio_suspend(struct vfs_handle_struct *handle, struct files_struct *fsp, const SMB_STRUCT_AIOCB * const aiocb[], int n, const struct timespec *timeout)
1577 {
1578         return sys_aio_suspend(aiocb, n, timeout);
1579 }
1580
1581 static bool vfswrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1582 {
1583         return false;
1584 }
1585
1586 static bool vfswrap_is_offline(struct vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
1587 {
1588         if (ISDOT(path) || ISDOTDOT(path)) {
1589                 return false;
1590         }
1591
1592         if (!lp_dmapi_support(SNUM(handle->conn)) || !dmapi_have_session()) {
1593 #if defined(ENOTSUP)
1594                 errno = ENOTSUP;
1595 #endif
1596                 return false;
1597         }
1598
1599         return (dmapi_file_flags(path) & FILE_ATTRIBUTE_OFFLINE) != 0;
1600 }
1601
1602 static int vfswrap_set_offline(struct vfs_handle_struct *handle, const char *path)
1603 {
1604         /* We don't know how to set offline bit by default, needs to be overriden in the vfs modules */
1605 #if defined(ENOTSUP)
1606         errno = ENOTSUP;
1607 #endif
1608         return -1;
1609 }
1610
1611 static struct vfs_fn_pointers vfs_default_fns = {
1612         /* Disk operations */
1613
1614         .connect_fn = vfswrap_connect,
1615         .disconnect = vfswrap_disconnect,
1616         .disk_free = vfswrap_disk_free,
1617         .get_quota = vfswrap_get_quota,
1618         .set_quota = vfswrap_set_quota,
1619         .get_shadow_copy_data = vfswrap_get_shadow_copy_data,
1620         .statvfs = vfswrap_statvfs,
1621         .fs_capabilities = vfswrap_fs_capabilities,
1622
1623         /* Directory operations */
1624
1625         .opendir = vfswrap_opendir,
1626         .readdir = vfswrap_readdir,
1627         .seekdir = vfswrap_seekdir,
1628         .telldir = vfswrap_telldir,
1629         .rewind_dir = vfswrap_rewinddir,
1630         .mkdir = vfswrap_mkdir,
1631         .rmdir = vfswrap_rmdir,
1632         .closedir = vfswrap_closedir,
1633         .init_search_op = vfswrap_init_search_op,
1634
1635         /* File operations */
1636
1637         .open = vfswrap_open,
1638         .create_file = vfswrap_create_file,
1639         .close_fn = vfswrap_close,
1640         .vfs_read = vfswrap_read,
1641         .pread = vfswrap_pread,
1642         .write = vfswrap_write,
1643         .pwrite = vfswrap_pwrite,
1644         .lseek = vfswrap_lseek,
1645         .sendfile = vfswrap_sendfile,
1646         .recvfile = vfswrap_recvfile,
1647         .rename = vfswrap_rename,
1648         .fsync = vfswrap_fsync,
1649         .stat = vfswrap_stat,
1650         .fstat = vfswrap_fstat,
1651         .lstat = vfswrap_lstat,
1652         .get_alloc_size = vfswrap_get_alloc_size,
1653         .unlink = vfswrap_unlink,
1654         .chmod = vfswrap_chmod,
1655         .fchmod = vfswrap_fchmod,
1656         .chown = vfswrap_chown,
1657         .fchown = vfswrap_fchown,
1658         .lchown = vfswrap_lchown,
1659         .chdir = vfswrap_chdir,
1660         .getwd = vfswrap_getwd,
1661         .ntimes = vfswrap_ntimes,
1662         .ftruncate = vfswrap_ftruncate,
1663         .fallocate = vfswrap_fallocate,
1664         .lock = vfswrap_lock,
1665         .kernel_flock = vfswrap_kernel_flock,
1666         .linux_setlease = vfswrap_linux_setlease,
1667         .getlock = vfswrap_getlock,
1668         .symlink = vfswrap_symlink,
1669         .vfs_readlink = vfswrap_readlink,
1670         .link = vfswrap_link,
1671         .mknod = vfswrap_mknod,
1672         .realpath = vfswrap_realpath,
1673         .notify_watch = vfswrap_notify_watch,
1674         .chflags = vfswrap_chflags,
1675         .file_id_create = vfswrap_file_id_create,
1676         .streaminfo = vfswrap_streaminfo,
1677         .get_real_filename = vfswrap_get_real_filename,
1678         .connectpath = vfswrap_connectpath,
1679         .brl_lock_windows = vfswrap_brl_lock_windows,
1680         .brl_unlock_windows = vfswrap_brl_unlock_windows,
1681         .brl_cancel_windows = vfswrap_brl_cancel_windows,
1682         .strict_lock = vfswrap_strict_lock,
1683         .strict_unlock = vfswrap_strict_unlock,
1684         .translate_name = vfswrap_translate_name,
1685
1686         /* NT ACL operations. */
1687
1688         .fget_nt_acl = vfswrap_fget_nt_acl,
1689         .get_nt_acl = vfswrap_get_nt_acl,
1690         .fset_nt_acl = vfswrap_fset_nt_acl,
1691
1692         /* POSIX ACL operations. */
1693
1694         .chmod_acl = vfswrap_chmod_acl,
1695         .fchmod_acl = vfswrap_fchmod_acl,
1696
1697         .sys_acl_get_entry = vfswrap_sys_acl_get_entry,
1698         .sys_acl_get_tag_type = vfswrap_sys_acl_get_tag_type,
1699         .sys_acl_get_permset = vfswrap_sys_acl_get_permset,
1700         .sys_acl_get_qualifier = vfswrap_sys_acl_get_qualifier,
1701         .sys_acl_get_file = vfswrap_sys_acl_get_file,
1702         .sys_acl_get_fd = vfswrap_sys_acl_get_fd,
1703         .sys_acl_clear_perms = vfswrap_sys_acl_clear_perms,
1704         .sys_acl_add_perm = vfswrap_sys_acl_add_perm,
1705         .sys_acl_to_text = vfswrap_sys_acl_to_text,
1706         .sys_acl_init = vfswrap_sys_acl_init,
1707         .sys_acl_create_entry = vfswrap_sys_acl_create_entry,
1708         .sys_acl_set_tag_type = vfswrap_sys_acl_set_tag_type,
1709         .sys_acl_set_qualifier = vfswrap_sys_acl_set_qualifier,
1710         .sys_acl_set_permset = vfswrap_sys_acl_set_permset,
1711         .sys_acl_valid = vfswrap_sys_acl_valid,
1712         .sys_acl_set_file = vfswrap_sys_acl_set_file,
1713         .sys_acl_set_fd = vfswrap_sys_acl_set_fd,
1714         .sys_acl_delete_def_file = vfswrap_sys_acl_delete_def_file,
1715         .sys_acl_get_perm = vfswrap_sys_acl_get_perm,
1716         .sys_acl_free_text = vfswrap_sys_acl_free_text,
1717         .sys_acl_free_acl = vfswrap_sys_acl_free_acl,
1718         .sys_acl_free_qualifier = vfswrap_sys_acl_free_qualifier,
1719
1720         /* EA operations. */
1721         .getxattr = vfswrap_getxattr,
1722         .lgetxattr = vfswrap_lgetxattr,
1723         .fgetxattr = vfswrap_fgetxattr,
1724         .listxattr = vfswrap_listxattr,
1725         .llistxattr = vfswrap_llistxattr,
1726         .flistxattr = vfswrap_flistxattr,
1727         .removexattr = vfswrap_removexattr,
1728         .lremovexattr = vfswrap_lremovexattr,
1729         .fremovexattr = vfswrap_fremovexattr,
1730         .setxattr = vfswrap_setxattr,
1731         .lsetxattr = vfswrap_lsetxattr,
1732         .fsetxattr = vfswrap_fsetxattr,
1733
1734         /* aio operations */
1735         .aio_read = vfswrap_aio_read,
1736         .aio_write = vfswrap_aio_write,
1737         .aio_return_fn = vfswrap_aio_return,
1738         .aio_cancel = vfswrap_aio_cancel,
1739         .aio_error_fn = vfswrap_aio_error,
1740         .aio_fsync = vfswrap_aio_fsync,
1741         .aio_suspend = vfswrap_aio_suspend,
1742         .aio_force = vfswrap_aio_force,
1743
1744         /* offline operations */
1745         .is_offline = vfswrap_is_offline,
1746         .set_offline = vfswrap_set_offline
1747 };
1748
1749 NTSTATUS vfs_default_init(void);
1750 NTSTATUS vfs_default_init(void)
1751 {
1752         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1753                                 DEFAULT_VFS_MODULE_NAME, &vfs_default_fns);
1754 }
1755
1756