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 if (src_fsp
->op
== NULL
|| dest_fsp
->op
== NULL
) {
120 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
121 return tevent_req_post(req
, ev
);
124 init_strict_lock_struct(src_fsp
,
125 src_fsp
->op
->global
->open_persistent_id
,
130 init_strict_lock_struct(dest_fsp
,
131 dest_fsp
->op
->global
->open_persistent_id
,
137 if (!SMB_VFS_STRICT_LOCK(src_fsp
->conn
, src_fsp
, &src_lck
)) {
138 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
139 return tevent_req_post(req
, ev
);
141 if (!SMB_VFS_STRICT_LOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
)) {
142 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
143 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
144 return tevent_req_post(req
, ev
);
147 ZERO_STRUCT(cr_args
);
148 cr_args
.src_fd
= src_fsp
->fh
->fd
;
149 cr_args
.src_offset
= (uint64_t)src_off
;
150 cr_args
.dest_offset
= (uint64_t)dest_off
;
151 cr_args
.src_length
= (uint64_t)num
;
153 ret
= ioctl(dest_fsp
->fh
->fd
, BTRFS_IOC_CLONE_RANGE
, &cr_args
);
154 SMB_VFS_STRICT_UNLOCK(dest_fsp
->conn
, dest_fsp
, &dest_lck
);
155 SMB_VFS_STRICT_UNLOCK(src_fsp
->conn
, src_fsp
, &src_lck
);
158 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
159 * cloning. Which is 4096 by default, therefore fall back to
160 * manual read/write on failure.
162 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
163 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
165 (unsigned long long)cr_args
.src_length
,
166 (long long)cr_args
.src_fd
,
167 (unsigned long long)cr_args
.src_offset
,
169 (unsigned long long)cr_args
.dest_offset
));
170 cc_state
->subreq
= SMB_VFS_NEXT_COPY_CHUNK_SEND(handle
,
176 if (tevent_req_nomem(cc_state
->subreq
, req
)) {
177 return tevent_req_post(req
, ev
);
179 /* wait for subreq completion */
180 tevent_req_set_callback(cc_state
->subreq
,
181 btrfs_copy_chunk_done
,
186 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret
));
187 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
188 cc_state
->copied
= num
;
189 tevent_req_done(req
);
190 return tevent_req_post(req
, ev
);
193 /* only used if the request is passed through to next VFS module */
194 static void btrfs_copy_chunk_done(struct tevent_req
*subreq
)
196 struct tevent_req
*req
= tevent_req_callback_data(
197 subreq
, struct tevent_req
);
198 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
199 struct btrfs_cc_state
);
202 status
= SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state
->handle
,
205 if (tevent_req_nterror(req
, status
)) {
208 tevent_req_done(req
);
211 static NTSTATUS
btrfs_copy_chunk_recv(struct vfs_handle_struct
*handle
,
212 struct tevent_req
*req
,
216 struct btrfs_cc_state
*cc_state
= tevent_req_data(req
,
217 struct btrfs_cc_state
);
219 if (tevent_req_is_nterror(req
, &status
)) {
220 DEBUG(4, ("server side copy chunk failed: %s\n",
222 tevent_req_received(req
);
226 DEBUG(10, ("server side copy chunk copied %llu\n",
227 (unsigned long long)cc_state
->copied
));
228 *copied
= cc_state
->copied
;
229 tevent_req_received(req
);
234 * caller must pass a non-null fsp or smb_fname. If fsp is null, then
235 * fall back to opening the corresponding file to issue the ioctl.
237 static NTSTATUS
btrfs_get_compression(struct vfs_handle_struct
*handle
,
239 struct files_struct
*fsp
,
240 struct smb_filename
*smb_fname
,
241 uint16_t *_compression_fmt
)
249 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
251 } else if (smb_fname
!= NULL
) {
252 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
253 DIR *dir
= opendir(smb_fname
->base_name
);
255 return NT_STATUS_UNSUCCESSFUL
;
259 fd
= open(smb_fname
->base_name
, O_RDONLY
);
262 return NT_STATUS_UNSUCCESSFUL
;
266 return NT_STATUS_INVALID_PARAMETER
;
269 ret
= ioctl(fd
, FS_IOC_GETFLAGS
, &flags
);
271 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
272 strerror(errno
), (long long)fd
));
273 status
= map_nt_error_from_unix(errno
);
276 if (flags
& FS_COMPR_FL
) {
277 *_compression_fmt
= COMPRESSION_FORMAT_LZNT1
;
279 *_compression_fmt
= COMPRESSION_FORMAT_NONE
;
281 status
= NT_STATUS_OK
;
290 static NTSTATUS
btrfs_set_compression(struct vfs_handle_struct
*handle
,
292 struct files_struct
*fsp
,
293 uint16_t compression_fmt
)
300 if ((fsp
== NULL
) || (fsp
->fh
->fd
== -1)) {
301 status
= NT_STATUS_INVALID_PARAMETER
;
306 ret
= ioctl(fd
, FS_IOC_GETFLAGS
, &flags
);
308 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
309 strerror(errno
), fd
));
310 status
= map_nt_error_from_unix(errno
);
314 if (compression_fmt
== COMPRESSION_FORMAT_NONE
) {
315 DEBUG(5, ("setting compression\n"));
316 flags
&= (~FS_COMPR_FL
);
317 } else if ((compression_fmt
== COMPRESSION_FORMAT_DEFAULT
)
318 || (compression_fmt
== COMPRESSION_FORMAT_LZNT1
)) {
319 DEBUG(5, ("clearing compression\n"));
320 flags
|= FS_COMPR_FL
;
322 DEBUG(1, ("invalid compression format 0x%x\n",
323 (int)compression_fmt
));
324 status
= NT_STATUS_INVALID_PARAMETER
;
328 ret
= ioctl(fd
, FS_IOC_SETFLAGS
, &flags
);
330 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
331 strerror(errno
), fd
));
332 status
= map_nt_error_from_unix(errno
);
335 status
= NT_STATUS_OK
;
341 static struct vfs_fn_pointers btrfs_fns
= {
342 .fs_capabilities_fn
= btrfs_fs_capabilities
,
343 .copy_chunk_send_fn
= btrfs_copy_chunk_send
,
344 .copy_chunk_recv_fn
= btrfs_copy_chunk_recv
,
345 .get_compression_fn
= btrfs_get_compression
,
346 .set_compression_fn
= btrfs_set_compression
,
349 NTSTATUS
vfs_btrfs_init(void);
350 NTSTATUS
vfs_btrfs_init(void)
352 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
353 "btrfs", &btrfs_fns
);