Removed version number from file header.
[samba.git] / source / smbd / fileio.c
index 79dcd8ab83fda04460150c1788ee3ace52d0b5c2..2aafd14c998eb3a889ee1544f1b29465c60f1cbd 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    read/write to a files_struct
    Copyright (C) Andrew Tridgell 1992-1998
    
@@ -21,8 +20,6 @@
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-
 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
 
 /****************************************************************************
@@ -37,7 +34,7 @@ SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
   if (fsp->print_file && lp_postscript(fsp->conn->service))
     offset = 3;
 
-  seek_ret = fsp->conn->vfs_ops.lseek(fsp->fd,pos+offset,SEEK_SET);
+  seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos+offset,SEEK_SET);
 
   /*
    * We want to maintain the fiction that we can seek
@@ -69,7 +66,6 @@ SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
  Read from write cache if we can.
 ****************************************************************************/
 
-static unsigned int cache_read_hits;
 
 BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
 {
@@ -83,7 +79,7 @@ BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
 
   memcpy(data, wcp->data + (pos - wcp->offset), n);
 
-  cache_read_hits++;
+  DO_PROFILE_INC(writecache_read_hits);
 
   return True;
 }
@@ -94,53 +90,57 @@ read from a file
 
 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
 {
-  ssize_t ret=0,readret;
+       ssize_t ret=0,readret;
 
-  /* you can't read from print files */
-  if (fsp->print_file) {
-         return -1;
-  }
+       /* you can't read from print files */
+       if (fsp->print_file)
+               return -1;
 
-  /*
-   * Serve from write cache if we can.
-   */
-  if(read_from_write_cache(fsp, data, pos, n))
-    return n;
+       /*
+        * Serve from write cache if we can.
+        */
 
-  flush_write_cache(fsp, READ_FLUSH);
+       if(read_from_write_cache(fsp, data, pos, n))
+               return n;
 
-  if (seek_file(fsp,pos) == -1) {
-    DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
-    return(ret);
-  }
+       flush_write_cache(fsp, READ_FLUSH);
+
+       if (seek_file(fsp,pos) == -1) {
+               DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
+               return(ret);
+       }
   
-  if (n > 0) {
-    readret = fsp->conn->vfs_ops.read(fsp->fd,data,n);
-    if (readret == -1)
-      return -1;
-    if (readret > 0) ret += readret;
-  }
+       if (n > 0) {
+#ifdef DMF_FIX
+               int numretries = 3;
+tryagain:
+               readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
+               if (readret == -1) {
+                       if ((errno == EAGAIN) && numretries) {
+                               DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
+                               (void)sleep(10);
+                               --numretries;
+                               goto tryagain;
+                       }
+                       return -1;
+               }
+#else /* NO DMF fix. */
+               readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
+               if (readret == -1)
+                       return -1;
+#endif
+               if (readret > 0)
+                       ret += readret;
+       }
 
-  return(ret);
+       return(ret);
 }
 
-/* Write cache static counters. */
-
-static unsigned int abutted_writes;
-static unsigned int total_writes;
-static unsigned int non_oplock_writes;
-static unsigned int direct_writes;
-static unsigned int init_writes;
-static unsigned int flushed_writes;
-static unsigned int num_perfect_writes;
-static unsigned int flush_reasons[NUM_FLUSH_REASONS];
-
 /* how many write cache buffers have been allocated */
 static unsigned int allocated_write_caches;
-static unsigned int num_write_caches;
 
 /****************************************************************************
- *Really* write to a file
+ *Really* write to a file.
 ****************************************************************************/
 
 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
@@ -148,7 +148,7 @@ static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_
   if ((pos != -1) && (seek_file(fsp,pos) == -1))
     return -1;
 
-  return write_data(fsp->fd,data,n);
+  return vfs_write_data(fsp,data,n);
 }
 
 /****************************************************************************
@@ -174,8 +174,9 @@ ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
     SMB_STRUCT_STAT st;
     fsp->modified = True;
 
-    if (fsp->conn->vfs_ops.fstat(fsp->fd,&st) == 0) {
+    if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
       int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
+      fsp->size = st.st_size;
       if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) {        
         file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
       }
@@ -185,17 +186,19 @@ ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
        * the write cache.
        */
 
-      if ((fsp->oplock_type == EXCLUSIVE_OPLOCK) && !wcp) {
+      if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
         setup_write_cache(fsp, st.st_size);
         wcp = fsp->wcp;
       } 
     }  
   }
 
-  total_writes++;
+#ifdef WITH_PROFILE
+  DO_PROFILE_INC(writecache_total_writes);
   if (!fsp->oplock_type) {
-    non_oplock_writes++;
+    DO_PROFILE_INC(writecache_non_oplock_writes);
   }
