adding some initiaial code to sert %a to Win2K3 (using Native LanMan string from...
[Samba.git] / source / smbd / fileio.c
blobb612b1a45149fd65b8b4ae51136d2775e4322866
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 read/write to a files_struct
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Jeremy Allison 2000-2002. - write cache.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
27 /****************************************************************************
28 Seek a file. Try to avoid the seek if possible.
29 ****************************************************************************/
31 static SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
33 SMB_OFF_T seek_ret;
35 seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos,SEEK_SET);
37 if(seek_ret == -1) {
38 DEBUG(0,("seek_file: (%s) sys_lseek failed. Error was %s\n",
39 fsp->fsp_name, strerror(errno) ));
40 fsp->pos = -1;
41 return -1;
44 fsp->pos = seek_ret;
46 DEBUG(10,("seek_file (%s): requested pos = %.0f, new pos = %.0f\n",
47 fsp->fsp_name, (double)pos, (double)fsp->pos ));
49 return(fsp->pos);
52 /****************************************************************************
53 Read from write cache if we can.
54 ****************************************************************************/
57 static BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
59 write_cache *wcp = fsp->wcp;
61 if(!wcp)
62 return False;
64 if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
65 return False;
67 memcpy(data, wcp->data + (pos - wcp->offset), n);
69 DO_PROFILE_INC(writecache_read_hits);
71 return True;
74 /****************************************************************************
75 Read from a file.
76 ****************************************************************************/
78 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
80 ssize_t ret=0,readret;
82 /* you can't read from print files */
83 if (fsp->print_file)
84 return -1;
87 * Serve from write cache if we can.
90 if(read_from_write_cache(fsp, data, pos, n))
91 return n;
93 flush_write_cache(fsp, READ_FLUSH);
95 if (seek_file(fsp,pos) == -1) {
96 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
97 return(ret);
100 if (n > 0) {
101 #ifdef DMF_FIX
102 int numretries = 3;
103 tryagain:
104 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
105 if (readret == -1) {
106 if ((errno == EAGAIN) && numretries) {
107 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
108 (void)sleep(10);
109 --numretries;
110 goto tryagain;
112 return -1;
114 #else /* NO DMF fix. */
115 readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
116 if (readret == -1)
117 return -1;
118 #endif
119 if (readret > 0)
120 ret += readret;
123 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
124 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
126 return(ret);
129 /* how many write cache buffers have been allocated */
130 static unsigned int allocated_write_caches;
132 /****************************************************************************
133 *Really* write to a file.
134 ****************************************************************************/
136 static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
138 ssize_t ret;
140 if ((pos != -1) && (seek_file(fsp,pos) == -1))
141 return -1;
143 ret = vfs_write_data(fsp,data,n);
145 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
146 fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
148 return ret;
151 /****************************************************************************
152 write to a file
153 ****************************************************************************/
155 ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
157 write_cache *wcp = fsp->wcp;
158 ssize_t total_written = 0;
159 int write_path = -1;
161 if (fsp->print_file) {
162 int snum;
163 uint32 jobid;
165 if (!rap_to_pjobid(fsp->rap_print_jobid, &snum, &jobid)) {
166 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
167 (unsigned int)fsp->rap_print_jobid ));
168 errno = EBADF;
169 return -1;
172 return print_job_write(SNUM(fsp->conn), jobid, data, n);
175 if (!fsp->can_write) {
176 errno = EPERM;
177 return(0);
180 if (!fsp->modified) {
181 SMB_STRUCT_STAT st;
182 fsp->modified = True;
184 if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
185 int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
186 fsp->size = (SMB_BIG_UINT)st.st_size;
187 if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode))
188 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
191 * If this is the first write and we have an exclusive oplock then setup
192 * the write cache.
195 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
196 setup_write_cache(fsp, st.st_size);
197 wcp = fsp->wcp;
202 #ifdef WITH_PROFILE
203 DO_PROFILE_INC(writecache_total_writes);
204 if (!fsp->oplock_type) {
205 DO_PROFILE_INC(writecache_non_oplock_writes);
207 #endif
210 * If this file is level II oplocked then we need
211 * to grab the shared memory lock and inform all
212 * other files with a level II lock that they need
213 * to flush their read caches. We keep the lock over
214 * the shared memory area whilst doing this.
217 release_level_2_oplocks_on_change(fsp);
219 #ifdef WITH_PROFILE
220 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
221 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
222 nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
223 profile_p->writecache_init_writes,
224 profile_p->writecache_abutted_writes,
225 profile_p->writecache_total_writes,
226 profile_p->writecache_non_oplock_writes,
227 profile_p->writecache_allocated_write_caches,
228 profile_p->writecache_num_write_caches,
229 profile_p->writecache_direct_writes,
230 profile_p->writecache_num_perfect_writes,
231 profile_p->writecache_read_hits ));
233 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
234 profile_p->writecache_flushed_writes[SEEK_FLUSH],
235 profile_p->writecache_flushed_writes[READ_FLUSH],
236 profile_p->writecache_flushed_writes[WRITE_FLUSH],
237 profile_p->writecache_flushed_writes[READRAW_FLUSH],
238 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
239 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
240 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
242 #endif
244 if(!wcp) {
245 DO_PROFILE_INC(writecache_direct_writes);
246 total_written = real_write_file(fsp, data, pos, n);
247 if ((total_written != -1) && (pos + total_written > (SMB_OFF_T)fsp->size))
248 fsp->size = (SMB_BIG_UINT)(pos + total_written);
249 return total_written;
252 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
253 fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
256 * If we have active cache and it isn't contiguous then we flush.
257 * NOTE: There is a small problem with running out of disk ....
260 if (wcp->data_size) {
262 BOOL cache_flush_needed = False;
264 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
266 /* ASCII art.... JRA.
268 +--------------+-----
269 | Cached data | Rest of allocated cache buffer....
270 +--------------+-----
272 +-------------------+
273 | Data to write |
274 +-------------------+
279 * Start of write overlaps or abutts the existing data.
282 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
284 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
287 * Update the current buffer size with the new data.
290 if(pos + data_used > wcp->offset + wcp->data_size)
291 wcp->data_size = pos + data_used - wcp->offset;
294 * Update the file size if changed.
297 if (wcp->offset + wcp->data_size > wcp->file_size) {
298 wcp->file_size = wcp->offset + wcp->data_size;
299 fsp->size = (SMB_BIG_UINT)wcp->file_size;
303 * If we used all the data then
304 * return here.
307 if(n == data_used)
308 return n;
309 else
310 cache_flush_needed = True;
313 * Move the start of data forward by the amount used,
314 * cut down the amount left by the same amount.
317 data += data_used;
318 pos += data_used;
319 n -= data_used;
321 DO_PROFILE_INC(writecache_abutted_writes);
322 total_written = data_used;
324 write_path = 1;
326 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
327 (pos + n <= wcp->offset + wcp->alloc_size)) {
329 /* ASCII art.... JRA.
331 +---------------+
332 | Cache buffer |
333 +---------------+
335 +-------------------+
336 | Data to write |
337 +-------------------+
342 * End of write overlaps the existing data.
345 size_t data_used = pos + n - wcp->offset;
347 memcpy(wcp->data, data + n - data_used, data_used);
350 * Update the current buffer size with the new data.
353 if(pos + n > wcp->offset + wcp->data_size)
354 wcp->data_size = pos + n - wcp->offset;
357 * Update the file size if changed.
360 if (wcp->offset + wcp->data_size > wcp->file_size) {
361 wcp->file_size = wcp->offset + wcp->data_size;
362 fsp->size = (SMB_BIG_UINT)wcp->file_size;
366 * We don't need to move the start of data, but we
367 * cut down the amount left by the amount used.
370 n -= data_used;
373 * We cannot have used all the data here.
376 cache_flush_needed = True;
378 DO_PROFILE_INC(writecache_abutted_writes);
379 total_written = data_used;
381 write_path = 2;
383 } else if ( (pos >= wcp->file_size) &&
384 (wcp->offset + wcp->data_size == wcp->file_size) &&
385 (pos > wcp->offset + wcp->data_size) &&
386 (pos < wcp->offset + wcp->alloc_size) ) {
388 /* ASCII art.... JRA.
390 End of file ---->|
392 +---------------+---------------+
393 | Cached data | Cache buffer |
394 +---------------+---------------+
396 +-------------------+
397 | Data to write |
398 +-------------------+
403 * Non-contiguous write part of which fits within
404 * the cache buffer and is extending the file
405 * and the cache contents reflect the current
406 * data up to the current end of the file.
409 size_t data_used;
411 if(pos + n <= wcp->offset + wcp->alloc_size)
412 data_used = n;
413 else
414 data_used = wcp->offset + wcp->alloc_size - pos;
417 * Fill in the non-continuous area with zeros.
420 memset(wcp->data + wcp->data_size, '\0',
421 pos - (wcp->offset + wcp->data_size) );
423 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
426 * Update the current buffer size with the new data.
429 if(pos + data_used > wcp->offset + wcp->data_size)
430 wcp->data_size = pos + data_used - wcp->offset;
433 * Update the file size if changed.
436 if (wcp->offset + wcp->data_size > wcp->file_size) {
437 wcp->file_size = wcp->offset + wcp->data_size;
438 fsp->size = (SMB_BIG_UINT)wcp->file_size;
442 * If we used all the data then
443 * return here.
446 if(n == data_used)
447 return n;
448 else
449 cache_flush_needed = True;
452 * Move the start of data forward by the amount used,
453 * cut down the amount left by the same amount.
456 data += data_used;
457 pos += data_used;
458 n -= data_used;
460 DO_PROFILE_INC(writecache_abutted_writes);
461 total_written = data_used;
463 write_path = 3;
465 } else {
467 /* ASCII art..... JRA.
469 Case 1).
471 +---------------+---------------+
472 | Cached data | Cache buffer |
473 +---------------+---------------+
475 +-------------------+
476 | Data to write |
477 +-------------------+
479 Case 2).
481 +---------------+---------------+
482 | Cached data | Cache buffer |
483 +---------------+---------------+
485 +-------------------+
486 | Data to write |
487 +-------------------+
489 Case 3).
491 +---------------+---------------+
492 | Cached data | Cache buffer |
493 +---------------+---------------+
495 +-----------------------------------------------------+
496 | Data to write |
497 +-----------------------------------------------------+
502 * Write is bigger than buffer, or there is no overlap on the
503 * low or high ends.
506 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
507 len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
510 * Update the file size if needed.
513 if(pos + n > wcp->file_size) {
514 wcp->file_size = pos + n;
515 fsp->size = (SMB_BIG_UINT)wcp->file_size;
519 * If write would fit in the cache, and is larger than
520 * the data already in the cache, flush the cache and
521 * preferentially copy the data new data into it. Otherwise
522 * just write the data directly.
525 if ( n <= wcp->alloc_size && n > wcp->data_size) {
526 cache_flush_needed = True;
527 } else {
528 ssize_t ret = real_write_file(fsp, data, pos, n);
531 * If the write overlaps the entire cache, then
532 * discard the current contents of the cache.
533 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
536 if ((pos <= wcp->offset) &&
537 (pos + n >= wcp->offset + wcp->data_size) ) {
538 DEBUG(9,("write_file: discarding overwritten write \
539 cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
540 wcp->data_size = 0;
543 DO_PROFILE_INC(writecache_direct_writes);
544 if (ret == -1)
545 return ret;
547 if (pos + ret > wcp->file_size) {
548 wcp->file_size = pos + ret;
549 fsp->size = (SMB_BIG_UINT)wcp->file_size;
552 return ret;
555 write_path = 4;
559 if(wcp->data_size > wcp->file_size) {
560 wcp->file_size = wcp->data_size;
561 fsp->size = (SMB_BIG_UINT)wcp->file_size;
564 if (cache_flush_needed) {
565 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
566 n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
567 write_path, fsp->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
568 (double)wcp->offset, (unsigned int)wcp->data_size ));
570 flush_write_cache(fsp, WRITE_FLUSH);
575 * If the write request is bigger than the cache
576 * size, write it all out.
579 if (n > wcp->alloc_size ) {
580 ssize_t ret = real_write_file(fsp, data, pos, n);
581 if (ret == -1)
582 return -1;
584 if (pos + ret > wcp->file_size) {
585 wcp->file_size = pos + n;
586 fsp->size = (SMB_BIG_UINT)wcp->file_size;
589 DO_PROFILE_INC(writecache_direct_writes);
590 return total_written + n;
594 * If there's any data left, cache it.
597 if (n) {
598 #ifdef WITH_PROFILE
599 if (wcp->data_size) {
600 DO_PROFILE_INC(writecache_abutted_writes);
601 } else {
602 DO_PROFILE_INC(writecache_init_writes);
604 #endif
605 memcpy(wcp->data+wcp->data_size, data, n);
606 if (wcp->data_size == 0) {
607 wcp->offset = pos;
608 DO_PROFILE_INC(writecache_num_write_caches);
610 wcp->data_size += n;
613 * Update the file size if changed.
616 if (wcp->offset + wcp->data_size > wcp->file_size) {
617 wcp->file_size = wcp->offset + wcp->data_size;
618 fsp->size = (SMB_BIG_UINT)wcp->file_size;
620 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
621 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
623 total_written += n;
624 return total_written; /* .... that's a write :) */
627 return total_written;
630 /****************************************************************************
631 Delete the write cache structure.
632 ****************************************************************************/
634 void delete_write_cache(files_struct *fsp)
636 write_cache *wcp;
638 if(!fsp)
639 return;
641 if(!(wcp = fsp->wcp))
642 return;
644 DO_PROFILE_DEC(writecache_allocated_write_caches);
645 allocated_write_caches--;
647 SMB_ASSERT(wcp->data_size == 0);
649 SAFE_FREE(wcp->data);
650 SAFE_FREE(fsp->wcp);
652 DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
655 /****************************************************************************
656 Setup the write cache structure.
657 ****************************************************************************/
659 static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
661 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
662 write_cache *wcp;
664 if (allocated_write_caches >= MAX_WRITE_CACHES)
665 return False;
667 if(alloc_size == 0 || fsp->wcp)
668 return False;
670 if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
671 DEBUG(0,("setup_write_cache: malloc fail.\n"));
672 return False;
675 wcp->file_size = file_size;
676 wcp->offset = 0;
677 wcp->alloc_size = alloc_size;
678 wcp->data_size = 0;
679 if((wcp->data = malloc(wcp->alloc_size)) == NULL) {
680 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
681 (unsigned int)wcp->alloc_size ));
682 SAFE_FREE(wcp);
683 return False;
686 memset(wcp->data, '\0', wcp->alloc_size );
688 fsp->wcp = wcp;
689 DO_PROFILE_INC(writecache_allocated_write_caches);
690 allocated_write_caches++;
692 DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
693 fsp->fsp_name, wcp->alloc_size ));
695 return True;
698 /****************************************************************************
699 Cope with a size change.
700 ****************************************************************************/
702 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
704 fsp->size = (SMB_BIG_UINT)file_size;
705 if(fsp->wcp) {
706 /* The cache *must* have been flushed before we do this. */
707 if (fsp->wcp->data_size != 0) {
708 pstring msg;
709 slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
710 on file %s with write cache size = %u\n", fsp->fsp_name, fsp->wcp->data_size );
711 smb_panic(msg);
713 fsp->wcp->file_size = file_size;
717 /*******************************************************************
718 Flush a write cache struct to disk.
719 ********************************************************************/
721 ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
723 write_cache *wcp = fsp->wcp;
724 size_t data_size;
725 ssize_t ret;
727 if(!wcp || !wcp->data_size)
728 return 0;
730 data_size = wcp->data_size;
731 wcp->data_size = 0;
733 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
735 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
736 fsp->fd, (double)wcp->offset, (unsigned int)data_size));
738 #ifdef WITH_PROFILE
739 if(data_size == wcp->alloc_size)
740 DO_PROFILE_INC(writecache_num_perfect_writes);
741 #endif
743 ret = real_write_file(fsp, wcp->data, wcp->offset, data_size);
746 * Ensure file size if kept up to date if write extends file.
749 if ((ret != -1) && (wcp->offset + ret > wcp->file_size))
750 wcp->file_size = wcp->offset + ret;
752 return ret;
755 /*******************************************************************
756 sync a file
757 ********************************************************************/
759 void sync_file(connection_struct *conn, files_struct *fsp)
761 if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
762 flush_write_cache(fsp, SYNC_FLUSH);
763 conn->vfs_ops.fsync(fsp,fsp->fd);
768 /************************************************************
769 Perform a stat whether a valid fd or not.
770 ************************************************************/
772 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
774 if (fsp->fd == -1)
775 return vfs_stat(fsp->conn, fsp->fsp_name, pst);
776 else
777 return vfs_fstat(fsp,fsp->fd, pst);