source: trunk/server/source3/smbd/fileio.c@ 421

Last change on this file since 421 was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 25.1 KB
Line 
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.
7
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 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "includes.h"
23#include "smbd/globals.h"
24
25static bool setup_write_cache(files_struct *, SMB_OFF_T);
26
27/****************************************************************************
28 Read from write cache if we can.
29****************************************************************************/
30
31static bool read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
32{
33 write_cache *wcp = fsp->wcp;
34
35 if(!wcp) {
36 return False;
37 }
38
39 if( n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size) {
40 return False;
41 }
42
43 memcpy(data, wcp->data + (pos - wcp->offset), n);
44
45 DO_PROFILE_INC(writecache_read_hits);
46
47 return True;
48}
49
50/****************************************************************************
51 Read from a file.
52****************************************************************************/
53
54ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
55{
56 ssize_t ret=0,readret;
57
58 /* you can't read from print files */
59 if (fsp->print_file) {
60 errno = EBADF;
61 return -1;
62 }
63
64 /*
65 * Serve from write cache if we can.
66 */
67
68 if(read_from_write_cache(fsp, data, pos, n)) {
69 fsp->fh->pos = pos + n;
70 fsp->fh->position_information = fsp->fh->pos;
71 return n;
72 }
73
74 flush_write_cache(fsp, READ_FLUSH);
75
76 fsp->fh->pos = pos;
77
78 if (n > 0) {
79#ifdef DMF_FIX
80 int numretries = 3;
81tryagain:
82 readret = SMB_VFS_PREAD(fsp,data,n,pos);
83
84 if (readret == -1) {
85 if ((errno == EAGAIN) && numretries) {
86 DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
87 (void)sleep(10);
88 --numretries;
89 goto tryagain;
90 }
91 return -1;
92 }
93#else /* NO DMF fix. */
94 readret = SMB_VFS_PREAD(fsp,data,n,pos);
95
96 if (readret == -1) {
97 return -1;
98 }
99#endif
100 if (readret > 0) {
101 ret += readret;
102 }
103 }
104
105 DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
106 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
107
108 fsp->fh->pos += ret;
109 fsp->fh->position_information = fsp->fh->pos;
110
111 return(ret);
112}
113
114/****************************************************************************
115 *Really* write to a file.
116****************************************************************************/
117
118static ssize_t real_write_file(struct smb_request *req,
119 files_struct *fsp,
120 const char *data,
121 SMB_OFF_T pos,
122 size_t n)
123{
124 ssize_t ret;
125
126 if (pos == -1) {
127 ret = vfs_write_data(req, fsp, data, n);
128 } else {
129 fsp->fh->pos = pos;
130 if (pos && lp_strict_allocate(SNUM(fsp->conn))) {
131 if (vfs_fill_sparse(fsp, pos) == -1) {
132 return -1;
133 }
134 }
135 ret = vfs_pwrite_data(req, fsp, data, n, pos);
136 }
137
138 DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
139 fsp_str_dbg(fsp), (double)pos, (unsigned long)n, (long)ret));
140
141 if (ret != -1) {
142 fsp->fh->pos += ret;
143
144/* Yes - this is correct - writes don't update this. JRA. */
145/* Found by Samba4 tests. */
146#if 0
147 fsp->position_information = fsp->pos;
148#endif
149 }
150
151 return ret;
152}
153
154/****************************************************************************
155 File size cache change.
156 Updates size on disk but doesn't flush the cache.
157****************************************************************************/
158
159static int wcp_file_size_change(files_struct *fsp)
160{
161 int ret;
162 write_cache *wcp = fsp->wcp;
163
164 wcp->file_size = wcp->offset + wcp->data_size;
165 ret = SMB_VFS_FTRUNCATE(fsp, wcp->file_size);
166 if (ret == -1) {
167 DEBUG(0,("wcp_file_size_change (%s): ftruncate of size %.0f "
168 "error %s\n", fsp_str_dbg(fsp),
169 (double)wcp->file_size, strerror(errno)));
170 }
171 return ret;
172}
173
174void update_write_time_handler(struct event_context *ctx,
175 struct timed_event *te,
176 struct timeval now,
177 void *private_data)
178{
179 files_struct *fsp = (files_struct *)private_data;
180
181 DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp)));
182
183 /* change the write time in the open file db. */
184 (void)set_write_time(fsp->file_id, timespec_current());
185
186 /* And notify. */
187 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
188 FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name);
189
190 /* Remove the timed event handler. */
191 TALLOC_FREE(fsp->update_write_time_event);
192}
193
194/*********************************************************
195 Schedule a write time update for WRITE_TIME_UPDATE_USEC_DELAY
196 in the future.
197*********************************************************/
198
199void trigger_write_time_update(struct files_struct *fsp)
200{
201 int delay;
202
203 if (fsp->posix_open) {
204 /* Don't use delayed writes on POSIX files. */
205 return;
206 }
207
208 if (fsp->write_time_forced) {
209 /* No point - "sticky" write times
210 * in effect.
211 */
212 return;
213 }
214
215 /* We need to remember someone did a write
216 * and update to current time on close. */
217
218 fsp->update_write_time_on_close = true;
219
220 if (fsp->update_write_time_triggered) {
221 /*
222 * We only update the write time after 2 seconds
223 * on the first normal write. After that
224 * no other writes affect this until close.
225 */
226 return;
227 }
228 fsp->update_write_time_triggered = true;
229
230 delay = lp_parm_int(SNUM(fsp->conn),
231 "smbd", "writetimeupdatedelay",
232 WRITE_TIME_UPDATE_USEC_DELAY);
233
234 DEBUG(5, ("Update write time %d usec later on %s\n",
235 delay, fsp_str_dbg(fsp)));
236
237 /* trigger the update 2 seconds later */
238 fsp->update_write_time_event =
239 event_add_timed(smbd_event_context(), NULL,
240 timeval_current_ofs(0, delay),
241 update_write_time_handler, fsp);
242}
243
244void trigger_write_time_update_immediate(struct files_struct *fsp)
245{
246 struct smb_file_time ft;
247
248 if (fsp->posix_open) {
249 /* Don't use delayed writes on POSIX files. */
250 return;
251 }
252
253 if (fsp->write_time_forced) {
254 /*
255 * No point - "sticky" write times
256 * in effect.
257 */
258 return;
259 }
260
261 TALLOC_FREE(fsp->update_write_time_event);
262 DEBUG(5, ("Update write time immediate on %s\n",
263 fsp_str_dbg(fsp)));
264
265 /* After an immediate update, reset the trigger. */
266 fsp->update_write_time_triggered = true;
267 fsp->update_write_time_on_close = false;
268
269 ZERO_STRUCT(ft);
270 ft.mtime = timespec_current();
271
272 /* Update the time in the open file db. */
273 (void)set_write_time(fsp->file_id, ft.mtime);
274
275 /* Now set on disk - takes care of notify. */
276 (void)smb_set_file_time(fsp->conn, fsp, fsp->fsp_name, &ft, false);
277}
278
279/****************************************************************************
280 Write to a file.
281****************************************************************************/
282
283ssize_t write_file(struct smb_request *req,
284 files_struct *fsp,
285 const char *data,
286 SMB_OFF_T pos,
287 size_t n)
288{
289 write_cache *wcp = fsp->wcp;
290 ssize_t total_written = 0;
291 int write_path = -1;
292
293 if (fsp->print_file) {
294 uint32 jobid;
295
296 if (!rap_to_pjobid(fsp->rap_print_jobid, NULL, &jobid)) {
297 DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
298 (unsigned int)fsp->rap_print_jobid ));
299 errno = EBADF;
300 return -1;
301 }
302
303 return print_job_write(SNUM(fsp->conn), jobid, data, pos, n);
304 }
305
306 if (!fsp->can_write) {
307 errno = EPERM;
308 return -1;
309 }
310
311 if (!fsp->modified) {
312 fsp->modified = True;
313
314 if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) == 0) {
315 int dosmode;
316 trigger_write_time_update(fsp);
317 dosmode = dos_mode(fsp->conn, fsp->fsp_name);
318 if ((lp_store_dos_attributes(SNUM(fsp->conn)) ||
319 MAP_ARCHIVE(fsp->conn)) &&
320 !IS_DOS_ARCHIVE(dosmode)) {
321 file_set_dosmode(fsp->conn, fsp->fsp_name,
322 dosmode | aARCH, NULL, false);
323 }
324
325 /*
326 * If this is the first write and we have an exclusive oplock then setup
327 * the write cache.
328 */
329
330 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
331 setup_write_cache(fsp,
332 fsp->fsp_name->st.st_ex_size);
333 wcp = fsp->wcp;
334 }
335 }
336 }
337
338#ifdef WITH_PROFILE
339 DO_PROFILE_INC(writecache_total_writes);
340 if (!fsp->oplock_type) {
341 DO_PROFILE_INC(writecache_non_oplock_writes);
342 }
343#endif
344
345 /*
346 * If this file is level II oplocked then we need
347 * to grab the shared memory lock and inform all
348 * other files with a level II lock that they need
349 * to flush their read caches. We keep the lock over
350 * the shared memory area whilst doing this.
351 */
352
353 /* This should actually be improved to span the write. */
354 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WRITE);
355 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WRITE);
356
357#ifdef WITH_PROFILE
358 if (profile_p && profile_p->writecache_total_writes % 500 == 0) {
359 DEBUG(3,("WRITECACHE: initwrites=%u abutted=%u total=%u \
360nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
361 profile_p->writecache_init_writes,
362 profile_p->writecache_abutted_writes,
363 profile_p->writecache_total_writes,
364 profile_p->writecache_non_oplock_writes,
365 profile_p->writecache_allocated_write_caches,
366 profile_p->writecache_num_write_caches,
367 profile_p->writecache_direct_writes,
368 profile_p->writecache_num_perfect_writes,
369 profile_p->writecache_read_hits ));
370
371 DEBUG(3,("WRITECACHE: Flushes SEEK=%d, READ=%d, WRITE=%d, READRAW=%d, OPLOCK=%d, CLOSE=%d, SYNC=%d\n",
372 profile_p->writecache_flushed_writes[SEEK_FLUSH],
373 profile_p->writecache_flushed_writes[READ_FLUSH],
374 profile_p->writecache_flushed_writes[WRITE_FLUSH],
375 profile_p->writecache_flushed_writes[READRAW_FLUSH],
376 profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH],
377 profile_p->writecache_flushed_writes[CLOSE_FLUSH],
378 profile_p->writecache_flushed_writes[SYNC_FLUSH] ));
379 }
380#endif
381
382 if (wcp && req->unread_bytes) {
383 /* If we're using receivefile don't
384 * deal with a write cache.
385 */
386 flush_write_cache(fsp, WRITE_FLUSH);
387 delete_write_cache(fsp);
388 wcp = NULL;
389 }
390
391 if(!wcp) {
392 DO_PROFILE_INC(writecache_direct_writes);
393 total_written = real_write_file(req, fsp, data, pos, n);
394 return total_written;
395 }
396
397 DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f "
398 "wcp->data_size=%u\n", fsp_str_dbg(fsp), fsp->fh->fd,
399 (double)pos, (unsigned int)n, (double)wcp->offset,
400 (unsigned int)wcp->data_size));
401
402 fsp->fh->pos = pos + n;
403
404 /*
405 * If we have active cache and it isn't contiguous then we flush.
406 * NOTE: There is a small problem with running out of disk ....
407 */
408
409 if (wcp->data_size) {
410 bool cache_flush_needed = False;
411
412 if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
413
414 /* ASCII art.... JRA.
415
416 +--------------+-----
417 | Cached data | Rest of allocated cache buffer....
418 +--------------+-----
419
420 +-------------------+
421 | Data to write |
422 +-------------------+
423
424 */
425
426 /*
427 * Start of write overlaps or abutts the existing data.
428 */
429
430 size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
431
432 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
433
434 /*
435 * Update the current buffer size with the new data.
436 */
437
438 if(pos + data_used > wcp->offset + wcp->data_size) {
439 wcp->data_size = pos + data_used - wcp->offset;
440 }
441
442 /*
443 * Update the file size if changed.
444 */
445
446 if (wcp->offset + wcp->data_size > wcp->file_size) {
447 if (wcp_file_size_change(fsp) == -1) {
448 return -1;
449 }
450 }
451
452 /*
453 * If we used all the data then
454 * return here.
455 */
456
457 if(n == data_used) {
458 return n;
459 } else {
460 cache_flush_needed = True;
461 }
462 /*
463 * Move the start of data forward by the amount used,
464 * cut down the amount left by the same amount.
465 */
466
467 data += data_used;
468 pos += data_used;
469 n -= data_used;
470
471 DO_PROFILE_INC(writecache_abutted_writes);
472 total_written = data_used;
473
474 write_path = 1;
475
476 } else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
477 (pos + n <= wcp->offset + wcp->alloc_size)) {
478
479 /* ASCII art.... JRA.
480
481 +---------------+
482 | Cache buffer |
483 +---------------+
484
485 +-------------------+
486 | Data to write |
487 +-------------------+
488
489 */
490
491 /*
492 * End of write overlaps the existing data.
493 */
494
495 size_t data_used = pos + n - wcp->offset;
496
497 memcpy(wcp->data, data + n - data_used, data_used);
498
499 /*
500 * Update the current buffer size with the new data.
501 */
502
503 if(pos + n > wcp->offset + wcp->data_size) {
504 wcp->data_size = pos + n - wcp->offset;
505 }
506
507 /*
508 * Update the file size if changed.
509 */
510
511 if (wcp->offset + wcp->data_size > wcp->file_size) {
512 if (wcp_file_size_change(fsp) == -1) {
513 return -1;
514 }
515 }
516
517 /*
518 * We don't need to move the start of data, but we
519 * cut down the amount left by the amount used.
520 */
521
522 n -= data_used;
523
524 /*
525 * We cannot have used all the data here.
526 */
527
528 cache_flush_needed = True;
529
530 DO_PROFILE_INC(writecache_abutted_writes);
531 total_written = data_used;
532
533 write_path = 2;
534
535 } else if ( (pos >= wcp->file_size) &&
536 (wcp->offset + wcp->data_size == wcp->file_size) &&
537 (pos > wcp->offset + wcp->data_size) &&
538 (pos < wcp->offset + wcp->alloc_size) ) {
539
540 /* ASCII art.... JRA.
541
542 End of file ---->|
543
544 +---------------+---------------+
545 | Cached data | Cache buffer |
546 +---------------+---------------+
547
548 +-------------------+
549 | Data to write |
550 +-------------------+
551
552 */
553
554 /*
555 * Non-contiguous write part of which fits within
556 * the cache buffer and is extending the file
557 * and the cache contents reflect the current
558 * data up to the current end of the file.
559 */
560
561 size_t data_used;
562
563 if(pos + n <= wcp->offset + wcp->alloc_size) {
564 data_used = n;
565 } else {
566 data_used = wcp->offset + wcp->alloc_size - pos;
567 }
568
569 /*
570 * Fill in the non-continuous area with zeros.
571 */
572
573 memset(wcp->data + wcp->data_size, '\0',
574 pos - (wcp->offset + wcp->data_size) );
575
576 memcpy(wcp->data + (pos - wcp->offset), data, data_used);
577
578 /*
579 * Update the current buffer size with the new data.
580 */
581
582 if(pos + data_used > wcp->offset + wcp->data_size) {
583 wcp->data_size = pos + data_used - wcp->offset;
584 }
585
586 /*
587 * Update the file size if changed.
588 */
589
590 if (wcp->offset + wcp->data_size > wcp->file_size) {
591 if (wcp_file_size_change(fsp) == -1) {
592 return -1;
593 }
594 }
595
596 /*
597 * If we used all the data then
598 * return here.
599 */
600
601 if(n == data_used) {
602 return n;
603 } else {
604 cache_flush_needed = True;
605 }
606
607 /*
608 * Move the start of data forward by the amount used,
609 * cut down the amount left by the same amount.
610 */
611
612 data += data_used;
613 pos += data_used;
614 n -= data_used;
615
616 DO_PROFILE_INC(writecache_abutted_writes);
617 total_written = data_used;
618
619 write_path = 3;
620
621 } else if ( (pos >= wcp->file_size) &&
622 (n == 1) &&
623 (wcp->file_size == wcp->offset + wcp->data_size) &&
624 (pos < wcp->file_size + wcp->alloc_size)) {
625
626 /*
627
628 End of file ---->|
629
630 +---------------+---------------+
631 | Cached data | Cache buffer |
632 +---------------+---------------+
633
634 |<------- allocated size ---------------->|
635
636 +--------+
637 | 1 Byte |
638 +--------+
639
640 MS-Office seems to do this a lot to determine if there's enough
641 space on the filesystem to write a new file.
642
643 Change to :
644
645 End of file ---->|
646 +-----------------------+--------+
647 | Zeroed Cached data | 1 Byte |
648 +-----------------------+--------+
649 */
650
651 flush_write_cache(fsp, WRITE_FLUSH);
652 wcp->offset = wcp->file_size;
653 wcp->data_size = pos - wcp->file_size + 1;
654 memset(wcp->data, '\0', wcp->data_size);
655 memcpy(wcp->data + wcp->data_size-1, data, 1);
656
657 /*
658 * Update the file size if changed.
659 */
660
661 if (wcp->offset + wcp->data_size > wcp->file_size) {
662 if (wcp_file_size_change(fsp) == -1) {
663 return -1;
664 }
665 }
666
667 return n;
668
669 } else {
670
671 /* ASCII art..... JRA.
672
673 Case 1).
674
675 +---------------+---------------+
676 | Cached data | Cache buffer |
677 +---------------+---------------+
678
679 +-------------------+
680 | Data to write |
681 +-------------------+
682
683 Case 2).
684
685 +---------------+---------------+
686 | Cached data | Cache buffer |
687 +---------------+---------------+
688
689 +-------------------+
690 | Data to write |
691 +-------------------+
692
693 Case 3).
694
695 +---------------+---------------+
696 | Cached data | Cache buffer |
697 +---------------+---------------+
698
699 +-----------------------------------------------------+
700 | Data to write |
701 +-----------------------------------------------------+
702
703 */
704
705 /*
706 * Write is bigger than buffer, or there is no overlap on the
707 * low or high ends.
708 */
709
710 DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
711len = %u\n",fsp->fh->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
712
713 /*
714 * If write would fit in the cache, and is larger than
715 * the data already in the cache, flush the cache and
716 * preferentially copy the data new data into it. Otherwise
717 * just write the data directly.
718 */
719
720 if ( n <= wcp->alloc_size && n > wcp->data_size) {
721 cache_flush_needed = True;
722 } else {
723 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
724
725 /*
726 * If the write overlaps the entire cache, then
727 * discard the current contents of the cache.
728 * Fix from Rasmus Borup Hansen rbh@math.ku.dk.
729 */
730
731 if ((pos <= wcp->offset) &&
732 (pos + n >= wcp->offset + wcp->data_size) ) {
733 DEBUG(9,("write_file: discarding overwritten write \
734cache: fd = %d, off=%.0f, size=%u\n", fsp->fh->fd, (double)wcp->offset, (unsigned int)wcp->data_size ));
735 wcp->data_size = 0;
736 }
737
738 DO_PROFILE_INC(writecache_direct_writes);
739 if (ret == -1) {
740 return ret;
741 }
742
743 if (pos + ret > wcp->file_size) {
744 wcp->file_size = pos + ret;
745 }
746
747 return ret;
748 }
749
750 write_path = 4;
751
752 }
753
754 if (cache_flush_needed) {
755 DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
756n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
757 write_path, fsp->fh->fd, (double)wcp->file_size, (double)pos, (unsigned int)n,
758 (double)wcp->offset, (unsigned int)wcp->data_size ));
759
760 flush_write_cache(fsp, WRITE_FLUSH);
761 }
762 }
763
764 /*
765 * If the write request is bigger than the cache
766 * size, write it all out.
767 */
768
769 if (n > wcp->alloc_size ) {
770 ssize_t ret = real_write_file(NULL,fsp, data, pos, n);
771 if (ret == -1) {
772 return -1;
773 }
774
775 if (pos + ret > wcp->file_size) {
776 wcp->file_size = pos + n;
777 }
778
779 DO_PROFILE_INC(writecache_direct_writes);
780 return total_written + n;
781 }
782
783 /*
784 * If there's any data left, cache it.
785 */
786
787 if (n) {
788#ifdef WITH_PROFILE
789 if (wcp->data_size) {
790 DO_PROFILE_INC(writecache_abutted_writes);
791 } else {
792 DO_PROFILE_INC(writecache_init_writes);
793 }
794#endif
795 memcpy(wcp->data+wcp->data_size, data, n);
796 if (wcp->data_size == 0) {
797 wcp->offset = pos;
798 DO_PROFILE_INC(writecache_num_write_caches);
799 }
800 wcp->data_size += n;
801
802 /*
803 * Update the file size if changed.
804 */
805
806 if (wcp->offset + wcp->data_size > wcp->file_size) {
807 if (wcp_file_size_change(fsp) == -1) {
808 return -1;
809 }
810 }
811 DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
812 (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
813
814 total_written += n;
815 return total_written; /* .... that's a write :) */
816 }
817
818 return total_written;
819}
820
821/****************************************************************************
822 Delete the write cache structure.
823****************************************************************************/
824
825void delete_write_cache(files_struct *fsp)
826{
827 write_cache *wcp;
828
829 if(!fsp) {
830 return;
831 }
832
833 if(!(wcp = fsp->wcp)) {
834 return;
835 }
836
837 DO_PROFILE_DEC(writecache_allocated_write_caches);
838 allocated_write_caches--;
839
840 SMB_ASSERT(wcp->data_size == 0);
841
842 SAFE_FREE(wcp->data);
843 SAFE_FREE(fsp->wcp);
844
845 DEBUG(10,("delete_write_cache: File %s deleted write cache\n",
846 fsp_str_dbg(fsp)));
847}
848
849/****************************************************************************
850 Setup the write cache structure.
851****************************************************************************/
852
853static bool setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
854{
855 ssize_t alloc_size = lp_write_cache_size(SNUM(fsp->conn));
856 write_cache *wcp;
857
858 if (allocated_write_caches >= MAX_WRITE_CACHES) {
859 return False;
860 }
861
862 if(alloc_size == 0 || fsp->wcp) {
863 return False;
864 }
865
866 if((wcp = SMB_MALLOC_P(write_cache)) == NULL) {
867 DEBUG(0,("setup_write_cache: malloc fail.\n"));
868 return False;
869 }
870
871 wcp->file_size = file_size;
872 wcp->offset = 0;
873 wcp->alloc_size = alloc_size;
874 wcp->data_size = 0;
875 if((wcp->data = (char *)SMB_MALLOC(wcp->alloc_size)) == NULL) {
876 DEBUG(0,("setup_write_cache: malloc fail for buffer size %u.\n",
877 (unsigned int)wcp->alloc_size ));
878 SAFE_FREE(wcp);
879 return False;
880 }
881
882 memset(wcp->data, '\0', wcp->alloc_size );
883
884 fsp->wcp = wcp;
885 DO_PROFILE_INC(writecache_allocated_write_caches);
886 allocated_write_caches++;
887
888 DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
889 fsp_str_dbg(fsp), (unsigned long)wcp->alloc_size));
890
891 return True;
892}
893
894/****************************************************************************
895 Cope with a size change.
896****************************************************************************/
897
898void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
899{
900 if(fsp->wcp) {
901 /* The cache *must* have been flushed before we do this. */
902 if (fsp->wcp->data_size != 0) {
903 char *msg;
904 if (asprintf(&msg, "set_filelen_write_cache: size change "
905 "on file %s with write cache size = %lu\n",
906 fsp->fsp_name->base_name,
907 (unsigned long)fsp->wcp->data_size) != -1) {
908 smb_panic(msg);
909 } else {
910 smb_panic("set_filelen_write_cache");
911 }
912 }
913 fsp->wcp->file_size = file_size;
914 }
915}
916
917/*******************************************************************
918 Flush a write cache struct to disk.
919********************************************************************/
920
921ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
922{
923 write_cache *wcp = fsp->wcp;
924 size_t data_size;
925 ssize_t ret;
926
927 if(!wcp || !wcp->data_size) {
928 return 0;
929 }
930
931 data_size = wcp->data_size;
932 wcp->data_size = 0;
933
934 DO_PROFILE_DEC_INC(writecache_num_write_caches,writecache_flushed_writes[reason]);
935
936 DEBUG(9,("flushing write cache: fd = %d, off=%.0f, size=%u\n",
937 fsp->fh->fd, (double)wcp->offset, (unsigned int)data_size));
938
939#ifdef WITH_PROFILE
940 if(data_size == wcp->alloc_size) {
941 DO_PROFILE_INC(writecache_num_perfect_writes);
942 }
943#endif
944
945 ret = real_write_file(NULL, fsp, wcp->data, wcp->offset, data_size);
946
947 /*
948 * Ensure file size if kept up to date if write extends file.
949 */
950
951 if ((ret != -1) && (wcp->offset + ret > wcp->file_size)) {
952 wcp->file_size = wcp->offset + ret;
953 }
954
955 return ret;
956}
957
958/*******************************************************************
959sync a file
960********************************************************************/
961
962NTSTATUS sync_file(connection_struct *conn, files_struct *fsp, bool write_through)
963{
964 if (fsp->fh->fd == -1)
965 return NT_STATUS_INVALID_HANDLE;
966
967 if (lp_strict_sync(SNUM(conn)) &&
968 (lp_syncalways(SNUM(conn)) || write_through)) {
969 int ret = flush_write_cache(fsp, SYNC_FLUSH);
970 if (ret == -1) {
971 return map_nt_error_from_unix(errno);
972 }
973 ret = SMB_VFS_FSYNC(fsp);
974 if (ret == -1) {
975 return map_nt_error_from_unix(errno);
976 }
977 }
978 return NT_STATUS_OK;
979}
980
981/************************************************************
982 Perform a stat whether a valid fd or not.
983************************************************************/
984
985int fsp_stat(files_struct *fsp)
986{
987 if (fsp->fh->fd == -1) {
988 if (fsp->posix_open) {
989 return SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
990 } else {
991 return SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
992 }
993 } else {
994 return SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
995 }
996}
Note: See TracBrowser for help on using the repository browser.