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 <sys/ioctl.h>
23 #include "system/filesys.h"
24 #include "smbd/smbd.h"
25 #include "../librpc/gen_ndr/smbXsrv.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct btrfs_ioctl_clone_range_args
{
35 #define BTRFS_IOCTL_MAGIC 0x94
36 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
37 struct btrfs_ioctl_clone_range_args)
39 struct btrfs_cc_state
{
40 struct vfs_handle_struct
*handle
;
42 struct tevent_req
*subreq
; /* non-null if passed to next VFS fn */
44 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
);
46 static struct tevent_req
*btrfs_copy_chunk_send(struct vfs_handle_struct
*handle
,
48 struct tevent_context
*ev
,
49 struct files_struct
*src_fsp
,
51 struct files_struct
*dest_fsp
,
55 struct tevent_req
*req
;
56 struct btrfs_cc_state
*cc_state
;
57 struct btrfs_ioctl_clone_range_args cr_args
;
58 struct lock_struct src_lck
;
59 struct lock_struct dest_lck
;
63 req
= tevent_req_create(mem_ctx
, &cc_state
, struct btrfs_cc_state
);
67 cc_state
->handle
= handle
;
69 status
= vfs_stat_fsp(src_fsp
);
70 if (tevent_req_nterror(req
, status
)) {
71 return tevent_req_post(req
, ev
);
74 if (src_fsp
->fsp_name
->st
.st_ex_size
< src_off
+ num
) {
75 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
76 tevent_req_nterror(req
, NT_STATUS_INVALID_VIEW_SIZE
);
77 return tevent_req_post(req
, ev
);
80 init_strict_lock_struct(src_fsp
,
81 src_fsp
->op
->global
->open_persistent_id
,
86 init_strict_lock_struct(dest_fsp
,
87 dest_fsp
->op
->global
->open_persistent_id
,
93 if (!SMB_VFS_STRICT_LOCK(src_fsp
->conn
, src_fsp
, &src_lck
)) {
94 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
95 return tevent_req_post(req
, ev
);
97 if (!SMB_VFS_STRICT_LOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
)) {
98 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
99 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
100 return tevent_req_post(req
, ev
);
103 ZERO_STRUCT(cr_args
);
104 cr_args
.src_fd
= src_fsp
->fh
->fd
;
105 cr_args
.src_offset
= (uint64_t)src_off
;
106 cr_args
.dest_offset
= (uint64_t)dest_off
;
107 cr_args
.src_length
= (uint64_t)num
;
109 ret
= ioctl(dest_fsp
->fh
->fd
, BTRFS_IOC_CLONE_RANGE
, &cr_args
);
110 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &dest_lck
);
111 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
114 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
115 * cloning. Which is 4096 by default, therefore fall back to
116 * manual read/write on failure.
118 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %lu, "
119 "src fd: %ld off: %lu, dest fd: %d off: %lu\n",
120 strerror(errno
), cr_args
.src_length
,
121 cr_args
.src_fd
, cr_args
.src_offset
,
122 dest_fsp
->fh
->fd
, cr_args
.dest_offset
));
123 cc_state
->subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
129 if (tevent_req_nomem(cc_state
->subreq
, req
)) {
130 return tevent_req_post(req
, ev
);
132 /* wait for subreq completion */
133 tevent_req_set_callback(cc_state
->subreq
,
134 btrfs_copy_chunk_done
,
140 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret
));
141 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
142 cc_state
->copied
= num
;
143 tevent_req_done(req
);
144 return tevent_req_post(req
, ev
);
147 /* only used if the request is passed through to next VFS module */
148 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
)
150 struct tevent_req
*req
= tevent_req_callback_data(
151 subreq
, struct tevent_req
);
152 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
153 struct btrfs_cc_state
);
156 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state
->handle
,
159 if (tevent_req_nterror(req
, status
)) {
162 tevent_req_done(req
);
165 static NTSTATUS
btrfs_copy_chunk_recv(struct vfs_handle_struct
*handle
,
166 struct tevent_req
*req
,
170 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
171 struct btrfs_cc_state
);
173 if (tevent_req_is_nterror(req
, &status
)) {
174 DEBUG(4, ("server side copy chunk failed: %s\n",
176 tevent_req_received(req
);
180 DEBUG(10, ("server side copy chunk copied %lu\n", cc_state
->copied
));
181 *copied
= cc_state
->copied
;
182 tevent_req_received(req
);
186 static struct vfs_fn_pointers btrfs_fns
= {
187 .copy_chunk_send_fn
= btrfs_copy_chunk_send
,
188 .copy_chunk_recv_fn
= btrfs_copy_chunk_recv
,
191 NTSTATUS
vfs_btrfs_init(void);
192 NTSTATUS
vfs_btrfs_init(void)
194 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
195 "btrfs", &btrfs_fns
);