Add an optional SMB_STRUCT_SMB parameter to VFS_OP_READDIR
[nivanova/samba-autobuild/.git] / source3 / modules / onefs_streams.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Support for OneFS Alternate Data Streams
5  *
6  * Copyright (C) Tim Prouty, 2008
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "onefs.h"
23 #include <sys/isi_enc.h>
24
25 /*
26  * OneFS stores streams without the explicit :$DATA at the end, so this strips
27  * it off.  All onefs_stream functions must call through this instead of
28  * split_ntfs_stream_name directly.
29  */
30 NTSTATUS onefs_split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
31                                       char **pbase, char **pstream)
32 {
33         NTSTATUS status;
34         char *stream;
35
36         status = split_ntfs_stream_name(mem_ctx, fname, pbase, pstream);
37         if (!NT_STATUS_IS_OK(status)) {
38                 return status;
39         }
40
41         /* Default $DATA stream.  */
42         if (pstream == NULL || *pstream == NULL) {
43                 return NT_STATUS_OK;
44         }
45
46         /* Strip off the $DATA. */
47         stream = strrchr_m(*pstream, ':');
48         SMB_ASSERT(stream);
49         stream[0] = '\0';
50
51         return NT_STATUS_OK;
52 }
53
54 int onefs_is_stream(const char *path, char **pbase, char **pstream,
55                     bool *is_stream)
56 {
57         (*is_stream) = is_ntfs_stream_name(path);
58
59         if (!(*is_stream)) {
60                 return 0;
61         }
62
63         if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
64                                                           pbase, pstream))) {
65                 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
66                 errno = ENOMEM;
67                 return -1;
68         }
69
70         return 0;
71 }
72
73 int onefs_close(vfs_handle_struct *handle, struct files_struct *fsp)
74 {
75         int ret2, ret = 0;
76
77         if (fsp->base_fsp) {
78                 ret = SMB_VFS_NEXT_CLOSE(handle, fsp->base_fsp);
79         }
80         ret2 = SMB_VFS_NEXT_CLOSE(handle, fsp);
81
82         return ret ? ret : ret2;
83 }
84
85 /*
86  * Get the ADS directory fd for a file.
87  */
88 static int get_stream_dir_fd(connection_struct *conn, const char *base,
89                              int *base_fdp)
90 {
91         int base_fd;
92         int dir_fd;
93         int saved_errno;
94
95         /* If a valid base_fdp was given, use it. */
96         if (base_fdp && *base_fdp >= 0) {
97                 base_fd = *base_fdp;
98         } else {
99                 base_fd = onefs_sys_create_file(conn,
100                                                 -1,
101                                                 base,
102                                                 0,
103                                                 0,
104                                                 0,
105                                                 0,
106                                                 0,
107                                                 0,
108                                                 INTERNAL_OPEN_ONLY,
109                                                 0,
110                                                 NULL,
111                                                 0,
112                                                 NULL);
113                 if (base_fd < 0) {
114                         return -1;
115                 }
116         }
117
118         /* Open the ADS directory. */
119         dir_fd = onefs_sys_create_file(conn,
120                                         base_fd,
121                                         ".",
122                                         0,
123                                         FILE_READ_DATA,
124                                         0,
125                                         0,
126                                         0,
127                                         0,
128                                         INTERNAL_OPEN_ONLY,
129                                         0,
130                                         NULL,
131                                         0,
132                                         NULL);
133
134         /* Close base_fd if it's not need or on error. */
135         if (!base_fdp || dir_fd < 0) {
136                 saved_errno = errno;
137                 close(base_fd);
138                 errno = saved_errno;
139         }
140
141         /* Set the out base_fdp if successful and it was requested. */
142         if (base_fdp && dir_fd >= 0) {
143                 *base_fdp = base_fd;
144         }
145
146         return dir_fd;
147 }
148
149 int onefs_rename(vfs_handle_struct *handle, const char *oldname,
150                  const char *newname)
151 {
152         TALLOC_CTX *frame = NULL;
153         int ret = -1;
154         int dir_fd = -1;
155         int saved_errno;
156         bool old_is_stream;
157         bool new_is_stream;
158         char *obase = NULL;
159         char *osname = NULL;
160         char *nbase = NULL;
161         char *nsname = NULL;
162
163         frame = talloc_stackframe();
164
165         ret = onefs_is_stream(oldname, &obase, &osname, &old_is_stream);
166         if (ret)
167                 return ret;
168
169         ret = onefs_is_stream(newname, &nbase, &nsname, &new_is_stream);
170         if (ret)
171                 return ret;
172
173         if (!old_is_stream && !new_is_stream) {
174                 return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
175         }
176
177         dir_fd = get_stream_dir_fd(handle->conn, obase, NULL);
178         if (dir_fd < -1) {
179                 goto done;
180         }
181
182         DEBUG(8,("onefs_rename called for %s : %s  => %s : %s\n",
183                 obase, osname,  nbase, nsname));
184
185         /* Handle rename of stream to default stream specially. */
186         if (nsname == NULL) {
187                 ret = enc_renameat(dir_fd, osname, ENC_DEFAULT, AT_FDCWD,
188                                    nbase, ENC_DEFAULT);
189         } else {
190                 ret = enc_renameat(dir_fd, osname, ENC_DEFAULT, dir_fd, nsname,
191                                    ENC_DEFAULT);
192         }
193
194  done:
195         saved_errno = errno;
196         if (dir_fd >= 0) {
197                 close(dir_fd);
198         }
199         errno = saved_errno;
200         TALLOC_FREE(frame);
201         return ret;
202 }
203
204 /*
205  * Merge a base file's sbuf into the a streams's sbuf.
206  */
207 static void merge_stat(SMB_STRUCT_STAT *stream_sbuf,
208                        const SMB_STRUCT_STAT *base_sbuf)
209 {
210         int dos_flags = (UF_DOS_NOINDEX | UF_DOS_ARCHIVE |
211             UF_DOS_HIDDEN | UF_DOS_RO | UF_DOS_SYSTEM);
212         stream_sbuf->st_mtime = base_sbuf->st_mtime;
213         stream_sbuf->st_ctime = base_sbuf->st_ctime;
214         stream_sbuf->st_atime = base_sbuf->st_atime;
215         stream_sbuf->st_flags &= ~dos_flags;
216         stream_sbuf->st_flags |= base_sbuf->st_flags & dos_flags;
217 }
218
219 /* fake timestamps */
220 static void onefs_adjust_stat_time(vfs_handle_struct *handle, const char *fname,
221                                    SMB_STRUCT_STAT *sbuf)
222 {
223         struct onefs_vfs_config cfg;
224         struct timeval tv_now = {0, 0};
225         bool static_mtime = False;
226         bool static_atime = False;
227
228         if (!onefs_get_config(SNUM(handle->conn),
229                               ONEFS_VFS_CONFIG_FAKETIMESTAMPS, &cfg)) {
230                 return;
231         }
232
233         if (IS_MTIME_STATIC_PATH(handle->conn, &cfg, fname)) {
234                 sbuf->st_mtime = sbuf->st_birthtime;
235                 static_mtime = True;
236         }
237         if (IS_ATIME_STATIC_PATH(handle->conn, &cfg, fname)) {
238                 sbuf->st_atime = sbuf->st_birthtime;
239                 static_atime = True;
240         }
241
242         if (IS_CTIME_NOW_PATH(handle->conn, &cfg, fname)) {
243                 if (cfg.ctime_slop < 0) {
244                         sbuf->st_birthtime = INT_MAX - 1;
245                 } else {
246                         GetTimeOfDay(&tv_now);
247                         sbuf->st_birthtime = tv_now.tv_sec + cfg.ctime_slop;
248                 }
249         }
250
251         if (!static_mtime && IS_MTIME_NOW_PATH(handle->conn,&cfg,fname)) {
252                 if (cfg.mtime_slop < 0) {
253                         sbuf->st_mtime = INT_MAX - 1;
254                 } else {
255                         if (tv_now.tv_sec == 0)
256                                 GetTimeOfDay(&tv_now);
257                         sbuf->st_mtime = tv_now.tv_sec + cfg.mtime_slop;
258                 }
259         }
260         if (!static_atime && IS_ATIME_NOW_PATH(handle->conn,&cfg,fname)) {
261                 if (cfg.atime_slop < 0) {
262                         sbuf->st_atime = INT_MAX - 1;
263                 } else {
264                         if (tv_now.tv_sec == 0)
265                                 GetTimeOfDay(&tv_now);
266                         sbuf->st_atime = tv_now.tv_sec + cfg.atime_slop;
267                 }
268         }
269 }
270
271 static int stat_stream(vfs_handle_struct *handle, const char *base,
272                        const char *stream, SMB_STRUCT_STAT *sbuf, int flags)
273 {
274         SMB_STRUCT_STAT base_sbuf;
275         int base_fd = -1, dir_fd, ret, saved_errno;
276
277         dir_fd = get_stream_dir_fd(handle->conn, base, &base_fd);
278         if (dir_fd < 0) {
279                 return -1;
280         }
281
282         /* Stat the stream. */
283         ret = enc_fstatat(dir_fd, stream, ENC_DEFAULT, sbuf, flags);
284         if (ret != -1) {
285                 /* Now stat the base file and merge the results. */
286                 ret = sys_fstat(base_fd, &base_sbuf);
287                 if (ret != -1) {
288                         merge_stat(sbuf, &base_sbuf);
289                 }
290         }
291
292         saved_errno = errno;
293         close(dir_fd);
294         close(base_fd);
295         errno = saved_errno;
296         return ret;
297 }
298
299 int onefs_stat(vfs_handle_struct *handle, const char *path,
300                SMB_STRUCT_STAT *sbuf)
301 {
302         int ret;
303         bool is_stream;
304         char *base = NULL;
305         char *stream = NULL;
306
307         ret = onefs_is_stream(path, &base, &stream, &is_stream);
308         if (ret)
309                 return ret;
310
311         if (!is_stream) {
312                 ret = SMB_VFS_NEXT_STAT(handle, path, sbuf);
313         } else if (!stream) {
314                 /* If it's the ::$DATA stream just stat the base file name. */
315                 ret = SMB_VFS_NEXT_STAT(handle, base, sbuf);
316         } else {
317                 ret = stat_stream(handle, base, stream, sbuf, 0);
318         }
319
320         onefs_adjust_stat_time(handle, path, sbuf);
321         return ret;
322 }
323
324 int onefs_fstat(vfs_handle_struct *handle, struct files_struct *fsp,
325                 SMB_STRUCT_STAT *sbuf)
326 {
327         SMB_STRUCT_STAT base_sbuf;
328         int ret;
329
330         /* Stat the stream, by calling next_fstat on the stream's fd. */
331         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
332         if (ret == -1) {
333                 return ret;
334         }
335
336         /* Stat the base file and merge the results. */
337         if (fsp != NULL && fsp->base_fsp != NULL) {
338                 ret = sys_fstat(fsp->base_fsp->fh->fd, &base_sbuf);
339                 if (ret != -1) {
340                         merge_stat(sbuf, &base_sbuf);
341                 }
342         }
343
344         onefs_adjust_stat_time(handle, fsp->fsp_name, sbuf);
345         return ret;
346 }
347
348 int onefs_lstat(vfs_handle_struct *handle, const char *path,
349                 SMB_STRUCT_STAT *sbuf)
350 {
351         int ret;
352         bool is_stream;
353         char *base = NULL;
354         char *stream = NULL;
355
356         ret = onefs_is_stream(path, &base, &stream, &is_stream);
357         if (ret)
358                 return ret;
359
360         if (!is_stream) {
361                 ret = SMB_VFS_NEXT_LSTAT(handle, path, sbuf);
362         } else if (!stream) {
363                 /* If it's the ::$DATA stream just stat the base file name. */
364                 ret = SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
365         } else {
366                 ret = stat_stream(handle, base, stream, sbuf,
367                                   AT_SYMLINK_NOFOLLOW);
368         }
369
370         onefs_adjust_stat_time(handle, path, sbuf);
371         return ret;
372 }
373
374 int onefs_unlink(vfs_handle_struct *handle, const char *path)
375 {
376         int ret;
377         bool is_stream;
378         char *base = NULL;
379         char *stream = NULL;
380         int dir_fd, saved_errno;
381
382         ret = onefs_is_stream(path, &base, &stream, &is_stream);
383         if (ret) {
384                 return ret;
385         }
386
387         if (!is_stream) {
388                 return SMB_VFS_NEXT_UNLINK(handle, path);
389         }
390
391         /* If it's the ::$DATA stream just unlink the base file name. */
392         if (!stream) {
393                 return SMB_VFS_NEXT_UNLINK(handle, base);
394         }
395
396         dir_fd = get_stream_dir_fd(handle->conn, base, NULL);
397         if (dir_fd < 0) {
398                 return -1;
399         }
400
401         ret = enc_unlinkat(dir_fd, stream, ENC_DEFAULT, 0);
402
403         saved_errno = errno;
404         close(dir_fd);
405         errno = saved_errno;
406         return ret;
407 }
408
409 int onefs_vtimes_streams(vfs_handle_struct *handle, const char *fname,
410                          int flags, struct timespec times[3])
411 {
412         int ret;
413         bool is_stream;
414         char *base;
415         char *stream;
416         int dirfd;
417         int saved_errno;
418
419         START_PROFILE(syscall_ntimes);
420
421         ret = onefs_is_stream(fname, &base, &stream, &is_stream);
422         if (ret)
423                 return ret;
424
425         if (!is_stream) {
426                 ret = vtimes(fname, times, flags);
427                 return ret;
428         }
429
430         dirfd = get_stream_dir_fd(handle->conn, base, NULL);
431         if (dirfd < -1) {
432                 return -1;
433         }
434
435         ret = enc_vtimesat(dirfd, stream, ENC_DEFAULT, times, flags);
436
437         END_PROFILE(syscall_ntimes);
438
439         saved_errno = errno;
440         close(dirfd);
441         errno = saved_errno;
442         return ret;
443 }
444
445 int onefs_chflags(vfs_handle_struct *handle, const char *path,
446                   unsigned int flags)
447 {
448         char *base = NULL;
449         char *stream = NULL;
450
451         if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
452                                                           &base, &stream))) {
453                 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
454                 errno = ENOMEM;
455                 return -1;
456         }
457
458         /*
459          * Only set the attributes on the base file.  ifs_createfile handles
460          * file creation attribute semantics.
461          */
462         return SMB_VFS_NEXT_CHFLAGS(handle, base, flags);
463 }
464
465 /*
466  * Streaminfo enumeration functionality
467  */
468 struct streaminfo_state {
469         TALLOC_CTX *mem_ctx;
470         vfs_handle_struct *handle;
471         unsigned int num_streams;
472         struct stream_struct *streams;
473         NTSTATUS status;
474 };
475
476 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
477                            struct stream_struct **streams,
478                            const char *name, SMB_OFF_T size,
479                            SMB_OFF_T alloc_size)
480 {
481         struct stream_struct *tmp;
482
483         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
484                                    (*num_streams)+1);
485         if (tmp == NULL) {
486                 return false;
487         }
488
489         tmp[*num_streams].name = talloc_asprintf(mem_ctx, ":%s:%s", name,
490                                                  "$DATA");
491         if (tmp[*num_streams].name == NULL) {
492                 return false;
493         }
494
495         tmp[*num_streams].size = size;
496         tmp[*num_streams].alloc_size = alloc_size;
497
498         *streams = tmp;
499         *num_streams += 1;
500         return true;
501 }
502
503 static NTSTATUS walk_onefs_streams(connection_struct *conn, files_struct *fsp,
504                                    const char *fname,
505                                    struct streaminfo_state *state,
506                                    SMB_STRUCT_STAT *base_sbuf)
507 {
508         NTSTATUS status = NT_STATUS_OK;
509         bool opened_base_fd = false;
510         int base_fd = -1;
511         int dir_fd = -1;
512         int stream_fd = -1;
513         int ret;
514         SMB_STRUCT_DIR *dirp = NULL;
515         SMB_STRUCT_DIRENT *dp = NULL;
516         files_struct fake_fs;
517         struct fd_handle fake_fh;
518         SMB_STRUCT_STAT stream_sbuf;
519
520         ZERO_STRUCT(fake_fh);
521         ZERO_STRUCT(fake_fs);
522
523         /* If the base file is already open, use its fd. */
524         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
525                 base_fd = fsp->fh->fd;
526         } else {
527                 opened_base_fd = true;
528         }
529
530         dir_fd = get_stream_dir_fd(conn, fname, &base_fd);
531         if (dir_fd < 0) {
532                 return map_nt_error_from_unix(errno);
533         }
534
535         /* Open the ADS directory. */
536         if ((dirp = fdopendir(dir_fd)) == NULL) {
537                 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
538                           fname, errno, strerror(errno)));
539                 status = map_nt_error_from_unix(errno);
540                 goto out;
541         }
542
543         fake_fs.conn = conn;
544         fake_fs.fh = &fake_fh;
545         fake_fs.fsp_name = SMB_STRDUP(fname);
546
547         /* Iterate over the streams in the ADS directory. */
548         while ((dp = SMB_VFS_READDIR(conn, dirp, NULL)) != NULL) {
549                 /* Skip the "." and ".." entries */
550                 if ((strcmp(dp->d_name, ".") == 0) ||
551                     (strcmp(dp->d_name, "..") == 0))
552                         continue;
553
554                 /* Open actual stream */
555                 if ((stream_fd = onefs_sys_create_file(conn,
556                                                          base_fd,
557                                                          dp->d_name,
558                                                          0,
559                                                          0,
560                                                          0,
561                                                          0,
562                                                          0,
563                                                          0,
564                                                          INTERNAL_OPEN_ONLY,
565                                                          0,
566                                                          NULL,
567                                                          0,
568                                                          NULL)) == -1) {
569                         DEBUG(0, ("Error opening stream %s:%s. "
570                                   "errno=%d (%s)\n", fname, dp->d_name, errno,
571                                   strerror(errno)));
572                         continue;
573                 }
574
575                 /* Figure out the stat info. */
576                 fake_fh.fd = stream_fd;
577                 ret = SMB_VFS_FSTAT(&fake_fs, &stream_sbuf);
578                 close(stream_fd);
579
580                 if (ret) {
581                         DEBUG(0, ("Error fstating stream %s:%s. "
582                                   "errno=%d (%s)\n", fname, dp->d_name, errno,
583                                   strerror(errno)));
584                         continue;
585                 }
586
587                 merge_stat(&stream_sbuf, base_sbuf);
588
589                 if (!add_one_stream(state->mem_ctx,
590                                     &state->num_streams, &state->streams,
591                                     dp->d_name, stream_sbuf.st_size,
592                                     SMB_VFS_GET_ALLOC_SIZE(conn, NULL,
593                                                            &stream_sbuf))) {
594                         state->status = NT_STATUS_NO_MEMORY;
595                         break;
596                 }
597         }
598
599 out:
600         /* Cleanup everything that was opened. */
601         if (dirp != NULL) {
602                 SMB_VFS_CLOSEDIR(conn, dirp);
603         }
604         if (dir_fd >= 0) {
605                 close(dir_fd);
606         }
607         if (opened_base_fd) {
608                 SMB_ASSERT(base_fd >= 0);
609                 close(base_fd);
610         }
611
612         SAFE_FREE(fake_fs.fsp_name);
613         return status;
614 }
615
616 NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
617                           struct files_struct *fsp,
618                           const char *fname,
619                           TALLOC_CTX *mem_ctx,
620                           unsigned int *num_streams,
621                           struct stream_struct **streams)
622 {
623         SMB_STRUCT_STAT sbuf;
624         int ret;
625         NTSTATUS status;
626         struct streaminfo_state state;
627
628         /* Get a valid stat. */
629         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
630                 if (is_ntfs_stream_name(fsp->fsp_name)) {
631                         return NT_STATUS_INVALID_PARAMETER;
632                 }
633                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
634         } else {
635                 if (is_ntfs_stream_name(fname)) {
636                         return NT_STATUS_INVALID_PARAMETER;
637                 }
638                 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
639         }
640
641         if (ret == -1) {
642                 return map_nt_error_from_unix(errno);
643         }
644
645         state.streams = NULL;
646         state.num_streams = 0;
647
648         /* Add the default stream. */
649         if (S_ISREG(sbuf.st_mode)) {
650                 if (!add_one_stream(mem_ctx,
651                                     &state.num_streams, &state.streams,
652                                     "", sbuf.st_size,
653                                     SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
654                                                            &sbuf))) {
655                         return NT_STATUS_NO_MEMORY;
656                 }
657         }
658
659         state.mem_ctx = mem_ctx;
660         state.handle = handle;
661         state.status = NT_STATUS_OK;
662
663         /* If there are more streams, add them too. */
664         if (sbuf.st_flags & UF_HASADS) {
665
666                 status = walk_onefs_streams(handle->conn, fsp, fname,
667                     &state, &sbuf);
668
669                 if (!NT_STATUS_IS_OK(status)) {
670                         TALLOC_FREE(state.streams);
671                         return status;
672                 }
673
674                 if (!NT_STATUS_IS_OK(state.status)) {
675                         TALLOC_FREE(state.streams);
676                         return state.status;
677                 }
678         }
679
680         *num_streams = state.num_streams;
681         *streams = state.streams;
682         return NT_STATUS_OK;
683 }