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