+#endif
 
   /*
    * If this file is level II oplocked then we need
@@ -205,103 +208,43 @@ ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
    * the shared memory area whilst doing this.
    */
 
-  if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
-    share_mode_entry *share_list = NULL;
-    pid_t pid = sys_getpid();
-    int token = -1;
-    int num_share_modes = 0;
-    int i;
-
-    if (lock_share_entry_fsp(fsp) == False) {
-      DEBUG(0,("write_file: failed to lock share mode entry for file %s.\n", fsp->fsp_name ));
-    }
-
-    num_share_modes = get_share_modes(fsp->conn, fsp->dev, fsp->inode, &share_list);
-
-    for(i = 0; i < num_share_modes; i++) {
-      share_mode_entry *share_entry = &share_list[i];
-
-      /*
-       * As there could have been multiple writes waiting at the lock_share_entry
-       * gate we may not be the first to enter. Hence the state of the op_types
-       * in the share mode entries may be partly NO_OPLOCK and partly LEVEL_II
-       * oplock. It will do no harm to re-send break messages to those smbd's
-       * that are still waiting their turn to remove their LEVEL_II state, and
-       * also no harm to ignore existing NO_OPLOCK states. JRA.
-       */
-
-      if (share_entry->op_type == NO_OPLOCK)
-        continue;
-
-      /* Paranoia .... */
-      if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
-        DEBUG(0,("write_file: PANIC. share mode entry %d is an exlusive oplock !\n", i ));
-        unlock_share_entry(fsp->conn, fsp->dev, fsp->inode);
-        abort();
-      }
-
-      /*
-       * Check if this is a file we have open (including the
-       * file we've been called to do write_file on. If so
-       * then break it directly without releasing the lock.
-       */
-
-      if (pid == share_entry->pid) {
-        files_struct *new_fsp = file_find_dit(fsp->dev, fsp->inode, &share_entry->time);
-
-        /* Paranoia check... */
-        if(new_fsp == NULL) {
-          DEBUG(0,("write_file: PANIC. share mode entry %d is not a local file !\n", i ));
-          unlock_share_entry(fsp->conn, fsp->dev, fsp->inode);
-          abort();
-        }
-        oplock_break_level2(new_fsp, True, token);
-
-      } else {
-
-        /*
-         * This is a remote file and so we send an asynchronous
-         * message.
-         */
-
-        request_oplock_break(share_entry, fsp->dev, fsp->inode);
-      }
-    }
-    free((char *)share_list);
-    unlock_share_entry_fsp(fsp);
-  }
-
-  /* Paranoia check... */
-  if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
-    DEBUG(0,("write_file: PANIC. File %s still has a level II oplock.\n", fsp->fsp_name));
-    abort();
-  }
+  release_level_2_oplocks_on_change(fsp);
 
-  if (total_writes % 500 == 0) {
-    DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u flushes=%u total=%u \
+#ifdef WITH_PROFILE
+  if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
+    DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
-         init_writes, abutted_writes, flushed_writes, total_writes, non_oplock_writes,
-         allocated_write_caches,
-         num_write_caches, direct_writes, num_perfect_writes, cache_read_hits ));
-
-    DEBUG(3,("WRITECACHE: SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
-    flush_reasons[SEEK_FLUSH],
-    flush_reasons[READ_FLUSH],
-    flush_reasons[WRITE_FLUSH],
-    flush_reasons[READRAW_FLUSH],
-    flush_reasons[OPLOCK_RELEASE_FLUSH],
-    flush_reasons[CLOSE_FLUSH],
-    flush_reasons[SYNC_FLUSH] ));
+       profile_p->writecache_init_writes,
+       profile_p->writecache_abutted_writes,
+       profile_p->writecache_total_writes,
+       profile_p->writecache_non_oplock_writes,
+       profile_p->writecache_allocated_write_caches,
+       profile_p->writecache_num_write_caches,
+       profile_p->writecache_direct_writes,
+       profile_p->writecache_num_perfect_writes,
+       profile_p->writecache_read_hits ));
+
+    DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
+       profile_p->writecache_flushed_writes[SEEK_FLUSH],
+       profile_p->writecache_flushed_writes[READ_FLUSH],
+       profile_p->writecache_flushed_writes[WRITE_FLUSH],
+       profile_p->writecache_flushed_writes[READRAW_FLUSH],
+       profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
+       profile_p->writecache_flushed_writes[CLOSE_FLUSH],
+       profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
   }
