s3 OneFS: Add a parameter that unconditionally allows execute access
[samba.git] / source3 / modules / onefs_system.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Support for OneFS system interfaces.
4  *
5  * Copyright (C) Tim Prouty, 2008
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "onefs.h"
22
23 #include <ifs/ifs_syscalls.h>
24 #include <isi_acl/isi_acl_util.h>
25
26 /*
27  * Initialize the sm_lock struct before passing it to ifs_createfile.
28  */
29 static void smlock_init(connection_struct *conn, struct sm_lock *sml,
30     bool isexe, uint32_t access_mask, uint32_t share_access,
31     uint32_t create_options)
32 {
33         sml->sm_type.doc = false;
34         sml->sm_type.isexe = isexe;
35         sml->sm_type.statonly = is_stat_open(access_mask);
36         sml->sm_type.access_mask = access_mask;
37         sml->sm_type.share_access = share_access;
38
39         /*
40          * private_options was previously used for DENY_DOS/DENY_FCB checks in
41          * the kernel, but are now properly handled by fcb_or_dos_open. In
42          * these cases, ifs_createfile will return a sharing violation, which
43          * gives fcb_or_dos_open the chance to open a duplicate file handle.
44          */
45         sml->sm_type.private_options = 0;
46
47         /* 1 second delay is handled in onefs_open.c by deferring the open */
48         sml->sm_timeout = timeval_set(0, 0);
49 }
50
51 static void smlock_dump(int debuglevel, const struct sm_lock *sml)
52 {
53         if (sml == NULL) {
54                 DEBUG(debuglevel, ("sml == NULL\n"));
55                 return;
56         }
57
58         DEBUG(debuglevel,
59               ("smlock: doc=%s, isexec=%s, statonly=%s, access_mask=0x%x, "
60                "share_access=0x%x, private_options=0x%x timeout=%d/%d\n",
61                sml->sm_type.doc ? "True" : "False",
62                sml->sm_type.isexe ? "True" : "False",
63                sml->sm_type.statonly ? "True" : "False",
64                sml->sm_type.access_mask,
65                sml->sm_type.share_access,
66                sml->sm_type.private_options,
67                (int)sml->sm_timeout.tv_sec,
68                (int)sml->sm_timeout.tv_usec));
69 }
70
71 /**
72  * External interface to ifs_createfile
73  */
74 int onefs_sys_create_file(connection_struct *conn,
75                           int base_fd,
76                           const char *path,
77                           uint32_t access_mask,
78                           uint32_t open_access_mask,
79                           uint32_t share_access,
80                           uint32_t create_options,
81                           int flags,
82                           mode_t mode,
83                           int oplock_request,
84                           uint64_t id,
85                           struct security_descriptor *sd,
86                           uint32_t dos_flags,
87                           int *granted_oplock)
88 {
89         struct sm_lock sml, *psml = NULL;
90         enum oplock_type onefs_oplock;
91         enum oplock_type onefs_granted_oplock = OPLOCK_NONE;
92         struct ifs_security_descriptor ifs_sd = {}, *pifs_sd = NULL;
93         int secinfo = 0;
94         int ret_fd = -1;
95         uint32_t onefs_dos_attributes;
96         struct ifs_createfile_flags cf_flags = CF_FLAGS_NONE;
97
98         /* Setup security descriptor and get secinfo. */
99         if (sd != NULL) {
100                 NTSTATUS status;
101
102                 secinfo = (get_sec_info(sd) & IFS_SEC_INFO_KNOWN_MASK);
103
104                 status = onefs_samba_sd_to_sd(secinfo, sd, &ifs_sd, SNUM(conn));
105
106                 if (!NT_STATUS_IS_OK(status)) {
107                         DEBUG(1, ("SD initialization failure: %s",
108                                   nt_errstr(status)));
109                         errno = EINVAL;
110                         goto out;
111                 }
112
113                 pifs_sd = &ifs_sd;
114         }
115
116         /* Stripping off private bits will be done for us. */
117         onefs_oplock = onefs_samba_oplock_to_oplock(oplock_request);
118
119         if (!lp_oplocks(SNUM(conn))) {
120                 SMB_ASSERT(onefs_oplock == OPLOCK_NONE);
121         }
122
123         /* Convert samba dos flags to UF_DOS_* attributes. */
124         onefs_dos_attributes = dos_attributes_to_stat_dos_flags(dos_flags);
125
126         /**
127          * Deal with kernel creating Default ACLs. (Isilon bug 47447.)
128          *
129          * 1) "nt acl support = no", default_acl = no
130          * 2) "inherit permissions = yes", default_acl = no
131          */
132         if (lp_nt_acl_support(SNUM(conn)) && !lp_inherit_perms(SNUM(conn)))
133                 cf_flags = cf_flags_or(cf_flags, CF_FLAGS_DEFAULT_ACL);
134
135         /*
136          * Some customer workflows require the execute bit to be ignored.
137          */
138         if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
139                          PARM_ALLOW_EXECUTE_ALWAYS,
140                          PARM_ALLOW_EXECUTE_ALWAYS_DEFAULT) &&
141             (open_access_mask & FILE_EXECUTE)) {
142
143                 DEBUG(3, ("Stripping execute bit from %s: (0x%x)\n", path,
144                           open_access_mask));
145
146                 /* Strip execute. */
147                 open_access_mask &= ~FILE_EXECUTE;
148
149                 /*
150                  * Add READ_DATA, so we're not left with desired_access=0. An
151                  * execute call should imply the client will read the data.
152                  */
153                 open_access_mask |= FILE_READ_DATA;
154
155                 DEBUGADD(3, ("New stripped access mask: 0x%x\n",
156                              open_access_mask));
157         }
158
159         DEBUG(10,("onefs_sys_create_file: base_fd = %d, "
160                   "open_access_mask = 0x%x, flags = 0x%x, mode = 0%o, "
161                   "desired_oplock = %s, id = 0x%x, secinfo = 0x%x, sd = %p, "
162                   "dos_attributes = 0x%x, path = %s, "
163                   "default_acl=%s\n", base_fd,
164                   (unsigned int)open_access_mask,
165                   (unsigned int)flags,
166                   (unsigned int)mode,
167                   onefs_oplock_str(onefs_oplock),
168                   (unsigned int)id,
169                   (unsigned int)secinfo, sd,
170                   (unsigned int)onefs_dos_attributes, path,
171                   cf_flags_and_bool(cf_flags, CF_FLAGS_DEFAULT_ACL) ?
172                       "true" : "false"));
173
174         /* Initialize smlock struct for files/dirs but not internal opens */
175         if (!(oplock_request & INTERNAL_OPEN_ONLY)) {
176                 smlock_init(conn, &sml, is_executable(path), access_mask,
177                     share_access, create_options);
178                 psml = &sml;
179         }
180
181         smlock_dump(10, psml);
182
183         ret_fd = ifs_createfile(base_fd, path,
184             (enum ifs_ace_rights)open_access_mask, flags & ~O_ACCMODE, mode,
185             onefs_oplock, id, psml, secinfo, pifs_sd, onefs_dos_attributes,
186             cf_flags, &onefs_granted_oplock);
187
188         DEBUG(10,("onefs_sys_create_file(%s): ret_fd = %d, "
189                   "onefs_granted_oplock = %s\n",
190                   ret_fd < 0 ? strerror(errno) : "success", ret_fd,
191                   onefs_oplock_str(onefs_granted_oplock)));
192
193         if (granted_oplock) {
194                 *granted_oplock =
195                     onefs_oplock_to_samba_oplock(onefs_granted_oplock);
196         }
197
198  out:
199         aclu_free_sd(pifs_sd, false);
200
201         return ret_fd;
202 }
203
204 /**
205  * FreeBSD based sendfile implementation that allows for atomic semantics.
206  */
207 static ssize_t onefs_sys_do_sendfile(int tofd, int fromfd,
208     const DATA_BLOB *header, SMB_OFF_T offset, size_t count, bool atomic)
209 {
210         size_t total=0;
211         struct sf_hdtr hdr;
212         struct iovec hdtrl;
213         size_t hdr_len = 0;
214         int flags = 0;
215
216         if (atomic) {
217                 flags = SF_ATOMIC;
218         }
219
220         hdr.headers = &hdtrl;
221         hdr.hdr_cnt = 1;
222         hdr.trailers = NULL;
223         hdr.trl_cnt = 0;
224
225         /* Set up the header iovec. */
226         if (header) {
227                 hdtrl.iov_base = header->data;
228                 hdtrl.iov_len = hdr_len = header->length;
229         } else {
230                 hdtrl.iov_base = NULL;
231                 hdtrl.iov_len = 0;
232         }
233
234         total = count;
235         while (total + hdtrl.iov_len) {
236                 SMB_OFF_T nwritten;
237                 int ret;
238
239                 /*
240                  * FreeBSD sendfile returns 0 on success, -1 on error.
241                  * Remember, the tofd and fromfd are reversed..... :-).
242                  * nwritten includes the header data sent.
243                  */
244
245                 do {
246                         ret = sendfile(fromfd, tofd, offset, total, &hdr,
247                                        &nwritten, flags);
248                 } while (ret == -1 && errno == EINTR);
249
250                 /* On error we're done. */
251                 if (ret == -1) {
252                         return -1;
253                 }
254
255                 /*
256                  * If this was an ATOMIC sendfile, nwritten doesn't
257                  * necessarily indicate an error.  It could mean count > than
258                  * what sendfile can handle atomically (usually 64K) or that
259                  * there was a short read due to the file being truncated.
260                  */
261                 if (nwritten == 0) {
262                         return atomic ? 0 : -1;
263                 }
264
265                 /*
266                  * An atomic sendfile should never send partial data!
267                  */
268                 if (atomic && nwritten != total + hdtrl.iov_len) {
269                         DEBUG(0,("Atomic sendfile() sent partial data: "
270                                  "%llu of %d\n", nwritten,
271                                  total + hdtrl.iov_len));
272                         return -1;
273                 }
274
275                 /*
276                  * If this was a short (signal interrupted) write we may need
277                  * to subtract it from the header data, or null out the header
278                  * data altogether if we wrote more than hdtrl.iov_len bytes.
279                  * We change nwritten to be the number of file bytes written.
280                  */
281
282                 if (hdtrl.iov_base && hdtrl.iov_len) {
283                         if (nwritten >= hdtrl.iov_len) {
284                                 nwritten -= hdtrl.iov_len;
285                                 hdtrl.iov_base = NULL;
286                                 hdtrl.iov_len = 0;
287                         } else {
288                                 hdtrl.iov_base =
289                                     (caddr_t)hdtrl.iov_base + nwritten;
290                                 hdtrl.iov_len -= nwritten;
291                                 nwritten = 0;
292                         }
293                 }
294                 total -= nwritten;
295                 offset += nwritten;
296         }
297         return count + hdr_len;
298 }
299
300 /**
301  * Handles the subtleties of using sendfile with CIFS.
302  */
303 ssize_t onefs_sys_sendfile(connection_struct *conn, int tofd, int fromfd,
304                            const DATA_BLOB *header, SMB_OFF_T offset,
305                            size_t count)
306 {
307         bool atomic = false;
308         ssize_t ret = 0;
309
310         if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
311                          PARM_ATOMIC_SENDFILE,
312                          PARM_ATOMIC_SENDFILE_DEFAULT)) {
313                 atomic = true;
314         }
315
316         /* Try the sendfile */
317         ret = onefs_sys_do_sendfile(tofd, fromfd, header, offset, count,
318                                     atomic);
319
320         /* If the sendfile wasn't atomic, we're done. */
321         if (!atomic) {
322                 DEBUG(10, ("non-atomic sendfile read %ul bytes", ret));
323                 return ret;
324         }
325
326         /*
327          * Atomic sendfile takes care to not write anything to the socket
328          * until all of the requested bytes have been read from the file.
329          * There are two atomic cases that need to be handled.
330          *
331          *  1. The file was truncated causing less data to be read than was
332          *     requested.  In this case, we return back to the caller to
333          *     indicate 0 bytes were written to the socket.  This should
334          *     prompt the caller to fallback to the standard read path: read
335          *     the data, create a header that indicates how many bytes were
336          *     actually read, and send the header/data back to the client.
337          *
338          *     This saves us from standard sendfile behavior of sending a
339          *     header promising more data then will actually be sent.  The
340          *     only two options are to close the socket and kill the client
341          *     connection, or write a bunch of 0s.  Closing the client
342          *     connection is bad because there could actually be multiple
343          *     sessions multiplexed from the same client that are all dropped
344          *     because of a truncate.  Writing the remaining data as 0s also
345          *     isn't good, because the client will have an incorrect version
346          *     of the file.  If the file is written back to the server, the 0s
347          *     will be written back.  Fortunately, atomic sendfile allows us
348          *     to avoid making this choice in most cases.
349          *
350          *  2. One downside of atomic sendfile, is that there is a limit on
351          *     the number of bytes that can be sent atomically.  The kernel
352          *     has a limited amount of mbuf space that it can read file data
353          *     into without exhausting the system's mbufs, so a buffer of
354          *     length xfsize is used.  The xfsize at the time of writing this
355          *     is 64K.  xfsize bytes are read from the file, and subsequently
356          *     written to the socket.  This makes it impossible to do the
357          *     sendfile atomically for a byte count > xfsize.
358          *
359          *     To cope with large requests, atomic sendfile returns -1 with
360          *     errno set to E2BIG.  Since windows maxes out at 64K writes,
361          *     this is currently only a concern with non-windows clients.
362          *     Posix extensions allow the full 24bit bytecount field to be
363          *     used in ReadAndX, and clients such as smbclient and the linux
364          *     cifs client can request up to 16MB reads!  There are a few
365          *     options for handling large sendfile requests.
366          *
367          *      a. Fall back to the standard read path.  This is unacceptable
368          *         because it would require prohibitively large mallocs.
369          *
370          *      b. Fall back to using samba's fake_send_file which emulates
371          *         the kernel sendfile in userspace.  This still has the same
372          *         problem of sending the header before all of the data has
373          *         been read, so it doesn't buy us anything, and has worse
374          *         performance than the kernel's zero-copy sendfile.
375          *
376          *      c. Use non-atomic sendfile syscall to attempt a zero copy
377          *         read, and hope that there isn't a short read due to
378          *         truncation.  In the case of a short read, there are two
379          *         options:
380          *
381          *          1. Kill the client connection
382          *
383          *          2. Write zeros to the socket for the remaining bytes
384          *             promised in the header.
385          *
386          *         It is safer from a data corruption perspective to kill the
387          *         client connection, so this is our default behavior, but if
388          *         this causes problems this can be configured to write zeros
389          *         via smb.conf.
390          */
391
392         /* Handle case 1: short read -> truncated file. */
393         if (ret == 0) {
394                 return ret;
395         }
396
397         /* Handle case 2: large read. */
398         if (ret == -1 && errno == E2BIG) {
399
400                 if (!lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
401                                  PARM_SENDFILE_LARGE_READS,
402                                  PARM_SENDFILE_LARGE_READS_DEFAULT)) {
403                         DEBUG(3, ("Not attempting non-atomic large sendfile: "
404                                   "%lu bytes\n", count));
405                         return 0;
406                 }
407
408                 if (count < 0x10000) {
409                         DEBUG(0, ("Count < 2^16 and E2BIG was returned! %lu",
410                                   count));
411                 }
412
413                 DEBUG(10, ("attempting non-atomic large sendfile: %lu bytes\n",
414                            count));
415
416                 /* Try a non-atomic sendfile. */
417                 ret = onefs_sys_do_sendfile(tofd, fromfd, header, offset,
418                                             count, false);
419                 /* Real error: kill the client connection. */
420                 if (ret == -1) {
421                         DEBUG(1, ("error on non-atomic large sendfile "
422                                   "(%lu bytes): %s\n", count,
423                                   strerror(errno)));
424                         return ret;
425                 }
426
427                 /* Short read: kill the client connection. */
428                 if (ret != count + header->length) {
429                         DEBUG(1, ("short read on non-atomic large sendfile "
430                                   "(%lu of %lu bytes): %s\n", ret, count,
431                                   strerror(errno)));
432
433                         /*
434                          * Returning ret here would cause us to drop into the
435                          * codepath that calls sendfile_short_send, which
436                          * sends the client a bunch of zeros instead.
437                          * Returning -1 kills the connection.
438                          */
439                         if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
440                                 PARM_SENDFILE_SAFE,
441                                 PARM_SENDFILE_SAFE_DEFAULT)) {
442                                 return -1;
443                         }
444
445                         return ret;
446                 }
447
448                 DEBUG(10, ("non-atomic large sendfile successful\n"));
449         }
450
451         /* There was error in the atomic sendfile. */
452         if (ret == -1) {
453                 DEBUG(1, ("error on %s sendfile (%lu bytes): %s\n",
454                           atomic ? "atomic" : "non-atomic",
455                           count, strerror(errno)));
456         }
457
458         return ret;
459 }
460
461 /**
462  * Only talloc the spill buffer once (reallocing when necessary).
463  */
464 static char *get_spill_buffer(size_t new_count)
465 {
466         static int cur_count = 0;
467         static char *spill_buffer = NULL;
468
469         /* If a sufficiently sized buffer exists, just return. */
470         if (new_count <= cur_count) {
471                 SMB_ASSERT(spill_buffer);
472                 return spill_buffer;
473         }
474
475         /* Allocate the first time. */
476         if (cur_count == 0) {
477                 SMB_ASSERT(!spill_buffer);
478                 spill_buffer = talloc_array(NULL, char, new_count);
479                 if (spill_buffer) {
480                         cur_count = new_count;
481                 }
482                 return spill_buffer;
483         }
484
485         /* A buffer exists, but it's not big enough, so realloc. */
486         SMB_ASSERT(spill_buffer);
487         spill_buffer = talloc_realloc(NULL, spill_buffer, char, new_count);
488         if (spill_buffer) {
489                 cur_count = new_count;
490         }
491         return spill_buffer;
492 }
493
494 /**
495  * recvfile does zero-copy writes given an fd to write to, and a socket with
496  * some data to write.  If recvfile read more than it was able to write, it
497  * spills the data into a buffer.  After first reading any additional data
498  * from the socket into the buffer, the spill buffer is then written with a
499  * standard pwrite.
500  */
501 ssize_t onefs_sys_recvfile(int fromfd, int tofd, SMB_OFF_T offset,
502                            size_t count)
503 {
504         char *spill_buffer = NULL;
505         bool socket_drained = false;
506         int ret;
507         off_t total_rbytes = 0;
508         off_t total_wbytes = 0;
509         off_t rbytes;
510         off_t wbytes;
511
512         DEBUG(10,("onefs_recvfile: from = %d, to = %d, offset=%llu, count = "
513                   "%lu\n", fromfd, tofd, offset, count));
514
515         if (count == 0) {
516                 return 0;
517         }
518
519         /*
520          * Setup up a buffer for recvfile to spill data that has been read
521          * from the socket but not written.
522          */
523         spill_buffer = get_spill_buffer(count);
524         if (spill_buffer == NULL) {
525                 ret = -1;
526                 goto out;
527         }
528
529         /*
530          * Keep trying recvfile until:
531          *  - There is no data left to read on the socket, or
532          *  - bytes read != bytes written, or
533          *  - An error is returned that isn't EINTR/EAGAIN
534          */
535         do {
536                 /* Keep track of bytes read/written for recvfile */
537                 rbytes = 0;
538                 wbytes = 0;
539
540                 DEBUG(10, ("calling recvfile loop, offset + total_wbytes = "
541                            "%llu, count - total_rbytes = %llu\n",
542                            offset + total_wbytes, count - total_rbytes));
543
544                 ret = recvfile(tofd, fromfd, offset + total_wbytes,
545                                count - total_wbytes, &rbytes, &wbytes, 0,
546                                spill_buffer);
547
548                 DEBUG(10, ("recvfile ret = %d, errno = %d, rbytes = %llu, "
549                            "wbytes = %llu\n", ret, ret >= 0 ? 0 : errno,
550                            rbytes, wbytes));
551
552                 /* Update our progress so far */
553                 total_rbytes += rbytes;
554                 total_wbytes += wbytes;
555
556         } while ((count - total_rbytes) && (rbytes == wbytes) &&
557                  (ret == -1 && (errno == EINTR || errno == EAGAIN)));
558
559         DEBUG(10, ("total_rbytes = %llu, total_wbytes = %llu\n",
560                    total_rbytes, total_wbytes));
561
562         /* Log if recvfile didn't write everything it read. */
563         if (total_rbytes != total_wbytes) {
564                 DEBUG(0, ("partial recvfile: total_rbytes=%llu but "
565                           "total_wbytes=%llu, diff = %llu\n", total_rbytes,
566                           total_wbytes, total_rbytes - total_wbytes));
567                 SMB_ASSERT(total_rbytes > total_wbytes);
568         }
569
570         /*
571          * If there is still data on the socket, read it off.
572          */
573         while (total_rbytes < count) {
574
575                 DEBUG(0, ("shallow recvfile, reading %llu\n",
576                           count - total_rbytes));
577
578                 /*
579                  * Read the remaining data into the spill buffer.  recvfile
580                  * may already have some data in the spill buffer, so start
581                  * filling the buffer at total_rbytes - total_wbytes.
582                  */
583                 ret = sys_read(fromfd,
584                                spill_buffer + (total_rbytes - total_wbytes),
585                                count - total_rbytes);
586
587                 if (ret == -1) {
588                         DEBUG(0, ("shallow recvfile read failed: %s\n",
589                                   strerror(errno)));
590                         /* Socket is dead, so treat as if it were drained. */
591                         socket_drained = true;
592                         goto out;
593                 }
594
595                 /* Data was read so update the rbytes */
596                 total_rbytes += ret;
597         }
598
599         if (total_rbytes != count) {
600                 smb_panic("Unread recvfile data still on the socket!");
601         }
602
603         /*
604          * Now write any spilled data + the extra data read off the socket.
605          */
606         while (total_wbytes < count) {
607
608                 DEBUG(0, ("partial recvfile, writing %llu\n", count - total_wbytes));
609
610                 ret = sys_pwrite(tofd, spill_buffer, count - total_wbytes,
611                                  offset + total_wbytes);
612
613                 if (ret == -1) {
614                         DEBUG(0, ("partial recvfile write failed: %s\n",
615                                   strerror(errno)));
616                         goto out;
617                 }
618
619                 /* Data was written so update the wbytes */
620                 total_wbytes += ret;
621         }
622
623         /* Success! */
624         ret = total_wbytes;
625
626 out:
627         /* Make sure we always try to drain the socket. */
628         if (!socket_drained && count - total_rbytes) {
629                 int saved_errno = errno;
630
631                 if (drain_socket(fromfd, count - total_rbytes) !=
632                     count - total_rbytes) {
633                         /* Socket is dead! */
634                         DEBUG(0, ("drain socket failed: %d\n", errno));
635                 }
636                 errno = saved_errno;
637         }
638
639         return ret;
640 }