autorid: improve wording in a debug message
[Samba.git] / source3 / modules / vfs_btrfs.c
blob997a5de02f24671afb14ff68941a14c55811fb79
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 if (num == 0) {
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,
94 cc_state, ev,
95 src_fsp,
96 src_off,
97 dest_fsp,
98 dest_off, num);
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,
104 req);
105 return req;
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,
121 src_off,
122 num,
123 READ_LOCK,
124 &src_lck);
125 init_strict_lock_struct(dest_fsp,
126 dest_fsp->op->global->open_persistent_id,
127 dest_off,
128 num,
129 WRITE_LOCK,
130 &dest_lck);
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);
151 if (ret < 0) {
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",
159 strerror(errno),
160 (unsigned long long)cr_args.src_length,
161 (long long)cr_args.src_fd,
162 (unsigned long long)cr_args.src_offset,
163 dest_fsp->fh->fd,
164 (unsigned long long)cr_args.dest_offset));
165 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
166 cc_state, ev,
167 src_fsp,
168 src_off,
169 dest_fsp,
170 dest_off, num);
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,
177 req);
178 return req;
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);
195 NTSTATUS status;
197 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
198 cc_state->subreq,
199 &cc_state->copied);
200 if (tevent_req_nterror(req, status)) {
201 return;
203 tevent_req_done(req);
206 static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
207 struct tevent_req *req,
208 off_t *copied)
210 NTSTATUS status;
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",
216 nt_errstr(status)));
217 tevent_req_received(req);
218 return status;
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);
225 return NT_STATUS_OK;
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,
233 TALLOC_CTX *mem_ctx,
234 struct files_struct *fsp,
235 struct smb_filename *smb_fname,
236 uint16_t *_compression_fmt)
238 int ret;
239 long flags = 0;
240 int fd;
241 bool opened = false;
242 NTSTATUS status;
244 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
245 fd = fsp->fh->fd;
246 } else if (smb_fname != NULL) {
247 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
248 DIR *dir = opendir(smb_fname->base_name);
249 if (dir == NULL) {
250 return NT_STATUS_UNSUCCESSFUL;
252 fd = dirfd(dir);
253 } else {
254 fd = open(smb_fname->base_name, O_RDONLY);
256 if (fd < 0) {
257 return NT_STATUS_UNSUCCESSFUL;
259 opened = true;
260 } else {
261 return NT_STATUS_INVALID_PARAMETER;
264 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
265 if (ret < 0) {
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);
269 goto err_close;
271 if (flags & FS_COMPR_FL) {
272 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
273 } else {
274 *_compression_fmt = COMPRESSION_FORMAT_NONE;
276 status = NT_STATUS_OK;
277 err_close:
278 if (opened) {
279 close(fd);
282 return status;
285 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
286 TALLOC_CTX *mem_ctx,
287 struct files_struct *fsp,
288 uint16_t compression_fmt)
290 int ret;
291 long flags = 0;
292 int fd;
293 NTSTATUS status;
295 if ((fsp == NULL) || (fsp->fh->fd == -1)) {
296 status = NT_STATUS_INVALID_PARAMETER;
297 goto err_out;
299 fd = fsp->fh->fd;
301 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
302 if (ret < 0) {
303 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
304 strerror(errno), fd));
305 status = map_nt_error_from_unix(errno);
306 goto err_out;
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;
316 } else {
317 DEBUG(1, ("invalid compression format 0x%x\n",
318 (int)compression_fmt));
319 status = NT_STATUS_INVALID_PARAMETER;
320 goto err_out;
323 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
324 if (ret < 0) {
325 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
326 strerror(errno), fd));
327 status = map_nt_error_from_unix(errno);
328 goto err_out;
330 status = NT_STATUS_OK;
331 err_out:
332 return status;
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);