vfs_btrfs: remove unused checks which are already caught by vfs_offload_token_check_h...
[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         DATA_BLOB token;
90 };
91
92 static void btrfs_offload_read_done(struct tevent_req *subreq);
93
94 static struct tevent_req *btrfs_offload_read_send(
95         TALLOC_CTX *mem_ctx,
96         struct tevent_context *ev,
97         struct vfs_handle_struct *handle,
98         files_struct *fsp,
99         uint32_t fsctl,
100         uint32_t ttl,
101         off_t offset,
102         size_t to_copy)
103 {
104         struct tevent_req *req = NULL;
105         struct tevent_req *subreq = NULL;
106         struct btrfs_offload_read_state *state = NULL;
107         NTSTATUS status;
108
109         req = tevent_req_create(mem_ctx, &state,
110                                 struct btrfs_offload_read_state);
111         if (req == NULL) {
112                 return NULL;
113         }
114         *state = (struct btrfs_offload_read_state) {
115                 .handle = handle,
116                 .fsp = fsp,
117         };
118
119         status = vfs_offload_token_ctx_init(fsp->conn->sconn->client,
120                                             &btrfs_offload_ctx);
121         if (tevent_req_nterror(req, status)) {
122                 return tevent_req_post(req, ev);
123         }
124
125         if (fsctl == FSCTL_DUP_EXTENTS_TO_FILE) {
126                 status = vfs_offload_token_create_blob(state, fsp, fsctl,
127                                                        &state->token);
128                 if (tevent_req_nterror(req, status)) {
129                         return tevent_req_post(req, ev);
130                 }
131
132                 status = vfs_offload_token_db_store_fsp(btrfs_offload_ctx, fsp,
133                                                         &state->token);
134                 if (tevent_req_nterror(req, status)) {
135                         return tevent_req_post(req, ev);
136                 }
137                 tevent_req_done(req);
138                 return tevent_req_post(req, ev);
139         }
140
141         subreq = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
142                                                 fsctl, ttl, offset, to_copy);
143         if (tevent_req_nomem(subreq, req)) {
144                 return tevent_req_post(req, ev);
145         }
146         tevent_req_set_callback(subreq, btrfs_offload_read_done, req);
147         return req;
148 }
149
150 static void btrfs_offload_read_done(struct tevent_req *subreq)
151 {
152         struct tevent_req *req = tevent_req_callback_data(
153                 subreq, struct tevent_req);
154         struct btrfs_offload_read_state *state = tevent_req_data(
155                 req, struct btrfs_offload_read_state);
156         NTSTATUS status;
157
158         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq,
159                                                 state->handle,
160                                                 state,
161                                                 &state->token);
162         TALLOC_FREE(subreq);
163         if (tevent_req_nterror(req, status)) {
164                 return;
165         }
166
167         status = vfs_offload_token_db_store_fsp(btrfs_offload_ctx,
168                                                 state->fsp,
169                                                 &state->token);
170         if (tevent_req_nterror(req, status)) {
171                 return;
172         }
173
174         tevent_req_done(req);
175         return;
176 }
177
178 static NTSTATUS btrfs_offload_read_recv(struct tevent_req *req,
179                                         struct vfs_handle_struct *handle,
180                                         TALLOC_CTX *mem_ctx,
181                                         DATA_BLOB *token)
182 {
183         struct btrfs_offload_read_state *state = tevent_req_data(
184                 req, struct btrfs_offload_read_state);
185         NTSTATUS status;
186
187         if (tevent_req_is_nterror(req, &status)) {
188                 tevent_req_received(req);
189                 return status;
190         }
191
192         token->length = state->token.length;
193         token->data = talloc_move(mem_ctx, &state->token.data);
194
195         tevent_req_received(req);
196         return NT_STATUS_OK;
197 }
198
199 struct btrfs_cc_state {
200         struct vfs_handle_struct *handle;
201         off_t copied;
202         struct tevent_req *subreq;      /* non-null if passed to next VFS fn */
203 };
204 static void btrfs_offload_write_done(struct tevent_req *subreq);
205
206 static struct tevent_req *btrfs_offload_write_send(struct vfs_handle_struct *handle,
207                                                 TALLOC_CTX *mem_ctx,
208                                                 struct tevent_context *ev,
209                                                 uint32_t fsctl,
210                                                 DATA_BLOB *token,
211                                                 off_t transfer_offset,
212                                                 struct files_struct *dest_fsp,
213                                                 off_t dest_off,
214                                                 off_t num)
215 {
216         struct tevent_req *req;
217         struct btrfs_cc_state *cc_state;
218         struct btrfs_ioctl_clone_range_args cr_args;
219         struct lock_struct src_lck;
220         struct lock_struct dest_lck;
221         off_t src_off = transfer_offset;
222         files_struct *src_fsp = NULL;
223         int ret;
224         bool handle_offload_write = true;
225         bool do_locking = false;
226         NTSTATUS status;
227
228         req = tevent_req_create(mem_ctx, &cc_state, struct btrfs_cc_state);
229         if (req == NULL) {
230                 return NULL;
231         }
232
233         cc_state->handle = handle;
234
235         status = vfs_offload_token_db_fetch_fsp(btrfs_offload_ctx,
236                                                 token, &src_fsp);
237         if (tevent_req_nterror(req, status)) {
238                 return tevent_req_post(req, ev);
239         }
240
241         switch (fsctl) {
242         case FSCTL_SRV_COPYCHUNK:
243         case FSCTL_SRV_COPYCHUNK_WRITE:
244                 do_locking = true;
245                 break;
246
247         case FSCTL_DUP_EXTENTS_TO_FILE:
248                 /* dup extents does not use locking */
249                 break;
250
251         default:
252                 handle_offload_write = false;
253                 break;
254         }
255
256         if (num == 0) {
257                 /*
258                  * With a @src_length of zero, BTRFS_IOC_CLONE_RANGE clones
259                  * all data from @src_offset->EOF! This is certainly not what
260                  * the caller expects, and not what vfs_default does.
261                  */
262                 handle_offload_write = false;
263         }
264
265         if (!handle_offload_write) {
266                 cc_state->subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
267                                                                 cc_state, ev,
268                                                                 fsctl,
269                                                                 token,
270                                                                 transfer_offset,
271                                                                 dest_fsp,
272                                                                 dest_off,
273                                                                 num);
274                 if (tevent_req_nomem(cc_state->subreq, req)) {
275                         return tevent_req_post(req, ev);
276                 }
277                 tevent_req_set_callback(cc_state->subreq,
278                                         btrfs_offload_write_done,
279                                         req);
280                 return req;
281         }
282
283         status = vfs_offload_token_check_handles(
284                 fsctl, src_fsp, dest_fsp);
285         if (!NT_STATUS_IS_OK(status)) {
286                 tevent_req_nterror(req, status);
287                 return tevent_req_post(req, ev);
288         }
289
290         status = vfs_stat_fsp(src_fsp);
291         if (tevent_req_nterror(req, status)) {
292                 return tevent_req_post(req, ev);
293         }
294
295         if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
296                 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
297                 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
298                 return tevent_req_post(req, ev);
299         }
300
301         if (do_locking) {
302                 init_strict_lock_struct(src_fsp,
303                                         src_fsp->op->global->open_persistent_id,
304                                         src_off,
305                                         num,
306                                         READ_LOCK,
307                                         &src_lck);
308                 init_strict_lock_struct(dest_fsp,
309                                        dest_fsp->op->global->open_persistent_id,
310                                         dest_off,
311                                         num,
312                                         WRITE_LOCK,
313                                         &dest_lck);
314
315                 if (!SMB_VFS_STRICT_LOCK_CHECK(src_fsp->conn, src_fsp, &src_lck)) {
316                         tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
317                         return tevent_req_post(req, ev);
318                 }
319                 if (!SMB_VFS_STRICT_LOCK_CHECK(dest_fsp->conn, dest_fsp, &dest_lck)) {
320                         tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
321                         return tevent_req_post(req, ev);
322                 }
323         }
324
325         ZERO_STRUCT(cr_args);
326         cr_args.src_fd = src_fsp->fh->fd;
327         cr_args.src_offset = (uint64_t)src_off;
328         cr_args.dest_offset = (uint64_t)dest_off;
329         cr_args.src_length = (uint64_t)num;
330
331         ret = ioctl(dest_fsp->fh->fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
332         if (ret < 0) {
333                 /*
334                  * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
335                  * cloning. Which is 4096 by default, therefore fall back to
336                  * manual read/write on failure.
337                  */
338                 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
339                           "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
340                           strerror(errno),
341                           (unsigned long long)cr_args.src_length,
342                           (long long)cr_args.src_fd,
343                           (unsigned long long)cr_args.src_offset,
344                           dest_fsp->fh->fd,
345                           (unsigned long long)cr_args.dest_offset));
346                 cc_state->subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
347                                                                 cc_state, ev,
348                                                                 fsctl,
349                                                                 token,
350                                                                 transfer_offset,
351                                                                 dest_fsp,
352                                                                 dest_off,
353                                                                 num);
354                 if (tevent_req_nomem(cc_state->subreq, req)) {
355                         return tevent_req_post(req, ev);
356                 }
357                 /* wait for subreq completion */
358                 tevent_req_set_callback(cc_state->subreq,
359                                         btrfs_offload_write_done,
360                                         req);
361                 return req;
362         }
363
364         DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
365         /* BTRFS_IOC_CLONE_RANGE is all or nothing */
366         cc_state->copied = num;
367         tevent_req_done(req);
368         return tevent_req_post(req, ev);
369 }
370
371 /* only used if the request is passed through to next VFS module */
372 static void btrfs_offload_write_done(struct tevent_req *subreq)
373 {
374         struct tevent_req *req = tevent_req_callback_data(
375                 subreq, struct tevent_req);
376         struct btrfs_cc_state *cc_state = tevent_req_data(req,
377                                                         struct btrfs_cc_state);
378         NTSTATUS status;
379
380         status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(cc_state->handle,
381                                               cc_state->subreq,
382                                               &cc_state->copied);
383         if (tevent_req_nterror(req, status)) {
384                 return;
385         }
386         tevent_req_done(req);
387 }
388
389 static NTSTATUS btrfs_offload_write_recv(struct vfs_handle_struct *handle,
390                                       struct tevent_req *req,
391                                       off_t *copied)
392 {
393         NTSTATUS status;
394         struct btrfs_cc_state *cc_state = tevent_req_data(req,
395                                                         struct btrfs_cc_state);
396
397         if (tevent_req_is_nterror(req, &status)) {
398                 DEBUG(4, ("server side copy chunk failed: %s\n",
399                           nt_errstr(status)));
400                 tevent_req_received(req);
401                 return status;
402         }
403
404         DEBUG(10, ("server side copy chunk copied %llu\n",
405                    (unsigned long long)cc_state->copied));
406         *copied = cc_state->copied;
407         tevent_req_received(req);
408         return NT_STATUS_OK;
409 }
410
411 /*
412  * caller must pass a non-null fsp or smb_fname. If fsp is null, then
413  * fall back to opening the corresponding file to issue the ioctl.
414  */
415 static NTSTATUS btrfs_get_compression(struct vfs_handle_struct *handle,
416                                       TALLOC_CTX *mem_ctx,
417                                       struct files_struct *fsp,
418                                       struct smb_filename *smb_fname,
419                                       uint16_t *_compression_fmt)
420 {
421         int ret;
422         long flags = 0;
423         int fd;
424         bool opened = false;
425         NTSTATUS status;
426         DIR *dir = NULL;
427
428         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
429                 fd = fsp->fh->fd;
430         } else if (smb_fname != NULL) {
431                 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
432                         dir = opendir(smb_fname->base_name);
433                         if (dir == NULL) {
434                                 return NT_STATUS_UNSUCCESSFUL;
435                         }
436                         opened = true;
437                         fd = dirfd(dir);
438                         if (fd < 0) {
439                                 status = NT_STATUS_UNSUCCESSFUL;
440                                 goto err_close;
441                         }
442                 } else {
443                         fd = open(smb_fname->base_name, O_RDONLY);
444                         if (fd < 0) {
445                                 return NT_STATUS_UNSUCCESSFUL;
446                         }
447                         opened = true;
448                 }
449         } else {
450                 return NT_STATUS_INVALID_PARAMETER;
451         }
452
453         ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
454         if (ret < 0) {
455                 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
456                           strerror(errno), (long long)fd));
457                 status = map_nt_error_from_unix(errno);
458                 goto err_close;
459         }
460         if (flags & FS_COMPR_FL) {
461                 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
462         } else {
463                 *_compression_fmt = COMPRESSION_FORMAT_NONE;
464         }
465         status = NT_STATUS_OK;
466 err_close:
467         if (opened) {
468                 if (dir != NULL) {
469                         closedir(dir);
470                 } else {
471                         close(fd);
472                 }
473         }
474
475         return status;
476 }
477
478 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
479                                       TALLOC_CTX *mem_ctx,
480                                       struct files_struct *fsp,
481                                       uint16_t compression_fmt)
482 {
483         int ret;
484         long flags = 0;
485         int fd;
486         NTSTATUS status;
487
488         if ((fsp == NULL) || (fsp->fh->fd == -1)) {
489                 status = NT_STATUS_INVALID_PARAMETER;
490                 goto err_out;
491         }
492         fd = fsp->fh->fd;
493
494         ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
495         if (ret < 0) {
496                 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
497                           strerror(errno), fd));
498                 status = map_nt_error_from_unix(errno);
499                 goto err_out;
500         }
501
502         if (compression_fmt == COMPRESSION_FORMAT_NONE) {
503                 DEBUG(5, ("setting compression\n"));
504                 flags &= (~FS_COMPR_FL);
505         } else if ((compression_fmt == COMPRESSION_FORMAT_DEFAULT)
506                 || (compression_fmt == COMPRESSION_FORMAT_LZNT1)) {
507                 DEBUG(5, ("clearing compression\n"));
508                 flags |= FS_COMPR_FL;
509         } else {
510                 DEBUG(1, ("invalid compression format 0x%x\n",
511                           (int)compression_fmt));
512                 status = NT_STATUS_INVALID_PARAMETER;
513                 goto err_out;
514         }
515
516         ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
517         if (ret < 0) {
518                 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
519                           strerror(errno), fd));
520                 status = map_nt_error_from_unix(errno);
521                 goto err_out;
522         }
523         status = NT_STATUS_OK;
524 err_out:
525         return status;
526 }
527
528 /*
529  * Check whether a path can be shadow copied. Return the base volume, allowing
530  * the caller to determine if multiple paths lie on the same base volume.
531  */
532 #define BTRFS_INODE_SUBVOL 256
533 static NTSTATUS btrfs_snap_check_path(struct vfs_handle_struct *handle,
534                                       TALLOC_CTX *mem_ctx,
535                                       const char *service_path,
536                                       char **base_volume)
537 {
538         struct stat st;
539         char *base;
540
541         if (!lp_parm_bool(SNUM(handle->conn),
542                          "btrfs", "manipulate snapshots", false)) {
543                 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
544                 return SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx,
545                                                     service_path, base_volume);
546         }
547
548         /* btrfs userspace uses this logic to confirm subvolume */
549         if (stat(service_path, &st) < 0) {
550                 return NT_STATUS_NOT_SUPPORTED;
551         }
552         if ((st.st_ino != BTRFS_INODE_SUBVOL) || !S_ISDIR(st.st_mode)) {
553                 DEBUG(0, ("%s not a btrfs subvolume, snapshots not available\n",
554                           service_path));
555                 return NT_STATUS_NOT_SUPPORTED;
556         }
557
558         /* we "snapshot" the service path itself */
559         base = talloc_strdup(mem_ctx, service_path);
560         if (base == NULL) {
561                 return NT_STATUS_NO_MEMORY;
562         }
563         *base_volume = base;
564
565         return NT_STATUS_OK;
566 }
567
568 static NTSTATUS btrfs_gen_snap_dest_path(TALLOC_CTX *mem_ctx,
569                                          const char *src_path,
570                                          time_t *tstamp,
571                                          char **dest_path, char **subvolume)
572 {
573         struct tm t_gmt;
574         char time_str[50];
575         size_t tlen;
576
577         gmtime_r(tstamp, &t_gmt);
578
579         tlen = strftime(time_str, ARRAY_SIZE(time_str),
580                         SHADOW_COPY_PATH_FORMAT, &t_gmt);
581         if (tlen <= 0) {
582                 return NT_STATUS_UNSUCCESSFUL;
583         }
584
585         *dest_path = talloc_strdup(mem_ctx, src_path);
586         *subvolume = talloc_strdup(mem_ctx, time_str);
587         if ((*dest_path == NULL) || (*subvolume == NULL)) {
588                 return NT_STATUS_NO_MEMORY;
589         }
590
591         return NT_STATUS_OK;
592 }
593
594 static NTSTATUS btrfs_snap_create(struct vfs_handle_struct *handle,
595                                   TALLOC_CTX *mem_ctx,
596                                   const char *base_volume,
597                                   time_t *tstamp,
598                                   bool rw,
599                                   char **_base_path,
600                                   char **_snap_path)
601 {
602         struct btrfs_ioctl_vol_args_v2 ioctl_arg;
603         DIR *src_dir;
604         DIR *dest_dir;
605         int src_fd;
606         int dest_fd;
607         char *dest_path = NULL;
608         char *dest_subvolume = NULL;
609         int ret;
610         NTSTATUS status;
611         char *base_path;
612         char *snap_path;
613         TALLOC_CTX *tmp_ctx;
614         int saved_errno;
615         size_t len;
616
617         if (!lp_parm_bool(SNUM(handle->conn),
618                           "btrfs", "manipulate snapshots", false)) {
619                 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
620                 return SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume,
621                                                 tstamp, rw, _base_path,
622                                                 _snap_path);
623         }
624
625         tmp_ctx = talloc_new(mem_ctx);
626         if (tmp_ctx == NULL) {
627                 return NT_STATUS_NO_MEMORY;
628         }
629
630         base_path = talloc_strdup(tmp_ctx, base_volume);
631         if (base_path == NULL) {
632                 talloc_free(tmp_ctx);
633                 return NT_STATUS_NO_MEMORY;
634         }
635
636         status = btrfs_gen_snap_dest_path(tmp_ctx, base_volume, tstamp,
637                                           &dest_path, &dest_subvolume);
638         if (!NT_STATUS_IS_OK(status)) {
639                 talloc_free(tmp_ctx);
640                 return status;
641         }
642
643         snap_path = talloc_asprintf(tmp_ctx, "%s/%s", dest_path,
644                                     dest_subvolume);
645         if (snap_path == NULL) {
646                 talloc_free(tmp_ctx);
647                 return NT_STATUS_NO_MEMORY;
648         }
649
650         src_dir = opendir(base_volume);
651         if (src_dir == NULL) {
652                 DEBUG(0, ("snap src %s open failed: %s\n",
653                           base_volume, strerror(errno)));
654                 status = map_nt_error_from_unix(errno);
655                 talloc_free(tmp_ctx);
656                 return status;
657         }
658         src_fd = dirfd(src_dir);
659         if (src_fd < 0) {
660                 status = map_nt_error_from_unix(errno);
661                 closedir(src_dir);
662                 talloc_free(tmp_ctx);
663                 return status;
664         }
665
666         dest_dir = opendir(dest_path);
667         if (dest_dir == NULL) {
668                 DEBUG(0, ("snap dest %s open failed: %s\n",
669                           dest_path, strerror(errno)));
670                 status = map_nt_error_from_unix(errno);
671                 closedir(src_dir);
672                 talloc_free(tmp_ctx);
673                 return status;
674         }
675         dest_fd = dirfd(dest_dir);
676         if (dest_fd < 0) {
677                 status = map_nt_error_from_unix(errno);
678                 closedir(src_dir);
679                 closedir(dest_dir);
680                 talloc_free(tmp_ctx);
681                 return status;
682         }
683
684         /* avoid zeroing the entire struct here, name is 4k */
685         ioctl_arg.fd = src_fd;
686         ioctl_arg.transid = 0;
687         ioctl_arg.flags = (rw == false) ? BTRFS_SUBVOL_RDONLY : 0;
688         memset(ioctl_arg.unused, 0, sizeof(ioctl_arg.unused));
689         len = strlcpy(ioctl_arg.name, dest_subvolume,
690                       ARRAY_SIZE(ioctl_arg.name));
691         if (len >= ARRAY_SIZE(ioctl_arg.name)) {
692                 DEBUG(1, ("subvolume name too long for SNAP_CREATE ioctl\n"));
693                 closedir(src_dir);
694                 closedir(dest_dir);
695                 talloc_free(tmp_ctx);
696                 return NT_STATUS_INVALID_PARAMETER;
697         }
698
699         become_root();
700         ret = ioctl(dest_fd, BTRFS_IOC_SNAP_CREATE_V2, &ioctl_arg);
701         saved_errno = errno;
702         unbecome_root();
703         if (ret < 0) {
704                 DEBUG(0, ("%s -> %s(%s) BTRFS_IOC_SNAP_CREATE_V2 failed: %s\n",
705                           base_volume, dest_path, dest_subvolume,
706                           strerror(saved_errno)));
707                 status = map_nt_error_from_unix(saved_errno);
708                 closedir(src_dir);
709                 closedir(dest_dir);
710                 talloc_free(tmp_ctx);
711                 return status;
712         }
713         DEBUG(5, ("%s -> %s(%s) BTRFS_IOC_SNAP_CREATE_V2 done\n",
714                   base_volume, dest_path, dest_subvolume));
715
716         *_base_path = talloc_steal(mem_ctx, base_path);
717         *_snap_path = talloc_steal(mem_ctx, snap_path);
718         closedir(src_dir);
719         closedir(dest_dir);
720         talloc_free(tmp_ctx);
721
722         return NT_STATUS_OK;
723 }
724
725 static NTSTATUS btrfs_snap_delete(struct vfs_handle_struct *handle,
726                                   TALLOC_CTX *mem_ctx,
727                                   char *base_path,
728                                   char *snap_path)
729 {
730         char *tstr;
731         struct tm t_gmt;
732         DIR *dest_dir;
733         int dest_fd;
734         struct btrfs_ioctl_vol_args ioctl_arg;
735         int ret;
736         NTSTATUS status;
737         char *dest_path;
738         char *subvolume;
739         TALLOC_CTX *tmp_ctx;
740         int saved_errno;
741         size_t len;
742
743         if (!lp_parm_bool(SNUM(handle->conn),
744                           "btrfs", "manipulate snapshots", false)) {
745                 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
746                 return SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx,
747                                                 base_path, snap_path);
748         }
749
750         tmp_ctx = talloc_new(mem_ctx);
751         if (tmp_ctx == NULL) {
752                 return NT_STATUS_NO_MEMORY;
753         }
754
755         dest_path = talloc_strdup(tmp_ctx, snap_path);
756         if (dest_path == NULL) {
757                 talloc_free(tmp_ctx);
758                 return NT_STATUS_NO_MEMORY;
759         }
760         subvolume = talloc_strdup(tmp_ctx, snap_path);
761         if (subvolume == NULL) {
762                 talloc_free(tmp_ctx);
763                 return NT_STATUS_NO_MEMORY;
764         }
765         dest_path = dirname(dest_path);
766         subvolume = basename(subvolume);
767
768         /* confirm snap_path matches creation format */
769         tstr = strptime(subvolume, SHADOW_COPY_PATH_FORMAT, &t_gmt);
770         if ((tstr == NULL) || (*tstr != '\0')) {
771                 DEBUG(0, ("snapshot path %s does not match creation format\n",
772                           snap_path));
773                 talloc_free(tmp_ctx);
774                 return NT_STATUS_UNSUCCESSFUL;
775         }
776
777         dest_dir = opendir(dest_path);
778         if (dest_dir == NULL) {
779                 DEBUG(0, ("snap destroy dest %s open failed: %s\n",
780                           dest_path, strerror(errno)));
781                 status = map_nt_error_from_unix(errno);
782                 talloc_free(tmp_ctx);
783                 return status;
784         }
785         dest_fd = dirfd(dest_dir);
786         if (dest_fd < 0) {
787                 status = map_nt_error_from_unix(errno);
788                 closedir(dest_dir);
789                 talloc_free(tmp_ctx);
790                 return status;
791         }
792
793         ioctl_arg.fd = -1;      /* not needed */
794         len = strlcpy(ioctl_arg.name, subvolume, ARRAY_SIZE(ioctl_arg.name));
795         if (len >= ARRAY_SIZE(ioctl_arg.name)) {
796                 DEBUG(1, ("subvolume name too long for SNAP_DESTROY ioctl\n"));
797                 closedir(dest_dir);
798                 talloc_free(tmp_ctx);
799                 return NT_STATUS_INVALID_PARAMETER;
800         }
801
802         become_root();
803         ret = ioctl(dest_fd, BTRFS_IOC_SNAP_DESTROY, &ioctl_arg);
804         saved_errno = errno;
805         unbecome_root();
806         if (ret < 0) {
807                 DEBUG(0, ("%s(%s) BTRFS_IOC_SNAP_DESTROY failed: %s\n",
808                           dest_path, subvolume, strerror(saved_errno)));
809                 status = map_nt_error_from_unix(saved_errno);
810                 closedir(dest_dir);
811                 talloc_free(tmp_ctx);
812                 return status;
813         }
814         DEBUG(5, ("%s(%s) BTRFS_IOC_SNAP_DESTROY done\n",
815                   dest_path, subvolume));
816
817         closedir(dest_dir);
818         talloc_free(tmp_ctx);
819         return NT_STATUS_OK;
820 }
821
822 static struct vfs_fn_pointers btrfs_fns = {
823         .fs_capabilities_fn = btrfs_fs_capabilities,
824         .offload_read_send_fn = btrfs_offload_read_send,
825         .offload_read_recv_fn = btrfs_offload_read_recv,
826         .offload_write_send_fn = btrfs_offload_write_send,
827         .offload_write_recv_fn = btrfs_offload_write_recv,
828         .get_compression_fn = btrfs_get_compression,
829         .set_compression_fn = btrfs_set_compression,
830         .snap_check_path_fn = btrfs_snap_check_path,
831         .snap_create_fn = btrfs_snap_create,
832         .snap_delete_fn = btrfs_snap_delete,
833 };
834
835 static_decl_vfs;
836 NTSTATUS vfs_btrfs_init(TALLOC_CTX *ctx)
837 {
838         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
839                                 "btrfs", &btrfs_fns);
840 }