+#endif
 
   if(!wcp) {
-    direct_writes++;
-    return real_write_file(fsp, data, pos, n);
+    DO_PROFILE_INC(writecache_direct_writes);
+    total_written = real_write_file(fsp, data, pos, n);
+    if ((total_written != -1) && (pos + total_written > fsp->size))
+      fsp->size = pos + total_written;
+    return total_written;
   }
 
-  DEBUG(9,("write_file(fd=%d pos=%d size=%d) wofs=%d wsize=%d\n",
-          fsp->fd, (int)pos, (int)n, (int)wcp->offset, (int)wcp->data_size));
+  DEBUG(9,("write_file(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
+          fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
 
   /* 
    * If we have active cache and it isn't contiguous then we flush.
@@ -329,6 +272,13 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
       if(pos + data_used > wcp->offset + wcp->data_size)
         wcp->data_size = pos + data_used - wcp->offset;
 
+      /*
+       * Update the file size if changed.
+       */
+
+      if (wcp->offset + wcp->data_size > wcp->file_size)
+        fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+
       /*
        * If we used all the data then
        * return here.
@@ -348,7 +298,7 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
       pos += data_used;
       n -= data_used;
 
-      abutted_writes++;
+      DO_PROFILE_INC(writecache_abutted_writes);
       total_written = data_used;
 
       write_path = 1;
@@ -371,6 +321,13 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
       if(pos + n > wcp->offset + wcp->data_size)
         wcp->data_size = pos + n - wcp->offset;
 
+      /*
+       * Update the file size if changed.
+       */
+
+      if (wcp->offset + wcp->data_size > wcp->file_size)
+        fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+
       /*
        * We don't need to move the start of data, but we
        * cut down the amount left by the amount used.
@@ -384,18 +341,21 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
 
       cache_flush_needed = True;
 
-      abutted_writes++;
+      DO_PROFILE_INC(writecache_abutted_writes);
       total_written = data_used;
 
       write_path = 2;
 
     } else if ( (pos >= wcp->file_size) && 
+                (wcp->offset + wcp->data_size == wcp->file_size) &&
                 (pos > wcp->offset + wcp->data_size) && 
                 (pos < wcp->offset + wcp->alloc_size) ) {
 
       /*
        * Non-contiguous write part of which fits within
-       * the cache buffer and is extending the file.
+       * the cache buffer and is extending the file
+       * and the cache contents reflect the current
+       * data up to the current end of the file.
        */
 
       size_t data_used;
@@ -422,18 +382,11 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
         wcp->data_size = pos + data_used - wcp->offset;
 
       /*
-       * Update the known file length.
+       * Update the file size if changed.
        */
 
-      wcp->file_size = wcp->offset + wcp->data_size;
-
-#if 0
-      if (set_filelen(fsp->fd, wcp->file_size) == -1) {
-        DEBUG(0,("write_file: error %s in setting file to length %.0f\n",
-          strerror(errno), (double)wcp->file_size ));
-        return -1;
-      }
-#endif
+      if (wcp->offset + wcp->data_size > wcp->file_size)
+        fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
 
       /*
        * If we used all the data then
@@ -454,7 +407,7 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
       pos += data_used;
       n -= data_used;
 
-      abutted_writes++;
+      DO_PROFILE_INC(writecache_abutted_writes);
       total_written = data_used;
 
       write_path = 3;
@@ -474,7 +427,7 @@ len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigne
        */
 
       if(pos + n > wcp->file_size)
-        wcp->file_size = pos + n;
+        fsp->size = wcp->file_size = pos + n;
 
       /*
        * If write would fit in the cache, and is larger than
@@ -486,8 +439,16 @@ len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigne
       if ( n <= wcp->alloc_size && n > wcp->data_size) {
         cache_flush_needed = True;
       } else {
-        direct_writes++;
-        return real_write_file(fsp, data, pos, n);
+        ssize_t ret = real_write_file(fsp, data, pos, n);
+
+        DO_PROFILE_INC(writecache_direct_writes);
+        if (ret == -1)
+          return ret;
+
+        if (pos + ret > wcp->file_size)
+          fsp->size = wcp->file_size = pos + ret;
+
+        return ret;
       }
 
       write_path = 4;
@@ -495,11 +456,9 @@ len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigne
     }
 
     if(wcp->data_size > wcp->file_size)
-      wcp->file_size = wcp->data_size;
+      fsp->size = wcp->file_size = wcp->data_size;
 
     if (cache_flush_needed) {
-      flushed_writes++;
-
       DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
              write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
@@ -515,9 +474,14 @@ n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
    */
 
   if (n > wcp->alloc_size ) {
-    if(real_write_file(fsp, data, pos, n) == -1)
+    ssize_t ret = real_write_file(fsp, data, pos, n);
+    if (ret == -1)
       return -1;
-    direct_writes++;
+
+    if (pos + ret > wcp->file_size)
+      fsp->size = wcp->file_size = pos + n;
+
+    DO_PROFILE_INC(writecache_direct_writes);
     return total_written + n;
   }
 
@@ -526,19 +490,29 @@ n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
    */
 
   if (n) {
+#ifdef WITH_PROFILE
     if (wcp->data_size) {
-      abutted_writes++;
-      DEBUG(9,("abutted write (%u)\n", abutted_writes));
+      DO_PROFILE_INC(writecache_abutted_writes);
     } else {
-      init_writes++;
+      DO_PROFILE_INC(writecache_init_writes);
     }
+#endif
     memcpy(wcp->data+wcp->data_size, data, n);
     if (wcp->data_size == 0) {
       wcp->offset = pos;
-      num_write_caches++;
+      DO_PROFILE_INC(writecache_num_write_caches);
     }
     wcp->data_size += n;
-    DEBUG(9,("cache return %u\n", (unsigned int)n));
+
+    /*
+     * Update the file size if changed.
+     */
+
+    if (wcp->offset + wcp->data_size > wcp->file_size)
+      fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+    DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
+               (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
+
     total_written += n;
     return total_written; /* .... that's a write :) */
   }
@@ -560,14 +534,16 @@ void delete_write_cache(files_struct *fsp)
   if(!(wcp = fsp->wcp))
     return;
 
+  DO_PROFILE_DEC(writecache_allocated_write_caches);
   allocated_write_caches--;
 
   SMB_ASSERT(wcp->data_size == 0);
 
-  free(wcp->data);
-  free(wcp);
+  SAFE_FREE(wcp->data);
+  SAFE_FREE(fsp->wcp);
+
+  DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
 
-  fsp->wcp = NULL;
 }
 
 /****************************************************************************
@@ -579,7 +555,8 @@ static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
   ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
   write_cache *wcp;
 
-  if (allocated_write_caches >= MAX_WRITE_CACHES) return False;
+  if (allocated_write_caches >= MAX_WRITE_CACHES) 
+       return False;
 
   if(alloc_size == 0 || fsp->wcp)
     return False;
@@ -596,13 +573,19 @@ static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
   if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
     DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
           (unsigned int)wcp->alloc_size ));
-    free(wcp);
+    SAFE_FREE(wcp);
     return False;
   }
 
+  memset(wcp->data, '\0', wcp->alloc_size );
+
   fsp->wcp = wcp;
+  DO_PROFILE_INC(writecache_allocated_write_caches);
   allocated_write_caches++;
 
+  DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
+               fsp->fsp_name, wcp->alloc_size ));
+
   return True;
 }
 
@@ -612,8 +595,15 @@ static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
 
 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
 {
+  fsp->size = file_size;
   if(fsp->wcp) {
-    flush_write_cache(fsp, SIZECHANGE_FLUSH);
+    /* The cache *must* have been flushed before we do this. */
+    if (fsp->wcp->data_size != 0) {
+      pstring msg;
+      slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
+on file %s with write cache size = %u\n", fsp->fsp_name, fsp->wcp->data_size );
+      smb_panic(msg);
+    }
     fsp->wcp->file_size = file_size;
   }
 }
