1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
#define OLD_NTDOMAIN 1
/*
Unix SMB/Netbios implementation.
Version 1.9.
read/write to a files_struct
Copyright (C) Andrew Tridgell 1992-1998
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
extern int DEBUGLEVEL;
static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
/****************************************************************************
seek a file. Try to avoid the seek if possible
****************************************************************************/
SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
{
SMB_OFF_T offset = 0;
SMB_OFF_T seek_ret;
if (fsp->print_file && lp_postscript(fsp->conn->service))
offset = 3;
seek_ret = fsp->conn->vfs_ops.lseek(fsp->fd,pos+offset,SEEK_SET);
/*
* We want to maintain the fiction that we can seek
* on a fifo for file system purposes. This allows
* people to set up UNIX fifo's that feed data to Windows
* applications. JRA.
*/
if((seek_ret == -1) && (errno == ESPIPE)) {
seek_ret = pos+offset;
errno = 0;
}
if((seek_ret == -1) || (seek_ret != pos+offset)) {
DEBUG(0,("seek_file: sys_lseek failed. Error was %s\n", strerror(errno) ));
fsp->pos = -1;
return -1;
}
fsp->pos = seek_ret - offset;
DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
(double)(pos+offset), (double)fsp->pos ));
return(fsp->pos);
}
/****************************************************************************
Read from write cache if we can.
****************************************************************************/
BOOL read_from_write_cache(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
{
write_cache *wcp = fsp->wcp;
if(!wcp)
return False;
if(n > wcp->data_size || pos < wcp->offset || pos + n > wcp->offset + wcp->data_size)
return False;
memcpy(data, wcp->data + (pos - wcp->offset), n);
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_read_hits);
#endif
return True;
}
/****************************************************************************
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;
/* 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;
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;
}
return(ret);
}
/* how many write cache buffers have been allocated */
static unsigned int allocated_write_caches;
/****************************************************************************
*Really* write to a file.
****************************************************************************/
static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_t n)
{
if ((pos != -1) && (seek_file(fsp,pos) == -1))
return -1;
return vfs_write_data(fsp,data,n);
}
/****************************************************************************
write to a file
****************************************************************************/
ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
{
write_cache *wcp = fsp->wcp;
ssize_t total_written = 0;
int write_path = -1;
if (fsp->print_file) {
return print_job_write(fsp->print_jobid, data, n);
}
if (!fsp->can_write) {
errno = EPERM;
return(0);
}
if (!fsp->modified) {
SMB_STRUCT_STAT st;
fsp->modified = True;
if (fsp->conn->vfs_ops.fstat(fsp->fd,&st) == 0) {
int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) {
file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
}
/*
* If this is the first write and we have an exclusive oplock then setup
* the write cache.
*/
if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && !wcp) {
setup_write_cache(fsp, st.st_size);
wcp = fsp->wcp;
}
}
}
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_total_writes);
if (!fsp->oplock_type) {
INC_PROFILE_COUNT(writecache_non_oplock_writes);
}
#endif
/*
* If this file is level II oplocked then we need
* to grab the shared memory lock and inform all
* other files with a level II lock that they need
* to flush their read caches. We keep the lock over
* 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();
}
#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",
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) {
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_direct_writes);
#endif
return real_write_file(fsp, data, pos, n);
}
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));
/*
* If we have active cache and it isn't contiguous then we flush.
* NOTE: There is a small problem with running out of disk ....
*/
if (wcp->data_size) {
BOOL cache_flush_needed = False;
if ((pos >= wcp->offset) && (pos <= wcp->offset + wcp->data_size)) {
/*
* Start of write overlaps or abutts the existing data.
*/
size_t data_used = MIN((wcp->alloc_size - (pos - wcp->offset)), n);
memcpy(wcp->data + (pos - wcp->offset), data, data_used);
/*
* Update the current buffer size with the new data.
*/
if(pos + data_used > wcp->offset + wcp->data_size)
wcp->data_size = pos + data_used - wcp->offset;
/*
* If we used all the data then
* return here.
*/
if(n == data_used)
return n;
else
cache_flush_needed = True;
/*
* Move the start of data forward by the amount used,
* cut down the amount left by the same amount.
*/
data += data_used;
pos += data_used;
n -= data_used;
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_abutted_writes);
#endif
total_written = data_used;
write_path = 1;
} else if ((pos < wcp->offset) && (pos + n > wcp->offset) &&
(pos + n <= wcp->offset + wcp->alloc_size)) {
/*
* End of write overlaps the existing data.
*/
size_t data_used = pos + n - wcp->offset;
memcpy(wcp->data, data + n - data_used, data_used);
/*
* Update the current buffer size with the new data.
*/
if(pos + n > wcp->offset + wcp->data_size)
wcp->data_size = pos + n - wcp->offset;
/*
* We don't need to move the start of data, but we
* cut down the amount left by the amount used.
*/
n -= data_used;
/*
* We cannot have used all the data here.
*/
cache_flush_needed = True;
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_abutted_writes);
#endif
total_written = data_used;
write_path = 2;
} else if ( (pos >= 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.
*/
size_t data_used;
if(pos + n <= wcp->offset + wcp->alloc_size)
data_used = n;
else
data_used = wcp->offset + wcp->alloc_size - pos;
/*
* Fill in the non-continuous area with zeros.
*/
memset(wcp->data + wcp->data_size, '\0',
pos - (wcp->offset + wcp->data_size) );
memcpy(wcp->data + (pos - wcp->offset), data, data_used);
/*
* Update the current buffer size with the new data.
*/
if(pos + data_used > wcp->offset + wcp->data_size)
wcp->data_size = pos + data_used - wcp->offset;
/*
* Update the known file length.
*/
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 we used all the data then
* return here.
*/
if(n == data_used)
return n;
else
cache_flush_needed = True;
/*
* Move the start of data forward by the amount used,
* cut down the amount left by the same amount.
*/
data += data_used;
pos += data_used;
n -= data_used;
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_abutted_writes);
#endif
total_written = data_used;
write_path = 3;
} else {
/*
* Write is bigger than buffer, or there is no overlap on the
* low or high ends.
*/
DEBUG(9,("write_file: non cacheable write : fd = %d, pos = %.0f, len = %u, current cache pos = %.0f \
len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size ));
/*
* Update the file size if needed.
*/
if(pos + n > wcp->file_size)
wcp->file_size = pos + n;
/*
* If write would fit in the cache, and is larger than
* the data already in the cache, flush the cache and
* preferentially copy the data new data into it. Otherwise
* just write the data directly.
*/
if ( n <= wcp->alloc_size && n > wcp->data_size) {
cache_flush_needed = True;
} else {
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_direct_writes);
#endif
return real_write_file(fsp, data, pos, n);
}
write_path = 4;
}
if(wcp->data_size > wcp->file_size)
wcp->file_size = wcp->data_size;
if (cache_flush_needed) {
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,
(double)wcp->offset, (unsigned int)wcp->data_size ));
flush_write_cache(fsp, WRITE_FLUSH);
}
}
/*
* If the write request is bigger than the cache
* size, write it all out.
*/
if (n > wcp->alloc_size ) {
if(real_write_file(fsp, data, pos, n) == -1)
return -1;
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_direct_writes);
#endif
return total_written + n;
}
/*
* If there's any data left, cache it.
*/
if (n) {
#ifdef WITH_PROFILE
if (wcp->data_size) {
INC_PROFILE_COUNT(writecache_abutted_writes);
} else {
INC_PROFILE_COUNT(writecache_init_writes);
}
#endif
memcpy(wcp->data+wcp->data_size, data, n);
if (wcp->data_size == 0) {
wcp->offset = pos;
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_num_write_caches);
#endif
}
wcp->data_size += n;
DEBUG(9,("cache return %u\n", (unsigned int)n));
total_written += n;
return total_written; /* .... that's a write :) */
}
return total_written;
}
/****************************************************************************
Delete the write cache structure.
****************************************************************************/
void delete_write_cache(files_struct *fsp)
{
write_cache *wcp;
if(!fsp)
return;
if(!(wcp = fsp->wcp))
return;
#ifdef WITH_PROFILE
DEC_PROFILE_COUNT(writecache_allocated_write_caches);
#endif
allocated_write_caches--;
SMB_ASSERT(wcp->data_size == 0);
free(wcp->data);
free(wcp);
fsp->wcp = NULL;
DEBUG(10,("delete_write_cache: File %s deleted write cache\n", fsp->fsp_name ));
}
/****************************************************************************
Setup the write cache structure.
****************************************************************************/
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(alloc_size == 0 || fsp->wcp)
return False;
if((wcp = (write_cache *)malloc(sizeof(write_cache))) == NULL) {
DEBUG(0,("setup_write_cache: malloc fail.\n"));
return False;
}
wcp->file_size = file_size;
wcp->offset = 0;
wcp->alloc_size = alloc_size;
wcp->data_size = 0;
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);
return False;
}
fsp->wcp = wcp;
#ifdef WITH_PROFILE
INC_PROFILE_COUNT(writecache_allocated_write_caches);
#endif
allocated_write_caches++;
DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
fsp->fsp_name, wcp->alloc_size ));
return True;
}
/****************************************************************************
Cope with a size change.
****************************************************************************/
void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
{
if(fsp->wcp) {
flush_write_cache(fsp, SIZECHANGE_FLUSH);
fsp->wcp->file_size = file_size;
}
}
/*******************************************************************
Flush a write cache struct to disk.
********************************************************************/
ssize_t flush_write_cache(files_struct *fsp, enum flush_reason_enum reason)
{
write_cache *wcp = fsp->wcp;
size_t data_size;
if(!wcp || !wcp->data_size)
return 0;
data_size = wcp->data_size;
wcp->data_size = 0;
#ifdef WITH_PROFILE
DEC_PROFILE_COUNT(writecache_num_write_caches);
INC_PROFILE_COUNT(writecache_flushed_writes[reason]);
#endif
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)
INC_PROFILE_COUNT(writecache_num_perfect_writes);
#endif
return real_write_file(fsp, wcp->data, wcp->offset, data_size);
}
/*******************************************************************
sync a file
********************************************************************/
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);
}
}
#undef OLD_NTDOMAIN
|