vfs_btrfs: update s/btrfs_cc_state/btrfs_offload_write_state/ s/cc_state/state/
[Samba.git] / source3 / modules / vfs_btrfs.c
blobcc082f7fbbf324df22ef39e0a3c14df99c1cc160
1 /*
2 * Module to make use of awesome Btrfs features
4 * Copyright (C) David Disseldorp 2011-2013
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.
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.
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/>.
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"
36 static uint32_t btrfs_fs_capabilities(struct vfs_handle_struct *handle,
37 enum timestamp_set_resolution *_ts_res)
39 uint32_t fs_capabilities;
40 enum timestamp_set_resolution ts_res;
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;
48 return fs_capabilities;
51 #define SHADOW_COPY_PREFIX "@GMT-" /* vfs_shadow_copy format */
52 #define SHADOW_COPY_PATH_FORMAT "@GMT-%Y.%m.%d-%H.%M.%S"
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];
64 struct btrfs_ioctl_vol_args {
65 int64_t fd;
66 char name[BTRFS_PATH_NAME_MAX + 1];
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;
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)
84 static struct vfs_offload_ctx *btrfs_offload_ctx;
86 struct btrfs_offload_read_state {
87 struct vfs_handle_struct *handle;
88 files_struct *fsp;
89 DATA_BLOB token;
92 static void btrfs_offload_read_done(struct tevent_req *subreq);
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)
104 struct tevent_req *req = NULL;
105 struct tevent_req *subreq = NULL;
106 struct btrfs_offload_read_state *state = NULL;
107 NTSTATUS status;
109 req = tevent_req_create(mem_ctx, &state,
110 struct btrfs_offload_read_state);
111 if (req == NULL) {
112 return NULL;
114 *state = (struct btrfs_offload_read_state) {
115 .handle = handle,
116 .fsp = fsp,
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);
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);
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);
137 tevent_req_done(req);
138 return tevent_req_post(req, ev);
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);
146 tevent_req_set_callback(subreq, btrfs_offload_read_done, req);
147 return req;
150 static void btrfs_offload_read_done(struct tevent_req *subreq)
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;
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;
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;
174 tevent_req_done(req);
175 return;
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)
183 struct btrfs_offload_read_state *state = tevent_req_data(
184 req, struct btrfs_offload_read_state);
185 NTSTATUS status;
187 if (tevent_req_is_nterror(req, &status)) {
188 tevent_req_received(req);
189 return status;
192 token->length = state->token.length;
193 token->data = talloc_move(mem_ctx, &state->token.data);
195 tevent_req_received(req);
196 return NT_STATUS_OK;
199 struct btrfs_offload_write_state {
200 struct vfs_handle_struct *handle;
201 off_t copied;
202 struct tevent_req *subreq; /* non-null if passed to next VFS fn */
204 static void btrfs_offload_write_done(struct tevent_req *subreq);
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)
216 struct tevent_req *req = NULL;
217 struct btrfs_offload_write_state *state = NULL;
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;
228 req = tevent_req_create(mem_ctx, &state,
229 struct btrfs_offload_write_state);
230 if (req == NULL) {
231 return NULL;
234 state->handle = handle;
236 status = vfs_offload_token_db_fetch_fsp(btrfs_offload_ctx,
237 token, &src_fsp);
238 if (tevent_req_nterror(req, status)) {
239 return tevent_req_post(req, ev);
242 switch (fsctl) {
243 case FSCTL_SRV_COPYCHUNK:
244 case FSCTL_SRV_COPYCHUNK_WRITE:
245 do_locking = true;
246 break;
248 case FSCTL_DUP_EXTENTS_TO_FILE:
249 /* dup extents does not use locking */
250 break;
252 default:
253 handle_offload_write = false;
254 break;
257 if (num == 0) {
259 * With a @src_length of zero, BTRFS_IOC_CLONE_RANGE clones
260 * all data from @src_offset->EOF! This is certainly not what
261 * the caller expects, and not what vfs_default does.
263 handle_offload_write = false;
266 if (!handle_offload_write) {
267 state->subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
268 state,
270 fsctl,
271 token,
272 transfer_offset,
273 dest_fsp,
274 dest_off,
275 num);
276 if (tevent_req_nomem(state->subreq, req)) {
277 return tevent_req_post(req, ev);
279 tevent_req_set_callback(state->subreq,
280 btrfs_offload_write_done,
281 req);
282 return req;
285 status = vfs_offload_token_check_handles(
286 fsctl, src_fsp, dest_fsp);
287 if (!NT_STATUS_IS_OK(status)) {
288 tevent_req_nterror(req, status);
289 return tevent_req_post(req, ev);
292 status = vfs_stat_fsp(src_fsp);
293 if (tevent_req_nterror(req, status)) {
294 return tevent_req_post(req, ev);
297 if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
298 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
299 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
300 return tevent_req_post(req, ev);
303 if (do_locking) {
304 init_strict_lock_struct(src_fsp,
305 src_fsp->op->global->open_persistent_id,
306 src_off,
307 num,
308 READ_LOCK,
309 &src_lck);
310 init_strict_lock_struct(dest_fsp,
311 dest_fsp->op->global->open_persistent_id,
312 dest_off,
313 num,
314 WRITE_LOCK,
315 &dest_lck);
317 if (!SMB_VFS_STRICT_LOCK_CHECK(src_fsp->conn, src_fsp, &src_lck)) {
318 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
319 return tevent_req_post(req, ev);
321 if (!SMB_VFS_STRICT_LOCK_CHECK(dest_fsp->conn, dest_fsp, &dest_lck)) {
322 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
323 return tevent_req_post(req, ev);
327 ZERO_STRUCT(cr_args);
328 cr_args.src_fd = src_fsp->fh->fd;
329 cr_args.src_offset = (uint64_t)src_off;
330 cr_args.dest_offset = (uint64_t)dest_off;
331 cr_args.src_length = (uint64_t)num;
333 ret = ioctl(dest_fsp->fh->fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
334 if (ret < 0) {
336 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
337 * cloning. Which is 4096 by default, therefore fall back to
338 * manual read/write on failure.
340 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
341 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
342 strerror(errno),
343 (unsigned long long)cr_args.src_length,
344 (long long)cr_args.src_fd,
345 (unsigned long long)cr_args.src_offset,
346 dest_fsp->fh->fd,
347 (unsigned long long)cr_args.dest_offset));
348 state->subreq = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle,
349 state,
351 fsctl,
352 token,
353 transfer_offset,
354 dest_fsp,
355 dest_off,
356 num);
357 if (tevent_req_nomem(state->subreq, req)) {
358 return tevent_req_post(req, ev);
360 /* wait for subreq completion */
361 tevent_req_set_callback(state->subreq,
362 btrfs_offload_write_done,
363 req);
364 return req;
367 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
368 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
369 state->copied = num;
370 tevent_req_done(req);
371 return tevent_req_post(req, ev);
374 /* only used if the request is passed through to next VFS module */
375 static void btrfs_offload_write_done(struct tevent_req *subreq)
377 struct tevent_req *req =
378 tevent_req_callback_data(subreq,
379 struct tevent_req);
380 struct btrfs_offload_write_state *state =
381 tevent_req_data(req,
382 struct btrfs_offload_write_state);
383 NTSTATUS status;
385 status = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state->handle,
386 state->subreq,
387 &state->copied);
388 if (tevent_req_nterror(req, status)) {
389 return;
391 tevent_req_done(req);
394 static NTSTATUS btrfs_offload_write_recv(struct vfs_handle_struct *handle,
395 struct tevent_req *req,
396 off_t *copied)
398 struct btrfs_offload_write_state *state =
399 tevent_req_data(req,
400 struct btrfs_offload_write_state);
401 NTSTATUS status;
403 if (tevent_req_is_nterror(req, &status)) {
404 DEBUG(4, ("server side copy chunk failed: %s\n",
405 nt_errstr(status)));
406 tevent_req_received(req);
407 return status;
410 DEBUG(10, ("server side copy chunk copied %llu\n",
411 (unsigned long long)state->copied));
412 *copied = state->copied;
413 tevent_req_received(req);
414 return NT_STATUS_OK;
418 * caller must pass a non-null fsp or smb_fname. If fsp is null, then
419 * fall back to opening the corresponding file to issue the ioctl.
421 static NTSTATUS btrfs_get_compression(struct vfs_handle_struct *handle,
422 TALLOC_CTX *mem_ctx,
423 struct files_struct *fsp,
424 struct smb_filename *smb_fname,
425 uint16_t *_compression_fmt)
427 int ret;
428 long flags = 0;
429 int fd;
430 bool opened = false;
431 NTSTATUS status;
432 DIR *dir = NULL;
434 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
435 fd = fsp->fh->fd;
436 } else if (smb_fname != NULL) {
437 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
438 dir = opendir(smb_fname->base_name);
439 if (dir == NULL) {
440 return NT_STATUS_UNSUCCESSFUL;
442 opened = true;
443 fd = dirfd(dir);
444 if (fd < 0) {
445 status = NT_STATUS_UNSUCCESSFUL;
446 goto err_close;
448 } else {
449 fd = open(smb_fname->base_name, O_RDONLY);
450 if (fd < 0) {
451 return NT_STATUS_UNSUCCESSFUL;
453 opened = true;
455 } else {
456 return NT_STATUS_INVALID_PARAMETER;
459 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
460 if (ret < 0) {
461 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
462 strerror(errno), (long long)fd));
463 status = map_nt_error_from_unix(errno);
464 goto err_close;
466 if (flags & FS_COMPR_FL) {
467 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
468 } else {
469 *_compression_fmt = COMPRESSION_FORMAT_NONE;
471 status = NT_STATUS_OK;
472 err_close:
473 if (opened) {
474 if (dir != NULL) {
475 closedir(dir);
476 } else {
477 close(fd);
481 return status;
484 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
485 TALLOC_CTX *mem_ctx,
486 struct files_struct *fsp,
487 uint16_t compression_fmt)
489 int ret;
490 long flags = 0;
491 int fd;
492 NTSTATUS status;
494 if ((fsp == NULL) || (fsp->fh->fd == -1)) {
495 status = NT_STATUS_INVALID_PARAMETER;
496 goto err_out;
498 fd = fsp->fh->fd;
500 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
501 if (ret < 0) {
502 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
503 strerror(errno), fd));
504 status = map_nt_error_from_unix(errno);
505 goto err_out;
508 if (compression_fmt == COMPRESSION_FORMAT_NONE) {
509 DEBUG(5, ("setting compression\n"));
510 flags &= (~FS_COMPR_FL);
511 } else if ((compression_fmt == COMPRESSION_FORMAT_DEFAULT)
512 || (compression_fmt == COMPRESSION_FORMAT_LZNT1)) {
513 DEBUG(5, ("clearing compression\n"));
514 flags |= FS_COMPR_FL;
515 } else {
516 DEBUG(1, ("invalid compression format 0x%x\n",
517 (int)compression_fmt));
518 status = NT_STATUS_INVALID_PARAMETER;
519 goto err_out;
522 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
523 if (ret < 0) {
524 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
525 strerror(errno), fd));
526 status = map_nt_error_from_unix(errno);
527 goto err_out;
529 status = NT_STATUS_OK;
530 err_out:
531 return status;
535 * Check whether a path can be shadow copied. Return the base volume, allowing
536 * the caller to determine if multiple paths lie on the same base volume.
538 #define BTRFS_INODE_SUBVOL 256
539 static NTSTATUS btrfs_snap_check_path(struct vfs_handle_struct *handle,
540 TALLOC_CTX *mem_ctx,
541 const char *service_path,
542 char **base_volume)
544 struct stat st;
545 char *base;
547 if (!lp_parm_bool(SNUM(handle->conn),
548 "btrfs", "manipulate snapshots", false)) {
549 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
550 return SMB_VFS_NEXT_SNAP_CHECK_PATH(handle, mem_ctx,
551 service_path, base_volume);
554 /* btrfs userspace uses this logic to confirm subvolume */
555 if (stat(service_path, &st) < 0) {
556 return NT_STATUS_NOT_SUPPORTED;
558 if ((st.st_ino != BTRFS_INODE_SUBVOL) || !S_ISDIR(st.st_mode)) {
559 DEBUG(0, ("%s not a btrfs subvolume, snapshots not available\n",
560 service_path));
561 return NT_STATUS_NOT_SUPPORTED;
564 /* we "snapshot" the service path itself */
565 base = talloc_strdup(mem_ctx, service_path);
566 if (base == NULL) {
567 return NT_STATUS_NO_MEMORY;
569 *base_volume = base;
571 return NT_STATUS_OK;
574 static NTSTATUS btrfs_gen_snap_dest_path(TALLOC_CTX *mem_ctx,
575 const char *src_path,
576 time_t *tstamp,
577 char **dest_path, char **subvolume)
579 struct tm t_gmt;
580 char time_str[50];
581 size_t tlen;
583 gmtime_r(tstamp, &t_gmt);
585 tlen = strftime(time_str, ARRAY_SIZE(time_str),
586 SHADOW_COPY_PATH_FORMAT, &t_gmt);
587 if (tlen <= 0) {
588 return NT_STATUS_UNSUCCESSFUL;
591 *dest_path = talloc_strdup(mem_ctx, src_path);
592 *subvolume = talloc_strdup(mem_ctx, time_str);
593 if ((*dest_path == NULL) || (*subvolume == NULL)) {
594 return NT_STATUS_NO_MEMORY;
597 return NT_STATUS_OK;
600 static NTSTATUS btrfs_snap_create(struct vfs_handle_struct *handle,
601 TALLOC_CTX *mem_ctx,
602 const char *base_volume,
603 time_t *tstamp,
604 bool rw,
605 char **_base_path,
606 char **_snap_path)
608 struct btrfs_ioctl_vol_args_v2 ioctl_arg;
609 DIR *src_dir;
610 DIR *dest_dir;
611 int src_fd;
612 int dest_fd;
613 char *dest_path = NULL;
614 char *dest_subvolume = NULL;
615 int ret;
616 NTSTATUS status;
617 char *base_path;
618 char *snap_path;
619 TALLOC_CTX *tmp_ctx;
620 int saved_errno;
621 size_t len;
623 if (!lp_parm_bool(SNUM(handle->conn),
624 "btrfs", "manipulate snapshots", false)) {
625 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
626 return SMB_VFS_NEXT_SNAP_CREATE(handle, mem_ctx, base_volume,
627 tstamp, rw, _base_path,
628 _snap_path);
631 tmp_ctx = talloc_new(mem_ctx);
632 if (tmp_ctx == NULL) {
633 return NT_STATUS_NO_MEMORY;
636 base_path = talloc_strdup(tmp_ctx, base_volume);
637 if (base_path == NULL) {
638 talloc_free(tmp_ctx);
639 return NT_STATUS_NO_MEMORY;
642 status = btrfs_gen_snap_dest_path(tmp_ctx, base_volume, tstamp,
643 &dest_path, &dest_subvolume);
644 if (!NT_STATUS_IS_OK(status)) {
645 talloc_free(tmp_ctx);
646 return status;
649 snap_path = talloc_asprintf(tmp_ctx, "%s/%s", dest_path,
650 dest_subvolume);
651 if (snap_path == NULL) {
652 talloc_free(tmp_ctx);
653 return NT_STATUS_NO_MEMORY;
656 src_dir = opendir(base_volume);
657 if (src_dir == NULL) {
658 DEBUG(0, ("snap src %s open failed: %s\n",
659 base_volume, strerror(errno)));
660 status = map_nt_error_from_unix(errno);
661 talloc_free(tmp_ctx);
662 return status;
664 src_fd = dirfd(src_dir);
665 if (src_fd < 0) {
666 status = map_nt_error_from_unix(errno);
667 closedir(src_dir);
668 talloc_free(tmp_ctx);
669 return status;
672 dest_dir = opendir(dest_path);
673 if (dest_dir == NULL) {
674 DEBUG(0, ("snap dest %s open failed: %s\n",
675 dest_path, strerror(errno)));
676 status = map_nt_error_from_unix(errno);
677 closedir(src_dir);
678 talloc_free(tmp_ctx);
679 return status;
681 dest_fd = dirfd(dest_dir);
682 if (dest_fd < 0) {
683 status = map_nt_error_from_unix(errno);
684 closedir(src_dir);
685 closedir(dest_dir);
686 talloc_free(tmp_ctx);
687 return status;
690 /* avoid zeroing the entire struct here, name is 4k */
691 ioctl_arg.fd = src_fd;
692 ioctl_arg.transid = 0;
693 ioctl_arg.flags = (rw == false) ? BTRFS_SUBVOL_RDONLY : 0;
694 memset(ioctl_arg.unused, 0, sizeof(ioctl_arg.unused));
695 len = strlcpy(ioctl_arg.name, dest_subvolume,
696 ARRAY_SIZE(ioctl_arg.name));
697 if (len >= ARRAY_SIZE(ioctl_arg.name)) {
698 DEBUG(1, ("subvolume name too long for SNAP_CREATE ioctl\n"));
699 closedir(src_dir);
700 closedir(dest_dir);
701 talloc_free(tmp_ctx);
702 return NT_STATUS_INVALID_PARAMETER;
705 become_root();
706 ret = ioctl(dest_fd, BTRFS_IOC_SNAP_CREATE_V2, &ioctl_arg);
707 saved_errno = errno;
708 unbecome_root();
709 if (ret < 0) {
710 DEBUG(0, ("%s -> %s(%s) BTRFS_IOC_SNAP_CREATE_V2 failed: %s\n",
711 base_volume, dest_path, dest_subvolume,
712 strerror(saved_errno)));
713 status = map_nt_error_from_unix(saved_errno);
714 closedir(src_dir);
715 closedir(dest_dir);
716 talloc_free(tmp_ctx);
717 return status;
719 DEBUG(5, ("%s -> %s(%s) BTRFS_IOC_SNAP_CREATE_V2 done\n",
720 base_volume, dest_path, dest_subvolume));
722 *_base_path = talloc_steal(mem_ctx, base_path);
723 *_snap_path = talloc_steal(mem_ctx, snap_path);
724 closedir(src_dir);
725 closedir(dest_dir);
726 talloc_free(tmp_ctx);
728 return NT_STATUS_OK;
731 static NTSTATUS btrfs_snap_delete(struct vfs_handle_struct *handle,
732 TALLOC_CTX *mem_ctx,
733 char *base_path,
734 char *snap_path)
736 char *tstr;
737 struct tm t_gmt;
738 DIR *dest_dir;
739 int dest_fd;
740 struct btrfs_ioctl_vol_args ioctl_arg;
741 int ret;
742 NTSTATUS status;
743 char *dest_path;
744 char *subvolume;
745 TALLOC_CTX *tmp_ctx;
746 int saved_errno;
747 size_t len;
749 if (!lp_parm_bool(SNUM(handle->conn),
750 "btrfs", "manipulate snapshots", false)) {
751 DEBUG(2, ("Btrfs snapshot manipulation disabled, passing\n"));
752 return SMB_VFS_NEXT_SNAP_DELETE(handle, mem_ctx,
753 base_path, snap_path);
756 tmp_ctx = talloc_new(mem_ctx);
757 if (tmp_ctx == NULL) {
758 return NT_STATUS_NO_MEMORY;
761 dest_path = talloc_strdup(tmp_ctx, snap_path);
762 if (dest_path == NULL) {
763 talloc_free(tmp_ctx);
764 return NT_STATUS_NO_MEMORY;
766 subvolume = talloc_strdup(tmp_ctx, snap_path);
767 if (subvolume == NULL) {
768 talloc_free(tmp_ctx);
769 return NT_STATUS_NO_MEMORY;
771 dest_path = dirname(dest_path);
772 subvolume = basename(subvolume);
774 /* confirm snap_path matches creation format */
775 tstr = strptime(subvolume, SHADOW_COPY_PATH_FORMAT, &t_gmt);
776 if ((tstr == NULL) || (*tstr != '\0')) {
777 DEBUG(0, ("snapshot path %s does not match creation format\n",
778 snap_path));
779 talloc_free(tmp_ctx);
780 return NT_STATUS_UNSUCCESSFUL;
783 dest_dir = opendir(dest_path);
784 if (dest_dir == NULL) {
785 DEBUG(0, ("snap destroy dest %s open failed: %s\n",
786 dest_path, strerror(errno)));
787 status = map_nt_error_from_unix(errno);
788 talloc_free(tmp_ctx);
789 return status;
791 dest_fd = dirfd(dest_dir);
792 if (dest_fd < 0) {
793 status = map_nt_error_from_unix(errno);
794 closedir(dest_dir);
795 talloc_free(tmp_ctx);
796 return status;
799 ioctl_arg.fd = -1; /* not needed */
800 len = strlcpy(ioctl_arg.name, subvolume, ARRAY_SIZE(ioctl_arg.name));
801 if (len >= ARRAY_SIZE(ioctl_arg.name)) {
802 DEBUG(1, ("subvolume name too long for SNAP_DESTROY ioctl\n"));
803 closedir(dest_dir);
804 talloc_free(tmp_ctx);
805 return NT_STATUS_INVALID_PARAMETER;
808 become_root();
809 ret = ioctl(dest_fd, BTRFS_IOC_SNAP_DESTROY, &ioctl_arg);
810 saved_errno = errno;
811 unbecome_root();
812 if (ret < 0) {
813 DEBUG(0, ("%s(%s) BTRFS_IOC_SNAP_DESTROY failed: %s\n",
814 dest_path, subvolume, strerror(saved_errno)));
815 status = map_nt_error_from_unix(saved_errno);
816 closedir(dest_dir);
817 talloc_free(tmp_ctx);
818 return status;
820 DEBUG(5, ("%s(%s) BTRFS_IOC_SNAP_DESTROY done\n",
821 dest_path, subvolume));
823 closedir(dest_dir);
824 talloc_free(tmp_ctx);
825 return NT_STATUS_OK;
828 static struct vfs_fn_pointers btrfs_fns = {
829 .fs_capabilities_fn = btrfs_fs_capabilities,
830 .offload_read_send_fn = btrfs_offload_read_send,
831 .offload_read_recv_fn = btrfs_offload_read_recv,
832 .offload_write_send_fn = btrfs_offload_write_send,
833 .offload_write_recv_fn = btrfs_offload_write_recv,
834 .get_compression_fn = btrfs_get_compression,
835 .set_compression_fn = btrfs_set_compression,
836 .snap_check_path_fn = btrfs_snap_check_path,
837 .snap_create_fn = btrfs_snap_create,
838 .snap_delete_fn = btrfs_snap_delete,
841 static_decl_vfs;
842 NTSTATUS vfs_btrfs_init(TALLOC_CTX *ctx)
844 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
845 "btrfs", &btrfs_fns);