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>
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
28 #include "smbd/smbd.h"
29 #include "librpc/gen_ndr/smbXsrv.h"
30 #include "librpc/gen_ndr/ioctl.h"
31 #include "lib/util/tevent_ntstatus.h"
33 struct btrfs_ioctl_clone_range_args {
40 #define BTRFS_IOCTL_MAGIC 0x94
41 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
42 struct btrfs_ioctl_clone_range_args)
44 struct btrfs_cc_state {
45 struct vfs_handle_struct *handle;
47 struct tevent_req *subreq; /* non-null if passed to next VFS fn */
49 static void btrfs_copy_chunk_done(struct tevent_req *subreq);
51 static struct tevent_req *btrfs_copy_chunk_send(struct vfs_handle_struct *handle,
53 struct tevent_context *ev,
54 struct files_struct *src_fsp,
56 struct files_struct *dest_fsp,
60 struct tevent_req *req;
61 struct btrfs_cc_state *cc_state;
62 struct btrfs_ioctl_clone_range_args cr_args;
63 struct lock_struct src_lck;
64 struct lock_struct dest_lck;
68 req = tevent_req_create(mem_ctx, &cc_state, struct btrfs_cc_state);
72 cc_state->handle = handle;
74 status = vfs_stat_fsp(src_fsp);
75 if (tevent_req_nterror(req, status)) {
76 return tevent_req_post(req, ev);
79 if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
80 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
81 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
82 return tevent_req_post(req, ev);
85 init_strict_lock_struct(src_fsp,
86 src_fsp->op->global->open_persistent_id,
91 init_strict_lock_struct(dest_fsp,
92 dest_fsp->op->global->open_persistent_id,
98 if (!SMB_VFS_STRICT_LOCK(src_fsp->conn, src_fsp, &src_lck)) {
99 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
100 return tevent_req_post(req, ev);
102 if (!SMB_VFS_STRICT_LOCK(dest_fsp->conn, dest_fsp, &dest_lck)) {
103 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &src_lck);
104 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
105 return tevent_req_post(req, ev);
108 ZERO_STRUCT(cr_args);
109 cr_args.src_fd = src_fsp->fh->fd;
110 cr_args.src_offset = (uint64_t)src_off;
111 cr_args.dest_offset = (uint64_t)dest_off;
112 cr_args.src_length = (uint64_t)num;
114 ret = ioctl(dest_fsp->fh->fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
115 SMB_VFS_STRICT_UNLOCK(dest_fsp->conn, dest_fsp, &dest_lck);
116 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &src_lck);
119 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
120 * cloning. Which is 4096 by default, therefore fall back to
121 * manual read/write on failure.
123 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
124 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
126 (unsigned long long)cr_args.src_length,
127 (long long)cr_args.src_fd,
128 (unsigned long long)cr_args.src_offset,
130 (unsigned long long)cr_args.dest_offset));
131 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
137 if (tevent_req_nomem(cc_state->subreq, req)) {
138 return tevent_req_post(req, ev);
140 /* wait for subreq completion */
141 tevent_req_set_callback(cc_state->subreq,
142 btrfs_copy_chunk_done,
148 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
149 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
150 cc_state->copied = num;
151 tevent_req_done(req);
152 return tevent_req_post(req, ev);
155 /* only used if the request is passed through to next VFS module */
156 static void btrfs_copy_chunk_done(struct tevent_req *subreq)
158 struct tevent_req *req = tevent_req_callback_data(
159 subreq, struct tevent_req);
160 struct btrfs_cc_state *cc_state = tevent_req_data(req,
161 struct btrfs_cc_state);
164 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
167 if (tevent_req_nterror(req, status)) {
170 tevent_req_done(req);
173 static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
174 struct tevent_req *req,
178 struct btrfs_cc_state *cc_state = tevent_req_data(req,
179 struct btrfs_cc_state);
181 if (tevent_req_is_nterror(req, &status)) {
182 DEBUG(4, ("server side copy chunk failed: %s\n",
184 tevent_req_received(req);
188 DEBUG(10, ("server side copy chunk copied %llu\n",
189 (unsigned long long)cc_state->copied));
190 *copied = cc_state->copied;
191 tevent_req_received(req);
196 * caller must pass a non-null fsp or smb_fname. If fsp is null, then
197 * fall back to opening the corresponding file to issue the ioctl.
199 static NTSTATUS btrfs_get_compression(struct vfs_handle_struct *handle,
201 struct files_struct *fsp,
202 struct smb_filename *smb_fname,
203 uint16_t *_compression_fmt)
211 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
213 } else if (smb_fname != NULL) {
214 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
215 DIR *dir = opendir(smb_fname->base_name);
217 return NT_STATUS_UNSUCCESSFUL;
221 fd = open(smb_fname->base_name, O_RDONLY);
224 return NT_STATUS_UNSUCCESSFUL;
228 return NT_STATUS_INVALID_PARAMETER;
231 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
233 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
234 strerror(errno), (long long)fd));
235 status = map_nt_error_from_unix(errno);
238 if (flags & FS_COMPR_FL) {
239 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
241 *_compression_fmt = COMPRESSION_FORMAT_NONE;
243 status = NT_STATUS_OK;
252 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
254 struct files_struct *fsp,
255 uint16_t compression_fmt)
262 if ((fsp == NULL) || (fsp->fh->fd == -1)) {
263 status = NT_STATUS_INVALID_PARAMETER;
268 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
270 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
271 strerror(errno), fd));
272 status = map_nt_error_from_unix(errno);
276 if (compression_fmt == COMPRESSION_FORMAT_NONE) {
277 DEBUG(5, ("setting compression\n"));
278 flags &= (~FS_COMPR_FL);
279 } else if ((compression_fmt == COMPRESSION_FORMAT_DEFAULT)
280 || (compression_fmt == COMPRESSION_FORMAT_LZNT1)) {
281 DEBUG(5, ("clearing compression\n"));
282 flags |= FS_COMPR_FL;
284 DEBUG(1, ("invalid compression format 0x%x\n",
285 (int)compression_fmt));
286 status = NT_STATUS_INVALID_PARAMETER;
290 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
292 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
293 strerror(errno), fd));
294 status = map_nt_error_from_unix(errno);
297 status = NT_STATUS_OK;
303 static struct vfs_fn_pointers btrfs_fns = {
304 .copy_chunk_send_fn = btrfs_copy_chunk_send,
305 .copy_chunk_recv_fn = btrfs_copy_chunk_recv,
306 .get_compression_fn = btrfs_get_compression,
307 .set_compression_fn = btrfs_set_compression,
310 NTSTATUS vfs_btrfs_init(void);
311 NTSTATUS vfs_btrfs_init(void)
313 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
314 "btrfs", &btrfs_fns);