spoolss: clear PrinterInfo on GetPrinter error
[Samba.git] / source3 / modules / vfs_btrfs.c
blob51442391ce68169dec1a92da50494c4356e3f306
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;
248 DIR *dir = NULL;
250 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
251 fd = fsp->fh->fd;
252 } else if (smb_fname != NULL) {
253 if (S_ISDIR(smb_fname->st.st_ex_mode)) {
254 dir = opendir(smb_fname->base_name);
255 if (dir == NULL) {
256 return NT_STATUS_UNSUCCESSFUL;
258 opened = true;
259 fd = dirfd(dir);
260 if (fd < 0) {
261 status = NT_STATUS_UNSUCCESSFUL;
262 goto err_close;
264 } else {
265 fd = open(smb_fname->base_name, O_RDONLY);
266 if (fd < 0) {
267 return NT_STATUS_UNSUCCESSFUL;
269 opened = true;
271 } else {
272 return NT_STATUS_INVALID_PARAMETER;
275 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
276 if (ret < 0) {
277 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %lld\n",
278 strerror(errno), (long long)fd));
279 status = map_nt_error_from_unix(errno);
280 goto err_close;
282 if (flags & FS_COMPR_FL) {
283 *_compression_fmt = COMPRESSION_FORMAT_LZNT1;
284 } else {
285 *_compression_fmt = COMPRESSION_FORMAT_NONE;
287 status = NT_STATUS_OK;
288 err_close:
289 if (opened) {
290 if (dir != NULL) {
291 closedir(dir);
292 } else {
293 close(fd);
297 return status;
300 static NTSTATUS btrfs_set_compression(struct vfs_handle_struct *handle,
301 TALLOC_CTX *mem_ctx,
302 struct files_struct *fsp,
303 uint16_t compression_fmt)
305 int ret;
306 long flags = 0;
307 int fd;
308 NTSTATUS status;
310 if ((fsp == NULL) || (fsp->fh->fd == -1)) {
311 status = NT_STATUS_INVALID_PARAMETER;
312 goto err_out;
314 fd = fsp->fh->fd;
316 ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
317 if (ret < 0) {
318 DEBUG(1, ("FS_IOC_GETFLAGS failed: %s, fd %d\n",
319 strerror(errno), fd));
320 status = map_nt_error_from_unix(errno);
321 goto err_out;
324 if (compression_fmt == COMPRESSION_FORMAT_NONE) {
325 DEBUG(5, ("setting compression\n"));
326 flags &= (~FS_COMPR_FL);
327 } else if ((compression_fmt == COMPRESSION_FORMAT_DEFAULT)
328 || (compression_fmt == COMPRESSION_FORMAT_LZNT1)) {
329 DEBUG(5, ("clearing compression\n"));
330 flags |= FS_COMPR_FL;
331 } else {
332 DEBUG(1, ("invalid compression format 0x%x\n",
333 (int)compression_fmt));
334 status = NT_STATUS_INVALID_PARAMETER;
335 goto err_out;
338 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
339 if (ret < 0) {
340 DEBUG(1, ("FS_IOC_SETFLAGS failed: %s, fd %d\n",
341 strerror(errno), fd));
342 status = map_nt_error_from_unix(errno);
343 goto err_out;
345 status = NT_STATUS_OK;
346 err_out:
347 return status;
351 static struct vfs_fn_pointers btrfs_fns = {
352 .fs_capabilities_fn = btrfs_fs_capabilities,
353 .copy_chunk_send_fn = btrfs_copy_chunk_send,
354 .copy_chunk_recv_fn = btrfs_copy_chunk_recv,
355 .get_compression_fn = btrfs_get_compression,
356 .set_compression_fn = btrfs_set_compression,
359 NTSTATUS vfs_btrfs_init(void);
360 NTSTATUS vfs_btrfs_init(void)
362 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
363 "btrfs", &btrfs_fns);