Move the checks for null timestamps down below the VFS_NTIMES
[samba.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 (null_timespec(ft->atime)) {
784                 ft->atime= smb_fname->st.st_ex_atime;
785         }
786
787         if (null_timespec(ft->mtime)) {
788                 ft->mtime = smb_fname->st.st_ex_mtime;
789         }
790
791         if ((timespec_compare(&ft->atime,
792                                 &smb_fname->st.st_ex_atime) == 0) &&
793                         (timespec_compare(&ft->mtime,
794                                 &smb_fname->st.st_ex_mtime) == 0)) {
795                 return 0;
796         }
797
798 #if defined(HAVE_UTIMES)
799         if (ft != NULL) {
800                 struct timeval tv[2];
801                 tv[0] = convert_timespec_to_timeval(ft->atime);
802                 tv[1] = convert_timespec_to_timeval(ft->mtime);
803                 result = utimes(smb_fname->base_name, tv);
804         } else {
805                 result = utimes(smb_fname->base_name, NULL);
806         }
807 #elif defined(HAVE_UTIME)
808         if (ft != NULL) {
809                 struct utimbuf times;
810                 times.actime = convert_timespec_to_time_t(ft->atime);
811                 times.modtime = convert_timespec_to_time_t(ft->mtime);
812                 result = utime(smb_fname->base_name, &times);
813         } else {
814                 result = utime(smb_fname->base_name, NULL);
815         }
816 #else
817         errno = ENOSYS;
818         result = -1;
819 #endif
820  out:
821         END_PROFILE(syscall_ntimes);
822         return result;
823 }
824
825 /*********************************************************************
826  A version of ftruncate that will write the space on disk if strict
827  allocate is set.
828 **********************************************************************/
829
830 static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T len)
831 {
832         SMB_STRUCT_STAT st;
833         SMB_OFF_T currpos = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
834         unsigned char zero_space[4096];
835         SMB_OFF_T space_to_write;
836
837         if (currpos == -1)
838                 return -1;
839
840         if (SMB_VFS_FSTAT(fsp, &st) == -1)
841                 return -1;
842
843         space_to_write = len - st.st_ex_size;
844
845 #ifdef S_ISFIFO
846         if (S_ISFIFO(st.st_ex_mode))
847                 return 0;
848 #endif
849
850         if (st.st_ex_size == len)
851                 return 0;
852
853         /* Shrink - just ftruncate. */
854         if (st.st_ex_size > len)
855                 return sys_ftruncate(fsp->fh->fd, len);
856
857         /* available disk space is enough or not? */
858         if (lp_strict_allocate(SNUM(fsp->conn))){
859                 uint64_t space_avail;
860                 uint64_t bsize,dfree,dsize;
861
862                 space_avail = get_dfree_info(fsp->conn,
863                                              fsp->fsp_name->base_name, false,
864                                              &bsize, &dfree, &dsize);
865                 /* space_avail is 1k blocks */
866                 if (space_avail == (uint64_t)-1 ||
867                                 ((uint64_t)space_to_write/1024 > space_avail) ) {
868                         errno = ENOSPC;
869                         return -1;
870                 }
871         }
872
873         /* Write out the real space on disk. */
874         if (SMB_VFS_LSEEK(fsp, st.st_ex_size, SEEK_SET) != st.st_ex_size)
875                 return -1;
876
877         space_to_write = len - st.st_ex_size;
878
879         memset(zero_space, '\0', sizeof(zero_space));
880         while ( space_to_write > 0) {
881                 SMB_OFF_T retlen;
882                 SMB_OFF_T current_len_to_write = MIN(sizeof(zero_space),space_to_write);
883
884                 retlen = SMB_VFS_WRITE(fsp,(char *)zero_space,current_len_to_write);
885                 if (retlen <= 0)
886                         return -1;
887
888                 space_to_write -= retlen;
889         }
890
891         /* Seek to where we were */
892         if (SMB_VFS_LSEEK(fsp, currpos, SEEK_SET) != currpos)
893                 return -1;
894
895         return 0;
896 }
897
898 static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T len)
899 {
900         int result = -1;
901         SMB_STRUCT_STAT st;
902         char c = 0;
903         SMB_OFF_T currpos;
904
905         START_PROFILE(syscall_ftruncate);
906
907         if (lp_strict_allocate(SNUM(fsp->conn))) {
908                 result = strict_allocate_ftruncate(handle, fsp, len);
909                 END_PROFILE(syscall_ftruncate);
910                 return result;
911         }
912
913         /* we used to just check HAVE_FTRUNCATE_EXTEND and only use
914            sys_ftruncate if the system supports it. Then I discovered that
915            you can have some filesystems that support ftruncate
916            expansion and some that don't! On Linux fat can't do
917            ftruncate extend but ext2 can. */
918
919         result = sys_ftruncate(fsp->fh->fd, len);
920         if (result == 0)
921                 goto done;
922
923         /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
924            extend a file with ftruncate. Provide alternate implementation
925            for this */
926         currpos = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
927         if (currpos == -1) {
928                 goto done;
929         }
930
931         /* Do an fstat to see if the file is longer than the requested
932            size in which case the ftruncate above should have
933            succeeded or shorter, in which case seek to len - 1 and
934            write 1 byte of zero */
935         if (SMB_VFS_FSTAT(fsp, &st) == -1) {
936                 goto done;
937         }
938
939 #ifdef S_ISFIFO
940         if (S_ISFIFO(st.st_ex_mode)) {
941                 result = 0;
942                 goto done;
943         }
944 #endif
945
946         if (st.st_ex_size == len) {
947                 result = 0;
948                 goto done;
949         }
950
951         if (st.st_ex_size > len) {
952                 /* the sys_ftruncate should have worked */
953                 goto done;
954         }
955
956         if (SMB_VFS_LSEEK(fsp, len-1, SEEK_SET) != len -1)
957                 goto done;
958
959         if (SMB_VFS_WRITE(fsp, &c, 1)!=1)
960                 goto done;
961
962         /* Seek to where we were */
963         if (SMB_VFS_LSEEK(fsp, currpos, SEEK_SET) != currpos)
964                 goto done;
965         result = 0;
966
967   done:
968
969         END_PROFILE(syscall_ftruncate);
970         return result;
971 }
972
973 static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
974 {
975         bool result;
976
977         START_PROFILE(syscall_fcntl_lock);
978         result =  fcntl_lock(fsp->fh->fd, op, offset, count, type);
979         END_PROFILE(syscall_fcntl_lock);
980         return result;
981 }
982
983 static int vfswrap_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
984                                 uint32 share_mode)
985 {
986         START_PROFILE(syscall_kernel_flock);
987         kernel_flock(fsp->fh->fd, share_mode);
988         END_PROFILE(syscall_kernel_flock);
989         return 0;
990 }
991
992 static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
993 {
994         bool result;
995
996         START_PROFILE(syscall_fcntl_getlock);
997         result =  fcntl_getlock(fsp->fh->fd, poffset, pcount, ptype, ppid);
998         END_PROFILE(syscall_fcntl_getlock);
999         return result;
1000 }
1001
1002 static int vfswrap_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1003                                 int leasetype)
1004 {
1005         int result = -1;
1006
1007         START_PROFILE(syscall_linux_setlease);
1008
1009 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1010         /* first set the signal handler */
1011         if(linux_set_lease_sighandler(fsp->fh->fd) == -1) {
1012                 return -1;
1013         }
1014
1015         result = linux_setlease(fsp->fh->fd, leasetype);
1016 #else
1017         errno = ENOSYS;
1018 #endif
1019         END_PROFILE(syscall_linux_setlease);
1020         return result;
1021 }
1022
1023 static int vfswrap_symlink(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
1024 {
1025         int result;
1026
1027         START_PROFILE(syscall_symlink);
1028         result = symlink(oldpath, newpath);
1029         END_PROFILE(syscall_symlink);
1030         return result;
1031 }
1032
1033 static int vfswrap_readlink(vfs_handle_struct *handle,  const char *path, char *buf, size_t bufsiz)
1034 {
1035         int result;
1036
1037         START_PROFILE(syscall_readlink);
1038         result = readlink(path, buf, bufsiz);
1039         END_PROFILE(syscall_readlink);
1040         return result;
1041 }
1042
1043 static int vfswrap_link(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
1044 {
1045         int result;
1046
1047         START_PROFILE(syscall_link);
1048         result = link(oldpath, newpath);
1049         END_PROFILE(syscall_link);
1050         return result;
1051 }
1052
1053 static int vfswrap_mknod(vfs_handle_struct *handle,  const char *pathname, mode_t mode, SMB_DEV_T dev)
1054 {
1055         int result;
1056
1057         START_PROFILE(syscall_mknod);
1058         result = sys_mknod(pathname, mode, dev);
1059         END_PROFILE(syscall_mknod);
1060         return result;
1061 }
1062
1063 static char *vfswrap_realpath(vfs_handle_struct *handle,  const char *path, char *resolved_path)
1064 {
1065         char *result;
1066
1067         START_PROFILE(syscall_realpath);
1068         result = realpath(path, resolved_path);
1069         END_PROFILE(syscall_realpath);
1070         return result;
1071 }
1072
1073 static NTSTATUS vfswrap_notify_watch(vfs_handle_struct *vfs_handle,
1074                                      struct sys_notify_context *ctx,
1075                                      struct notify_entry *e,
1076                                      void (*callback)(struct sys_notify_context *ctx, 
1077                                                       void *private_data,
1078                                                       struct notify_event *ev),
1079                                      void *private_data, void *handle)
1080 {
1081         /*
1082          * So far inotify is the only supported default notify mechanism. If
1083          * another platform like the the BSD's or a proprietary Unix comes
1084          * along and wants another default, we can play the same trick we
1085          * played with Posix ACLs.
1086          *
1087          * Until that is the case, hard-code inotify here.
1088          */
1089 #ifdef HAVE_INOTIFY
1090         if (lp_kernel_change_notify(ctx->conn->params)) {
1091                 return inotify_watch(ctx, e, callback, private_data, handle);
1092         }
1093 #endif
1094         /*
1095          * Do nothing, leave everything to notify_internal.c
1096          */
1097         return NT_STATUS_OK;
1098 }
1099
1100 static int vfswrap_chflags(vfs_handle_struct *handle, const char *path,
1101                            unsigned int flags)
1102 {
1103 #ifdef HAVE_CHFLAGS
1104         return chflags(path, flags);
1105 #else
1106         errno = ENOSYS;
1107         return -1;
1108 #endif
1109 }
1110
1111 static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle,
1112                                              const SMB_STRUCT_STAT *sbuf)
1113 {
1114         struct file_id key;
1115
1116         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
1117          * blob */
1118         ZERO_STRUCT(key);
1119
1120         key.devid = sbuf->st_ex_dev;
1121         key.inode = sbuf->st_ex_ino;
1122         /* key.extid is unused by default. */
1123
1124         return key;
1125 }
1126
1127 static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
1128                                    struct files_struct *fsp,
1129                                    const char *fname,
1130                                    TALLOC_CTX *mem_ctx,
1131                                    unsigned int *pnum_streams,
1132                                    struct stream_struct **pstreams)
1133 {
1134         SMB_STRUCT_STAT sbuf;
1135         unsigned int num_streams = 0;
1136         struct stream_struct *streams = NULL;
1137         int ret;
1138
1139         if ((fsp != NULL) && (fsp->is_directory)) {
1140                 /*
1141                  * No default streams on directories
1142                  */
1143                 goto done;
1144         }
1145
1146         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
1147                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
1148         }
1149         else {
1150                 struct smb_filename *smb_fname = NULL;
1151                 NTSTATUS status;
1152
1153                 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
1154                                                     NULL, &smb_fname);
1155                 if (!NT_STATUS_IS_OK(status)) {
1156                         return status;
1157                 }
1158                 ret = SMB_VFS_STAT(handle->conn, smb_fname);
1159                 sbuf = smb_fname->st;
1160                 TALLOC_FREE(smb_fname);
1161         }
1162
1163         if (ret == -1) {
1164                 return map_nt_error_from_unix(errno);
1165         }
1166
1167         if (S_ISDIR(sbuf.st_ex_mode)) {
1168                 goto done;
1169         }
1170
1171         streams = talloc(mem_ctx, struct stream_struct);
1172
1173         if (streams == NULL) {
1174                 return NT_STATUS_NO_MEMORY;
1175         }
1176
1177         streams->size = sbuf.st_ex_size;
1178         streams->alloc_size = SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp, &sbuf);
1179
1180         streams->name = talloc_strdup(streams, "::$DATA");
1181         if (streams->name == NULL) {
1182                 TALLOC_FREE(streams);
1183                 return NT_STATUS_NO_MEMORY;
1184         }
1185
1186         num_streams = 1;
1187  done:
1188         *pnum_streams = num_streams;
1189         *pstreams = streams;
1190         return NT_STATUS_OK;
1191 }
1192
1193 static int vfswrap_get_real_filename(struct vfs_handle_struct *handle,
1194                                      const char *path,
1195                                      const char *name,
1196                                      TALLOC_CTX *mem_ctx,
1197                                      char **found_name)
1198 {
1199         /*
1200          * Don't fall back to get_real_filename so callers can differentiate
1201          * between a full directory scan and an actual case-insensitive stat.
1202          */
1203         errno = EOPNOTSUPP;
1204         return -1;
1205 }
1206
1207 static const char *vfswrap_connectpath(struct vfs_handle_struct *handle,
1208                                        const char *fname)
1209 {
1210         return handle->conn->connectpath;
1211 }
1212
1213 static NTSTATUS vfswrap_brl_lock_windows(struct vfs_handle_struct *handle,
1214                                          struct byte_range_lock *br_lck,
1215                                          struct lock_struct *plock,
1216                                          bool blocking_lock,
1217                                          struct blocking_lock_record *blr)
1218 {
1219         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1220
1221         /* Note: blr is not used in the default implementation. */
1222         return brl_lock_windows_default(br_lck, plock, blocking_lock);
1223 }
1224
1225 static bool vfswrap_brl_unlock_windows(struct vfs_handle_struct *handle,
1226                                        struct messaging_context *msg_ctx,
1227                                        struct byte_range_lock *br_lck,
1228                                        const struct lock_struct *plock)
1229 {
1230         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1231
1232         return brl_unlock_windows_default(msg_ctx, br_lck, plock);
1233 }
1234
1235 static bool vfswrap_brl_cancel_windows(struct vfs_handle_struct *handle,
1236                                        struct byte_range_lock *br_lck,
1237                                        struct lock_struct *plock,
1238                                        struct blocking_lock_record *blr)
1239 {
1240         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1241
1242         /* Note: blr is not used in the default implementation. */
1243         return brl_lock_cancel_default(br_lck, plock);
1244 }
1245
1246 static bool vfswrap_strict_lock(struct vfs_handle_struct *handle,
1247                                 files_struct *fsp,
1248                                 struct lock_struct *plock)
1249 {
1250         SMB_ASSERT(plock->lock_type == READ_LOCK ||
1251             plock->lock_type == WRITE_LOCK);
1252
1253         return strict_lock_default(fsp, plock);
1254 }
1255
1256 static void vfswrap_strict_unlock(struct vfs_handle_struct *handle,
1257                                 files_struct *fsp,
1258                                 struct lock_struct *plock)
1259 {
1260         SMB_ASSERT(plock->lock_type == READ_LOCK ||
1261             plock->lock_type == WRITE_LOCK);
1262
1263         strict_unlock_default(fsp, plock);
1264 }
1265
1266 /* NT ACL operations. */
1267
1268 static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
1269                                     files_struct *fsp,
1270                                     uint32 security_info, SEC_DESC **ppdesc)
1271 {
1272         NTSTATUS result;
1273
1274         START_PROFILE(fget_nt_acl);
1275         result = posix_fget_nt_acl(fsp, security_info, ppdesc);
1276         END_PROFILE(fget_nt_acl);
1277         return result;
1278 }
1279
1280 static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
1281                                    const char *name,
1282                                    uint32 security_info, SEC_DESC **ppdesc)
1283 {
1284         NTSTATUS result;
1285
1286         START_PROFILE(get_nt_acl);
1287         result = posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
1288         END_PROFILE(get_nt_acl);
1289         return result;
1290 }
1291
1292 static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
1293 {
1294         NTSTATUS result;
1295
1296         START_PROFILE(fset_nt_acl);
1297         result = set_nt_acl(fsp, security_info_sent, psd);
1298         END_PROFILE(fset_nt_acl);
1299         return result;
1300 }
1301
1302 static int vfswrap_chmod_acl(vfs_handle_struct *handle,  const char *name, mode_t mode)
1303 {
1304 #ifdef HAVE_NO_ACL
1305         errno = ENOSYS;
1306         return -1;
1307 #else
1308         int result;
1309
1310         START_PROFILE(chmod_acl);
1311         result = chmod_acl(handle->conn, name, mode);
1312         END_PROFILE(chmod_acl);
1313         return result;
1314 #endif
1315 }
1316
1317 static int vfswrap_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1318 {
1319 #ifdef HAVE_NO_ACL
1320         errno = ENOSYS;
1321         return -1;
1322 #else
1323         int result;
1324
1325         START_PROFILE(fchmod_acl);
1326         result = fchmod_acl(fsp, mode);
1327         END_PROFILE(fchmod_acl);
1328         return result;
1329 #endif
1330 }
1331
1332 static int vfswrap_sys_acl_get_entry(vfs_handle_struct *handle,  SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1333 {
1334         return sys_acl_get_entry(theacl, entry_id, entry_p);
1335 }
1336
1337 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)
1338 {
1339         return sys_acl_get_tag_type(entry_d, tag_type_p);
1340 }
1341
1342 static int vfswrap_sys_acl_get_permset(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1343 {
1344         return sys_acl_get_permset(entry_d, permset_p);
1345 }
1346
1347 static void * vfswrap_sys_acl_get_qualifier(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d)
1348 {
1349         return sys_acl_get_qualifier(entry_d);
1350 }
1351
1352 static SMB_ACL_T vfswrap_sys_acl_get_file(vfs_handle_struct *handle,  const char *path_p, SMB_ACL_TYPE_T type)
1353 {
1354         return sys_acl_get_file(handle, path_p, type);
1355 }
1356
1357 static SMB_ACL_T vfswrap_sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp)
1358 {
1359         return sys_acl_get_fd(handle, fsp);
1360 }
1361
1362 static int vfswrap_sys_acl_clear_perms(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset)
1363 {
1364         return sys_acl_clear_perms(permset);
1365 }
1366
1367 static int vfswrap_sys_acl_add_perm(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1368 {
1369         return sys_acl_add_perm(permset, perm);
1370 }
1371
1372 static char * vfswrap_sys_acl_to_text(vfs_handle_struct *handle,  SMB_ACL_T theacl, ssize_t *plen)
1373 {
1374         return sys_acl_to_text(theacl, plen);
1375 }
1376
1377 static SMB_ACL_T vfswrap_sys_acl_init(vfs_handle_struct *handle,  int count)
1378 {
1379         return sys_acl_init(count);
1380 }
1381
1382 static int vfswrap_sys_acl_create_entry(vfs_handle_struct *handle,  SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1383 {
1384         return sys_acl_create_entry(pacl, pentry);
1385 }
1386
1387 static int vfswrap_sys_acl_set_tag_type(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1388 {
1389         return sys_acl_set_tag_type(entry, tagtype);
1390 }
1391
1392 static int vfswrap_sys_acl_set_qualifier(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, void *qual)
1393 {
1394         return sys_acl_set_qualifier(entry, qual);
1395 }
1396
1397 static int vfswrap_sys_acl_set_permset(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1398 {
1399         return sys_acl_set_permset(entry, permset);
1400 }
1401
1402 static int vfswrap_sys_acl_valid(vfs_handle_struct *handle,  SMB_ACL_T theacl )
1403 {
1404         return sys_acl_valid(theacl );
1405 }
1406
1407 static int vfswrap_sys_acl_set_file(vfs_handle_struct *handle,  const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1408 {
1409         return sys_acl_set_file(handle, name, acltype, theacl);
1410 }
1411
1412 static int vfswrap_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp, SMB_ACL_T theacl)
1413 {
1414         return sys_acl_set_fd(handle, fsp, theacl);
1415 }
1416
1417 static int vfswrap_sys_acl_delete_def_file(vfs_handle_struct *handle,  const char *path)
1418 {
1419         return sys_acl_delete_def_file(handle, path);
1420 }
1421
1422 static int vfswrap_sys_acl_get_perm(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1423 {
1424         return sys_acl_get_perm(permset, perm);
1425 }
1426
1427 static int vfswrap_sys_acl_free_text(vfs_handle_struct *handle,  char *text)
1428 {
1429         return sys_acl_free_text(text);
1430 }
1431
1432 static int vfswrap_sys_acl_free_acl(vfs_handle_struct *handle,  SMB_ACL_T posix_acl)
1433 {
1434         return sys_acl_free_acl(posix_acl);
1435 }
1436
1437 static int vfswrap_sys_acl_free_qualifier(vfs_handle_struct *handle,  void *qualifier, SMB_ACL_TAG_T tagtype)
1438 {
1439         return sys_acl_free_qualifier(qualifier, tagtype);
1440 }
1441
1442 /****************************************************************
1443  Extended attribute operations.
1444 *****************************************************************/
1445
1446 static ssize_t vfswrap_getxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
1447 {
1448         return sys_getxattr(path, name, value, size);
1449 }
1450
1451 static ssize_t vfswrap_lgetxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
1452 {
1453         return sys_lgetxattr(path, name, value, size);
1454 }
1455
1456 static ssize_t vfswrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1457 {
1458         return sys_fgetxattr(fsp->fh->fd, name, value, size);
1459 }
1460
1461 static ssize_t vfswrap_listxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
1462 {
1463         return sys_listxattr(path, list, size);
1464 }
1465
1466 ssize_t vfswrap_llistxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
1467 {
1468         return sys_llistxattr(path, list, size);
1469 }
1470
1471 ssize_t vfswrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1472 {
1473         return sys_flistxattr(fsp->fh->fd, list, size);
1474 }
1475
1476 static int vfswrap_removexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
1477 {
1478         return sys_removexattr(path, name);
1479 }
1480
1481 static int vfswrap_lremovexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
1482 {
1483         return sys_lremovexattr(path, name);
1484 }
1485
1486 static int vfswrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1487 {
1488         return sys_fremovexattr(fsp->fh->fd, name);
1489 }
1490
1491 static int vfswrap_setxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
1492 {
1493         return sys_setxattr(path, name, value, size, flags);
1494 }
1495
1496 static int vfswrap_lsetxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
1497 {
1498         return sys_lsetxattr(path, name, value, size, flags);
1499 }
1500
1501 static int vfswrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1502 {
1503         return sys_fsetxattr(fsp->fh->fd, name, value, size, flags);
1504 }
1505
1506 static int vfswrap_aio_read(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1507 {
1508         int ret;
1509         /*
1510          * aio_read 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_read(aiocb);
1517         unbecome_root();
1518         return ret;
1519 }
1520
1521 static int vfswrap_aio_write(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1522 {
1523         int ret;
1524         /*
1525          * aio_write must be done as root, because in the glibc aio
1526          * implementation the helper thread needs to be able to send a signal
1527          * to the main thread, even when it has done a seteuid() to a
1528          * different user.
1529          */
1530         become_root();
1531         ret = sys_aio_write(aiocb);
1532         unbecome_root();
1533         return ret;
1534 }
1535
1536 static ssize_t vfswrap_aio_return(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1537 {
1538         return sys_aio_return(aiocb);
1539 }
1540
1541 static int vfswrap_aio_cancel(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1542 {
1543         return sys_aio_cancel(fsp->fh->fd, aiocb);
1544 }
1545
1546 static int vfswrap_aio_error(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1547 {
1548         return sys_aio_error(aiocb);
1549 }
1550
1551 static int vfswrap_aio_fsync(struct vfs_handle_struct *handle, struct files_struct *fsp, int op, SMB_STRUCT_AIOCB *aiocb)
1552 {
1553         return sys_aio_fsync(op, aiocb);
1554 }
1555
1556 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)
1557 {
1558         return sys_aio_suspend(aiocb, n, timeout);
1559 }
1560
1561 static bool vfswrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1562 {
1563         return false;
1564 }
1565
1566 static bool vfswrap_is_offline(struct vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
1567 {
1568         if (ISDOT(path) || ISDOTDOT(path)) {
1569                 return false;
1570         }
1571
1572         if (!lp_dmapi_support(SNUM(handle->conn)) || !dmapi_have_session()) {
1573 #if defined(ENOTSUP)
1574                 errno = ENOTSUP;
1575 #endif
1576                 return false;
1577         }
1578
1579         return (dmapi_file_flags(path) & FILE_ATTRIBUTE_OFFLINE) != 0;
1580 }
1581
1582 static int vfswrap_set_offline(struct vfs_handle_struct *handle, const char *path)
1583 {
1584         /* We don't know how to set offline bit by default, needs to be overriden in the vfs modules */
1585 #if defined(ENOTSUP)
1586         errno = ENOTSUP;
1587 #endif
1588         return -1;
1589 }
1590
1591 static struct vfs_fn_pointers vfs_default_fns = {
1592         /* Disk operations */
1593
1594         .connect_fn = vfswrap_connect,
1595         .disconnect = vfswrap_disconnect,
1596         .disk_free = vfswrap_disk_free,
1597         .get_quota = vfswrap_get_quota,
1598         .set_quota = vfswrap_set_quota,
1599         .get_shadow_copy_data = vfswrap_get_shadow_copy_data,
1600         .statvfs = vfswrap_statvfs,
1601         .fs_capabilities = vfswrap_fs_capabilities,
1602
1603         /* Directory operations */
1604
1605         .opendir = vfswrap_opendir,
1606         .readdir = vfswrap_readdir,
1607         .seekdir = vfswrap_seekdir,
1608         .telldir = vfswrap_telldir,
1609         .rewind_dir = vfswrap_rewinddir,
1610         .mkdir = vfswrap_mkdir,
1611         .rmdir = vfswrap_rmdir,
1612         .closedir = vfswrap_closedir,
1613         .init_search_op = vfswrap_init_search_op,
1614
1615         /* File operations */
1616
1617         .open = vfswrap_open,
1618         .create_file = vfswrap_create_file,
1619         .close_fn = vfswrap_close,
1620         .vfs_read = vfswrap_read,
1621         .pread = vfswrap_pread,
1622         .write = vfswrap_write,
1623         .pwrite = vfswrap_pwrite,
1624         .lseek = vfswrap_lseek,
1625         .sendfile = vfswrap_sendfile,
1626         .recvfile = vfswrap_recvfile,
1627         .rename = vfswrap_rename,
1628         .fsync = vfswrap_fsync,
1629         .stat = vfswrap_stat,
1630         .fstat = vfswrap_fstat,
1631         .lstat = vfswrap_lstat,
1632         .get_alloc_size = vfswrap_get_alloc_size,
1633         .unlink = vfswrap_unlink,
1634         .chmod = vfswrap_chmod,
1635         .fchmod = vfswrap_fchmod,
1636         .chown = vfswrap_chown,
1637         .fchown = vfswrap_fchown,
1638         .lchown = vfswrap_lchown,
1639         .chdir = vfswrap_chdir,
1640         .getwd = vfswrap_getwd,
1641         .ntimes = vfswrap_ntimes,
1642         .ftruncate = vfswrap_ftruncate,
1643         .lock = vfswrap_lock,
1644         .kernel_flock = vfswrap_kernel_flock,
1645         .linux_setlease = vfswrap_linux_setlease,
1646         .getlock = vfswrap_getlock,
1647         .symlink = vfswrap_symlink,
1648         .vfs_readlink = vfswrap_readlink,
1649         .link = vfswrap_link,
1650         .mknod = vfswrap_mknod,
1651         .realpath = vfswrap_realpath,
1652         .notify_watch = vfswrap_notify_watch,
1653         .chflags = vfswrap_chflags,
1654         .file_id_create = vfswrap_file_id_create,
1655         .streaminfo = vfswrap_streaminfo,
1656         .get_real_filename = vfswrap_get_real_filename,
1657         .connectpath = vfswrap_connectpath,
1658         .brl_lock_windows = vfswrap_brl_lock_windows,
1659         .brl_unlock_windows = vfswrap_brl_unlock_windows,
1660         .brl_cancel_windows = vfswrap_brl_cancel_windows,
1661         .strict_lock = vfswrap_strict_lock,
1662         .strict_unlock = vfswrap_strict_unlock,
1663
1664         /* NT ACL operations. */
1665
1666         .fget_nt_acl = vfswrap_fget_nt_acl,
1667         .get_nt_acl = vfswrap_get_nt_acl,
1668         .fset_nt_acl = vfswrap_fset_nt_acl,
1669
1670         /* POSIX ACL operations. */
1671
1672         .chmod_acl = vfswrap_chmod_acl,
1673         .fchmod_acl = vfswrap_fchmod_acl,
1674
1675         .sys_acl_get_entry = vfswrap_sys_acl_get_entry,
1676         .sys_acl_get_tag_type = vfswrap_sys_acl_get_tag_type,
1677         .sys_acl_get_permset = vfswrap_sys_acl_get_permset,
1678         .sys_acl_get_qualifier = vfswrap_sys_acl_get_qualifier,
1679         .sys_acl_get_file = vfswrap_sys_acl_get_file,
1680         .sys_acl_get_fd = vfswrap_sys_acl_get_fd,
1681         .sys_acl_clear_perms = vfswrap_sys_acl_clear_perms,
1682         .sys_acl_add_perm = vfswrap_sys_acl_add_perm,
1683         .sys_acl_to_text = vfswrap_sys_acl_to_text,
1684         .sys_acl_init = vfswrap_sys_acl_init,
1685         .sys_acl_create_entry = vfswrap_sys_acl_create_entry,
1686         .sys_acl_set_tag_type = vfswrap_sys_acl_set_tag_type,
1687         .sys_acl_set_qualifier = vfswrap_sys_acl_set_qualifier,
1688         .sys_acl_set_permset = vfswrap_sys_acl_set_permset,
1689         .sys_acl_valid = vfswrap_sys_acl_valid,
1690         .sys_acl_set_file = vfswrap_sys_acl_set_file,
1691         .sys_acl_set_fd = vfswrap_sys_acl_set_fd,
1692         .sys_acl_delete_def_file = vfswrap_sys_acl_delete_def_file,
1693         .sys_acl_get_perm = vfswrap_sys_acl_get_perm,
1694         .sys_acl_free_text = vfswrap_sys_acl_free_text,
1695         .sys_acl_free_acl = vfswrap_sys_acl_free_acl,
1696         .sys_acl_free_qualifier = vfswrap_sys_acl_free_qualifier,
1697
1698         /* EA operations. */
1699         .getxattr = vfswrap_getxattr,
1700         .lgetxattr = vfswrap_lgetxattr,
1701         .fgetxattr = vfswrap_fgetxattr,
1702         .listxattr = vfswrap_listxattr,
1703         .llistxattr = vfswrap_llistxattr,
1704         .flistxattr = vfswrap_flistxattr,
1705         .removexattr = vfswrap_removexattr,
1706         .lremovexattr = vfswrap_lremovexattr,
1707         .fremovexattr = vfswrap_fremovexattr,
1708         .setxattr = vfswrap_setxattr,
1709         .lsetxattr = vfswrap_lsetxattr,
1710         .fsetxattr = vfswrap_fsetxattr,
1711
1712         /* aio operations */
1713         .aio_read = vfswrap_aio_read,
1714         .aio_write = vfswrap_aio_write,
1715         .aio_return_fn = vfswrap_aio_return,
1716         .aio_cancel = vfswrap_aio_cancel,
1717         .aio_error_fn = vfswrap_aio_error,
1718         .aio_fsync = vfswrap_aio_fsync,
1719         .aio_suspend = vfswrap_aio_suspend,
1720         .aio_force = vfswrap_aio_force,
1721
1722         /* offline operations */
1723         .is_offline = vfswrap_is_offline,
1724         .set_offline = vfswrap_set_offline
1725 };
1726
1727 NTSTATUS vfs_default_init(void);
1728 NTSTATUS vfs_default_init(void)
1729 {
1730         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1731                                 DEFAULT_VFS_MODULE_NAME, &vfs_default_fns);
1732 }
1733
1734