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