Revert "vfs_btrfs: fix compile on 32-bit platforms."
[samba.git] / source3 / modules / vfs_btrfs.c
1 /*
2  * Module to make use of awesome Btrfs features
3  *
4  * Copyright (C) David Disseldorp 2011-2013
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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"
27
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;
33 };
34
35 #define BTRFS_IOCTL_MAGIC 0x94
36 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
37                                    struct btrfs_ioctl_clone_range_args)
38
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 */
43 };
44 static void btrfs_copy_chunk_done(struct tevent_req *subreq);
45
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)
54 {
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;
62
63         req = tevent_req_create(mem_ctx, &cc_state, struct btrfs_cc_state);
64         if (req == NULL) {
65                 return NULL;
66         }
67         cc_state->handle = handle;
68
69         status = vfs_stat_fsp(src_fsp);
70         if (tevent_req_nterror(req, status)) {
71                 return tevent_req_post(req, ev);
72         }
73
74         if (src_fsp->fsp_name->st.st_ex_size < src_off + num) {
75                 /* [MS-SMB2] Handling a Server-Side Data Copy Request */
76                 tevent_req_nterror(req, NT_STATUS_INVALID_VIEW_SIZE);
77                 return tevent_req_post(req, ev);
78         }
79
80         init_strict_lock_struct(src_fsp,
81                                 src_fsp->op->global->open_persistent_id,
82                                 src_off,
83                                 num,
84                                 READ_LOCK,
85                                 &src_lck);
86         init_strict_lock_struct(dest_fsp,
87                                 dest_fsp->op->global->open_persistent_id,
88                                 dest_off,
89                                 num,
90                                 WRITE_LOCK,
91                                 &dest_lck);
92
93         if (!SMB_VFS_STRICT_LOCK(src_fsp->conn, src_fsp, &src_lck)) {
94                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
95                 return tevent_req_post(req, ev);
96         }
97         if (!SMB_VFS_STRICT_LOCK(dest_fsp->conn, dest_fsp, &dest_lck)) {
98                 SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &src_lck);
99                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
100                 return tevent_req_post(req, ev);
101         }
102
103         ZERO_STRUCT(cr_args);
104         cr_args.src_fd = src_fsp->fh->fd;
105         cr_args.src_offset = (uint64_t)src_off;
106         cr_args.dest_offset = (uint64_t)dest_off;
107         cr_args.src_length = (uint64_t)num;
108
109         ret = ioctl(dest_fsp->fh->fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
110         SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &dest_lck);
111         SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &src_lck);
112         if (ret < 0) {
113                 /*
114                  * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
115                  * cloning. Which is 4096 by default, therefore fall back to
116                  * manual read/write on failure.
117                  */
118                 DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %lu, "
119                           "src fd: %ld off: %lu, dest fd: %d off: %lu\n",
120                           strerror(errno), cr_args.src_length,
121                           cr_args.src_fd, cr_args.src_offset,
122                           dest_fsp->fh->fd, cr_args.dest_offset));
123                 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
124                                                                 cc_state, ev,
125                                                                 src_fsp,
126                                                                 src_off,
127                                                                 dest_fsp,
128                                                                 dest_off, num);
129                 if (tevent_req_nomem(cc_state->subreq, req)) {
130                         return tevent_req_post(req, ev);
131                 }
132                 /* wait for subreq completion */
133                 tevent_req_set_callback(cc_state->subreq,
134                                         btrfs_copy_chunk_done,
135                                         req);
136                 return req;
137
138         }
139
140         DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
141         /* BTRFS_IOC_CLONE_RANGE is all or nothing */
142         cc_state->copied = num;
143         tevent_req_done(req);
144         return tevent_req_post(req, ev);
145 }
146
147 /* only used if the request is passed through to next VFS module */
148 static void btrfs_copy_chunk_done(struct tevent_req *subreq)
149 {
150         struct tevent_req *req = tevent_req_callback_data(
151                 subreq, struct tevent_req);
152         struct btrfs_cc_state *cc_state = tevent_req_data(req,
153                                                         struct btrfs_cc_state);
154         NTSTATUS status;
155
156         status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
157                                               cc_state->subreq,
158                                               &cc_state->copied);
159         if (tevent_req_nterror(req, status)) {
160                 return;
161         }
162         tevent_req_done(req);
163 }
164
165 static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
166                                       struct tevent_req *req,
167                                       off_t *copied)
168 {
169         NTSTATUS status;
170         struct btrfs_cc_state *cc_state = tevent_req_data(req,
171                                                         struct btrfs_cc_state);
172
173         if (tevent_req_is_nterror(req, &status)) {
174                 DEBUG(4, ("server side copy chunk failed: %s\n",
175                           nt_errstr(status)));
176                 tevent_req_received(req);
177                 return status;
178         }
179
180         DEBUG(10, ("server side copy chunk copied %lu\n", cc_state->copied));
181         *copied = cc_state->copied;
182         tevent_req_received(req);
183         return NT_STATUS_OK;
184 }
185
186 static struct vfs_fn_pointers btrfs_fns = {
187         .copy_chunk_send_fn = btrfs_copy_chunk_send,
188         .copy_chunk_recv_fn = btrfs_copy_chunk_recv,
189 };
190
191 NTSTATUS vfs_btrfs_init(void);
192 NTSTATUS vfs_btrfs_init(void)
193 {
194         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
195                                 "btrfs", &btrfs_fns);
196 }