@@ -626,6 +616,7 @@ ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
 {
   write_cache *wcp = fsp->wcp;
   size_t data_size;
+  ssize_t ret;
 
   if(!wcp || !wcp->data_size)
     return 0;
@@ -633,17 +624,26 @@ ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
   data_size = wcp->data_size;
   wcp->data_size = 0;
 
-  num_write_caches--;
-
-  flush_reasons[(int)reason]++;
+  DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
 
   DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
           fsp->fd, (double)wcp->offset, (unsigned int)data_size));
 
+#ifdef WITH_PROFILE
   if(data_size == wcp->alloc_size)
-    num_perfect_writes++;
+    DO_PROFILE_INC(writecache_num_perfect_writes);
+#endif
+
+  ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
+
+  /*
+   * Ensure file size if kept up to date if write extends file.
+   */
+
+  if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
+    wcp->file_size = wcp->offset + ret;
 
-  return real_write_file(fsp, wcp->data, wcp->offset, data_size);
+  return ret;
 }
 
 /*******************************************************************
@@ -654,6 +654,6 @@ void sync_file(connection_struct *conn, files_struct *fsp)
 {
     if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
       flush_write_cache(fsp, SYNC_FLUSH);
-      conn->vfs_ops.fsync(fsp->fd);
+      conn->vfs_ops.fsync(fsp,fsp->fd);
     }
 }