s3 onefs: Turn up the debug level for non-error cases
[Samba.git] / source3 / modules / onefs_system.c
blobcf99a27a87c3edc800984c3a54f4daf7c0f040b3
1 /*
2 * Unix SMB/CIFS implementation.
3 * Support for OneFS system interfaces.
5 * Copyright (C) Tim Prouty, 2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "onefs.h"
23 #include "onefs_config.h"
24 #include "oplock_onefs.h"
26 #include <ifs/ifs_syscalls.h>
27 #include <isi_acl/isi_acl_util.h>
28 #include <sys/isi_acl.h>
31 * Initialize the sm_lock struct before passing it to ifs_createfile.
33 static void smlock_init(connection_struct *conn, struct sm_lock *sml,
34 bool isexe, uint32_t access_mask, uint32_t share_access,
35 uint32_t create_options)
37 sml->sm_type.doc = false;
38 sml->sm_type.isexe = isexe;
39 sml->sm_type.statonly = is_stat_open(access_mask);
40 sml->sm_type.access_mask = access_mask;
41 sml->sm_type.share_access = share_access;
44 * private_options was previously used for DENY_DOS/DENY_FCB checks in
45 * the kernel, but are now properly handled by fcb_or_dos_open. In
46 * these cases, ifs_createfile will return a sharing violation, which
47 * gives fcb_or_dos_open the chance to open a duplicate file handle.
49 sml->sm_type.private_options = 0;
51 /* 1 second delay is handled in onefs_open.c by deferring the open */
52 sml->sm_timeout = timeval_set(0, 0);
55 static void smlock_dump(int debuglevel, const struct sm_lock *sml)
57 if (sml == NULL) {
58 DEBUG(debuglevel, ("sml == NULL\n"));
59 return;
62 DEBUG(debuglevel,
63 ("smlock: doc=%s, isexec=%s, statonly=%s, access_mask=0x%x, "
64 "share_access=0x%x, private_options=0x%x timeout=%d/%d\n",
65 sml->sm_type.doc ? "True" : "False",
66 sml->sm_type.isexe ? "True" : "False",
67 sml->sm_type.statonly ? "True" : "False",
68 sml->sm_type.access_mask,
69 sml->sm_type.share_access,
70 sml->sm_type.private_options,
71 (int)sml->sm_timeout.tv_sec,
72 (int)sml->sm_timeout.tv_usec));
75 /**
76 * External interface to ifs_createfile
78 int onefs_sys_create_file(connection_struct *conn,
79 int base_fd,
80 const char *path,
81 uint32_t access_mask,
82 uint32_t open_access_mask,
83 uint32_t share_access,
84 uint32_t create_options,
85 int flags,
86 mode_t mode,
87 int oplock_request,
88 uint64_t id,
89 struct security_descriptor *sd,
90 uint32_t dos_flags,
91 int *granted_oplock)
93 struct sm_lock sml, *psml = NULL;
94 enum oplock_type onefs_oplock;
95 enum oplock_type onefs_granted_oplock = OPLOCK_NONE;
96 struct ifs_security_descriptor ifs_sd = {}, *pifs_sd = NULL;
97 int secinfo = 0;
98 int ret_fd = -1;
99 uint32_t onefs_dos_attributes;
100 struct ifs_createfile_flags cf_flags = CF_FLAGS_NONE;
102 START_PROFILE(syscall_createfile);
104 /* Setup security descriptor and get secinfo. */
105 if (sd != NULL) {
106 NTSTATUS status;
108 secinfo = (get_sec_info(sd) & IFS_SEC_INFO_KNOWN_MASK);
110 status = onefs_samba_sd_to_sd(secinfo, sd, &ifs_sd, SNUM(conn));
112 if (!NT_STATUS_IS_OK(status)) {
113 DEBUG(1, ("SD initialization failure: %s\n",
114 nt_errstr(status)));
115 errno = EINVAL;
116 goto out;
119 pifs_sd = &ifs_sd;
122 /* Stripping off private bits will be done for us. */
123 onefs_oplock = onefs_samba_oplock_to_oplock(oplock_request);
125 if (!lp_oplocks(SNUM(conn))) {
126 SMB_ASSERT(onefs_oplock == OPLOCK_NONE);
129 /* Convert samba dos flags to UF_DOS_* attributes. */
130 onefs_dos_attributes = dos_attributes_to_stat_dos_flags(dos_flags);
133 * Deal with kernel creating Default ACLs. (Isilon bug 47447.)
135 * 1) "nt acl support = no", default_acl = no
136 * 2) "inherit permissions = yes", default_acl = no
138 if (lp_nt_acl_support(SNUM(conn)) && !lp_inherit_perms(SNUM(conn)))
139 cf_flags = cf_flags_or(cf_flags, CF_FLAGS_DEFAULT_ACL);
142 * Some customer workflows require the execute bit to be ignored.
144 if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
145 PARM_ALLOW_EXECUTE_ALWAYS,
146 PARM_ALLOW_EXECUTE_ALWAYS_DEFAULT) &&
147 (open_access_mask & FILE_EXECUTE)) {
149 DEBUG(3, ("Stripping execute bit from %s: (0x%x)\n", path,
150 open_access_mask));
152 /* Strip execute. */
153 open_access_mask &= ~FILE_EXECUTE;
156 * Add READ_DATA, so we're not left with desired_access=0. An
157 * execute call should imply the client will read the data.
159 open_access_mask |= FILE_READ_DATA;
161 DEBUGADD(3, ("New stripped access mask: 0x%x\n",
162 open_access_mask));
165 DEBUG(10,("onefs_sys_create_file: base_fd = %d, fname = %s"
166 "open_access_mask = 0x%x, flags = 0x%x, mode = 0%o, "
167 "desired_oplock = %s, id = 0x%x, secinfo = 0x%x, sd = %p, "
168 "dos_attributes = 0x%x, path = %s, "
169 "default_acl=%s\n", base_fd, path,
170 (unsigned int)open_access_mask,
171 (unsigned int)flags,
172 (unsigned int)mode,
173 onefs_oplock_str(onefs_oplock),
174 (unsigned int)id,
175 (unsigned int)secinfo, sd,
176 (unsigned int)onefs_dos_attributes, path,
177 cf_flags_and_bool(cf_flags, CF_FLAGS_DEFAULT_ACL) ?
178 "true" : "false"));
180 /* Initialize smlock struct for files/dirs but not internal opens */
181 if (!(oplock_request & INTERNAL_OPEN_ONLY)) {
182 smlock_init(conn, &sml, is_executable(path), access_mask,
183 share_access, create_options);
184 psml = &sml;
187 smlock_dump(10, psml);
189 ret_fd = ifs_createfile(base_fd, path,
190 (enum ifs_ace_rights)open_access_mask, flags & ~O_ACCMODE, mode,
191 onefs_oplock, id, psml, secinfo, pifs_sd, onefs_dos_attributes,
192 cf_flags, &onefs_granted_oplock);
194 DEBUG(10,("onefs_sys_create_file(%s): ret_fd = %d, "
195 "onefs_granted_oplock = %s\n",
196 ret_fd < 0 ? strerror(errno) : "success", ret_fd,
197 onefs_oplock_str(onefs_granted_oplock)));
199 if (granted_oplock) {
200 *granted_oplock =
201 onefs_oplock_to_samba_oplock(onefs_granted_oplock);
204 out:
205 END_PROFILE(syscall_createfile);
206 aclu_free_sd(pifs_sd, false);
208 return ret_fd;
212 * FreeBSD based sendfile implementation that allows for atomic semantics.
214 static ssize_t onefs_sys_do_sendfile(int tofd, int fromfd,
215 const DATA_BLOB *header, SMB_OFF_T offset, size_t count, bool atomic)
217 size_t total=0;
218 struct sf_hdtr hdr;
219 struct iovec hdtrl;
220 size_t hdr_len = 0;
221 int flags = 0;
223 if (atomic) {
224 flags = SF_ATOMIC;
227 hdr.headers = &hdtrl;
228 hdr.hdr_cnt = 1;
229 hdr.trailers = NULL;
230 hdr.trl_cnt = 0;
232 /* Set up the header iovec. */
233 if (header) {
234 hdtrl.iov_base = header->data;
235 hdtrl.iov_len = hdr_len = header->length;
236 } else {
237 hdtrl.iov_base = NULL;
238 hdtrl.iov_len = 0;
241 total = count;
242 while (total + hdtrl.iov_len) {
243 SMB_OFF_T nwritten;
244 int ret;
247 * FreeBSD sendfile returns 0 on success, -1 on error.
248 * Remember, the tofd and fromfd are reversed..... :-).
249 * nwritten includes the header data sent.
252 do {
253 ret = sendfile(fromfd, tofd, offset, total, &hdr,
254 &nwritten, flags);
255 } while (ret == -1 && errno == EINTR);
257 /* On error we're done. */
258 if (ret == -1) {
259 return -1;
263 * If this was an ATOMIC sendfile, nwritten doesn't
264 * necessarily indicate an error. It could mean count > than
265 * what sendfile can handle atomically (usually 64K) or that
266 * there was a short read due to the file being truncated.
268 if (nwritten == 0) {
269 return atomic ? 0 : -1;
273 * An atomic sendfile should never send partial data!
275 if (atomic && nwritten != total + hdtrl.iov_len) {
276 DEBUG(0,("Atomic sendfile() sent partial data: "
277 "%llu of %d\n", nwritten,
278 total + hdtrl.iov_len));
279 return -1;
283 * If this was a short (signal interrupted) write we may need
284 * to subtract it from the header data, or null out the header
285 * data altogether if we wrote more than hdtrl.iov_len bytes.
286 * We change nwritten to be the number of file bytes written.
289 if (hdtrl.iov_base && hdtrl.iov_len) {
290 if (nwritten >= hdtrl.iov_len) {
291 nwritten -= hdtrl.iov_len;
292 hdtrl.iov_base = NULL;
293 hdtrl.iov_len = 0;
294 } else {
295 hdtrl.iov_base =
296 (caddr_t)hdtrl.iov_base + nwritten;
297 hdtrl.iov_len -= nwritten;
298 nwritten = 0;
301 total -= nwritten;
302 offset += nwritten;
304 return count + hdr_len;
308 * Handles the subtleties of using sendfile with CIFS.
310 ssize_t onefs_sys_sendfile(connection_struct *conn, int tofd, int fromfd,
311 const DATA_BLOB *header, SMB_OFF_T offset,
312 size_t count)
314 bool atomic = false;
315 ssize_t ret = 0;
317 START_PROFILE_BYTES(syscall_sendfile, count);
319 if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
320 PARM_ATOMIC_SENDFILE,
321 PARM_ATOMIC_SENDFILE_DEFAULT)) {
322 atomic = true;
325 /* Try the sendfile */
326 ret = onefs_sys_do_sendfile(tofd, fromfd, header, offset, count,
327 atomic);
329 /* If the sendfile wasn't atomic, we're done. */
330 if (!atomic) {
331 DEBUG(10, ("non-atomic sendfile read %ul bytes\n", ret));
332 END_PROFILE(syscall_sendfile);
333 return ret;
337 * Atomic sendfile takes care to not write anything to the socket
338 * until all of the requested bytes have been read from the file.
339 * There are two atomic cases that need to be handled.
341 * 1. The file was truncated causing less data to be read than was
342 * requested. In this case, we return back to the caller to
343 * indicate 0 bytes were written to the socket. This should
344 * prompt the caller to fallback to the standard read path: read
345 * the data, create a header that indicates how many bytes were
346 * actually read, and send the header/data back to the client.
348 * This saves us from standard sendfile behavior of sending a
349 * header promising more data then will actually be sent. The
350 * only two options are to close the socket and kill the client
351 * connection, or write a bunch of 0s. Closing the client
352 * connection is bad because there could actually be multiple
353 * sessions multiplexed from the same client that are all dropped
354 * because of a truncate. Writing the remaining data as 0s also
355 * isn't good, because the client will have an incorrect version
356 * of the file. If the file is written back to the server, the 0s
357 * will be written back. Fortunately, atomic sendfile allows us
358 * to avoid making this choice in most cases.
360 * 2. One downside of atomic sendfile, is that there is a limit on
361 * the number of bytes that can be sent atomically. The kernel
362 * has a limited amount of mbuf space that it can read file data
363 * into without exhausting the system's mbufs, so a buffer of
364 * length xfsize is used. The xfsize at the time of writing this
365 * is 64K. xfsize bytes are read from the file, and subsequently
366 * written to the socket. This makes it impossible to do the
367 * sendfile atomically for a byte count > xfsize.
369 * To cope with large requests, atomic sendfile returns -1 with
370 * errno set to E2BIG. Since windows maxes out at 64K writes,
371 * this is currently only a concern with non-windows clients.
372 * Posix extensions allow the full 24bit bytecount field to be
373 * used in ReadAndX, and clients such as smbclient and the linux
374 * cifs client can request up to 16MB reads! There are a few
375 * options for handling large sendfile requests.
377 * a. Fall back to the standard read path. This is unacceptable
378 * because it would require prohibitively large mallocs.
380 * b. Fall back to using samba's fake_send_file which emulates
381 * the kernel sendfile in userspace. This still has the same
382 * problem of sending the header before all of the data has
383 * been read, so it doesn't buy us anything, and has worse
384 * performance than the kernel's zero-copy sendfile.
386 * c. Use non-atomic sendfile syscall to attempt a zero copy
387 * read, and hope that there isn't a short read due to
388 * truncation. In the case of a short read, there are two
389 * options:
391 * 1. Kill the client connection
393 * 2. Write zeros to the socket for the remaining bytes
394 * promised in the header.
396 * It is safer from a data corruption perspective to kill the
397 * client connection, so this is our default behavior, but if
398 * this causes problems this can be configured to write zeros
399 * via smb.conf.
402 /* Handle case 1: short read -> truncated file. */
403 if (ret == 0) {
404 END_PROFILE(syscall_sendfile);
405 return ret;
408 /* Handle case 2: large read. */
409 if (ret == -1 && errno == E2BIG) {
411 if (!lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
412 PARM_SENDFILE_LARGE_READS,
413 PARM_SENDFILE_LARGE_READS_DEFAULT)) {
414 DEBUG(3, ("Not attempting non-atomic large sendfile: "
415 "%lu bytes\n", count));
416 END_PROFILE(syscall_sendfile);
417 return 0;
420 if (count < 0x10000) {
421 DEBUG(0, ("Count < 2^16 and E2BIG was returned! %lu\n",
422 count));
425 DEBUG(10, ("attempting non-atomic large sendfile: %lu bytes\n",
426 count));
428 /* Try a non-atomic sendfile. */
429 ret = onefs_sys_do_sendfile(tofd, fromfd, header, offset,
430 count, false);
431 /* Real error: kill the client connection. */
432 if (ret == -1) {
433 DEBUG(1, ("error on non-atomic large sendfile "
434 "(%lu bytes): %s\n", count,
435 strerror(errno)));
436 END_PROFILE(syscall_sendfile);
437 return ret;
440 /* Short read: kill the client connection. */
441 if (ret != count + header->length) {
442 DEBUG(1, ("short read on non-atomic large sendfile "
443 "(%lu of %lu bytes): %s\n", ret, count,
444 strerror(errno)));
447 * Returning ret here would cause us to drop into the
448 * codepath that calls sendfile_short_send, which
449 * sends the client a bunch of zeros instead.
450 * Returning -1 kills the connection.
452 if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
453 PARM_SENDFILE_SAFE,
454 PARM_SENDFILE_SAFE_DEFAULT)) {
455 END_PROFILE(syscall_sendfile);
456 return -1;
459 END_PROFILE(syscall_sendfile);
460 return ret;
463 DEBUG(10, ("non-atomic large sendfile successful\n"));
466 /* There was error in the atomic sendfile. */
467 if (ret == -1) {
468 DEBUG(1, ("error on %s sendfile (%lu bytes): %s\n",
469 atomic ? "atomic" : "non-atomic",
470 count, strerror(errno)));
473 END_PROFILE(syscall_sendfile);
474 return ret;
478 * Only talloc the spill buffer once (reallocing when necessary).
480 static char *get_spill_buffer(size_t new_count)
482 static int cur_count = 0;
483 static char *spill_buffer = NULL;
485 /* If a sufficiently sized buffer exists, just return. */
486 if (new_count <= cur_count) {
487 SMB_ASSERT(spill_buffer);
488 return spill_buffer;
491 /* Allocate the first time. */
492 if (cur_count == 0) {
493 SMB_ASSERT(!spill_buffer);
494 spill_buffer = talloc_array(NULL, char, new_count);
495 if (spill_buffer) {
496 cur_count = new_count;
498 return spill_buffer;
501 /* A buffer exists, but it's not big enough, so realloc. */
502 SMB_ASSERT(spill_buffer);
503 spill_buffer = talloc_realloc(NULL, spill_buffer, char, new_count);
504 if (spill_buffer) {
505 cur_count = new_count;
507 return spill_buffer;
511 * recvfile does zero-copy writes given an fd to write to, and a socket with
512 * some data to write. If recvfile read more than it was able to write, it
513 * spills the data into a buffer. After first reading any additional data
514 * from the socket into the buffer, the spill buffer is then written with a
515 * standard pwrite.
517 ssize_t onefs_sys_recvfile(int fromfd, int tofd, SMB_OFF_T offset,
518 size_t count)
520 char *spill_buffer = NULL;
521 bool socket_drained = false;
522 int ret;
523 off_t total_rbytes = 0;
524 off_t total_wbytes = 0;
525 off_t rbytes;
526 off_t wbytes;
528 START_PROFILE_BYTES(syscall_recvfile, count);
530 DEBUG(10,("onefs_recvfile: from = %d, to = %d, offset=%llu, count = "
531 "%lu\n", fromfd, tofd, offset, count));
533 if (count == 0) {
534 END_PROFILE(syscall_recvfile);
535 return 0;
539 * Setup up a buffer for recvfile to spill data that has been read
540 * from the socket but not written.
542 spill_buffer = get_spill_buffer(count);
543 if (spill_buffer == NULL) {
544 ret = -1;
545 goto out;
549 * Keep trying recvfile until:
550 * - There is no data left to read on the socket, or
551 * - bytes read != bytes written, or
552 * - An error is returned that isn't EINTR/EAGAIN
554 do {
555 /* Keep track of bytes read/written for recvfile */
556 rbytes = 0;
557 wbytes = 0;
559 DEBUG(10, ("calling recvfile loop, offset + total_wbytes = "
560 "%llu, count - total_rbytes = %llu\n",
561 offset + total_wbytes, count - total_rbytes));
563 ret = recvfile(tofd, fromfd, offset + total_wbytes,
564 count - total_wbytes, &rbytes, &wbytes, 0,
565 spill_buffer);
567 DEBUG(10, ("recvfile ret = %d, errno = %d, rbytes = %llu, "
568 "wbytes = %llu\n", ret, ret >= 0 ? 0 : errno,
569 rbytes, wbytes));
571 /* Update our progress so far */
572 total_rbytes += rbytes;
573 total_wbytes += wbytes;
575 } while ((count - total_rbytes) && (rbytes == wbytes) &&
576 (ret == -1 && (errno == EINTR || errno == EAGAIN)));
578 DEBUG(10, ("total_rbytes = %llu, total_wbytes = %llu\n",
579 total_rbytes, total_wbytes));
581 /* Log if recvfile didn't write everything it read. */
582 if (total_rbytes != total_wbytes) {
583 DEBUG(3, ("partial recvfile: total_rbytes=%llu but "
584 "total_wbytes=%llu, diff = %llu\n", total_rbytes,
585 total_wbytes, total_rbytes - total_wbytes));
586 SMB_ASSERT(total_rbytes > total_wbytes);
590 * If there is still data on the socket, read it off.
592 while (total_rbytes < count) {
594 DEBUG(3, ("shallow recvfile (%s), reading %llu\n",
595 strerror(errno), count - total_rbytes));
598 * Read the remaining data into the spill buffer. recvfile
599 * may already have some data in the spill buffer, so start
600 * filling the buffer at total_rbytes - total_wbytes.
602 ret = sys_read(fromfd,
603 spill_buffer + (total_rbytes - total_wbytes),
604 count - total_rbytes);
606 if (ret <= 0) {
607 if (ret == 0) {
608 DEBUG(0, ("shallow recvfile read: EOF\n"));
609 } else {
610 DEBUG(0, ("shallow recvfile read failed: %s\n",
611 strerror(errno)));
613 /* Socket is dead, so treat as if it were drained. */
614 socket_drained = true;
615 goto out;
618 /* Data was read so update the rbytes */
619 total_rbytes += ret;
622 if (total_rbytes != count) {
623 smb_panic("Unread recvfile data still on the socket!");
627 * Now write any spilled data + the extra data read off the socket.
629 while (total_wbytes < count) {
631 DEBUG(3, ("partial recvfile, writing %llu\n", count - total_wbytes));
633 ret = sys_pwrite(tofd, spill_buffer, count - total_wbytes,
634 offset + total_wbytes);
636 if (ret == -1) {
637 DEBUG(0, ("partial recvfile write failed: %s\n",
638 strerror(errno)));
639 goto out;
642 /* Data was written so update the wbytes */
643 total_wbytes += ret;
646 /* Success! */
647 ret = total_wbytes;
649 out:
651 END_PROFILE(syscall_recvfile);
653 /* Make sure we always try to drain the socket. */
654 if (!socket_drained && count - total_rbytes) {
655 int saved_errno = errno;
657 if (drain_socket(fromfd, count - total_rbytes) !=
658 count - total_rbytes) {
659 /* Socket is dead! */
660 DEBUG(0, ("drain socket failed: %d\n", errno));
662 errno = saved_errno;
665 return ret;