root/source3/modules/onefs_system.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. smlock_init
  2. smlock_dump
  3. onefs_sys_create_file
  4. onefs_sys_do_sendfile
  5. onefs_sys_sendfile
  6. get_spill_buffer
  7. onefs_sys_recvfile

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

/* [<][>][^][v][top][bottom][index][help] */