wafsamba: remove unused variable from copy_and_fix_python_path
[Samba.git] / source3 / modules / vfs_btrfs.c
blobf062eba37be9f304a274714c4f736c7015d2b8a9
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 <sys/ioctl.h>
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "smbd/smbd.h"
25 #include "../librpc/gen_ndr/smbXsrv.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct btrfs_ioctl_clone_range_args {
29 int64_t src_fd;
30 uint64_t src_offset;
31 uint64_t src_length;
32 uint64_t dest_offset;
35 #define BTRFS_IOCTL_MAGIC 0x94
36 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
37 struct btrfs_ioctl_clone_range_args)
39 struct btrfs_cc_state {
40 struct vfs_handle_struct *handle;
41 off_t copied;
42 struct tevent_req *subreq; /* non-null if passed to next VFS fn */
44 static void btrfs_copy_chunk_done(struct tevent_req *subreq);
46 static struct tevent_req *btrfs_copy_chunk_send(struct vfs_handle_struct *handle,
47 TALLOC_CTX *mem_ctx,
48 struct tevent_context *ev,
49 struct files_struct *src_fsp,
50 off_t src_off,
51 struct files_struct *dest_fsp,
52 off_t dest_off,
53 off_t num)
55 struct tevent_req *req;
56 struct btrfs_cc_state *cc_state;
57 struct btrfs_ioctl_clone_range_args cr_args;
58 struct lock_struct src_lck;
59 struct lock_struct dest_lck;
60 int ret;
61 NTSTATUS status;
63 req = tevent_req_create(mem_ctx, &cc_state, struct btrfs_cc_state);
64 if (req == NULL) {
65 return NULL;
67 cc_state->handle = handle;
69 if (num == 0) {
71 * With a @src_length of zero, BTRFS_IOC_CLONE_RANGE clones
72 * all data from @src_offset->EOF! This is certainly not what
73 * the caller expects, and not what vfs_default does.
75 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
76 cc_state, ev,
77 src_fsp,
78 src_off,
79 dest_fsp,
80 dest_off, num);
81 if (tevent_req_nomem(cc_state->subreq, req)) {
82 return tevent_req_post(req, ev);
84 tevent_req_set_callback(cc_state->subreq,
85 btrfs_copy_chunk_done,
86 req);
87 return req;
90 status = vfs_stat_fsp(src_fsp);
91 if (tevent_req_nterror(req, status)) {
92 return tevent_req_post(req, ev);
95 if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
96 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
97 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
98 return tevent_req_post(req, ev);
101 if (src_fsp->op == NULL || dest_fsp->op == NULL) {
102 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
103 return tevent_req_post(req, ev);
106 init_strict_lock_struct(src_fsp,
107 src_fsp->op->global->open_persistent_id,
108 src_off,
109 num,
110 READ_LOCK,
111 &src_lck);
112 init_strict_lock_struct(dest_fsp,
113 dest_fsp->op->global->open_persistent_id,
114 dest_off,
115 num,
116 WRITE_LOCK,
117 &dest_lck);
119 if (!SMB_VFS_STRICT_LOCK(src_fsp->conn, src_fsp, &src_lck)) {
120 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
121 return tevent_req_post(req, ev);
123 if (!SMB_VFS_STRICT_LOCK(dest_fsp->conn, dest_fsp, &dest_lck)) {
124 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &src_lck);
125 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
126 return tevent_req_post(req, ev);
129 ZERO_STRUCT(cr_args);
130 cr_args.src_fd = src_fsp->fh->fd;
131 cr_args.src_offset = (uint64_t)src_off;
132 cr_args.dest_offset = (uint64_t)dest_off;
133 cr_args.src_length = (uint64_t)num;
135 ret = ioctl(dest_fsp->fh->fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
136 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &dest_lck);
137 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &src_lck);
138 if (ret < 0) {
140 * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
141 * cloning. Which is 4096 by default, therefore fall back to
142 * manual read/write on failure.
144 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
145 "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
146 strerror(errno),
147 (unsigned long long)cr_args.src_length,
148 (long long)cr_args.src_fd,
149 (unsigned long long)cr_args.src_offset,
150 dest_fsp->fh->fd,
151 (unsigned long long)cr_args.dest_offset));
152 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
153 cc_state, ev,
154 src_fsp,
155 src_off,
156 dest_fsp,
157 dest_off, num);
158 if (tevent_req_nomem(cc_state->subreq, req)) {
159 return tevent_req_post(req, ev);
161 /* wait for subreq completion */
162 tevent_req_set_callback(cc_state->subreq,
163 btrfs_copy_chunk_done,
164 req);
165 return req;
168 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
169 /* BTRFS_IOC_CLONE_RANGE is all or nothing */
170 cc_state->copied = num;
171 tevent_req_done(req);
172 return tevent_req_post(req, ev);
175 /* only used if the request is passed through to next VFS module */
176 static void btrfs_copy_chunk_done(struct tevent_req *subreq)
178 struct tevent_req *req = tevent_req_callback_data(
179 subreq, struct tevent_req);
180 struct btrfs_cc_state *cc_state = tevent_req_data(req,
181 struct btrfs_cc_state);
182 NTSTATUS status;
184 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
185 cc_state->subreq,
186 &cc_state->copied);
187 if (tevent_req_nterror(req, status)) {
188 return;
190 tevent_req_done(req);
193 static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
194 struct tevent_req *req,
195 off_t *copied)
197 NTSTATUS status;
198 struct btrfs_cc_state *cc_state = tevent_req_data(req,
199 struct btrfs_cc_state);
201 if (tevent_req_is_nterror(req, &status)) {
202 DEBUG(4, ("server side copy chunk failed: %s\n",
203 nt_errstr(status)));
204 tevent_req_received(req);
205 return status;
208 DEBUG(10, ("server side copy chunk copied %llu\n",
209 (unsigned long long)cc_state->copied));
210 *copied = cc_state->copied;
211 tevent_req_received(req);
212 return NT_STATUS_OK;
215 static struct vfs_fn_pointers btrfs_fns = {
216 .copy_chunk_send_fn = btrfs_copy_chunk_send,
217 .copy_chunk_recv_fn = btrfs_copy_chunk_recv,
220 NTSTATUS vfs_btrfs_init(void);
221 NTSTATUS vfs_btrfs_init(void)
223 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
224 "btrfs", &btrfs_fns);