s3: smbd: Change open_streams_for_delete() to take a struct smb_filename *.
[samba.git] / source3 / modules / vfs_time_audit.c
1 /*
2  * Time auditing VFS module for samba.  Log time taken for VFS call to syslog
3  * facility.
4  *
5  * Copyright (C) Abhidnya Chirmule <achirmul@in.ibm.com> 2009
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 /*
22  * This module implements logging for time taken for all Samba VFS operations.
23  *
24  * vfs objects = time_audit
25  */
26
27
28 #include "includes.h"
29 #include "smbd/smbd.h"
30 #include "ntioctl.h"
31 #include "lib/util/tevent_unix.h"
32 #include "lib/util/tevent_ntstatus.h"
33
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_VFS
36
37 static double audit_timeout;
38
39 static void smb_time_audit_log_msg(const char *syscallname, double elapsed,
40                                     const char *msg)
41 {
42         DEBUG(0, ("WARNING: VFS call \"%s\" took unexpectedly long "
43                   "(%.2f seconds) %s%s-- Validate that file and storage "
44                   "subsystems are operating normally\n", syscallname,
45                   elapsed, (msg != NULL) ? msg : "",
46                   (msg != NULL) ? " " : ""));
47 }
48
49 static void smb_time_audit_log(const char *syscallname, double elapsed)
50 {
51         smb_time_audit_log_msg(syscallname, elapsed, NULL);
52 }
53
54 static void smb_time_audit_log_fsp(const char *syscallname, double elapsed,
55                                    const struct files_struct *fsp)
56 {
57         char *base_name = NULL;
58         char *connectpath = NULL;
59         char *msg = NULL;
60
61         if (fsp == NULL) {
62                 smb_time_audit_log(syscallname, elapsed);
63                 return;
64         }
65         if (fsp->conn)
66                 connectpath = fsp->conn->connectpath;
67         if (fsp->fsp_name)
68                 base_name = fsp->fsp_name->base_name;
69
70         if (connectpath != NULL && base_name != NULL) {
71                 msg = talloc_asprintf(talloc_tos(), "filename = \"%s/%s\"",
72                                       connectpath, base_name);
73         } else if (connectpath != NULL && base_name == NULL) {
74                 msg = talloc_asprintf(talloc_tos(), "connectpath = \"%s\", "
75                                       "base_name = <NULL>",
76                                       connectpath);
77         } else if (connectpath == NULL && base_name != NULL) {
78                 msg = talloc_asprintf(talloc_tos(), "connectpath = <NULL>, "
79                                       "base_name = \"%s\"",
80                                       base_name);
81         } else { /* connectpath == NULL && base_name == NULL */
82                 msg = talloc_asprintf(talloc_tos(), "connectpath = <NULL>, "
83                                       "base_name = <NULL>");
84         }
85         smb_time_audit_log_msg(syscallname, elapsed, msg);
86         TALLOC_FREE(msg);
87 }
88
89 static void smb_time_audit_log_fname(const char *syscallname, double elapsed,
90                                     const char *fname)
91 {
92         char cwd[PATH_MAX];
93         char *msg = NULL;
94
95         if (getcwd(cwd, sizeof(cwd)) == NULL) {
96                 snprintf(cwd, sizeof(cwd), "<getcwd() error %d>", errno);
97         }
98         if (fname != NULL) {
99                 msg = talloc_asprintf(talloc_tos(),
100                                       "cwd = \"%s\", filename = \"%s\"",
101                                       cwd, fname);
102         } else {
103                 msg = talloc_asprintf(talloc_tos(),
104                                       "cwd = \"%s\", filename = <NULL>",
105                                       cwd);
106         }
107         smb_time_audit_log_msg(syscallname, elapsed, msg);
108         TALLOC_FREE(msg);
109 }
110
111 static void smb_time_audit_log_smb_fname(const char *syscallname, double elapsed,
112                                        const struct smb_filename *smb_fname)
113 {
114         if (smb_fname != NULL) {
115                 smb_time_audit_log_fname(syscallname, elapsed,
116                                          smb_fname->base_name);
117         } else {
118                 smb_time_audit_log_fname(syscallname, elapsed,
119                                          "smb_fname = <NULL>");
120         }
121 }
122
123 static int smb_time_audit_connect(vfs_handle_struct *handle,
124                                   const char *svc, const char *user)
125 {
126         int result;
127         struct timespec ts1,ts2;
128         double timediff;
129
130         if (!handle) {
131                 return -1;
132         }
133
134         clock_gettime_mono(&ts1);
135         result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
136         clock_gettime_mono(&ts2);
137         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
138         if (timediff > audit_timeout) {
139                 smb_time_audit_log_msg("connect", timediff, user);
140         }
141         return result;
142 }
143
144 static void smb_time_audit_disconnect(vfs_handle_struct *handle)
145 {
146         struct timespec ts1,ts2;
147         double timediff;
148
149         clock_gettime_mono(&ts1);
150         SMB_VFS_NEXT_DISCONNECT(handle);
151         clock_gettime_mono(&ts2);
152         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
153
154         if (timediff > audit_timeout) {
155                 smb_time_audit_log("disconnect", timediff);
156         }
157 }
158
159 static uint64_t smb_time_audit_disk_free(vfs_handle_struct *handle,
160                                          const char *path, uint64_t *bsize,
161                                          uint64_t *dfree, uint64_t *dsize)
162 {
163         uint64_t result;
164         struct timespec ts1,ts2;
165         double timediff;
166
167         clock_gettime_mono(&ts1);
168         result = SMB_VFS_NEXT_DISK_FREE(handle, path, bsize, dfree, dsize);
169         clock_gettime_mono(&ts2);
170         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
171
172         /* Don't have a reasonable notion of failure here */
173         if (timediff > audit_timeout) {
174                 smb_time_audit_log_fname("disk_free", timediff, path);
175         }
176
177         return result;
178 }
179
180 static int smb_time_audit_get_quota(struct vfs_handle_struct *handle,
181                                     const char *path, enum SMB_QUOTA_TYPE qtype,
182                                     unid_t id, SMB_DISK_QUOTA *qt)
183 {
184         int result;
185         struct timespec ts1,ts2;
186         double timediff;
187
188         clock_gettime_mono(&ts1);
189         result = SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, qt);
190         clock_gettime_mono(&ts2);
191         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
192
193         if (timediff > audit_timeout) {
194                 smb_time_audit_log("get_quota", timediff);
195         }
196         return result;
197 }
198
199 static int smb_time_audit_set_quota(struct vfs_handle_struct *handle,
200                                     enum SMB_QUOTA_TYPE qtype, unid_t id,
201                                     SMB_DISK_QUOTA *qt)
202 {
203         int result;
204         struct timespec ts1,ts2;
205         double timediff;
206
207         clock_gettime_mono(&ts1);
208         result = SMB_VFS_NEXT_SET_QUOTA(handle, qtype, id, qt);
209         clock_gettime_mono(&ts2);
210         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
211
212         if (timediff > audit_timeout) {
213                 smb_time_audit_log("set_quota", timediff);
214         }
215
216         return result;
217 }
218
219 static int smb_time_audit_get_shadow_copy_data(struct vfs_handle_struct *handle,
220                                                struct files_struct *fsp,
221                                                struct shadow_copy_data *shadow_copy_data,
222                                                bool labels)
223 {
224         int result;
225         struct timespec ts1,ts2;
226         double timediff;
227
228         clock_gettime_mono(&ts1);
229         result = SMB_VFS_NEXT_GET_SHADOW_COPY_DATA(handle, fsp,
230                                                    shadow_copy_data, labels);
231         clock_gettime_mono(&ts2);
232         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
233
234         if (timediff > audit_timeout) {
235                 smb_time_audit_log_fsp("get_shadow_copy_data", timediff, fsp);
236         }
237
238         return result;
239 }
240
241 static int smb_time_audit_statvfs(struct vfs_handle_struct *handle,
242                                   const char *path,
243                                   struct vfs_statvfs_struct *statbuf)
244 {
245         int result;
246         struct timespec ts1,ts2;
247         double timediff;
248
249         clock_gettime_mono(&ts1);
250         result = SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
251         clock_gettime_mono(&ts2);
252         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
253
254         if (timediff > audit_timeout) {
255                 smb_time_audit_log_fname("statvfs", timediff, path);
256         }
257
258         return result;
259 }
260
261 static uint32_t smb_time_audit_fs_capabilities(struct vfs_handle_struct *handle,
262                                                enum timestamp_set_resolution *p_ts_res)
263 {
264         uint32_t result;
265         struct timespec ts1,ts2;
266         double timediff;
267
268         clock_gettime_mono(&ts1);
269         result = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
270         clock_gettime_mono(&ts2);
271         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
272
273         if (timediff > audit_timeout) {
274                 smb_time_audit_log("fs_capabilities", timediff);
275         }
276
277         return result;
278 }
279
280 static NTSTATUS smb_time_audit_snap_check_path(struct vfs_handle_struct *handle,
281                                                TALLOC_CTX *mem_ctx,
282                                                const char *service_path,
283                                                char **base_volume)
284 {
285         NTSTATUS status;
286         struct timespec ts1,ts2;
287         double timediff;
288
289         clock_gettime_mono(&ts1);
290         status = SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx, service_path,
291                                               base_volume);
292         clock_gettime_mono(&ts2);
293         timediff = nsec_time_diff(&ts2, &ts1) * 1.0e-9;
294
295         if (timediff > audit_timeout) {
296                 smb_time_audit_log("snap_check_path", timediff);
297         }
298
299         return status;
300 }
301
302 static NTSTATUS smb_time_audit_snap_create(struct vfs_handle_struct *handle,
303                                            TALLOC_CTX *mem_ctx,
304                                            const char *base_volume,
305                                            time_t *tstamp,
306                                            bool rw,
307                                            char **base_path,
308                                            char **snap_path)
309 {
310         NTSTATUS status;
311         struct timespec ts1,ts2;
312         double timediff;
313
314         clock_gettime_mono(&ts1);
315         status = SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume, tstamp,
316                                           rw, base_path, snap_path);
317         clock_gettime_mono(&ts2);
318         timediff = nsec_time_diff(&ts2 ,&ts1) * 1.0e-9;
319
320         if (timediff > audit_timeout) {
321                 smb_time_audit_log("snap_create", timediff);
322         }
323
324         return status;
325 }
326
327 static NTSTATUS smb_time_audit_snap_delete(struct vfs_handle_struct *handle,
328                                            TALLOC_CTX *mem_ctx,
329                                            char *base_path,
330                                            char *snap_path)
331 {
332         NTSTATUS status;
333         struct timespec ts1,ts2;
334         double timediff;
335
336         clock_gettime_mono(&ts1);
337         status = SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx, base_path,
338                                           snap_path);
339         clock_gettime_mono(&ts2);
340         timediff = nsec_time_diff(&ts2, &ts1) * 1.0e-9;
341
342         if (timediff > audit_timeout) {
343                 smb_time_audit_log("snap_delete", timediff);
344         }
345
346         return status;
347 }
348
349 static DIR *smb_time_audit_opendir(vfs_handle_struct *handle,
350                                    const struct smb_filename *smb_fname,
351                                    const char *mask, uint32_t attr)
352 {
353         DIR *result;
354         struct timespec ts1,ts2;
355         double timediff;
356
357         clock_gettime_mono(&ts1);
358         result = SMB_VFS_NEXT_OPENDIR(handle, smb_fname, mask, attr);
359         clock_gettime_mono(&ts2);
360         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
361
362         if (timediff > audit_timeout) {
363                 smb_time_audit_log_smb_fname("opendir", timediff, smb_fname);
364         }
365
366         return result;
367 }
368
369 static DIR *smb_time_audit_fdopendir(vfs_handle_struct *handle,
370                                               files_struct *fsp,
371                                               const char *mask, uint32_t attr)
372 {
373         DIR *result;
374         struct timespec ts1,ts2;
375         double timediff;
376
377         clock_gettime_mono(&ts1);
378         result = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
379         clock_gettime_mono(&ts2);
380         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
381
382         if (timediff > audit_timeout) {
383                 smb_time_audit_log_fsp("fdopendir", timediff, fsp);
384         }
385
386         return result;
387 }
388
389 static struct dirent *smb_time_audit_readdir(vfs_handle_struct *handle,
390                                                  DIR *dirp,
391                                                  SMB_STRUCT_STAT *sbuf)
392 {
393         struct dirent *result;
394         struct timespec ts1,ts2;
395         double timediff;
396
397         clock_gettime_mono(&ts1);
398         result = SMB_VFS_NEXT_READDIR(handle, dirp, sbuf);
399         clock_gettime_mono(&ts2);
400         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
401
402         if (timediff > audit_timeout) {
403                 smb_time_audit_log("readdir", timediff);
404         }
405
406         return result;
407 }
408
409 static void smb_time_audit_seekdir(vfs_handle_struct *handle,
410                                    DIR *dirp, long offset)
411 {
412         struct timespec ts1,ts2;
413         double timediff;
414
415         clock_gettime_mono(&ts1);
416         SMB_VFS_NEXT_SEEKDIR(handle, dirp, offset);
417         clock_gettime_mono(&ts2);
418         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
419
420         if (timediff > audit_timeout) {
421                 smb_time_audit_log("seekdir", timediff);
422         }
423
424 }
425
426 static long smb_time_audit_telldir(vfs_handle_struct *handle,
427                                    DIR *dirp)
428 {
429         long result;
430         struct timespec ts1,ts2;
431         double timediff;
432
433         clock_gettime_mono(&ts1);
434         result = SMB_VFS_NEXT_TELLDIR(handle, dirp);
435         clock_gettime_mono(&ts2);
436         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
437
438         if (timediff > audit_timeout) {
439                 smb_time_audit_log("telldir", timediff);
440         }
441
442         return result;
443 }
444
445 static void smb_time_audit_rewinddir(vfs_handle_struct *handle,
446                                      DIR *dirp)
447 {
448         struct timespec ts1,ts2;
449         double timediff;
450
451         clock_gettime_mono(&ts1);
452         SMB_VFS_NEXT_REWINDDIR(handle, dirp);
453         clock_gettime_mono(&ts2);
454         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
455
456         if (timediff > audit_timeout) {
457                 smb_time_audit_log("rewinddir", timediff);
458         }
459
460 }
461
462 static int smb_time_audit_mkdir(vfs_handle_struct *handle,
463                                 const struct smb_filename *smb_fname,
464                                 mode_t mode)
465 {
466         int result;
467         struct timespec ts1,ts2;
468         double timediff;
469
470         clock_gettime_mono(&ts1);
471         result = SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
472         clock_gettime_mono(&ts2);
473         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
474
475         if (timediff > audit_timeout) {
476                 smb_time_audit_log_smb_fname("mkdir",
477                         timediff,
478                         smb_fname);
479         }
480
481         return result;
482 }
483
484 static int smb_time_audit_rmdir(vfs_handle_struct *handle,
485                                 const struct smb_filename *smb_fname)
486 {
487         int result;
488         struct timespec ts1,ts2;
489         double timediff;
490
491         clock_gettime_mono(&ts1);
492         result = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
493         clock_gettime_mono(&ts2);
494         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
495
496         if (timediff > audit_timeout) {
497                 smb_time_audit_log_smb_fname("rmdir",
498                         timediff,
499                         smb_fname);
500         }
501
502         return result;
503 }
504
505 static int smb_time_audit_closedir(vfs_handle_struct *handle,
506                                    DIR *dirp)
507 {
508         int result;
509         struct timespec ts1,ts2;
510         double timediff;
511
512         clock_gettime_mono(&ts1);
513         result = SMB_VFS_NEXT_CLOSEDIR(handle, dirp);
514         clock_gettime_mono(&ts2);
515         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
516
517         if (timediff > audit_timeout) {
518                 smb_time_audit_log("closedir", timediff);
519         }
520
521         return result;
522 }
523
524 static void smb_time_audit_init_search_op(vfs_handle_struct *handle,
525                                           DIR *dirp)
526 {
527         struct timespec ts1,ts2;
528         double timediff;
529
530         clock_gettime_mono(&ts1);
531         SMB_VFS_NEXT_INIT_SEARCH_OP(handle, dirp);
532         clock_gettime_mono(&ts2);
533         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
534
535         if (timediff > audit_timeout) {
536                 smb_time_audit_log("init_search_op", timediff);
537         }
538 }
539
540 static int smb_time_audit_open(vfs_handle_struct *handle,
541                                struct smb_filename *fname,
542                                files_struct *fsp,
543                                int flags, mode_t mode)
544 {
545         int result;
546         struct timespec ts1,ts2;
547         double timediff;
548
549         clock_gettime_mono(&ts1);
550         result = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
551         clock_gettime_mono(&ts2);
552         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
553
554         if (timediff > audit_timeout) {
555                 smb_time_audit_log_fsp("open", timediff, fsp);
556         }
557
558         return result;
559 }
560
561 static NTSTATUS smb_time_audit_create_file(vfs_handle_struct *handle,
562                                            struct smb_request *req,
563                                            uint16_t root_dir_fid,
564                                            struct smb_filename *fname,
565                                            uint32_t access_mask,
566                                            uint32_t share_access,
567                                            uint32_t create_disposition,
568                                            uint32_t create_options,
569                                            uint32_t file_attributes,
570                                            uint32_t oplock_request,
571                                            struct smb2_lease *lease,
572                                            uint64_t allocation_size,
573                                            uint32_t private_flags,
574                                            struct security_descriptor *sd,
575                                            struct ea_list *ea_list,
576                                            files_struct **result_fsp,
577                                            int *pinfo,
578                                            const struct smb2_create_blobs *in_context_blobs,
579                                            struct smb2_create_blobs *out_context_blobs)
580 {
581         NTSTATUS result;
582         struct timespec ts1,ts2;
583         double timediff;
584
585         clock_gettime_mono(&ts1);
586         result = SMB_VFS_NEXT_CREATE_FILE(
587                 handle,                                 /* handle */
588                 req,                                    /* req */
589                 root_dir_fid,                           /* root_dir_fid */
590                 fname,                                  /* fname */
591                 access_mask,                            /* access_mask */
592                 share_access,                           /* share_access */
593                 create_disposition,                     /* create_disposition*/
594                 create_options,                         /* create_options */
595                 file_attributes,                        /* file_attributes */
596                 oplock_request,                         /* oplock_request */
597                 lease,                                  /* lease */
598                 allocation_size,                        /* allocation_size */
599                 private_flags,
600                 sd,                                     /* sd */
601                 ea_list,                                /* ea_list */
602                 result_fsp,                             /* result */
603                 pinfo,
604                 in_context_blobs, out_context_blobs);   /* create context */
605         clock_gettime_mono(&ts2);
606         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
607
608         if (timediff > audit_timeout) {
609                 /*
610                  * can't use result_fsp this time, may have
611                  * invalid content causing smbd crash
612                  */
613                 smb_time_audit_log_smb_fname("create_file", timediff,
614                                            fname);
615         }
616
617         return result;
618 }
619
620 static int smb_time_audit_close(vfs_handle_struct *handle, files_struct *fsp)
621 {
622         int result;
623         struct timespec ts1,ts2;
624         double timediff;
625
626         clock_gettime_mono(&ts1);
627         result = SMB_VFS_NEXT_CLOSE(handle, fsp);
628         clock_gettime_mono(&ts2);
629         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
630
631         if (timediff > audit_timeout) {
632                 smb_time_audit_log_fsp("close", timediff, fsp);
633         }
634
635         return result;
636 }
637
638 static ssize_t smb_time_audit_read(vfs_handle_struct *handle,
639                                    files_struct *fsp, void *data, size_t n)
640 {
641         ssize_t result;
642         struct timespec ts1,ts2;
643         double timediff;
644
645         clock_gettime_mono(&ts1);
646         result = SMB_VFS_NEXT_READ(handle, fsp, data, n);
647         clock_gettime_mono(&ts2);
648         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
649
650         if (timediff > audit_timeout) {
651                 smb_time_audit_log_fsp("read", timediff, fsp);
652         }
653
654         return result;
655 }
656
657 static ssize_t smb_time_audit_pread(vfs_handle_struct *handle,
658                                     files_struct *fsp,
659                                     void *data, size_t n, off_t offset)
660 {
661         ssize_t result;
662         struct timespec ts1,ts2;
663         double timediff;
664
665         clock_gettime_mono(&ts1);
666         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
667         clock_gettime_mono(&ts2);
668         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
669
670         if (timediff > audit_timeout) {
671                 smb_time_audit_log_fsp("pread", timediff, fsp);
672         }
673
674         return result;
675 }
676
677 struct smb_time_audit_pread_state {
678         struct files_struct *fsp;
679         ssize_t ret;
680         struct vfs_aio_state vfs_aio_state;
681 };
682
683 static void smb_time_audit_pread_done(struct tevent_req *subreq);
684
685 static struct tevent_req *smb_time_audit_pread_send(
686         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
687         struct tevent_context *ev, struct files_struct *fsp,
688         void *data, size_t n, off_t offset)
689 {
690         struct tevent_req *req, *subreq;
691         struct smb_time_audit_pread_state *state;
692
693         req = tevent_req_create(mem_ctx, &state,
694                                 struct smb_time_audit_pread_state);
695         if (req == NULL) {
696                 return NULL;
697         }
698         state->fsp = fsp;
699
700         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
701                                          n, offset);
702         if (tevent_req_nomem(subreq, req)) {
703                 return tevent_req_post(req, ev);
704         }
705         tevent_req_set_callback(subreq, smb_time_audit_pread_done, req);
706         return req;
707 }
708
709 static void smb_time_audit_pread_done(struct tevent_req *subreq)
710 {
711         struct tevent_req *req = tevent_req_callback_data(
712                 subreq, struct tevent_req);
713         struct smb_time_audit_pread_state *state = tevent_req_data(
714                 req, struct smb_time_audit_pread_state);
715
716         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
717         TALLOC_FREE(subreq);
718         tevent_req_done(req);
719 }
720
721 static ssize_t smb_time_audit_pread_recv(struct tevent_req *req,
722                                          struct vfs_aio_state *vfs_aio_state)
723 {
724         struct smb_time_audit_pread_state *state = tevent_req_data(
725                 req, struct smb_time_audit_pread_state);
726         double timediff;
727
728         timediff = state->vfs_aio_state.duration * 1.0e-9;
729
730         if (timediff > audit_timeout) {
731                 smb_time_audit_log_fsp("async pread", timediff, state->fsp);
732         }
733
734         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
735                 return -1;
736         }
737         *vfs_aio_state = state->vfs_aio_state;
738         return state->ret;
739 }
740
741 static ssize_t smb_time_audit_write(vfs_handle_struct *handle,
742                                     files_struct *fsp,
743                                     const void *data, size_t n)
744 {
745         ssize_t result;
746         struct timespec ts1,ts2;
747         double timediff;
748
749         clock_gettime_mono(&ts1);
750         result = SMB_VFS_NEXT_WRITE(handle, fsp, data, n);
751         clock_gettime_mono(&ts2);
752         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
753
754         if (timediff > audit_timeout) {
755                 smb_time_audit_log_fsp("write", timediff, fsp);
756         }
757
758         return result;
759 }
760
761 static ssize_t smb_time_audit_pwrite(vfs_handle_struct *handle,
762                                      files_struct *fsp,
763                                      const void *data, size_t n,
764                                      off_t offset)
765 {
766         ssize_t result;
767         struct timespec ts1,ts2;
768         double timediff;
769
770         clock_gettime_mono(&ts1);
771         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
772         clock_gettime_mono(&ts2);
773         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
774
775         if (timediff > audit_timeout) {
776                 smb_time_audit_log_fsp("pwrite", timediff, fsp);
777         }
778
779         return result;
780 }
781
782 struct smb_time_audit_pwrite_state {
783         struct files_struct *fsp;
784         ssize_t ret;
785         struct vfs_aio_state vfs_aio_state;
786 };
787
788 static void smb_time_audit_pwrite_done(struct tevent_req *subreq);
789
790 static struct tevent_req *smb_time_audit_pwrite_send(
791         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
792         struct tevent_context *ev, struct files_struct *fsp,
793         const void *data, size_t n, off_t offset)
794 {
795         struct tevent_req *req, *subreq;
796         struct smb_time_audit_pwrite_state *state;
797
798         req = tevent_req_create(mem_ctx, &state,
799                                 struct smb_time_audit_pwrite_state);
800         if (req == NULL) {
801                 return NULL;
802         }
803         state->fsp = fsp;
804
805         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
806                                          n, offset);
807         if (tevent_req_nomem(subreq, req)) {
808                 return tevent_req_post(req, ev);
809         }
810         tevent_req_set_callback(subreq, smb_time_audit_pwrite_done, req);
811         return req;
812 }
813
814 static void smb_time_audit_pwrite_done(struct tevent_req *subreq)
815 {
816         struct tevent_req *req = tevent_req_callback_data(
817                 subreq, struct tevent_req);
818         struct smb_time_audit_pwrite_state *state = tevent_req_data(
819                 req, struct smb_time_audit_pwrite_state);
820
821         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
822         TALLOC_FREE(subreq);
823         tevent_req_done(req);
824 }
825
826 static ssize_t smb_time_audit_pwrite_recv(struct tevent_req *req,
827                                           struct vfs_aio_state *vfs_aio_state)
828 {
829         struct smb_time_audit_pwrite_state *state = tevent_req_data(
830                 req, struct smb_time_audit_pwrite_state);
831         double timediff;
832
833         timediff = state->vfs_aio_state.duration * 1.0e-9;
834
835         if (timediff > audit_timeout) {
836                 smb_time_audit_log_fsp("async pwrite", timediff, state->fsp);
837         }
838
839         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
840                 return -1;
841         }
842         *vfs_aio_state = state->vfs_aio_state;
843         return state->ret;
844 }
845
846 static off_t smb_time_audit_lseek(vfs_handle_struct *handle,
847                                       files_struct *fsp,
848                                       off_t offset, int whence)
849 {
850         off_t result;
851         struct timespec ts1,ts2;
852         double timediff;
853
854         clock_gettime_mono(&ts1);
855         result = SMB_VFS_NEXT_LSEEK(handle, fsp, offset, whence);
856         clock_gettime_mono(&ts2);
857         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
858
859         if (timediff > audit_timeout) {
860                 smb_time_audit_log_fsp("lseek", timediff, fsp);
861         }
862
863         return result;
864 }
865
866 static ssize_t smb_time_audit_sendfile(vfs_handle_struct *handle, int tofd,
867                                        files_struct *fromfsp,
868                                        const DATA_BLOB *hdr, off_t offset,
869                                        size_t n)
870 {
871         ssize_t result;
872         struct timespec ts1,ts2;
873         double timediff;
874
875         clock_gettime_mono(&ts1);
876         result = SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp, hdr, offset, n);
877         clock_gettime_mono(&ts2);
878         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
879
880         if (timediff > audit_timeout) {
881                 smb_time_audit_log_fsp("sendfile", timediff, fromfsp);
882         }
883
884         return result;
885 }
886
887 static ssize_t smb_time_audit_recvfile(vfs_handle_struct *handle, int fromfd,
888                                        files_struct *tofsp,
889                                        off_t offset,
890                                        size_t n)
891 {
892         ssize_t result;
893         struct timespec ts1,ts2;
894         double timediff;
895
896         clock_gettime_mono(&ts1);
897         result = SMB_VFS_NEXT_RECVFILE(handle, fromfd, tofsp, offset, n);
898         clock_gettime_mono(&ts2);
899         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
900
901         if (timediff > audit_timeout) {
902                 smb_time_audit_log_fsp("recvfile", timediff, tofsp);
903         }
904
905         return result;
906 }
907
908 static int smb_time_audit_rename(vfs_handle_struct *handle,
909                                  const struct smb_filename *oldname,
910                                  const struct smb_filename *newname)
911 {
912         int result;
913         struct timespec ts1,ts2;
914         double timediff;
915
916         clock_gettime_mono(&ts1);
917         result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
918         clock_gettime_mono(&ts2);
919         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
920
921         if (timediff > audit_timeout) {
922                 smb_time_audit_log_smb_fname("rename", timediff, newname);
923         }
924
925         return result;
926 }
927
928 static int smb_time_audit_fsync(vfs_handle_struct *handle, files_struct *fsp)
929 {
930         int result;
931         struct timespec ts1,ts2;
932         double timediff;
933
934         clock_gettime_mono(&ts1);
935         result = SMB_VFS_NEXT_FSYNC(handle, fsp);
936         clock_gettime_mono(&ts2);
937         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
938
939         if (timediff > audit_timeout) {
940                 smb_time_audit_log_fsp("fsync", timediff, fsp);
941         }
942
943         return result;
944 }
945
946 struct smb_time_audit_fsync_state {
947         struct files_struct *fsp;
948         int ret;
949         struct vfs_aio_state vfs_aio_state;
950 };
951
952 static void smb_time_audit_fsync_done(struct tevent_req *subreq);
953
954 static struct tevent_req *smb_time_audit_fsync_send(
955         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
956         struct tevent_context *ev, struct files_struct *fsp)
957 {
958         struct tevent_req *req, *subreq;
959         struct smb_time_audit_fsync_state *state;
960
961         req = tevent_req_create(mem_ctx, &state,
962                                 struct smb_time_audit_fsync_state);
963         if (req == NULL) {
964                 return NULL;
965         }
966         state->fsp = fsp;
967
968         subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
969         if (tevent_req_nomem(subreq, req)) {
970                 return tevent_req_post(req, ev);
971         }
972         tevent_req_set_callback(subreq, smb_time_audit_fsync_done, req);
973         return req;
974 }
975
976 static void smb_time_audit_fsync_done(struct tevent_req *subreq)
977 {
978         struct tevent_req *req = tevent_req_callback_data(
979                 subreq, struct tevent_req);
980         struct smb_time_audit_fsync_state *state = tevent_req_data(
981                 req, struct smb_time_audit_fsync_state);
982
983         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
984         TALLOC_FREE(subreq);
985         tevent_req_done(req);
986 }
987
988 static int smb_time_audit_fsync_recv(struct tevent_req *req,
989                                      struct vfs_aio_state *vfs_aio_state)
990 {
991         struct smb_time_audit_fsync_state *state = tevent_req_data(
992                 req, struct smb_time_audit_fsync_state);
993         double timediff;
994
995         timediff = state->vfs_aio_state.duration * 1.0e-9;
996
997         if (timediff > audit_timeout) {
998                 smb_time_audit_log_fsp("async fsync", timediff, state->fsp);
999         }
1000
1001         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1002                 return -1;
1003         }
1004         *vfs_aio_state = state->vfs_aio_state;
1005         return state->ret;
1006 }
1007
1008 static int smb_time_audit_stat(vfs_handle_struct *handle,
1009                                struct smb_filename *fname)
1010 {
1011         int result;
1012         struct timespec ts1,ts2;
1013         double timediff;
1014
1015         clock_gettime_mono(&ts1);
1016         result = SMB_VFS_NEXT_STAT(handle, fname);
1017         clock_gettime_mono(&ts2);
1018         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1019
1020         if (timediff > audit_timeout) {
1021                 smb_time_audit_log_smb_fname("stat", timediff, fname);
1022         }
1023
1024         return result;
1025 }
1026
1027 static int smb_time_audit_fstat(vfs_handle_struct *handle, files_struct *fsp,
1028                                 SMB_STRUCT_STAT *sbuf)
1029 {
1030         int result;
1031         struct timespec ts1,ts2;
1032         double timediff;
1033
1034         clock_gettime_mono(&ts1);
1035         result = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1036         clock_gettime_mono(&ts2);
1037         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1038
1039         if (timediff > audit_timeout) {
1040                 smb_time_audit_log_fsp("fstat", timediff, fsp);
1041         }
1042
1043         return result;
1044 }
1045
1046 static int smb_time_audit_lstat(vfs_handle_struct *handle,
1047                                 struct smb_filename *path)
1048 {
1049         int result;
1050         struct timespec ts1,ts2;
1051         double timediff;
1052
1053         clock_gettime_mono(&ts1);
1054         result = SMB_VFS_NEXT_LSTAT(handle, path);
1055         clock_gettime_mono(&ts2);
1056         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1057
1058         if (timediff > audit_timeout) {
1059                 smb_time_audit_log_smb_fname("lstat", timediff, path);
1060         }
1061
1062         return result;
1063 }
1064
1065 static uint64_t smb_time_audit_get_alloc_size(vfs_handle_struct *handle,
1066                                               files_struct *fsp,
1067                                               const SMB_STRUCT_STAT *sbuf)
1068 {
1069         uint64_t result;
1070         struct timespec ts1,ts2;
1071         double timediff;
1072
1073         clock_gettime_mono(&ts1);
1074         result = SMB_VFS_NEXT_GET_ALLOC_SIZE(handle, fsp, sbuf);
1075         clock_gettime_mono(&ts2);
1076         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1077
1078         if (timediff > audit_timeout) {
1079                 smb_time_audit_log_fsp("get_alloc_size", timediff, fsp);
1080         }
1081
1082         return result;
1083 }
1084
1085 static int smb_time_audit_unlink(vfs_handle_struct *handle,
1086                                  const struct smb_filename *path)
1087 {
1088         int result;
1089         struct timespec ts1,ts2;
1090         double timediff;
1091
1092         clock_gettime_mono(&ts1);
1093         result = SMB_VFS_NEXT_UNLINK(handle, path);
1094         clock_gettime_mono(&ts2);
1095         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1096
1097         if (timediff > audit_timeout) {
1098                 smb_time_audit_log_smb_fname("unlink", timediff, path);
1099         }
1100
1101         return result;
1102 }
1103
1104 static int smb_time_audit_chmod(vfs_handle_struct *handle,
1105                         const struct smb_filename *smb_fname,
1106                         mode_t mode)
1107 {
1108         int result;
1109         struct timespec ts1,ts2;
1110         double timediff;
1111
1112         clock_gettime_mono(&ts1);
1113         result = SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1114         clock_gettime_mono(&ts2);
1115         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1116
1117         if (timediff > audit_timeout) {
1118                 smb_time_audit_log_fname("chmod",
1119                         timediff,
1120                         smb_fname->base_name);
1121         }
1122
1123         return result;
1124 }
1125
1126 static int smb_time_audit_fchmod(vfs_handle_struct *handle, files_struct *fsp,
1127                                  mode_t mode)
1128 {
1129         int result;
1130         struct timespec ts1,ts2;
1131         double timediff;
1132
1133         clock_gettime_mono(&ts1);
1134         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1135         clock_gettime_mono(&ts2);
1136         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1137
1138         if (timediff > audit_timeout) {
1139                 smb_time_audit_log_fsp("fchmod", timediff, fsp);
1140         }
1141
1142         return result;
1143 }
1144
1145 static int smb_time_audit_chown(vfs_handle_struct *handle,
1146                         const struct smb_filename *smb_fname,
1147                         uid_t uid,
1148                         gid_t gid)
1149 {
1150         int result;
1151         struct timespec ts1,ts2;
1152         double timediff;
1153
1154         clock_gettime_mono(&ts1);
1155         result = SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
1156         clock_gettime_mono(&ts2);
1157         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1158
1159         if (timediff > audit_timeout) {
1160                 smb_time_audit_log_fname("chown",
1161                         timediff,
1162                         smb_fname->base_name);
1163         }
1164
1165         return result;
1166 }
1167
1168 static int smb_time_audit_fchown(vfs_handle_struct *handle, files_struct *fsp,
1169                                  uid_t uid, gid_t gid)
1170 {
1171         int result;
1172         struct timespec ts1,ts2;
1173         double timediff;
1174
1175         clock_gettime_mono(&ts1);
1176         result = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1177         clock_gettime_mono(&ts2);
1178         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1179
1180         if (timediff > audit_timeout) {
1181                 smb_time_audit_log_fsp("fchown", timediff, fsp);
1182         }
1183
1184         return result;
1185 }
1186
1187 static int smb_time_audit_lchown(vfs_handle_struct *handle,
1188                         const struct smb_filename *smb_fname,
1189                         uid_t uid,
1190                         gid_t gid)
1191 {
1192         int result;
1193         struct timespec ts1,ts2;
1194         double timediff;
1195
1196         clock_gettime_mono(&ts1);
1197         result = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1198         clock_gettime_mono(&ts2);
1199         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1200
1201         if (timediff > audit_timeout) {
1202                 smb_time_audit_log_fname("lchown",
1203                         timediff,
1204                         smb_fname->base_name);
1205         }
1206
1207         return result;
1208 }
1209
1210 static int smb_time_audit_chdir(vfs_handle_struct *handle, const char *path)
1211 {
1212         int result;
1213         struct timespec ts1,ts2;
1214         double timediff;
1215
1216         clock_gettime_mono(&ts1);
1217         result = SMB_VFS_NEXT_CHDIR(handle, path);
1218         clock_gettime_mono(&ts2);
1219         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1220
1221         if (timediff > audit_timeout) {
1222                 smb_time_audit_log_fname("chdir", timediff, path);
1223         }
1224
1225         return result;
1226 }
1227
1228 static char *smb_time_audit_getwd(vfs_handle_struct *handle)
1229 {
1230         char *result;
1231         struct timespec ts1,ts2;
1232         double timediff;
1233
1234         clock_gettime_mono(&ts1);
1235         result = SMB_VFS_NEXT_GETWD(handle);
1236         clock_gettime_mono(&ts2);
1237         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1238
1239         if (timediff > audit_timeout) {
1240                 smb_time_audit_log("getwd", timediff);
1241         }
1242
1243         return result;
1244 }
1245
1246 static int smb_time_audit_ntimes(vfs_handle_struct *handle,
1247                                  const struct smb_filename *path,
1248                                  struct smb_file_time *ft)
1249 {
1250         int result;
1251         struct timespec ts1,ts2;
1252         double timediff;
1253
1254         clock_gettime_mono(&ts1);
1255         result = SMB_VFS_NEXT_NTIMES(handle, path, ft);
1256         clock_gettime_mono(&ts2);
1257         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1258
1259         if (timediff > audit_timeout) {
1260                 smb_time_audit_log_smb_fname("ntimes", timediff, path);
1261         }
1262
1263         return result;
1264 }
1265
1266 static int smb_time_audit_ftruncate(vfs_handle_struct *handle,
1267                                     files_struct *fsp,
1268                                     off_t len)
1269 {
1270         int result;
1271         struct timespec ts1,ts2;
1272         double timediff;
1273
1274         clock_gettime_mono(&ts1);
1275         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1276         clock_gettime_mono(&ts2);
1277         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1278
1279         if (timediff > audit_timeout) {
1280                 smb_time_audit_log_fsp("ftruncate", timediff, fsp);
1281         }
1282
1283         return result;
1284 }
1285
1286 static int smb_time_audit_fallocate(vfs_handle_struct *handle,
1287                                     files_struct *fsp,
1288                                     uint32_t mode,
1289                                     off_t offset,
1290                                     off_t len)
1291 {
1292         int result;
1293         int saved_errno = 0;
1294         struct timespec ts1,ts2;
1295         double timediff;
1296
1297         clock_gettime_mono(&ts1);
1298         result = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1299         if (result == -1) {
1300                 saved_errno = errno;
1301         }
1302         clock_gettime_mono(&ts2);
1303         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1304
1305         if (timediff > audit_timeout) {
1306                 smb_time_audit_log_fsp("fallocate", timediff, fsp);
1307         }
1308         if (result == -1) {
1309                 errno = saved_errno;
1310         }
1311         return result;
1312 }
1313
1314 static bool smb_time_audit_lock(vfs_handle_struct *handle, files_struct *fsp,
1315                                 int op, off_t offset, off_t count,
1316                                 int type)
1317 {
1318         bool result;
1319         struct timespec ts1,ts2;
1320         double timediff;
1321
1322         clock_gettime_mono(&ts1);
1323         result = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1324         clock_gettime_mono(&ts2);
1325         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1326
1327         if (timediff > audit_timeout) {
1328                 smb_time_audit_log_fsp("lock", timediff, fsp);
1329         }
1330
1331         return result;
1332 }
1333
1334 static int smb_time_audit_kernel_flock(struct vfs_handle_struct *handle,
1335                                        struct files_struct *fsp,
1336                                        uint32_t share_mode, uint32_t access_mask)
1337 {
1338         int result;
1339         struct timespec ts1,ts2;
1340         double timediff;
1341
1342         clock_gettime_mono(&ts1);
1343         result = SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp, share_mode,
1344                                            access_mask);
1345         clock_gettime_mono(&ts2);
1346         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1347
1348         if (timediff > audit_timeout) {
1349                 smb_time_audit_log_fsp("kernel_flock", timediff, fsp);
1350         }
1351
1352         return result;
1353 }
1354
1355 static int smb_time_audit_linux_setlease(vfs_handle_struct *handle,
1356                                          files_struct *fsp,
1357                                          int leasetype)
1358 {
1359         int result;
1360         struct timespec ts1,ts2;
1361         double timediff;
1362
1363         clock_gettime_mono(&ts1);
1364         result = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1365         clock_gettime_mono(&ts2);
1366         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1367
1368         if (timediff > audit_timeout) {
1369                 smb_time_audit_log_fsp("linux_setlease", timediff, fsp);
1370         }
1371
1372         return result;
1373 }
1374
1375 static bool smb_time_audit_getlock(vfs_handle_struct *handle,
1376                                    files_struct *fsp,
1377                                    off_t *poffset, off_t *pcount,
1378                                    int *ptype, pid_t *ppid)
1379 {
1380         bool result;
1381         struct timespec ts1,ts2;
1382         double timediff;
1383
1384         clock_gettime_mono(&ts1);
1385         result = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype,
1386                                       ppid);
1387         clock_gettime_mono(&ts2);
1388         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1389
1390         if (timediff > audit_timeout) {
1391                 smb_time_audit_log_fsp("getlock", timediff, fsp);
1392         }
1393
1394         return result;
1395 }
1396
1397 static int smb_time_audit_symlink(vfs_handle_struct *handle,
1398                                   const char *oldpath, const char *newpath)
1399 {
1400         int result;
1401         struct timespec ts1,ts2;
1402         double timediff;
1403
1404         clock_gettime_mono(&ts1);
1405         result = SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
1406         clock_gettime_mono(&ts2);
1407         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1408
1409         if (timediff > audit_timeout) {
1410                 smb_time_audit_log_fname("symlink", timediff, newpath);
1411         }
1412
1413         return result;
1414 }
1415
1416 static int smb_time_audit_readlink(vfs_handle_struct *handle,
1417                           const char *path, char *buf, size_t bufsiz)
1418 {
1419         int result;
1420         struct timespec ts1,ts2;
1421         double timediff;
1422
1423         clock_gettime_mono(&ts1);
1424         result = SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
1425         clock_gettime_mono(&ts2);
1426         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1427
1428         if (timediff > audit_timeout) {
1429                 smb_time_audit_log_fname("readlink", timediff, path);
1430         }
1431
1432         return result;
1433 }
1434
1435 static int smb_time_audit_link(vfs_handle_struct *handle,
1436                                const char *oldpath, const char *newpath)
1437 {
1438         int result;
1439         struct timespec ts1,ts2;
1440         double timediff;
1441
1442         clock_gettime_mono(&ts1);
1443         result = SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
1444         clock_gettime_mono(&ts2);
1445         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1446
1447         if (timediff > audit_timeout) {
1448                 smb_time_audit_log_fname("link", timediff, newpath);
1449         }
1450
1451         return result;
1452 }
1453
1454 static int smb_time_audit_mknod(vfs_handle_struct *handle,
1455                                 const char *pathname, mode_t mode,
1456                                 SMB_DEV_T dev)
1457 {
1458         int result;
1459         struct timespec ts1,ts2;
1460         double timediff;
1461
1462         clock_gettime_mono(&ts1);
1463         result = SMB_VFS_NEXT_MKNOD(handle, pathname, mode, dev);
1464         clock_gettime_mono(&ts2);
1465         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1466
1467         if (timediff > audit_timeout) {
1468                 smb_time_audit_log_fname("mknod", timediff, pathname);
1469         }
1470
1471         return result;
1472 }
1473
1474 static char *smb_time_audit_realpath(vfs_handle_struct *handle,
1475                                      const char *path)
1476 {
1477         char *result;
1478         struct timespec ts1,ts2;
1479         double timediff;
1480
1481         clock_gettime_mono(&ts1);
1482         result = SMB_VFS_NEXT_REALPATH(handle, path);
1483         clock_gettime_mono(&ts2);
1484         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1485
1486         if (timediff > audit_timeout) {
1487                 smb_time_audit_log_fname("realpath", timediff, path);
1488         }
1489
1490         return result;
1491 }
1492
1493 static int smb_time_audit_chflags(vfs_handle_struct *handle,
1494                                   const char *path, unsigned int flags)
1495 {
1496         int result;
1497         struct timespec ts1,ts2;
1498         double timediff;
1499
1500         clock_gettime_mono(&ts1);
1501         result = SMB_VFS_NEXT_CHFLAGS(handle, path, flags);
1502         clock_gettime_mono(&ts2);
1503         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1504
1505         if (timediff > audit_timeout) {
1506                 smb_time_audit_log_fname("chflags", timediff, path);
1507         }
1508
1509         return result;
1510 }
1511
1512 static struct file_id smb_time_audit_file_id_create(struct vfs_handle_struct *handle,
1513                                                     const SMB_STRUCT_STAT *sbuf)
1514 {
1515         struct file_id id_zero;
1516         struct file_id result;
1517         struct timespec ts1,ts2;
1518         double timediff;
1519
1520         ZERO_STRUCT(id_zero);
1521
1522         clock_gettime_mono(&ts1);
1523         result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
1524         clock_gettime_mono(&ts2);
1525         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1526
1527         if (timediff > audit_timeout) {
1528                 smb_time_audit_log("file_id_create", timediff);
1529         }
1530
1531         return result;
1532 }
1533
1534 static NTSTATUS smb_time_audit_streaminfo(vfs_handle_struct *handle,
1535                                           struct files_struct *fsp,
1536                                           const struct smb_filename *smb_fname,
1537                                           TALLOC_CTX *mem_ctx,
1538                                           unsigned int *pnum_streams,
1539                                           struct stream_struct **pstreams)
1540 {
1541         NTSTATUS result;
1542         struct timespec ts1,ts2;
1543         double timediff;
1544
1545         clock_gettime_mono(&ts1);
1546         result = SMB_VFS_NEXT_STREAMINFO(handle, fsp, smb_fname, mem_ctx,
1547                                          pnum_streams, pstreams);
1548         clock_gettime_mono(&ts2);
1549         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1550
1551         if (timediff > audit_timeout) {
1552                 smb_time_audit_log_fsp("streaminfo", timediff, fsp);
1553         }
1554
1555         return result;
1556 }
1557
1558 static int smb_time_audit_get_real_filename(struct vfs_handle_struct *handle,
1559                                             const char *path,
1560                                             const char *name,
1561                                             TALLOC_CTX *mem_ctx,
1562                                             char **found_name)
1563 {
1564         int result;
1565         struct timespec ts1,ts2;
1566         double timediff;
1567
1568         clock_gettime_mono(&ts1);
1569         result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
1570                                                 found_name);
1571         clock_gettime_mono(&ts2);
1572         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1573
1574         if (timediff > audit_timeout) {
1575                 smb_time_audit_log_fname("get_real_filename", timediff, path);
1576         }
1577
1578         return result;
1579 }
1580
1581 static const char *smb_time_audit_connectpath(vfs_handle_struct *handle,
1582                                               const char *fname)
1583 {
1584         const char *result;
1585         struct timespec ts1,ts2;
1586         double timediff;
1587
1588         clock_gettime_mono(&ts1);
1589         result = SMB_VFS_NEXT_CONNECTPATH(handle, fname);
1590         clock_gettime_mono(&ts2);
1591         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1592
1593         if (timediff > audit_timeout) {
1594                 smb_time_audit_log_fname("connectpath", timediff, fname);
1595         }
1596
1597         return result;
1598 }
1599
1600 static NTSTATUS smb_time_audit_brl_lock_windows(struct vfs_handle_struct *handle,
1601                                                 struct byte_range_lock *br_lck,
1602                                                 struct lock_struct *plock,
1603                                                 bool blocking_lock)
1604 {
1605         NTSTATUS result;
1606         struct timespec ts1,ts2;
1607         double timediff;
1608
1609         clock_gettime_mono(&ts1);
1610         result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock,
1611                                                blocking_lock);
1612         clock_gettime_mono(&ts2);
1613         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1614
1615         if (timediff > audit_timeout) {
1616                 smb_time_audit_log_fsp("brl_lock_windows", timediff,
1617                                        brl_fsp(br_lck));
1618         }
1619
1620         return result;
1621 }
1622
1623 static bool smb_time_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
1624                                               struct messaging_context *msg_ctx,
1625                                               struct byte_range_lock *br_lck,
1626                                               const struct lock_struct *plock)
1627 {
1628         bool result;
1629         struct timespec ts1,ts2;
1630         double timediff;
1631
1632         clock_gettime_mono(&ts1);
1633         result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, msg_ctx, br_lck,
1634                                                  plock);
1635         clock_gettime_mono(&ts2);
1636         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1637
1638         if (timediff > audit_timeout) {
1639                 smb_time_audit_log_fsp("brl_unlock_windows", timediff,
1640                                        brl_fsp(br_lck));
1641         }
1642
1643         return result;
1644 }
1645
1646 static bool smb_time_audit_brl_cancel_windows(struct vfs_handle_struct *handle,
1647                                               struct byte_range_lock *br_lck,
1648                                               struct lock_struct *plock)
1649 {
1650         bool result;
1651         struct timespec ts1,ts2;
1652         double timediff;
1653
1654         clock_gettime_mono(&ts1);
1655         result = SMB_VFS_NEXT_BRL_CANCEL_WINDOWS(handle, br_lck, plock);
1656         clock_gettime_mono(&ts2);
1657         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1658
1659         if (timediff > audit_timeout) {
1660                 smb_time_audit_log_fsp("brl_cancel_windows", timediff,
1661                                        brl_fsp(br_lck));
1662         }
1663
1664         return result;
1665 }
1666
1667 static bool smb_time_audit_strict_lock(struct vfs_handle_struct *handle,
1668                                        struct files_struct *fsp,
1669                                        struct lock_struct *plock)
1670 {
1671         bool result;
1672         struct timespec ts1,ts2;
1673         double timediff;
1674
1675         clock_gettime_mono(&ts1);
1676         result = SMB_VFS_NEXT_STRICT_LOCK(handle, fsp, plock);
1677         clock_gettime_mono(&ts2);
1678         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1679
1680         if (timediff > audit_timeout) {
1681                 smb_time_audit_log_fsp("strict_lock", timediff, fsp);
1682         }
1683
1684         return result;
1685 }
1686
1687 static void smb_time_audit_strict_unlock(struct vfs_handle_struct *handle,
1688                                          struct files_struct *fsp,
1689                                          struct lock_struct *plock)
1690 {
1691         struct timespec ts1,ts2;
1692         double timediff;
1693
1694         clock_gettime_mono(&ts1);
1695         SMB_VFS_NEXT_STRICT_UNLOCK(handle, fsp, plock);
1696         clock_gettime_mono(&ts2);
1697         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1698
1699         if (timediff > audit_timeout) {
1700                 smb_time_audit_log_fsp("strict_unlock", timediff, fsp);
1701         }
1702 }
1703
1704 static NTSTATUS smb_time_audit_translate_name(struct vfs_handle_struct *handle,
1705                                               const char *name,
1706                                               enum vfs_translate_direction direction,
1707                                               TALLOC_CTX *mem_ctx,
1708                                               char **mapped_name)
1709 {
1710         NTSTATUS result;
1711         struct timespec ts1,ts2;
1712         double timediff;
1713
1714         clock_gettime_mono(&ts1);
1715         result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
1716                                              mapped_name);
1717         clock_gettime_mono(&ts2);
1718         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1719
1720         if (timediff > audit_timeout) {
1721                 smb_time_audit_log_fname("translate_name", timediff, name);
1722         }
1723
1724         return result;
1725 }
1726
1727 struct time_audit_cc_state {
1728         struct timespec ts_send;
1729         struct vfs_handle_struct *handle;
1730         off_t copied;
1731 };
1732 static void smb_time_audit_copy_chunk_done(struct tevent_req *subreq);
1733
1734 static struct tevent_req *smb_time_audit_copy_chunk_send(struct vfs_handle_struct *handle,
1735                                                          TALLOC_CTX *mem_ctx,
1736                                                          struct tevent_context *ev,
1737                                                          struct files_struct *src_fsp,
1738                                                          off_t src_off,
1739                                                          struct files_struct *dest_fsp,
1740                                                          off_t dest_off,
1741                                                          off_t num)
1742 {
1743         struct tevent_req *req;
1744         struct tevent_req *subreq;
1745         struct time_audit_cc_state *cc_state;
1746
1747         req = tevent_req_create(mem_ctx, &cc_state, struct time_audit_cc_state);
1748         if (req == NULL) {
1749                 return NULL;
1750         }
1751
1752         cc_state->handle = handle;
1753         clock_gettime_mono(&cc_state->ts_send);
1754         subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle, cc_state, ev,
1755                                               src_fsp, src_off,
1756                                               dest_fsp, dest_off, num);
1757         if (tevent_req_nomem(subreq, req)) {
1758                 return tevent_req_post(req, ev);
1759         }
1760
1761         tevent_req_set_callback(subreq, smb_time_audit_copy_chunk_done, req);
1762         return req;
1763 }
1764
1765 static void smb_time_audit_copy_chunk_done(struct tevent_req *subreq)
1766 {
1767         struct tevent_req *req = tevent_req_callback_data(
1768                 subreq, struct tevent_req);
1769         struct time_audit_cc_state *cc_state
1770                         = tevent_req_data(req, struct time_audit_cc_state);
1771         NTSTATUS status;
1772
1773         status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
1774                                               subreq,
1775                                               &cc_state->copied);
1776         TALLOC_FREE(subreq);
1777         if (tevent_req_nterror(req, status)) {
1778                 return;
1779         }
1780         tevent_req_done(req);
1781 }
1782
1783 static NTSTATUS smb_time_audit_copy_chunk_recv(struct vfs_handle_struct *handle,
1784                                                struct tevent_req *req,
1785                                                off_t *copied)
1786 {
1787         struct time_audit_cc_state *cc_state
1788                         = tevent_req_data(req, struct time_audit_cc_state);
1789         struct timespec ts_recv;
1790         double timediff;
1791         NTSTATUS status;
1792
1793         clock_gettime_mono(&ts_recv);
1794         timediff = nsec_time_diff(&ts_recv, &cc_state->ts_send)*1.0e-9;
1795         if (timediff > audit_timeout) {
1796                 smb_time_audit_log("copy_chunk", timediff);
1797         }
1798
1799         *copied = cc_state->copied;
1800         if (tevent_req_is_nterror(req, &status)) {
1801                 tevent_req_received(req);
1802                 return status;
1803         }
1804
1805         tevent_req_received(req);
1806         return NT_STATUS_OK;
1807 }
1808
1809 static NTSTATUS smb_time_audit_get_compression(vfs_handle_struct *handle,
1810                                                TALLOC_CTX *mem_ctx,
1811                                                struct files_struct *fsp,
1812                                                struct smb_filename *smb_fname,
1813                                                uint16_t *_compression_fmt)
1814 {
1815         NTSTATUS result;
1816         struct timespec ts1,ts2;
1817         double timediff;
1818
1819         clock_gettime_mono(&ts1);
1820         result = SMB_VFS_NEXT_GET_COMPRESSION(handle, mem_ctx, fsp, smb_fname,
1821                                               _compression_fmt);
1822         clock_gettime_mono(&ts2);
1823         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1824
1825         if (timediff > audit_timeout) {
1826                 if (fsp !=  NULL) {
1827                         smb_time_audit_log_fsp("get_compression",
1828                                                timediff, fsp);
1829                 } else {
1830                         smb_time_audit_log_smb_fname("get_compression",
1831                                                      timediff, smb_fname);
1832                 }
1833         }
1834
1835         return result;
1836 }
1837
1838 static NTSTATUS smb_time_audit_set_compression(vfs_handle_struct *handle,
1839                                                TALLOC_CTX *mem_ctx,
1840                                                struct files_struct *fsp,
1841                                                uint16_t compression_fmt)
1842 {
1843         NTSTATUS result;
1844         struct timespec ts1,ts2;
1845         double timediff;
1846
1847         clock_gettime_mono(&ts1);
1848         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
1849                                               compression_fmt);
1850         clock_gettime_mono(&ts2);
1851         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1852
1853         if (timediff > audit_timeout) {
1854                 smb_time_audit_log_fsp("set_compression", timediff, fsp);
1855         }
1856
1857         return result;
1858 }
1859
1860 static NTSTATUS smb_time_audit_readdir_attr(struct vfs_handle_struct *handle,
1861                                             const struct smb_filename *fname,
1862                                             TALLOC_CTX *mem_ctx,
1863                                             struct readdir_attr_data **pattr_data)
1864 {
1865         NTSTATUS status;
1866         struct timespec ts1,ts2;
1867         double timediff;
1868
1869         clock_gettime_mono(&ts1);
1870         status = SMB_VFS_NEXT_READDIR_ATTR(handle, fname, mem_ctx, pattr_data);
1871         clock_gettime_mono(&ts2);
1872         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1873
1874         if (timediff > audit_timeout) {
1875                 smb_time_audit_log_smb_fname("readdir_attr", timediff, fname);
1876         }
1877
1878         return status;
1879 }
1880
1881 static NTSTATUS smb_time_audit_fget_nt_acl(vfs_handle_struct *handle,
1882                                            files_struct *fsp,
1883                                            uint32_t security_info,
1884                                            TALLOC_CTX *mem_ctx,
1885                                            struct security_descriptor **ppdesc)
1886 {
1887         NTSTATUS result;
1888         struct timespec ts1,ts2;
1889         double timediff;
1890
1891         clock_gettime_mono(&ts1);
1892         result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1893                                           mem_ctx, ppdesc);
1894         clock_gettime_mono(&ts2);
1895         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1896
1897         if (timediff > audit_timeout) {
1898                 smb_time_audit_log_fsp("fget_nt_acl", timediff, fsp);
1899         }
1900
1901         return result;
1902 }
1903
1904 static NTSTATUS smb_time_audit_get_nt_acl(vfs_handle_struct *handle,
1905                                           const struct smb_filename *smb_fname,
1906                                           uint32_t security_info,
1907                                           TALLOC_CTX *mem_ctx,
1908                                           struct security_descriptor **ppdesc)
1909 {
1910         NTSTATUS result;
1911         struct timespec ts1,ts2;
1912         double timediff;
1913
1914         clock_gettime_mono(&ts1);
1915         result = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
1916                                          mem_ctx, ppdesc);
1917         clock_gettime_mono(&ts2);
1918         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1919
1920         if (timediff > audit_timeout) {
1921                 smb_time_audit_log_fname("get_nt_acl",
1922                         timediff,
1923                         smb_fname->base_name);
1924         }
1925
1926         return result;
1927 }
1928
1929 static NTSTATUS smb_time_audit_fset_nt_acl(vfs_handle_struct *handle,
1930                                            files_struct *fsp,
1931                                            uint32_t security_info_sent,
1932                                            const struct security_descriptor *psd)
1933 {
1934         NTSTATUS result;
1935         struct timespec ts1,ts2;
1936         double timediff;
1937
1938         clock_gettime_mono(&ts1);
1939         result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent,
1940                                           psd);
1941         clock_gettime_mono(&ts2);
1942         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1943
1944         if (timediff > audit_timeout) {
1945                 smb_time_audit_log_fsp("fset_nt_acl", timediff, fsp);
1946         }
1947
1948         return result;
1949 }
1950
1951 static int smb_time_audit_chmod_acl(vfs_handle_struct *handle,
1952                         const struct smb_filename *smb_fname,
1953                         mode_t mode)
1954 {
1955         int result;
1956         struct timespec ts1,ts2;
1957         double timediff;
1958
1959         clock_gettime_mono(&ts1);
1960         result = SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1961         clock_gettime_mono(&ts2);
1962         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1963
1964         if (timediff > audit_timeout) {
1965                 smb_time_audit_log_fname("chmod_acl",
1966                         timediff,
1967                         smb_fname->base_name);
1968         }
1969
1970         return result;
1971 }
1972
1973 static int smb_time_audit_fchmod_acl(vfs_handle_struct *handle,
1974                                      files_struct *fsp, mode_t mode)
1975 {
1976         int result;
1977         struct timespec ts1,ts2;
1978         double timediff;
1979
1980         clock_gettime_mono(&ts1);
1981         result = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1982         clock_gettime_mono(&ts2);
1983         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
1984
1985         if (timediff > audit_timeout) {
1986                 smb_time_audit_log_fsp("fchmod_acl", timediff, fsp);
1987         }
1988
1989         return result;
1990 }
1991
1992 static SMB_ACL_T smb_time_audit_sys_acl_get_file(vfs_handle_struct *handle,
1993                                                  const char *path_p,
1994                                                  SMB_ACL_TYPE_T type,
1995                                                  TALLOC_CTX *mem_ctx)
1996 {
1997         SMB_ACL_T result;
1998         struct timespec ts1,ts2;
1999         double timediff;
2000
2001         clock_gettime_mono(&ts1);
2002         result = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p, type, mem_ctx);
2003         clock_gettime_mono(&ts2);
2004         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2005
2006         if (timediff > audit_timeout) {
2007                 smb_time_audit_log_fname("sys_acl_get_file", timediff, path_p);
2008         }
2009
2010         return result;
2011 }
2012
2013 static SMB_ACL_T smb_time_audit_sys_acl_get_fd(vfs_handle_struct *handle,
2014                                                files_struct *fsp,
2015                                                TALLOC_CTX *mem_ctx)
2016 {
2017         SMB_ACL_T result;
2018         struct timespec ts1,ts2;
2019         double timediff;
2020
2021         clock_gettime_mono(&ts1);
2022         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
2023         clock_gettime_mono(&ts2);
2024         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2025
2026         if (timediff > audit_timeout) {
2027                 smb_time_audit_log_fsp("sys_acl_get_fd", timediff, fsp);
2028         }
2029
2030         return result;
2031 }
2032
2033
2034 static int smb_time_audit_sys_acl_blob_get_file(vfs_handle_struct *handle,
2035                                                 const char *path_p,
2036                                                 TALLOC_CTX *mem_ctx, 
2037                                                 char **blob_description,
2038                                                 DATA_BLOB *blob)
2039 {
2040         int result;
2041         struct timespec ts1,ts2;
2042         double timediff;
2043
2044         clock_gettime_mono(&ts1);
2045         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle, path_p, mem_ctx, blob_description, blob);
2046         clock_gettime_mono(&ts2);
2047         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2048
2049         if (timediff > audit_timeout) {
2050                 smb_time_audit_log("sys_acl_blob_get_file", timediff);
2051         }
2052
2053         return result;
2054 }
2055
2056 static int smb_time_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
2057                                               files_struct *fsp,
2058                                               TALLOC_CTX *mem_ctx, 
2059                                               char **blob_description,
2060                                               DATA_BLOB *blob)
2061 {
2062         int result;
2063         struct timespec ts1,ts2;
2064         double timediff;
2065
2066         clock_gettime_mono(&ts1);
2067         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
2068         clock_gettime_mono(&ts2);
2069         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2070
2071         if (timediff > audit_timeout) {
2072                 smb_time_audit_log("sys_acl_blob_get_fd", timediff);
2073         }
2074
2075         return result;
2076 }
2077
2078 static int smb_time_audit_sys_acl_set_file(vfs_handle_struct *handle,
2079                                            const char *name,
2080                                            SMB_ACL_TYPE_T acltype,
2081                                            SMB_ACL_T theacl)
2082 {
2083         int result;
2084         struct timespec ts1,ts2;
2085         double timediff;
2086
2087         clock_gettime_mono(&ts1);
2088         result = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name, acltype,
2089                                                theacl);
2090         clock_gettime_mono(&ts2);
2091         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2092
2093         if (timediff > audit_timeout) {
2094                 smb_time_audit_log_fname("sys_acl_set_file", timediff, name);
2095         }
2096
2097         return result;
2098 }
2099
2100 static int smb_time_audit_sys_acl_set_fd(vfs_handle_struct *handle,
2101                                          files_struct *fsp,
2102                                          SMB_ACL_T theacl)
2103 {
2104         int result;
2105         struct timespec ts1,ts2;
2106         double timediff;
2107
2108         clock_gettime_mono(&ts1);
2109         result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
2110         clock_gettime_mono(&ts2);
2111         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2112
2113         if (timediff > audit_timeout) {
2114                 smb_time_audit_log_fsp("sys_acl_set_fd", timediff, fsp);
2115         }
2116
2117         return result;
2118 }
2119
2120 static int smb_time_audit_sys_acl_delete_def_file(vfs_handle_struct *handle,
2121                                                   const char *path)
2122 {
2123         int result;
2124         struct timespec ts1,ts2;
2125         double timediff;
2126
2127         clock_gettime_mono(&ts1);
2128         result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
2129         clock_gettime_mono(&ts2);
2130         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2131
2132         if (timediff > audit_timeout) {
2133                 smb_time_audit_log_fname("sys_acl_delete_def_file", timediff, path);
2134         }
2135
2136         return result;
2137 }
2138
2139 static ssize_t smb_time_audit_getxattr(struct vfs_handle_struct *handle,
2140                                        const char *path, const char *name,
2141                                        void *value, size_t size)
2142 {
2143         ssize_t result;
2144         struct timespec ts1,ts2;
2145         double timediff;
2146
2147         clock_gettime_mono(&ts1);
2148         result = SMB_VFS_NEXT_GETXATTR(handle, path, name, value, size);
2149         clock_gettime_mono(&ts2);
2150         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2151
2152         if (timediff > audit_timeout) {
2153                 smb_time_audit_log_fname("getxattr", timediff, path);
2154         }
2155
2156         return result;
2157 }
2158
2159 static ssize_t smb_time_audit_fgetxattr(struct vfs_handle_struct *handle,
2160                                         struct files_struct *fsp,
2161                                         const char *name, void *value,
2162                                         size_t size)
2163 {
2164         ssize_t result;
2165         struct timespec ts1,ts2;
2166         double timediff;
2167
2168         clock_gettime_mono(&ts1);
2169         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
2170         clock_gettime_mono(&ts2);
2171         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2172
2173         if (timediff > audit_timeout) {
2174                 smb_time_audit_log_fsp("fgetxattr", timediff, fsp);
2175         }
2176
2177         return result;
2178 }
2179
2180 static ssize_t smb_time_audit_listxattr(struct vfs_handle_struct *handle,
2181                                         const char *path, char *list,
2182                                         size_t size)
2183 {
2184         ssize_t result;
2185         struct timespec ts1,ts2;
2186         double timediff;
2187
2188         clock_gettime_mono(&ts1);
2189         result = SMB_VFS_NEXT_LISTXATTR(handle, path, list, size);
2190         clock_gettime_mono(&ts2);
2191         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2192
2193         if (timediff > audit_timeout) {
2194                 smb_time_audit_log_fname("listxattr", timediff, path);
2195         }
2196
2197         return result;
2198 }
2199
2200 static ssize_t smb_time_audit_flistxattr(struct vfs_handle_struct *handle,
2201                                          struct files_struct *fsp, char *list,
2202                                          size_t size)
2203 {
2204         ssize_t result;
2205         struct timespec ts1,ts2;
2206         double timediff;
2207
2208         clock_gettime_mono(&ts1);
2209         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
2210         clock_gettime_mono(&ts2);
2211         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2212
2213         if (timediff > audit_timeout) {
2214                 smb_time_audit_log_fsp("flistxattr", timediff, fsp);
2215         }
2216
2217         return result;
2218 }
2219
2220 static int smb_time_audit_removexattr(struct vfs_handle_struct *handle,
2221                                       const char *path, const char *name)
2222 {
2223         int result;
2224         struct timespec ts1,ts2;
2225         double timediff;
2226
2227         clock_gettime_mono(&ts1);
2228         result = SMB_VFS_NEXT_REMOVEXATTR(handle, path, name);
2229         clock_gettime_mono(&ts2);
2230         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2231
2232         if (timediff > audit_timeout) {
2233                 smb_time_audit_log_fname("removexattr", timediff, path);
2234         }
2235
2236         return result;
2237 }
2238
2239 static int smb_time_audit_fremovexattr(struct vfs_handle_struct *handle,
2240                                        struct files_struct *fsp,
2241                                        const char *name)
2242 {
2243         int result;
2244         struct timespec ts1,ts2;
2245         double timediff;
2246
2247         clock_gettime_mono(&ts1);
2248         result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2249         clock_gettime_mono(&ts2);
2250         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2251
2252         if (timediff > audit_timeout) {
2253                 smb_time_audit_log_fsp("fremovexattr", timediff, fsp);
2254         }
2255
2256         return result;
2257 }
2258
2259 static int smb_time_audit_setxattr(struct vfs_handle_struct *handle,
2260                                    const char *path, const char *name,
2261                                    const void *value, size_t size,
2262                                    int flags)
2263 {
2264         int result;
2265         struct timespec ts1,ts2;
2266         double timediff;
2267
2268         clock_gettime_mono(&ts1);
2269         result = SMB_VFS_NEXT_SETXATTR(handle, path, name, value, size,
2270                                        flags);
2271         clock_gettime_mono(&ts2);
2272         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2273
2274         if (timediff > audit_timeout) {
2275                 smb_time_audit_log_fname("setxattr", timediff, path);
2276         }
2277
2278         return result;
2279 }
2280
2281 static int smb_time_audit_fsetxattr(struct vfs_handle_struct *handle,
2282                                     struct files_struct *fsp, const char *name,
2283                                     const void *value, size_t size, int flags)
2284 {
2285         int result;
2286         struct timespec ts1,ts2;
2287         double timediff;
2288
2289         clock_gettime_mono(&ts1);
2290         result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2291         clock_gettime_mono(&ts2);
2292         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2293
2294         if (timediff > audit_timeout) {
2295                 smb_time_audit_log_fsp("fsetxattr", timediff, fsp);
2296         }
2297
2298         return result;
2299 }
2300
2301 static bool smb_time_audit_aio_force(struct vfs_handle_struct *handle,
2302                                      struct files_struct *fsp)
2303 {
2304         bool result;
2305         struct timespec ts1,ts2;
2306         double timediff;
2307
2308         clock_gettime_mono(&ts1);
2309         result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2310         clock_gettime_mono(&ts2);
2311         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2312
2313         if (timediff > audit_timeout) {
2314                 smb_time_audit_log_fsp("aio_force", timediff, fsp);
2315         }
2316
2317         return result;
2318 }
2319
2320 static bool smb_time_audit_is_offline(struct vfs_handle_struct *handle,
2321                                       const struct smb_filename *fname,
2322                                       SMB_STRUCT_STAT *sbuf)
2323 {
2324         bool result;
2325         struct timespec ts1,ts2;
2326         double timediff;
2327
2328         clock_gettime_mono(&ts1);
2329         result = SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
2330         clock_gettime_mono(&ts2);
2331         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2332
2333         if (timediff > audit_timeout) {
2334                 smb_time_audit_log_smb_fname("is_offline", timediff, fname);
2335         }
2336
2337         return result;
2338 }
2339
2340 static int smb_time_audit_set_offline(struct vfs_handle_struct *handle,
2341                                       const struct smb_filename *fname)
2342 {
2343         int result;
2344         struct timespec ts1,ts2;
2345         double timediff;
2346
2347         clock_gettime_mono(&ts1);
2348         result = SMB_VFS_NEXT_SET_OFFLINE(handle, fname);
2349         clock_gettime_mono(&ts2);
2350         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2351
2352         if (timediff > audit_timeout) {
2353                 smb_time_audit_log_smb_fname("set_offline", timediff, fname);
2354         }
2355
2356         return result;
2357 }
2358
2359 static NTSTATUS smb_time_audit_durable_cookie(struct vfs_handle_struct *handle,
2360                                               struct files_struct *fsp,
2361                                               TALLOC_CTX *mem_ctx,
2362                                               DATA_BLOB *cookie)
2363 {
2364         NTSTATUS result;
2365         struct timespec ts1,ts2;
2366         double timediff;
2367
2368         clock_gettime_mono(&ts1);
2369         result = SMB_VFS_NEXT_DURABLE_COOKIE(handle, fsp, mem_ctx, cookie);
2370         clock_gettime_mono(&ts2);
2371         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2372
2373         if (timediff > audit_timeout) {
2374                 smb_time_audit_log_fsp("durable_cookie", timediff, fsp);
2375         }
2376
2377         return result;
2378 }
2379
2380 static NTSTATUS smb_time_audit_durable_disconnect(struct vfs_handle_struct *handle,
2381                                                   struct files_struct *fsp,
2382                                                   const DATA_BLOB old_cookie,
2383                                                   TALLOC_CTX *mem_ctx,
2384                                                   DATA_BLOB *new_cookie)
2385 {
2386         NTSTATUS result;
2387         struct timespec ts1,ts2;
2388         double timediff;
2389
2390         clock_gettime_mono(&ts1);
2391         result = SMB_VFS_NEXT_DURABLE_DISCONNECT(handle, fsp, old_cookie,
2392                                                  mem_ctx, new_cookie);
2393         clock_gettime_mono(&ts2);
2394         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2395
2396         if (timediff > audit_timeout) {
2397                 smb_time_audit_log_fsp("durable_disconnect", timediff, fsp);
2398         }
2399
2400         return result;
2401 }
2402
2403 static NTSTATUS smb_time_audit_durable_reconnect(struct vfs_handle_struct *handle,
2404                                                  struct smb_request *smb1req,
2405                                                  struct smbXsrv_open *op,
2406                                                  const DATA_BLOB old_cookie,
2407                                                  TALLOC_CTX *mem_ctx,
2408                                                  struct files_struct **fsp,
2409                                                  DATA_BLOB *new_cookie)
2410 {
2411         NTSTATUS result;
2412         struct timespec ts1,ts2;
2413         double timediff;
2414
2415         clock_gettime_mono(&ts1);
2416         result = SMB_VFS_NEXT_DURABLE_RECONNECT(handle, smb1req, op, old_cookie,
2417                                                 mem_ctx, fsp, new_cookie);
2418         clock_gettime_mono(&ts2);
2419         timediff = nsec_time_diff(&ts2,&ts1)*1.0e-9;
2420
2421         if (timediff > audit_timeout) {
2422                 smb_time_audit_log("durable_reconnect", timediff);
2423         }
2424
2425         return result;
2426 }
2427
2428 /* VFS operations */
2429
2430 static struct vfs_fn_pointers vfs_time_audit_fns = {
2431         .connect_fn = smb_time_audit_connect,
2432         .disconnect_fn = smb_time_audit_disconnect,
2433         .disk_free_fn = smb_time_audit_disk_free,
2434         .get_quota_fn = smb_time_audit_get_quota,
2435         .set_quota_fn = smb_time_audit_set_quota,
2436         .get_shadow_copy_data_fn = smb_time_audit_get_shadow_copy_data,
2437         .statvfs_fn = smb_time_audit_statvfs,
2438         .fs_capabilities_fn = smb_time_audit_fs_capabilities,
2439         .snap_check_path_fn = smb_time_audit_snap_check_path,
2440         .snap_create_fn = smb_time_audit_snap_create,
2441         .snap_delete_fn = smb_time_audit_snap_delete,
2442         .opendir_fn = smb_time_audit_opendir,
2443         .fdopendir_fn = smb_time_audit_fdopendir,
2444         .readdir_fn = smb_time_audit_readdir,
2445         .seekdir_fn = smb_time_audit_seekdir,
2446         .telldir_fn = smb_time_audit_telldir,
2447         .rewind_dir_fn = smb_time_audit_rewinddir,
2448         .mkdir_fn = smb_time_audit_mkdir,
2449         .rmdir_fn = smb_time_audit_rmdir,
2450         .closedir_fn = smb_time_audit_closedir,
2451         .init_search_op_fn = smb_time_audit_init_search_op,
2452         .open_fn = smb_time_audit_open,
2453         .create_file_fn = smb_time_audit_create_file,
2454         .close_fn = smb_time_audit_close,
2455         .read_fn = smb_time_audit_read,
2456         .pread_fn = smb_time_audit_pread,
2457         .pread_send_fn = smb_time_audit_pread_send,
2458         .pread_recv_fn = smb_time_audit_pread_recv,
2459         .write_fn = smb_time_audit_write,
2460         .pwrite_fn = smb_time_audit_pwrite,
2461         .pwrite_send_fn = smb_time_audit_pwrite_send,
2462         .pwrite_recv_fn = smb_time_audit_pwrite_recv,
2463         .lseek_fn = smb_time_audit_lseek,
2464         .sendfile_fn = smb_time_audit_sendfile,
2465         .recvfile_fn = smb_time_audit_recvfile,
2466         .rename_fn = smb_time_audit_rename,
2467         .fsync_fn = smb_time_audit_fsync,
2468         .fsync_send_fn = smb_time_audit_fsync_send,
2469         .fsync_recv_fn = smb_time_audit_fsync_recv,
2470         .stat_fn = smb_time_audit_stat,
2471         .fstat_fn = smb_time_audit_fstat,
2472         .lstat_fn = smb_time_audit_lstat,
2473         .get_alloc_size_fn = smb_time_audit_get_alloc_size,
2474         .unlink_fn = smb_time_audit_unlink,
2475         .chmod_fn = smb_time_audit_chmod,
2476         .fchmod_fn = smb_time_audit_fchmod,
2477         .chown_fn = smb_time_audit_chown,
2478         .fchown_fn = smb_time_audit_fchown,
2479         .lchown_fn = smb_time_audit_lchown,
2480         .chdir_fn = smb_time_audit_chdir,
2481         .getwd_fn = smb_time_audit_getwd,
2482         .ntimes_fn = smb_time_audit_ntimes,
2483         .ftruncate_fn = smb_time_audit_ftruncate,
2484         .fallocate_fn = smb_time_audit_fallocate,
2485         .lock_fn = smb_time_audit_lock,
2486         .kernel_flock_fn = smb_time_audit_kernel_flock,
2487         .linux_setlease_fn = smb_time_audit_linux_setlease,
2488         .getlock_fn = smb_time_audit_getlock,
2489         .symlink_fn = smb_time_audit_symlink,
2490         .readlink_fn = smb_time_audit_readlink,
2491         .link_fn = smb_time_audit_link,
2492         .mknod_fn = smb_time_audit_mknod,
2493         .realpath_fn = smb_time_audit_realpath,
2494         .chflags_fn = smb_time_audit_chflags,
2495         .file_id_create_fn = smb_time_audit_file_id_create,
2496         .streaminfo_fn = smb_time_audit_streaminfo,
2497         .get_real_filename_fn = smb_time_audit_get_real_filename,
2498         .connectpath_fn = smb_time_audit_connectpath,
2499         .brl_lock_windows_fn = smb_time_audit_brl_lock_windows,
2500         .brl_unlock_windows_fn = smb_time_audit_brl_unlock_windows,
2501         .brl_cancel_windows_fn = smb_time_audit_brl_cancel_windows,
2502         .strict_lock_fn = smb_time_audit_strict_lock,
2503         .strict_unlock_fn = smb_time_audit_strict_unlock,
2504         .translate_name_fn = smb_time_audit_translate_name,
2505         .copy_chunk_send_fn = smb_time_audit_copy_chunk_send,
2506         .copy_chunk_recv_fn = smb_time_audit_copy_chunk_recv,
2507         .get_compression_fn = smb_time_audit_get_compression,
2508         .set_compression_fn = smb_time_audit_set_compression,
2509         .readdir_attr_fn = smb_time_audit_readdir_attr,
2510         .fget_nt_acl_fn = smb_time_audit_fget_nt_acl,
2511         .get_nt_acl_fn = smb_time_audit_get_nt_acl,
2512         .fset_nt_acl_fn = smb_time_audit_fset_nt_acl,
2513         .chmod_acl_fn = smb_time_audit_chmod_acl,
2514         .fchmod_acl_fn = smb_time_audit_fchmod_acl,
2515         .sys_acl_get_file_fn = smb_time_audit_sys_acl_get_file,
2516         .sys_acl_get_fd_fn = smb_time_audit_sys_acl_get_fd,
2517         .sys_acl_blob_get_file_fn = smb_time_audit_sys_acl_blob_get_file,
2518         .sys_acl_blob_get_fd_fn = smb_time_audit_sys_acl_blob_get_fd,
2519         .sys_acl_set_file_fn = smb_time_audit_sys_acl_set_file,
2520         .sys_acl_set_fd_fn = smb_time_audit_sys_acl_set_fd,
2521         .sys_acl_delete_def_file_fn = smb_time_audit_sys_acl_delete_def_file,
2522         .getxattr_fn = smb_time_audit_getxattr,
2523         .fgetxattr_fn = smb_time_audit_fgetxattr,
2524         .listxattr_fn = smb_time_audit_listxattr,
2525         .flistxattr_fn = smb_time_audit_flistxattr,
2526         .removexattr_fn = smb_time_audit_removexattr,
2527         .fremovexattr_fn = smb_time_audit_fremovexattr,
2528         .setxattr_fn = smb_time_audit_setxattr,
2529         .fsetxattr_fn = smb_time_audit_fsetxattr,
2530         .aio_force_fn = smb_time_audit_aio_force,
2531         .is_offline_fn = smb_time_audit_is_offline,
2532         .set_offline_fn = smb_time_audit_set_offline,
2533         .durable_cookie_fn = smb_time_audit_durable_cookie,
2534         .durable_disconnect_fn = smb_time_audit_durable_disconnect,
2535         .durable_reconnect_fn = smb_time_audit_durable_reconnect,
2536 };
2537
2538
2539 NTSTATUS vfs_time_audit_init(void);
2540 NTSTATUS vfs_time_audit_init(void)
2541 {
2542         audit_timeout = (double)lp_parm_int(-1, "time_audit", "timeout",
2543                                             10000) / 1000.0;
2544         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "time_audit",
2545                                 &vfs_time_audit_fns);
2546 }