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
;
87 status
= vfs_stat_fsp(src_fsp
);
88 if (tevent_req_nterror(req
, status
)) {
89 return tevent_req_post(req
, ev
);
92 if (src_fsp
->fsp_name
->st
.st_ex_size
< src_off
+ num
) {
93 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
94 tevent_req_nterror(req
, NT_STATUS_INVALID_VIEW_SIZE
);
95 return tevent_req_post(req
, ev
);
98 init_strict_lock_struct(src_fsp
,
99 src_fsp
->op
->global
->open_persistent_id
,
104 init_strict_lock_struct(dest_fsp
,
105 dest_fsp
->op
->global
->open_persistent_id
,
111 if (!SMB_VFS_STRICT_LOCK(src_fsp
->conn
, src_fsp
, &src_lck
)) {
112 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
113 return tevent_req_post(req
, ev
);
115 if (!SMB_VFS_STRICT_LOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
)) {
116 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
117 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
118 return tevent_req_post(req
, ev
);
121 ZERO_STRUCT(cr_args
);
122 cr_args
.src_fd
= src_fsp
->fh
->fd
;
123 cr_args
.src_offset
= (uint64_t)src_off
;
124 cr_args
.dest_offset
= (uint64_t)dest_off
;
125 cr_args
.src_length
= (uint64_t)num
;
127 ret
= ioctl(dest_fsp
->fh
->fd
, BTRFS_IOC_CLONE_RANGE
, &cr_args
);
128 SMB_VFS_STRICT_UNLOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
);
129 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
132 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
133 * cloning. Which is 4096 by default, therefore fall back to
134 * manual read/write on failure.
136 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
137 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
139 (unsigned long long)cr_args
.src_length
,
140 (long long)cr_args
.src_fd
,
141 (unsigned long long)cr_args
.src_offset
,
143 (unsigned long long)cr_args
.dest_offset
));
144 cc_state
->subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
150 if (tevent_req_nomem(cc_state
->subreq
, req
)) {
151 return tevent_req_post(req
, ev
);
153 /* wait for subreq completion */
154 tevent_req_set_callback(cc_state
->subreq
,
155 btrfs_copy_chunk_done
,
161 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret
));
162 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
163 cc_state
->copied
= num
;
164 tevent_req_done(req
);
165 return tevent_req_post(req
, ev
);
168 /* only used if the request is passed through to next VFS module */
169 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
)
171 struct tevent_req
*req
= tevent_req_callback_data(
172 subreq
, struct tevent_req
);
173 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
174 struct btrfs_cc_state
);
177 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state
->handle
,
180 if (tevent_req_nterror(req
, status
)) {
183 tevent_req_done(req
);
186 static NTSTATUS
btrfs_copy_chunk_recv(struct vfs_handle_struct
*handle
,
187 struct tevent_req
*req
,
191 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
192 struct btrfs_cc_state
);
194 if (tevent_req_is_nterror(req
, &status
)) {
195 DEBUG(4, ("server side copy chunk failed: %s\n",
197 tevent_req_received(req
);
201 DEBUG(10, ("server side copy chunk copied %llu\n",
202 (unsigned long long)cc_state
->copied
));
203 *copied
= cc_state
->copied
;
204 tevent_req_received(req
);
209 * caller must pass a non-null fsp or smb_fname. If fsp is null, then
210 * fall back to opening the corresponding file to issue the ioctl.
212 static NTSTATUS
btrfs_get_compression(struct vfs_handle_struct
*handle
,
214 struct files_struct
*fsp
,
215 struct smb_filename
*smb_fname
,
216 uint16_t *_compression_fmt
)
224 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
226 } else if (smb_fname
!= NULL
) {
227 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
228 DIR *dir
= opendir(smb_fname
->base_name
);
230 return NT_STATUS_UNSUCCESSFUL
;
234 fd
= open(smb_fname
->base_name
, O_RDONLY
);
237 return NT_STATUS_UNSUCCESSFUL
;
241 return NT_STATUS_INVALID_PARAMETER
;
244 ret
= ioctl(fd
, FS_IOC_GETFLAGS
, &flags
);
246 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
247 strerror(errno
), (long long)fd
));
248 status
= map_nt_error_from_unix(errno
);
251 if (flags
& FS_COMPR_FL
) {
252 *_compression_fmt
= COMPRESSION_FORMAT_LZNT1
;
254 *_compression_fmt
= COMPRESSION_FORMAT_NONE
;
256 status
= NT_STATUS_OK
;
265 static NTSTATUS
btrfs_set_compression(struct vfs_handle_struct
*handle
,
267 struct files_struct
*fsp
,
268 uint16_t compression_fmt
)
275 if ((fsp
== NULL
) || (fsp
->fh
->fd
== -1)) {
276 status
= NT_STATUS_INVALID_PARAMETER
;
281 ret
= ioctl(fd
, FS_IOC_GETFLAGS
, &flags
);
283 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
284 strerror(errno
), fd
));
285 status
= map_nt_error_from_unix(errno
);
289 if (compression_fmt
== COMPRESSION_FORMAT_NONE
) {
290 DEBUG(5, ("setting compression\n"));
291 flags
&= (~FS_COMPR_FL
);
292 } else if ((compression_fmt
== COMPRESSION_FORMAT_DEFAULT
)
293 || (compression_fmt
== COMPRESSION_FORMAT_LZNT1
)) {
294 DEBUG(5, ("clearing compression\n"));
295 flags
|= FS_COMPR_FL
;
297 DEBUG(1, ("invalid compression format 0x%x\n",
298 (int)compression_fmt
));
299 status
= NT_STATUS_INVALID_PARAMETER
;
303 ret
= ioctl(fd
, FS_IOC_SETFLAGS
, &flags
);
305 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
306 strerror(errno
), fd
));
307 status
= map_nt_error_from_unix(errno
);
310 status
= NT_STATUS_OK
;
316 static struct vfs_fn_pointers btrfs_fns
= {
317 .fs_capabilities_fn
= btrfs_fs_capabilities
,
318 .copy_chunk_send_fn
= btrfs_copy_chunk_send
,
319 .copy_chunk_recv_fn
= btrfs_copy_chunk_recv
,
320 .get_compression_fn
= btrfs_get_compression
,
321 .set_compression_fn
= btrfs_set_compression
,
324 NTSTATUS
vfs_btrfs_init(void);
325 NTSTATUS
vfs_btrfs_init(void)
327 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
328 "btrfs", &btrfs_fns
);