s3-winbindd: Implement SamLogon IRPC call
[Samba.git] / source3 / modules / vfs_btrfs.c
blobc1e17b301ec35e132243086540b8382780c8e48c
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 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,
126 src_off,
127 num,
128 READ_LOCK,
129 &src_lck);
130 init_strict_lock_struct(dest_fsp,
131 dest_fsp->op->global->open_persistent_id,
132 dest_off,
133 num,
134 WRITE_LOCK,
135 &dest_lck);
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);
156 if (ret < 0) {
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",
164 strerror(errno),
165 (unsigned long long)cr_args.src_length,
166 (long long)cr_args.src_fd,
167 (unsigned long long)cr_args.src_offset,
168 dest_fsp->fh->fd,
169 (unsigned long long)cr_args.dest_offset));
170 cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
171 cc_state, ev,
172 src_fsp,
173 src_off,
174 dest_fsp,
175 dest_off, num);
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,
182 req);
183 return req;
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);
200 NTSTATUS status;
202 status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
203 cc_state->subreq,
204 &cc_state->copied);
205 if (tevent_req_nterror(req, status)) {
206 return;
208 tevent_req_done(req);
211 static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
212 struct tevent_req *req,
213 off_t *copied)
215 NTSTATUS status;
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",
221 nt_errstr(status)));
222 tevent_req_received(req);
223 return status;
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);
230 return NT_STATUS_OK;
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,
238 TALLOC_CTX *mem_ctx,
239 struct files_struct *fsp,
240 struct smb_filename *smb_fname,
241 uint16_t *_compression_fmt)
243 int ret;
244 long flags = 0;
245 int fd;
246 bool opened = false;
247 NTSTATUS status;
249 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
250 fd = fsp->fh->fd;
251 } else if (smb_fname != NULL) {
252 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
253 DIR *dir = opendir(smb_fname->base_name);
254 if (dir == NULL) {
255 return NT_STATUS_UNSUCCESSFUL;
257 fd = dirfd(dir);
258 } else {
259 fd = open(smb_fname->base_name, O_RDONLY);
261 if (fd < 0) {
262 return NT_STATUS_UNSUCCESSFUL;
264 opened = true;
265 } else {
266 return NT_STATUS_INVALID_PARAMETER;
269 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
270 if (ret < 0) {
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);
274 goto err_close;
276 if (flags & FS_COMPR_FL) {
277 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
278 } else {
279 *_compression_fmt = COMPRESSION_FORMAT_NONE;
281 status = NT_STATUS_OK;
282 err_close:
283 if (opened) {
284 close(fd);
287 return status;
290 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
291 TALLOC_CTX *mem_ctx,
292 struct files_struct *fsp,
293 uint16_t compression_fmt)
295 int ret;
296 long flags = 0;
297 int fd;
298 NTSTATUS status;
300 if ((fsp == NULL) || (fsp->fh->fd == -1)) {
301 status = NT_STATUS_INVALID_PARAMETER;
302 goto err_out;
304 fd = fsp->fh->fd;
306 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
307 if (ret < 0) {
308 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
309 strerror(errno), fd));
310 status = map_nt_error_from_unix(errno);
311 goto err_out;
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;
321 } else {
322 DEBUG(1, ("invalid compression format 0x%x\n",
323 (int)compression_fmt));
324 status = NT_STATUS_INVALID_PARAMETER;
325 goto err_out;
328 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
329 if (ret < 0) {
330 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
331 strerror(errno), fd));
332 status = map_nt_error_from_unix(errno);
333 goto err_out;
335 status = NT_STATUS_OK;
336 err_out:
337 return status;
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);