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 %llu, "
119 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
121 (unsigned long long)cr_args
.src_length
,
122 (long long)cr_args
.src_fd
,
123 (unsigned long long)cr_args
.src_offset
,
125 (unsigned long long)cr_args
.dest_offset
));
126 cc_state
->subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
132 if (tevent_req_nomem(cc_state
->subreq
, req
)) {
133 return tevent_req_post(req
, ev
);
135 /* wait for subreq completion */
136 tevent_req_set_callback(cc_state
->subreq
,
137 btrfs_copy_chunk_done
,
143 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret
));
144 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
145 cc_state
->copied
= num
;
146 tevent_req_done(req
);
147 return tevent_req_post(req
, ev
);
150 /* only used if the request is passed through to next VFS module */
151 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
)
153 struct tevent_req
*req
= tevent_req_callback_data(
154 subreq
, struct tevent_req
);
155 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
156 struct btrfs_cc_state
);
159 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state
->handle
,
162 if (tevent_req_nterror(req
, status
)) {
165 tevent_req_done(req
);
168 static NTSTATUS
btrfs_copy_chunk_recv(struct vfs_handle_struct
*handle
,
169 struct tevent_req
*req
,
173 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
174 struct btrfs_cc_state
);
176 if (tevent_req_is_nterror(req
, &status
)) {
177 DEBUG(4, ("server side copy chunk failed: %s\n",
179 tevent_req_received(req
);
183 DEBUG(10, ("server side copy chunk copied %llu\n",
184 (unsigned long long)cc_state
->copied
));
185 *copied
= cc_state
->copied
;
186 tevent_req_received(req
);
190 static struct vfs_fn_pointers btrfs_fns
= {
191 .copy_chunk_send_fn
= btrfs_copy_chunk_send
,
192 .copy_chunk_recv_fn
= btrfs_copy_chunk_recv
,
195 NTSTATUS
vfs_btrfs_init(void);
196 NTSTATUS
vfs_btrfs_init(void)
198 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
199 "btrfs", &btrfs_fns
);