vfs: Add flags and xferlen args to SMB_VFS_OFFLOAD_READ_RECV
[samba.git] / source3 / modules / vfs_btrfs.c
1 /*
2  * Module to make use of awesome Btrfs features
3  *
4  * Copyright (C) David Disseldorp 2011-2013
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/ioctl.h>
21 #include <linux/fs.h>
22 #include <sys/ioctl.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <dirent.h>
26 #include <libgen.h>
27 #include "system/filesys.h"
28 #include "includes.h"
29 #include "smbd/smbd.h"
30 #include "smbd/globals.h"
31 #include "librpc/gen_ndr/smbXsrv.h"
32 #include "librpc/gen_ndr/ioctl.h"
33 #include "lib/util/tevent_ntstatus.h"
34 #include "offload_token.h"
35
36 static uint32_t btrfs_fs_capabilities(struct vfs_handle_struct *handle,
37                                       enum timestamp_set_resolution *_ts_res)
38 {
39         uint32_t fs_capabilities;
40         enum timestamp_set_resolution ts_res;
41
42         /* inherit default capabilities, expose compression support */
43         fs_capabilities = SMB_VFS_NEXT_FS_CAPABILITIES(handle, &ts_res);
44         fs_capabilities |= (FILE_FILE_COMPRESSION
45                             | FILE_SUPPORTS_BLOCK_REFCOUNTING);
46         *_ts_res = ts_res;
47
48         return fs_capabilities;
49 }
50
51 #define SHADOW_COPY_PREFIX "@GMT-"      /* vfs_shadow_copy format */
52 #define SHADOW_COPY_PATH_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S"
53
54 #define BTRFS_SUBVOL_RDONLY             (1ULL << 1)
55 #define BTRFS_SUBVOL_NAME_MAX           4039
56 #define BTRFS_PATH_NAME_MAX             4087
57 struct btrfs_ioctl_vol_args_v2 {
58         int64_t fd;
59         uint64_t transid;
60         uint64_t flags;
61         uint64_t unused[4];
62         char name[BTRFS_SUBVOL_NAME_MAX + 1];
63 };
64 struct btrfs_ioctl_vol_args {
65         int64_t fd;
66         char name[BTRFS_PATH_NAME_MAX + 1];
67 };
68
69 struct btrfs_ioctl_clone_range_args {
70         int64_t src_fd;
71         uint64_t src_offset;
72         uint64_t src_length;
73         uint64_t dest_offset;
74 };
75
76 #define BTRFS_IOCTL_MAGIC 0x94
77 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
78                                    struct btrfs_ioctl_clone_range_args)
79 #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
80                                     struct btrfs_ioctl_vol_args)
81 #define BTRFS_IOC_SNAP_CREATE_V2 _IOW(BTRFS_IOCTL_MAGIC, 23, \
82                                       struct btrfs_ioctl_vol_args_v2)
83
84 static struct vfs_offload_ctx *btrfs_offload_ctx;
85
86 struct btrfs_offload_read_state {
87         struct vfs_handle_struct *handle;
88         files_struct *fsp;
89         uint32_t flags;
90         uint64_t xferlen;
91         DATA_BLOB token;
92 };
93
94 static void btrfs_offload_read_done(struct tevent_req *subreq);
95
96 static struct tevent_req *btrfs_offload_read_send(
97         TALLOC_CTX *mem_ctx,
98         struct tevent_context *ev,
99         struct vfs_handle_struct *handle,
100         files_struct *fsp,
101         uint32_t fsctl,
102         uint32_t ttl,
103         off_t offset,
104         size_t to_copy)
105 {
106         struct tevent_req *req = NULL;
107         struct tevent_req *subreq = NULL;
108         struct btrfs_offload_read_state *state = NULL;
109         NTSTATUS status;
110
111         req = tevent_req_create(mem_ctx, &state,
112                                 struct btrfs_offload_read_state);
113         if (req == NULL) {
114                 return NULL;
115         }
116         *state = (struct btrfs_offload_read_state) {
117                 .handle = handle,
118                 .fsp = fsp,
119         };
120
121         status = vfs_offload_token_ctx_init(fsp->conn->sconn->client,
122                                             &btrfs_offload_ctx);
123         if (tevent_req_nterror(req, status)) {
124                 return tevent_req_post(req, ev);
125         }
126
127         if (fsctl == FSCTL_DUP_EXTENTS_TO_FILE) {
128                 status = vfs_offload_token_create_blob(state, fsp, fsctl,
129                                                        &state->token);
130                 if (tevent_req_nterror(req, status)) {
131                         return tevent_req_post(req, ev);
132                 }
133
134                 status = vfs_offload_token_db_store_fsp(btrfs_offload_ctx, fsp,
135                                                         &state->token);
136                 if (tevent_req_nterror(req, status)) {
137                         return tevent_req_post(req, ev);
138                 }
139                 tevent_req_done(req);
140                 return tevent_req_post(req, ev);
141         }
142
143         subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
144                                                 fsctl, ttl, offset, to_copy);
145         if (tevent_req_nomem(subreq, req)) {
146                 return tevent_req_post(req, ev);
147         }
148         tevent_req_set_callback(subreq, btrfs_offload_read_done, req);
149         return req;
150 }
151
152 static void btrfs_offload_read_done(struct tevent_req *subreq)
153 {
154         struct tevent_req *req = tevent_req_callback_data(
155                 subreq, struct tevent_req);
156         struct btrfs_offload_read_state *state = tevent_req_data(
157                 req, struct btrfs_offload_read_state);
158         NTSTATUS status;
159
160         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
161                                                 state->handle,
162                                                 state,
163                                                 &state->flags,
164                                                 &state->xferlen,
165                                                 &state->token);
166         TALLOC_FREE(subreq);
167         if (tevent_req_nterror(req, status)) {
168                 return;
169         }
170
171         status = vfs_offload_token_db_store_fsp(btrfs_offload_ctx,
172                                                 state->fsp,
173                                                 &state->token);
174         if (tevent_req_nterror(req, status)) {
175                 return;
176         }
177
178         tevent_req_done(req);
179         return;
180 }
181
182 static NTSTATUS btrfs_offload_read_recv(struct tevent_req *req,
183                                         struct vfs_handle_struct *handle,
184                                         TALLOC_CTX *mem_ctx,
185                                         uint32_t *flags,
186                                         uint64_t *xferlen,
187                                         DATA_BLOB *token)
188 {
189         struct btrfs_offload_read_state *state = tevent_req_data(
190                 req, struct btrfs_offload_read_state);
191         NTSTATUS status;
192
193         if (tevent_req_is_nterror(req, &status)) {
194                 tevent_req_received(req);
195                 return status;
196         }
197
198         *flags = state->flags;
199         *xferlen = state->xferlen;
200         token->length = state->token.length;
201         token->data = talloc_move(mem_ctx, &state->token.data);
202
203         tevent_req_received(req);
204         return NT_STATUS_OK;
205 }
206
207 struct btrfs_offload_write_state {
208         struct vfs_handle_struct *handle;
209         off_t copied;
210         bool need_unbecome_user;
211 };
212
213 static void btrfs_offload_write_cleanup(struct tevent_req *req,
214                                         enum tevent_req_state req_state)
215 {
216         struct btrfs_offload_write_state *state =
217                 tevent_req_data(req,
218                 struct btrfs_offload_write_state);
219         bool ok;
220
221         if (!state->need_unbecome_user) {
222                 return;
223         }
224
225         ok = unbecome_user_without_service();
226         SMB_ASSERT(ok);
227         state->need_unbecome_user = false;
228 }
229
230 static void btrfs_offload_write_done(struct tevent_req *subreq);
231
232 static struct tevent_req *btrfs_offload_write_send(struct vfs_handle_struct *handle,
233                                                 TALLOC_CTX *mem_ctx,
234                                                 struct tevent_context *ev,
235                                                 uint32_t fsctl,
236                                                 DATA_BLOB *token,
237                                                 off_t transfer_offset,
238                                                 struct files_struct *dest_fsp,
239                                                 off_t dest_off,
240                                                 off_t num)
241 {
242         struct tevent_req *req = NULL;
243         struct btrfs_offload_write_state *state = NULL;
244         struct tevent_req *subreq = NULL;
245         struct btrfs_ioctl_clone_range_args cr_args;
246         struct lock_struct src_lck;
247         struct lock_struct dest_lck;
248         off_t src_off = transfer_offset;
249         files_struct *src_fsp = NULL;
250         int ret;
251         bool handle_offload_write = true;
252         bool do_locking = false;
253         NTSTATUS status;
254         bool ok;
255
256         req = tevent_req_create(mem_ctx, &state,
257                                 struct btrfs_offload_write_state);
258         if (req == NULL) {
259                 return NULL;
260         }
261
262         state->handle = handle;
263
264         tevent_req_set_cleanup_fn(req, btrfs_offload_write_cleanup);
265
266         status = vfs_offload_token_db_fetch_fsp(btrfs_offload_ctx,
267                                                 token, &src_fsp);
268         if (tevent_req_nterror(req, status)) {
269                 return tevent_req_post(req, ev);
270         }
271
272         switch (fsctl) {
273         case FSCTL_SRV_COPYCHUNK:
274         case FSCTL_SRV_COPYCHUNK_WRITE:
275                 do_locking = true;
276                 break;
277
278         case FSCTL_DUP_EXTENTS_TO_FILE:
279                 /* dup extents does not use locking */
280                 break;
281
282         default:
283                 handle_offload_write = false;
284                 break;
285         }
286
287         if (num == 0) {
288                 /*
289                  * With a @src_length of zero, BTRFS_IOC_CLONE_RANGE clones
290                  * all data from @src_offset->EOF! This is certainly not what
291                  * the caller expects, and not what vfs_default does.
292                  */
293                 handle_offload_write = false;
294         }
295
296         if (!handle_offload_write) {
297                 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
298                                                          state,
299                                                          ev,
300                                                          fsctl,
301                                                          token,
302                                                          transfer_offset,
303                                                          dest_fsp,
304                                                          dest_off,
305                                                          num);
306                 if (tevent_req_nomem(subreq, req)) {
307                         return tevent_req_post(req, ev);
308                 }
309                 tevent_req_set_callback(subreq,
310                                         btrfs_offload_write_done,
311                                         req);
312                 return req;
313         }
314
315         status = vfs_offload_token_check_handles(
316                 fsctl, src_fsp, dest_fsp);
317         if (!NT_STATUS_IS_OK(status)) {
318                 tevent_req_nterror(req, status);
319                 return tevent_req_post(req, ev);
320         }
321
322         ok = become_user_without_service_by_fsp(src_fsp);
323         if (!ok) {
324                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
325                 return tevent_req_post(req, ev);
326         }
327         state->need_unbecome_user = true;
328
329         status = vfs_stat_fsp(src_fsp);
330         if (tevent_req_nterror(req, status)) {
331                 return tevent_req_post(req, ev);
332         }
333
334         if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
335                 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
336                 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
337                 return tevent_req_post(req, ev);
338         }
339
340         if (do_locking) {
341                 init_strict_lock_struct(src_fsp,
342                                         src_fsp->op->global->open_persistent_id,
343                                         src_off,
344                                         num,
345                                         READ_LOCK,
346                                         &src_lck);
347                 if (!SMB_VFS_STRICT_LOCK_CHECK(src_fsp->conn, src_fsp, &src_lck)) {
348                         tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
349                         return tevent_req_post(req, ev);
350                 }
351         }
352
353         ok = unbecome_user_without_service();
354         SMB_ASSERT(ok);
355         state->need_unbecome_user = false;
356
357         if (do_locking) {
358                 init_strict_lock_struct(dest_fsp,
359                                         dest_fsp->op->global->open_persistent_id,
360                                         dest_off,
361                                         num,
362                                         WRITE_LOCK,
363                                         &dest_lck);
364
365                 if (!SMB_VFS_STRICT_LOCK_CHECK(dest_fsp->conn, dest_fsp, &dest_lck)) {
366                         tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
367                         return tevent_req_post(req, ev);
368                 }
369         }
370
371         ZERO_STRUCT(cr_args);
372         cr_args.src_fd = fsp_get_io_fd(src_fsp);
373         cr_args.src_offset = (uint64_t)src_off;
374         cr_args.dest_offset = (uint64_t)dest_off;
375         cr_args.src_length = (uint64_t)num;
376
377         ret = ioctl(fsp_get_io_fd(dest_fsp), BTRFS_IOC_CLONE_RANGE, &cr_args);
378         if (ret < 0) {
379                 /*
380                  * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
381                  * cloning. Which is 4096 by default, therefore fall back to
382                  * manual read/write on failure.
383                  */
384                 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
385                           "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
386                           strerror(errno),
387                           (unsigned long long)cr_args.src_length,
388                           (long long)cr_args.src_fd,
389                           (unsigned long long)cr_args.src_offset,
390                           fsp_get_io_fd(dest_fsp),
391                           (unsigned long long)cr_args.dest_offset));
392                 subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
393                                                          state,
394                                                          ev,
395                                                          fsctl,
396                                                          token,
397                                                          transfer_offset,
398                                                          dest_fsp,
399                                                          dest_off,
400                                                          num);
401                 if (tevent_req_nomem(subreq, req)) {
402                         return tevent_req_post(req, ev);
403                 }
404                 /* wait for subreq completion */
405                 tevent_req_set_callback(subreq,
406                                         btrfs_offload_write_done,
407                                         req);
408                 return req;
409         }
410
411         DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
412         /* BTRFS_IOC_CLONE_RANGE is all or nothing */
413         state->copied = num;
414         tevent_req_done(req);
415         return tevent_req_post(req, ev);
416 }
417
418 /* only used if the request is passed through to next VFS module */
419 static void btrfs_offload_write_done(struct tevent_req *subreq)
420 {
421         struct tevent_req *req =
422                 tevent_req_callback_data(subreq,
423                 struct tevent_req);
424         struct btrfs_offload_write_state *state =
425                 tevent_req_data(req,
426                 struct btrfs_offload_write_state);
427         NTSTATUS status;
428
429         status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
430                                                  subreq,
431                                                  &state->copied);
432         TALLOC_FREE(subreq);
433         if (tevent_req_nterror(req, status)) {
434                 return;
435         }
436         tevent_req_done(req);
437 }
438
439 static NTSTATUS btrfs_offload_write_recv(struct vfs_handle_struct *handle,
440                                          struct tevent_req *req,
441                                          off_t *copied)
442 {
443         struct btrfs_offload_write_state *state =
444                 tevent_req_data(req,
445                 struct btrfs_offload_write_state);
446         NTSTATUS status;
447
448         if (tevent_req_is_nterror(req, &status)) {
449                 DEBUG(4, ("server side copy chunk failed: %s\n",
450                           nt_errstr(status)));
451                 tevent_req_received(req);
452                 return status;
453         }
454
455         DEBUG(10, ("server side copy chunk copied %llu\n",
456                    (unsigned long long)state->copied));
457         *copied = state->copied;
458         tevent_req_received(req);
459         return NT_STATUS_OK;
460 }
461
462 static NTSTATUS btrfs_fget_compression(struct vfs_handle_struct *handle,
463                                        TALLOC_CTX *mem_ctx,
464                                        struct files_struct *fsp,
465                                        uint16_t *_compression_fmt)
466 {
467         char buf[PATH_MAX];
468         const char *p = NULL;
469         int ret;
470         long flags = 0;
471         int fsp_fd = fsp_get_pathref_fd(fsp);
472         int fd = -1;
473         NTSTATUS status;
474
475         if (!fsp->fsp_flags.is_pathref) {
476                 ret = ioctl(fsp_fd, FS_IOC_GETFLAGS, &flags);
477                 if (ret < 0) {
478                         DBG_WARNING("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
479                                     strerror(errno), (long long)fd);
480                         return map_nt_error_from_unix(errno);
481                 }
482                 if (flags & FS_COMPR_FL) {
483                         *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
484                 } else {
485                         *_compression_fmt = COMPRESSION_FORMAT_NONE;
486                 }
487                 return NT_STATUS_OK;
488         }
489
490         if (!fsp->fsp_flags.have_proc_fds) {
491                 return NT_STATUS_NOT_IMPLEMENTED;
492         }
493
494         p = sys_proc_fd_path(fsp_fd, buf, sizeof(buf));
495         if (p == NULL) {
496                 return NT_STATUS_NO_MEMORY;
497         }
498
499         fd = open(p, O_RDONLY);
500         if (fd == -1) {
501                 DBG_ERR("/proc open of %s failed: %s\n", p, strerror(errno));
502                 return map_nt_error_from_unix(errno);
503         }
504
505         ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
506         if (ret < 0) {
507                 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
508                           strerror(errno), (long long)fd));
509                 status = map_nt_error_from_unix(errno);
510                 goto err_close;
511         }
512         if (flags & FS_COMPR_FL) {
513                 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
514         } else {
515                 *_compression_fmt = COMPRESSION_FORMAT_NONE;
516         }
517         status = NT_STATUS_OK;
518
519 err_close:
520         if (fd != -1) {
521                 close(fd);
522         }
523
524         return status;
525 }
526
527 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
528                                       TALLOC_CTX *mem_ctx,
529                                       struct files_struct *fsp,
530                                       uint16_t compression_fmt)
531 {
532         int ret;
533         long flags = 0;
534         int fd;
535         NTSTATUS status;
536
537         if ((fsp == NULL) || (fsp_get_io_fd(fsp) == -1)) {
538                 status = NT_STATUS_INVALID_PARAMETER;
539                 goto err_out;
540         }
541         fd = fsp_get_io_fd(fsp);
542
543         ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
544         if (ret < 0) {
545                 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
546                           strerror(errno), fd));
547                 status = map_nt_error_from_unix(errno);
548                 goto err_out;
549         }
550
551         if (compression_fmt == COMPRESSION_FORMAT_NONE) {
552                 DEBUG(5, ("setting compression\n"));
553                 flags &= (~FS_COMPR_FL);
554         } else if ((compression_fmt == COMPRESSION_FORMAT_DEFAULT)
555                 || (compression_fmt == COMPRESSION_FORMAT_LZNT1)) {
556                 DEBUG(5, ("clearing compression\n"));
557                 flags |= FS_COMPR_FL;
558         } else {
559                 DEBUG(1, ("invalid compression format 0x%x\n",
560                           (int)compression_fmt));
561                 status = NT_STATUS_INVALID_PARAMETER;
562                 goto err_out;
563         }
564
565         ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
566         if (ret < 0) {
567                 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
568                           strerror(errno), fd));
569                 status = map_nt_error_from_unix(errno);
570                 goto err_out;
571         }
572         status = NT_STATUS_OK;
573 err_out:
574         return status;
575 }
576
577 /*
578  * Check whether a path can be shadow copied. Return the base volume, allowing
579  * the caller to determine if multiple paths lie on the same base volume.
580  */
581 #define BTRFS_INODE_SUBVOL 256
582 static NTSTATUS btrfs_snap_check_path(struct vfs_handle_struct *handle,
583                                       TALLOC_CTX *mem_ctx,
584                                       const char *service_path,
585                                       char **base_volume)
586 {
587         struct stat st;
588         char *base;
589
590         if (!lp_parm_bool(SNUM(handle->conn),
591                          "btrfs", "manipulate snapshots", false)) {
592                 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
593                 return SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx,
594                                                     service_path, base_volume);
595         }
596
597         /* btrfs userspace uses this logic to confirm subvolume */
598         if (stat(service_path, &st) < 0) {
599                 return NT_STATUS_NOT_SUPPORTED;
600         }
601         if ((st.st_ino != BTRFS_INODE_SUBVOL) || !S_ISDIR(st.st_mode)) {
602                 DEBUG(0, ("%s not a btrfs subvolume, snapshots not available\n",
603                           service_path));
604                 return NT_STATUS_NOT_SUPPORTED;
605         }
606
607         /* we "snapshot" the service path itself */
608         base = talloc_strdup(mem_ctx, service_path);
609         if (base == NULL) {
610                 return NT_STATUS_NO_MEMORY;
611         }
612         *base_volume = base;
613
614         return NT_STATUS_OK;
615 }
616
617 static NTSTATUS btrfs_gen_snap_dest_path(TALLOC_CTX *mem_ctx,
618                                          const char *src_path,
619                                          time_t *tstamp,
620                                          char **dest_path, char **subvolume)
621 {
622         struct tm t_gmt;
623         char time_str[50];
624         size_t tlen;
625
626         gmtime_r(tstamp, &t_gmt);
627
628         tlen = strftime(time_str, ARRAY_SIZE(time_str),
629                         SHADOW_COPY_PATH_FORMAT, &t_gmt);
630         if (tlen <= 0) {
631                 return NT_STATUS_UNSUCCESSFUL;
632         }
633
634         *dest_path = talloc_strdup(mem_ctx, src_path);
635         *subvolume = talloc_strdup(mem_ctx, time_str);
636         if ((*dest_path == NULL) || (*subvolume == NULL)) {
637                 return NT_STATUS_NO_MEMORY;
638         }
639
640         return NT_STATUS_OK;
641 }
642
643 static NTSTATUS btrfs_snap_create(struct vfs_handle_struct *handle,
644                                   TALLOC_CTX *mem_ctx,
645                                   const char *base_volume,
646                                   time_t *tstamp,
647                                   bool rw,
648                                   char **_base_path,
649                                   char **_snap_path)
650 {
651         struct btrfs_ioctl_vol_args_v2 ioctl_arg;
652         DIR *src_dir;
653         DIR *dest_dir;
654         int src_fd;
655         int dest_fd;
656         char *dest_path = NULL;
657         char *dest_subvolume = NULL;
658         int ret;
659         NTSTATUS status;
660         char *base_path;
661         char *snap_path;
662         TALLOC_CTX *tmp_ctx;
663         int saved_errno;
664         size_t len;
665
666         if (!lp_parm_bool(SNUM(handle->conn),
667                           "btrfs", "manipulate snapshots", false)) {
668                 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
669                 return SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume,
670                                                 tstamp, rw, _base_path,
671                                                 _snap_path);
672         }
673
674         tmp_ctx = talloc_new(mem_ctx);
675         if (tmp_ctx == NULL) {
676                 return NT_STATUS_NO_MEMORY;
677         }
678
679         base_path = talloc_strdup(tmp_ctx, base_volume);
680         if (base_path == NULL) {
681                 talloc_free(tmp_ctx);
682                 return NT_STATUS_NO_MEMORY;
683         }
684
685         status = btrfs_gen_snap_dest_path(tmp_ctx, base_volume, tstamp,
686                                           &dest_path, &dest_subvolume);
687         if (!NT_STATUS_IS_OK(status)) {
688                 talloc_free(tmp_ctx);
689                 return status;
690         }
691
692         snap_path = talloc_asprintf(tmp_ctx, "%s/%s", dest_path,
693                                     dest_subvolume);
694         if (snap_path == NULL) {
695                 talloc_free(tmp_ctx);
696                 return NT_STATUS_NO_MEMORY;
697         }
698
699         src_dir = opendir(base_volume);
700         if (src_dir == NULL) {
701                 DEBUG(0, ("snap src %s open failed: %s\n",
702                           base_volume, strerror(errno)));
703                 status = map_nt_error_from_unix(errno);
704                 talloc_free(tmp_ctx);
705                 return status;
706         }
707         src_fd = dirfd(src_dir);
708         if (src_fd < 0) {
709                 status = map_nt_error_from_unix(errno);
710                 closedir(src_dir);
711                 talloc_free(tmp_ctx);
712                 return status;
713         }
714
715         dest_dir = opendir(dest_path);
716         if (dest_dir == NULL) {
717                 DEBUG(0, ("snap dest %s open failed: %s\n",
718                           dest_path, strerror(errno)));
719                 status = map_nt_error_from_unix(errno);
720                 closedir(src_dir);
721                 talloc_free(tmp_ctx);
722                 return status;
723         }
724         dest_fd = dirfd(dest_dir);
725         if (dest_fd < 0) {
726                 status = map_nt_error_from_unix(errno);
727                 closedir(src_dir);
728                 closedir(dest_dir);
729                 talloc_free(tmp_ctx);
730                 return status;
731         }
732
733         /* avoid zeroing the entire struct here, name is 4k */
734         ioctl_arg.fd = src_fd;
735         ioctl_arg.transid = 0;
736         ioctl_arg.flags = (rw == false) ? BTRFS_SUBVOL_RDONLY : 0;
737         memset(ioctl_arg.unused, 0, sizeof(ioctl_arg.unused));
738         len = strlcpy(ioctl_arg.name, dest_subvolume,
739                       ARRAY_SIZE(ioctl_arg.name));
740         if (len >= ARRAY_SIZE(ioctl_arg.name)) {
741                 DEBUG(1, ("subvolume name too long for SNAP_CREATE ioctl\n"));
742                 closedir(src_dir);
743                 closedir(dest_dir);
744                 talloc_free(tmp_ctx);
745                 return NT_STATUS_INVALID_PARAMETER;
746         }
747
748         become_root();
749         ret = ioctl(dest_fd, BTRFS_IOC_SNAP_CREATE_V2, &ioctl_arg);
750         saved_errno = errno;
751         unbecome_root();
752         if (ret < 0) {
753                 DEBUG(0, ("%s -> %s(%s) BTRFS_IOC_SNAP_CREATE_V2 failed: %s\n",
754                           base_volume, dest_path, dest_subvolume,
755                           strerror(saved_errno)));
756                 status = map_nt_error_from_unix(saved_errno);
757                 closedir(src_dir);
758                 closedir(dest_dir);
759                 talloc_free(tmp_ctx);
760                 return status;
761         }
762         DEBUG(5, ("%s -> %s(%s) BTRFS_IOC_SNAP_CREATE_V2 done\n",
763                   base_volume, dest_path, dest_subvolume));
764
765         *_base_path = talloc_steal(mem_ctx, base_path);
766         *_snap_path = talloc_steal(mem_ctx, snap_path);
767         closedir(src_dir);
768         closedir(dest_dir);
769         talloc_free(tmp_ctx);
770
771         return NT_STATUS_OK;
772 }
773
774 static NTSTATUS btrfs_snap_delete(struct vfs_handle_struct *handle,
775                                   TALLOC_CTX *mem_ctx,
776                                   char *base_path,
777                                   char *snap_path)
778 {
779         char *tstr;
780         struct tm t_gmt;
781         DIR *dest_dir;
782         int dest_fd;
783         struct btrfs_ioctl_vol_args ioctl_arg;
784         int ret;
785         NTSTATUS status;
786         char *dest_path;
787         char *subvolume;
788         TALLOC_CTX *tmp_ctx;
789         int saved_errno;
790         size_t len;
791
792         if (!lp_parm_bool(SNUM(handle->conn),
793                           "btrfs", "manipulate snapshots", false)) {
794                 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
795                 return SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx,
796                                                 base_path, snap_path);
797         }
798
799         tmp_ctx = talloc_new(mem_ctx);
800         if (tmp_ctx == NULL) {
801                 return NT_STATUS_NO_MEMORY;
802         }
803
804         dest_path = talloc_strdup(tmp_ctx, snap_path);
805         if (dest_path == NULL) {
806                 talloc_free(tmp_ctx);
807                 return NT_STATUS_NO_MEMORY;
808         }
809         subvolume = talloc_strdup(tmp_ctx, snap_path);
810         if (subvolume == NULL) {
811                 talloc_free(tmp_ctx);
812                 return NT_STATUS_NO_MEMORY;
813         }
814         dest_path = dirname(dest_path);
815         subvolume = basename(subvolume);
816
817         /* confirm snap_path matches creation format */
818         tstr = strptime(subvolume, SHADOW_COPY_PATH_FORMAT, &t_gmt);
819         if ((tstr == NULL) || (*tstr != '\0')) {
820                 DEBUG(0, ("snapshot path %s does not match creation format\n",
821                           snap_path));
822                 talloc_free(tmp_ctx);
823                 return NT_STATUS_UNSUCCESSFUL;
824         }
825
826         dest_dir = opendir(dest_path);
827         if (dest_dir == NULL) {
828                 DEBUG(0, ("snap destroy dest %s open failed: %s\n",
829                           dest_path, strerror(errno)));
830                 status = map_nt_error_from_unix(errno);
831                 talloc_free(tmp_ctx);
832                 return status;
833         }
834         dest_fd = dirfd(dest_dir);
835         if (dest_fd < 0) {
836                 status = map_nt_error_from_unix(errno);
837                 closedir(dest_dir);
838                 talloc_free(tmp_ctx);
839                 return status;
840         }
841
842         ioctl_arg.fd = -1;      /* not needed */
843         len = strlcpy(ioctl_arg.name, subvolume, ARRAY_SIZE(ioctl_arg.name));
844         if (len >= ARRAY_SIZE(ioctl_arg.name)) {
845                 DEBUG(1, ("subvolume name too long for SNAP_DESTROY ioctl\n"));
846                 closedir(dest_dir);
847                 talloc_free(tmp_ctx);
848                 return NT_STATUS_INVALID_PARAMETER;
849         }
850
851         become_root();
852         ret = ioctl(dest_fd, BTRFS_IOC_SNAP_DESTROY, &ioctl_arg);
853         saved_errno = errno;
854         unbecome_root();
855         if (ret < 0) {
856                 DEBUG(0, ("%s(%s) BTRFS_IOC_SNAP_DESTROY failed: %s\n",
857                           dest_path, subvolume, strerror(saved_errno)));
858                 status = map_nt_error_from_unix(saved_errno);
859                 closedir(dest_dir);
860                 talloc_free(tmp_ctx);
861                 return status;
862         }
863         DEBUG(5, ("%s(%s) BTRFS_IOC_SNAP_DESTROY done\n",
864                   dest_path, subvolume));
865
866         closedir(dest_dir);
867         talloc_free(tmp_ctx);
868         return NT_STATUS_OK;
869 }
870
871 static struct vfs_fn_pointers btrfs_fns = {
872         .fs_capabilities_fn = btrfs_fs_capabilities,
873         .offload_read_send_fn = btrfs_offload_read_send,
874         .offload_read_recv_fn = btrfs_offload_read_recv,
875         .offload_write_send_fn = btrfs_offload_write_send,
876         .offload_write_recv_fn = btrfs_offload_write_recv,
877         .fget_compression_fn = btrfs_fget_compression,
878         .set_compression_fn = btrfs_set_compression,
879         .snap_check_path_fn = btrfs_snap_check_path,
880         .snap_create_fn = btrfs_snap_create,
881         .snap_delete_fn = btrfs_snap_delete,
882 };
883
884 static_decl_vfs;
885 NTSTATUS vfs_btrfs_init(TALLOC_CTX *ctx)
886 {
887         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
888                                 "btrfs", &btrfs_fns);
889 }