s3:vfs_btrfs change includes
[Samba.git] / source3 / modules / vfs_btrfs.c
blob2254fa2f68fc37368df09394c10d69b1a8194468
1 /*
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 <linux/fs.h>
22 #include <sys/ioctl.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include "system/filesys.h"
26 #include "includes.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;
41 *_ts_res = ts_res;
43 return fs_capabilities;
46 struct btrfs_ioctl_clone_range_args {
47 int64_t src_fd;
48 uint64_t src_offset;
49 uint64_t src_length;
50 uint64_t dest_offset;
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;
59 off_t copied;
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,
65 TALLOC_CTX *mem_ctx,
66 struct tevent_context *ev,
67 struct files_struct *src_fsp,
68 off_t src_off,
69 struct files_struct *dest_fsp,
70 off_t dest_off,
71 off_t num)
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;
78 int ret;
79 NTSTATUS status;
81 req = tevent_req_create(mem_ctx, &cc_state, struct btrfs_cc_state);
82 if (req == NULL) {
83 return NULL;
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,
100 src_off,
101 num,
102 READ_LOCK,
103 &src_lck);
104 init_strict_lock_struct(dest_fsp,
105 dest_fsp->op->global->open_persistent_id,
106 dest_off,
107 num,
108 WRITE_LOCK,
109 &dest_lck);
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);
130 if (ret < 0) {
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",
138 strerror(errno),
139 (unsigned long long)cr_args.src_length,
140 (long long)cr_args.src_fd,
141 (unsigned long long)cr_args.src_offset,
142 dest_fsp->fh->fd,
143 (unsigned long long)cr_args.dest_offset));
144 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
145 cc_state, ev,
146 src_fsp,
147 src_off,
148 dest_fsp,
149 dest_off, num);
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,
156 req);
157 return req;
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);
175 NTSTATUS status;
177 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
178 cc_state->subreq,
179 &cc_state->copied);
180 if (tevent_req_nterror(req, status)) {
181 return;
183 tevent_req_done(req);
186 static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
187 struct tevent_req *req,
188 off_t *copied)
190 NTSTATUS status;
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",
196 nt_errstr(status)));
197 tevent_req_received(req);
198 return status;
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);
205 return NT_STATUS_OK;
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,
213 TALLOC_CTX *mem_ctx,
214 struct files_struct *fsp,
215 struct smb_filename *smb_fname,
216 uint16_t *_compression_fmt)
218 int ret;
219 long flags = 0;
220 int fd;
221 bool opened = false;
222 NTSTATUS status;
224 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
225 fd = fsp->fh->fd;
226 } else if (smb_fname != NULL) {
227 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
228 DIR *dir = opendir(smb_fname->base_name);
229 if (dir == NULL) {
230 return NT_STATUS_UNSUCCESSFUL;
232 fd = dirfd(dir);
233 } else {
234 fd = open(smb_fname->base_name, O_RDONLY);
236 if (fd < 0) {
237 return NT_STATUS_UNSUCCESSFUL;
239 opened = true;
240 } else {
241 return NT_STATUS_INVALID_PARAMETER;
244 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
245 if (ret < 0) {
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);
249 goto err_close;
251 if (flags & FS_COMPR_FL) {
252 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
253 } else {
254 *_compression_fmt = COMPRESSION_FORMAT_NONE;
256 status = NT_STATUS_OK;
257 err_close:
258 if (opened) {
259 close(fd);
262 return status;
265 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
266 TALLOC_CTX *mem_ctx,
267 struct files_struct *fsp,
268 uint16_t compression_fmt)
270 int ret;
271 long flags = 0;
272 int fd;
273 NTSTATUS status;
275 if ((fsp == NULL) || (fsp->fh->fd == -1)) {
276 status = NT_STATUS_INVALID_PARAMETER;
277 goto err_out;
279 fd = fsp->fh->fd;
281 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
282 if (ret < 0) {
283 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
284 strerror(errno), fd));
285 status = map_nt_error_from_unix(errno);
286 goto err_out;
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;
296 } else {
297 DEBUG(1, ("invalid compression format 0x%x\n",
298 (int)compression_fmt));
299 status = NT_STATUS_INVALID_PARAMETER;
300 goto err_out;
303 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
304 if (ret < 0) {
305 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
306 strerror(errno), fd));
307 status = map_nt_error_from_unix(errno);
308 goto err_out;
310 status = NT_STATUS_OK;
311 err_out:
312 return status;
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);