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