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>
25 #include "system/filesys.h"
27 #include "smbd/smbd.h"
28 #include "librpc/gen_ndr/smbXsrv.h"
29 #include "librpc/gen_ndr/ioctl.h"
30 #include "lib/util/tevent_ntstatus.h"
32 static uint32_t btrfs_fs_capabilities(struct vfs_handle_struct
*handle
,
33 enum timestamp_set_resolution
*_ts_res
)
35 uint32_t fs_capabilities
;
36 enum timestamp_set_resolution ts_res
;
38 /* inherit default capabilities, expose compression support */
39 fs_capabilities
= SMB_VFS_NEXT_FS_CAPABILITIES(handle
, &ts_res
);
40 fs_capabilities
|= FILE_FILE_COMPRESSION
;
43 return fs_capabilities
;
46 struct btrfs_ioctl_clone_range_args
{
53 #define BTRFS_IOCTL_MAGIC 0x94
54 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
55 struct btrfs_ioctl_clone_range_args)
57 struct btrfs_cc_state
{
58 struct vfs_handle_struct
*handle
;
60 struct tevent_req
*subreq
; /* non-null if passed to next VFS fn */
62 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
);
64 static struct tevent_req
*btrfs_copy_chunk_send(struct vfs_handle_struct
*handle
,
66 struct tevent_context
*ev
,
67 struct files_struct
*src_fsp
,
69 struct files_struct
*dest_fsp
,
73 struct tevent_req
*req
;
74 struct btrfs_cc_state
*cc_state
;
75 struct btrfs_ioctl_clone_range_args cr_args
;
76 struct lock_struct src_lck
;
77 struct lock_struct dest_lck
;
81 req
= tevent_req_create(mem_ctx
, &cc_state
, struct btrfs_cc_state
);
85 cc_state
->handle
= handle
;
89 * With a @src_length of zero, BTRFS_IOC_CLONE_RANGE clones
90 * all data from @src_offset->EOF! This is certainly not what
91 * the caller expects, and not what vfs_default does.
93 cc_state
->subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
99 if (tevent_req_nomem(cc_state
->subreq
, req
)) {
100 return tevent_req_post(req
, ev
);
102 tevent_req_set_callback(cc_state
->subreq
,
103 btrfs_copy_chunk_done
,
108 status
= vfs_stat_fsp(src_fsp
);
109 if (tevent_req_nterror(req
, status
)) {
110 return tevent_req_post(req
, ev
);
113 if (src_fsp
->fsp_name
->st
.st_ex_size
< src_off
+ num
) {
114 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
115 tevent_req_nterror(req
, NT_STATUS_INVALID_VIEW_SIZE
);
116 return tevent_req_post(req
, ev
);
119 init_strict_lock_struct(src_fsp
,
120 src_fsp
->op
->global
->open_persistent_id
,
125 init_strict_lock_struct(dest_fsp
,
126 dest_fsp
->op
->global
->open_persistent_id
,
132 if (!SMB_VFS_STRICT_LOCK(src_fsp
->conn
, src_fsp
, &src_lck
)) {
133 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
134 return tevent_req_post(req
, ev
);
136 if (!SMB_VFS_STRICT_LOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
)) {
137 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
138 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
139 return tevent_req_post(req
, ev
);
142 ZERO_STRUCT(cr_args
);
143 cr_args
.src_fd
= src_fsp
->fh
->fd
;
144 cr_args
.src_offset
= (uint64_t)src_off
;
145 cr_args
.dest_offset
= (uint64_t)dest_off
;
146 cr_args
.src_length
= (uint64_t)num
;
148 ret
= ioctl(dest_fsp
->fh
->fd
, BTRFS_IOC_CLONE_RANGE
, &cr_args
);
149 SMB_VFS_STRICT_UNLOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
);
150 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
153 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
154 * cloning. Which is 4096 by default, therefore fall back to
155 * manual read/write on failure.
157 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
158 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
160 (unsigned long long)cr_args
.src_length
,
161 (long long)cr_args
.src_fd
,
162 (unsigned long long)cr_args
.src_offset
,
164 (unsigned long long)cr_args
.dest_offset
));
165 cc_state
->subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
171 if (tevent_req_nomem(cc_state
->subreq
, req
)) {
172 return tevent_req_post(req
, ev
);
174 /* wait for subreq completion */
175 tevent_req_set_callback(cc_state
->subreq
,
176 btrfs_copy_chunk_done
,
181 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret
));
182 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
183 cc_state
->copied
= num
;
184 tevent_req_done(req
);
185 return tevent_req_post(req
, ev
);
188 /* only used if the request is passed through to next VFS module */
189 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
)
191 struct tevent_req
*req
= tevent_req_callback_data(
192 subreq
, struct tevent_req
);
193 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
194 struct btrfs_cc_state
);
197 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state
->handle
,
200 if (tevent_req_nterror(req
, status
)) {
203 tevent_req_done(req
);
206 static NTSTATUS
btrfs_copy_chunk_recv(struct vfs_handle_struct
*handle
,
207 struct tevent_req
*req
,
211 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
212 struct btrfs_cc_state
);
214 if (tevent_req_is_nterror(req
, &status
)) {
215 DEBUG(4, ("server side copy chunk failed: %s\n",
217 tevent_req_received(req
);
221 DEBUG(10, ("server side copy chunk copied %llu\n",
222 (unsigned long long)cc_state
->copied
));
223 *copied
= cc_state
->copied
;
224 tevent_req_received(req
);
229 * caller must pass a non-null fsp or smb_fname. If fsp is null, then
230 * fall back to opening the corresponding file to issue the ioctl.
232 static NTSTATUS
btrfs_get_compression(struct vfs_handle_struct
*handle
,
234 struct files_struct
*fsp
,
235 struct smb_filename
*smb_fname
,
236 uint16_t *_compression_fmt
)
244 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
246 } else if (smb_fname
!= NULL
) {
247 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
248 DIR *dir
= opendir(smb_fname
->base_name
);
250 return NT_STATUS_UNSUCCESSFUL
;
254 fd
= open(smb_fname
->base_name
, O_RDONLY
);
257 return NT_STATUS_UNSUCCESSFUL
;
261 return NT_STATUS_INVALID_PARAMETER
;
264 ret
= ioctl(fd
, FS_IOC_GETFLAGS
, &flags
);
266 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
267 strerror(errno
), (long long)fd
));
268 status
= map_nt_error_from_unix(errno
);
271 if (flags
& FS_COMPR_FL
) {
272 *_compression_fmt
= COMPRESSION_FORMAT_LZNT1
;
274 *_compression_fmt
= COMPRESSION_FORMAT_NONE
;
276 status
= NT_STATUS_OK
;
285 static NTSTATUS
btrfs_set_compression(struct vfs_handle_struct
*handle
,
287 struct files_struct
*fsp
,
288 uint16_t compression_fmt
)
295 if ((fsp
== NULL
) || (fsp
->fh
->fd
== -1)) {
296 status
= NT_STATUS_INVALID_PARAMETER
;
301 ret
= ioctl(fd
, FS_IOC_GETFLAGS
, &flags
);
303 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
304 strerror(errno
), fd
));
305 status
= map_nt_error_from_unix(errno
);
309 if (compression_fmt
== COMPRESSION_FORMAT_NONE
) {
310 DEBUG(5, ("setting compression\n"));
311 flags
&= (~FS_COMPR_FL
);
312 } else if ((compression_fmt
== COMPRESSION_FORMAT_DEFAULT
)
313 || (compression_fmt
== COMPRESSION_FORMAT_LZNT1
)) {
314 DEBUG(5, ("clearing compression\n"));
315 flags
|= FS_COMPR_FL
;
317 DEBUG(1, ("invalid compression format 0x%x\n",
318 (int)compression_fmt
));
319 status
= NT_STATUS_INVALID_PARAMETER
;
323 ret
= ioctl(fd
, FS_IOC_SETFLAGS
, &flags
);
325 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
326 strerror(errno
), fd
));
327 status
= map_nt_error_from_unix(errno
);
330 status
= NT_STATUS_OK
;
336 static struct vfs_fn_pointers btrfs_fns
= {
337 .fs_capabilities_fn
= btrfs_fs_capabilities
,
338 .copy_chunk_send_fn
= btrfs_copy_chunk_send
,
339 .copy_chunk_recv_fn
= btrfs_copy_chunk_recv
,
340 .get_compression_fn
= btrfs_get_compression
,
341 .set_compression_fn
= btrfs_set_compression
,
344 NTSTATUS
vfs_btrfs_init(void);
345 NTSTATUS
vfs_btrfs_init(void)
347 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
348 "btrfs", &btrfs_fns
);