Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[ira/wip.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 static int stat_stream(vfs_handle_struct *handle, const char *base,
220                        const char *stream, SMB_STRUCT_STAT *sbuf, int flags)
221 {
222         SMB_STRUCT_STAT base_sbuf;
223         int base_fd = -1, dir_fd, ret, saved_errno;
224
225         dir_fd = get_stream_dir_fd(handle->conn, base, &base_fd);
226         if (dir_fd < 0) {
227                 return -1;
228         }
229
230         /* Stat the stream. */
231         ret = enc_fstatat(dir_fd, stream, ENC_DEFAULT, sbuf, flags);
232         if (ret != -1) {
233                 /* Now stat the base file and merge the results. */
234                 ret = sys_fstat(base_fd, &base_sbuf);
235                 if (ret != -1) {
236                         merge_stat(sbuf, &base_sbuf);
237                 }
238         }
239
240         saved_errno = errno;
241         close(dir_fd);
242         close(base_fd);
243         errno = saved_errno;
244         return ret;
245 }
246
247 int onefs_stat(vfs_handle_struct *handle, const char *path,
248                SMB_STRUCT_STAT *sbuf)
249 {
250         int ret;
251         bool is_stream;
252         char *base = NULL;
253         char *stream = NULL;
254
255         ret = onefs_is_stream(path, &base, &stream, &is_stream);
256         if (ret)
257                 return ret;
258
259         if (!is_stream) {
260                 return SMB_VFS_NEXT_STAT(handle, path, sbuf);
261         }
262
263         /* If it's the ::$DATA stream just stat the base file name. */
264         if (!stream) {
265                 return SMB_VFS_NEXT_STAT(handle, base, sbuf);
266         }
267
268         return stat_stream(handle, base, stream, sbuf, 0);
269 }
270
271 int onefs_fstat(vfs_handle_struct *handle, struct files_struct *fsp,
272                 SMB_STRUCT_STAT *sbuf)
273 {
274         SMB_STRUCT_STAT base_sbuf;
275         int ret;
276
277         /* Stat the stream, by calling next_fstat on the stream's fd. */
278         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
279         if (ret == -1) {
280                 return ret;
281         }
282
283         /* Stat the base file and merge the results. */
284         if (fsp != NULL && fsp->base_fsp != NULL) {
285                 ret = sys_fstat(fsp->base_fsp->fh->fd, &base_sbuf);
286                 if (ret != -1) {
287                         merge_stat(sbuf, &base_sbuf);
288                 }
289         }
290
291         return ret;
292 }
293
294 int onefs_lstat(vfs_handle_struct *handle, const char *path,
295                 SMB_STRUCT_STAT *sbuf)
296 {
297         int ret;
298         bool is_stream;
299         char *base = NULL;
300         char *stream = NULL;
301
302         ret = onefs_is_stream(path, &base, &stream, &is_stream);
303         if (ret)
304                 return ret;
305
306         if (!is_stream) {
307                 return SMB_VFS_NEXT_LSTAT(handle, path, sbuf);
308         }
309
310         /* If it's the ::$DATA stream just stat the base file name. */
311         if (!stream) {
312                 return SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
313         }
314
315         return stat_stream(handle, base, stream, sbuf, AT_SYMLINK_NOFOLLOW);
316 }
317
318 int onefs_unlink(vfs_handle_struct *handle, const char *path)
319 {
320         int ret;
321         bool is_stream;
322         char *base = NULL;
323         char *stream = NULL;
324         int dir_fd, saved_errno;
325
326         ret = onefs_is_stream(path, &base, &stream, &is_stream);
327         if (ret) {
328                 return ret;
329         }
330
331         if (!is_stream) {
332                 return SMB_VFS_NEXT_UNLINK(handle, path);
333         }
334
335         /* If it's the ::$DATA stream just unlink the base file name. */
336         if (!stream) {
337                 return SMB_VFS_NEXT_UNLINK(handle, base);
338         }
339
340         dir_fd = get_stream_dir_fd(handle->conn, base, NULL);
341         if (dir_fd < 0) {
342                 return -1;
343         }
344
345         ret = enc_unlinkat(dir_fd, stream, ENC_DEFAULT, 0);
346
347         saved_errno = errno;
348         close(dir_fd);
349         errno = saved_errno;
350         return ret;
351 }
352
353 int onefs_vtimes_streams(vfs_handle_struct *handle, const char *fname,
354                          int flags, struct timespec times[3])
355 {
356         int ret;
357         bool is_stream;
358         char *base;
359         char *stream;
360         int dirfd;
361         int saved_errno;
362
363         START_PROFILE(syscall_ntimes);
364
365         ret = onefs_is_stream(fname, &base, &stream, &is_stream);
366         if (ret)
367                 return ret;
368
369         if (!is_stream) {
370                 ret = vtimes(fname, times, flags);
371                 return ret;
372         }
373
374         dirfd = get_stream_dir_fd(handle->conn, base, NULL);
375         if (dirfd < -1) {
376                 return -1;
377         }
378
379         ret = enc_vtimesat(dirfd, stream, ENC_DEFAULT, times, flags);
380
381         END_PROFILE(syscall_ntimes);
382
383         saved_errno = errno;
384         close(dirfd);
385         errno = saved_errno;
386         return ret;
387 }
388
389 int onefs_chflags(vfs_handle_struct *handle, const char *path,
390                   unsigned int flags)
391 {
392         char *base = NULL;
393         char *stream = NULL;
394
395         if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
396                                                           &base, &stream))) {
397                 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
398                 errno = ENOMEM;
399                 return -1;
400         }
401
402         /*
403          * Only set the attributes on the base file.  ifs_createfile handles
404          * file creation attribute semantics.
405          */
406         return SMB_VFS_NEXT_CHFLAGS(handle, base, flags);
407 }
408
409 /*
410  * Streaminfo enumeration functionality
411  */
412 struct streaminfo_state {
413         TALLOC_CTX *mem_ctx;
414         vfs_handle_struct *handle;
415         unsigned int num_streams;
416         struct stream_struct *streams;
417         NTSTATUS status;
418 };
419
420 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
421                            struct stream_struct **streams,
422                            const char *name, SMB_OFF_T size,
423                            SMB_OFF_T alloc_size)
424 {
425         struct stream_struct *tmp;
426
427         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
428                                    (*num_streams)+1);
429         if (tmp == NULL) {
430                 return false;
431         }
432
433         tmp[*num_streams].name = talloc_asprintf(mem_ctx, ":%s:%s", name,
434                                                  "$DATA");
435         if (tmp[*num_streams].name == NULL) {
436                 return false;
437         }
438
439         tmp[*num_streams].size = size;
440         tmp[*num_streams].alloc_size = alloc_size;
441
442         *streams = tmp;
443         *num_streams += 1;
444         return true;
445 }
446
447 static NTSTATUS walk_onefs_streams(connection_struct *conn, files_struct *fsp,
448                                    const char *fname,
449                                    struct streaminfo_state *state,
450                                    SMB_STRUCT_STAT *base_sbuf)
451 {
452         NTSTATUS status = NT_STATUS_OK;
453         bool opened_base_fd = false;
454         int base_fd = -1;
455         int dir_fd = -1;
456         int stream_fd = -1;
457         int ret;
458         SMB_STRUCT_DIR *dirp = NULL;
459         SMB_STRUCT_DIRENT *dp = NULL;
460         files_struct fake_fs;
461         struct fd_handle fake_fh;
462         SMB_STRUCT_STAT stream_sbuf;
463
464         ZERO_STRUCT(fake_fh);
465         ZERO_STRUCT(fake_fs);
466
467         /* If the base file is already open, use its fd. */
468         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
469                 base_fd = fsp->fh->fd;
470         } else {
471                 opened_base_fd = true;
472         }
473
474         dir_fd = get_stream_dir_fd(conn, fname, &base_fd);
475         if (dir_fd < 0) {
476                 return map_nt_error_from_unix(errno);
477         }
478
479         /* Open the ADS directory. */
480         if ((dirp = fdopendir(dir_fd)) == NULL) {
481                 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
482                           fname, errno, strerror(errno)));
483                 status = map_nt_error_from_unix(errno);
484                 goto out;
485         }
486
487         fake_fs.conn = conn;
488         fake_fs.fh = &fake_fh;
489         fake_fs.fsp_name = SMB_STRDUP(fname);
490
491         /* Iterate over the streams in the ADS directory. */
492         while ((dp = SMB_VFS_READDIR(conn, dirp)) != NULL) {
493                 /* Skip the "." and ".." entries */
494                 if ((strcmp(dp->d_name, ".") == 0) ||
495                     (strcmp(dp->d_name, "..") == 0))
496                         continue;
497
498                 /* Open actual stream */
499                 if ((stream_fd = onefs_sys_create_file(conn,
500                                                          base_fd,
501                                                          dp->d_name,
502                                                          0,
503                                                          0,
504                                                          0,
505                                                          0,
506                                                          0,
507                                                          0,
508                                                          INTERNAL_OPEN_ONLY,
509                                                          0,
510                                                          NULL,
511                                                          0,
512                                                          NULL)) == -1) {
513                         DEBUG(0, ("Error opening stream %s:%s. "
514                                   "errno=%d (%s)\n", fname, dp->d_name, errno,
515                                   strerror(errno)));
516                         continue;
517                 }
518
519                 /* Figure out the stat info. */
520                 fake_fh.fd = stream_fd;
521                 ret = SMB_VFS_FSTAT(&fake_fs, &stream_sbuf);
522                 close(stream_fd);
523
524                 if (ret) {
525                         DEBUG(0, ("Error fstating stream %s:%s. "
526                                   "errno=%d (%s)\n", fname, dp->d_name, errno,
527                                   strerror(errno)));
528                         continue;
529                 }
530
531                 merge_stat(&stream_sbuf, base_sbuf);
532
533                 if (!add_one_stream(state->mem_ctx,
534                                     &state->num_streams, &state->streams,
535                                     dp->d_name, stream_sbuf.st_size,
536                                     SMB_VFS_GET_ALLOC_SIZE(conn, NULL,
537                                                            &stream_sbuf))) {
538                         state->status = NT_STATUS_NO_MEMORY;
539                         break;
540                 }
541         }
542
543 out:
544         /* Cleanup everything that was opened. */
545         if (dirp != NULL) {
546                 SMB_VFS_CLOSEDIR(conn, dirp);
547         }
548         if (dir_fd >= 0) {
549                 close(dir_fd);
550         }
551         if (opened_base_fd) {
552                 SMB_ASSERT(base_fd >= 0);
553                 close(base_fd);
554         }
555
556         SAFE_FREE(fake_fs.fsp_name);
557         return status;
558 }
559
560 NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
561                           struct files_struct *fsp,
562                           const char *fname,
563                           TALLOC_CTX *mem_ctx,
564                           unsigned int *num_streams,
565                           struct stream_struct **streams)
566 {
567         SMB_STRUCT_STAT sbuf;
568         int ret;
569         NTSTATUS status;
570         struct streaminfo_state state;
571
572         /* Get a valid stat. */
573         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
574                 if (is_ntfs_stream_name(fsp->fsp_name)) {
575                         return NT_STATUS_INVALID_PARAMETER;
576                 }
577                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
578         } else {
579                 if (is_ntfs_stream_name(fname)) {
580                         return NT_STATUS_INVALID_PARAMETER;
581                 }
582                 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
583         }
584
585         if (ret == -1) {
586                 return map_nt_error_from_unix(errno);
587         }
588
589         state.streams = NULL;
590         state.num_streams = 0;
591
592         /* Add the default stream. */
593         if (S_ISREG(sbuf.st_mode)) {
594                 if (!add_one_stream(mem_ctx,
595                                     &state.num_streams, &state.streams,
596                                     "", sbuf.st_size,
597                                     SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
598                                                            &sbuf))) {
599                         return NT_STATUS_NO_MEMORY;
600                 }
601         }
602
603         state.mem_ctx = mem_ctx;
604         state.handle = handle;
605         state.status = NT_STATUS_OK;
606
607         /* If there are more streams, add them too. */
608         if (sbuf.st_flags & UF_HASADS) {
609
610                 status = walk_onefs_streams(handle->conn, fsp, fname,
611                     &state, &sbuf);
612
613                 if (!NT_STATUS_IS_OK(status)) {
614                         TALLOC_FREE(state.streams);
615                         return status;
616                 }
617
618                 if (!NT_STATUS_IS_OK(state.status)) {
619                         TALLOC_FREE(state.streams);
620                         return state.status;
621                 }
622         }
623
624         *num_streams = state.num_streams;
625         *streams = state.streams;
626         return NT_STATUS_OK;
627 }