1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * 4 * vfs operations that deal with files 5 * 6 * Copyright (C) International Business Machines Corp., 2002,2010 7 * Author(s): Steve French (sfrench@us.ibm.com) 8 * Jeremy Allison (jra@samba.org) 9 * 10 */ 11 #include <linux/fs.h> 12 #include <linux/fs_struct.h> 13 #include <linux/filelock.h> 14 #include <linux/backing-dev.h> 15 #include <linux/stat.h> 16 #include <linux/fcntl.h> 17 #include <linux/pagemap.h> 18 #include <linux/writeback.h> 19 #include <linux/task_io_accounting_ops.h> 20 #include <linux/delay.h> 21 #include <linux/mount.h> 22 #include <linux/slab.h> 23 #include <linux/swap_ops.h> 24 #include <linux/mm.h> 25 #include <asm/div64.h> 26 #include "cifsfs.h" 27 #include "cifsglob.h" 28 #include "cifsproto.h" 29 #include "smb2proto.h" 30 #include "cifs_unicode.h" 31 #include "cifs_debug.h" 32 #include "cifs_fs_sb.h" 33 #include "fscache.h" 34 #include "smbdirect.h" 35 #include "fs_context.h" 36 #include "cifs_ioctl.h" 37 #include "cached_dir.h" 38 #include <trace/events/netfs.h> 39 40 /* flags for cifs_reopen_file() */ 41 #define CIFS_REOPEN_BOUND BIT(0) /* caller bounds the retries */ 42 #define CIFS_REOPEN_NOWAIT BIT(1) /* fail fast, skip reconnect wait */ 43 44 static int cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush, 45 unsigned int flags); 46 47 bool cifs_io_stopped(struct cifs_io_subrequest *subreq) 48 { 49 struct super_block *sb = subreq->rreq->inode->i_sb; 50 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 51 int aborts; 52 53 if (cifs_forced_shutdown(cifs_sb) || READ_ONCE(cifs_sb->exiting)) 54 return true; 55 aborts = atomic_read(&cifs_sb->umount_aborts); 56 if (aborts != subreq->req->umount_aborts) 57 return true; 58 return aborts && rwsem_is_contended(&sb->s_umount); 59 } 60 61 static bool cifs_writeback_retry_expired(struct cifs_io_subrequest *subreq, 62 struct cifs_tcon *tcon) 63 { 64 struct TCP_Server_Info *server = tcon->ses->server; 65 unsigned long window; 66 67 if (tcon->retry) 68 return false; 69 if (!subreq->retry_deadline) { 70 window = max(3U, server->retrans) * server->echo_interval; 71 subreq->retry_deadline = (jiffies + window) | 1; 72 } 73 return time_after(jiffies, subreq->retry_deadline); 74 } 75 76 bool cifs_io_should_replay(struct cifs_io_subrequest *subreq) 77 { 78 struct cifs_tcon *tcon = tlink_tcon(subreq->req->cfile->tlink); 79 80 if (cifs_io_stopped(subreq)) 81 return false; 82 if (subreq->rreq->origin == NETFS_WRITEBACK) { 83 if (cifs_writeback_retry_expired(subreq, tcon)) 84 return false; 85 smb2_replay_backoff(&subreq->cur_sleep); 86 return true; 87 } 88 return smb2_should_replay(tcon, &subreq->retries, &subreq->cur_sleep); 89 } 90 91 static bool cifs_io_aborted(struct cifs_io_subrequest *subreq) 92 { 93 struct cifs_tcon *tcon = tlink_tcon(subreq->req->cfile->tlink); 94 95 if (!cifs_io_stopped(subreq)) 96 return false; 97 return subreq->subreq.retry_count || subreq->replay || 98 READ_ONCE(subreq->server->tcpStatus) == CifsNeedReconnect || 99 tcon->need_reconnect; 100 } 101 102 static bool cifs_writeback_transient(int rc) 103 { 104 if (is_replayable_error(rc) || is_interrupt_error(rc)) 105 return true; 106 107 switch (rc) { 108 case -ENOMEM: 109 case -EHOSTDOWN: 110 case -ENETRESET: 111 case -ESHUTDOWN: 112 case -EREMCHG: 113 case -EBADF: 114 return true; 115 } 116 return false; 117 } 118 119 static int cifs_writeback_error(struct netfs_io_subrequest *subreq, int rc) 120 { 121 if (subreq->rreq->origin == NETFS_WRITEBACK && 122 cifs_writeback_transient(rc)) 123 return -EIO; 124 return rc; 125 } 126 127 static bool cifs_writeback_dying(struct netfs_io_subrequest *subreq) 128 { 129 return subreq->rreq->origin == NETFS_WRITEBACK && 130 fatal_signal_pending(current); 131 } 132 133 static unsigned int cifs_ses_reconnects(struct cifs_ses *ses) 134 { 135 unsigned int n = 0; 136 137 guard(spinlock)(&ses->chan_lock); 138 for (size_t i = 0; i < ses->chan_count; i++) { 139 struct TCP_Server_Info *server = ses->chans[i].server; 140 141 if (server) 142 n += READ_ONCE(server->reconnect_instance); 143 } 144 return n; 145 } 146 147 static bool cifs_tcon_reconnecting(struct cifs_tcon *tcon) 148 { 149 struct cifs_ses *ses = tcon->ses; 150 151 if (tcon->need_reconnect) 152 return true; 153 154 guard(spinlock)(&ses->chan_lock); 155 for (size_t i = 0; i < ses->chan_count; i++) { 156 struct TCP_Server_Info *server = ses->chans[i].server; 157 158 if (server && READ_ONCE(server->tcpStatus) == CifsNeedReconnect) 159 return true; 160 } 161 return false; 162 } 163 164 static int cifs_reopen_file_retry(struct cifsFileInfo *cfile, bool can_flush, 165 struct cifs_io_subrequest *subreq) 166 { 167 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 168 struct cifs_ses *ses = tcon->ses; 169 unsigned int reconnects; 170 bool free_retry = true; 171 bool retrying = false; 172 int rc; 173 174 for (;;) { 175 if ((retrying && cifs_io_stopped(subreq)) || 176 cifs_io_aborted(subreq)) 177 return -EIO; 178 retrying = true; 179 reconnects = cifs_ses_reconnects(ses); 180 rc = cifs_reopen_file(cfile, can_flush, CIFS_REOPEN_BOUND); 181 if (!rc || !is_replayable_error(rc)) 182 return rc; 183 if (free_retry && !cifs_io_stopped(subreq) && 184 (cifs_ses_reconnects(ses) != reconnects || 185 cifs_tcon_reconnecting(tcon))) { 186 free_retry = false; 187 continue; 188 } 189 if (!cifs_io_should_replay(subreq)) 190 return rc; 191 msleep(subreq->cur_sleep); 192 } 193 } 194 195 /* 196 * Record, as generation + 1 (0 means none), the newest reopen generation of a 197 * handle that a writeback subrequest found bad (-EBADF). Subrequests of the 198 * same request can fail concurrently on different channels, so keep the newest. 199 */ 200 static void cifs_mark_bad_handle(struct cifs_io_request *req, unsigned int gen) 201 { 202 int cur = atomic_read(&req->bad_reopens); 203 int want = gen + 1; 204 205 while (want > cur && !atomic_try_cmpxchg(&req->bad_reopens, &cur, want)) 206 ; 207 } 208 209 static bool cifs_bad_handle_pending(struct cifs_io_request *req) 210 { 211 unsigned int bad = atomic_read(&req->bad_reopens); 212 213 return bad && bad - 1 == READ_ONCE(req->cfile->reopens); 214 } 215 216 static void __cifs_invalidate_bad_handle(struct cifs_io_request *req) 217 { 218 unsigned int bad = atomic_read(&req->bad_reopens); 219 struct cifsFileInfo *cfile = req->cfile; 220 221 lockdep_assert_held(&cfile->fh_mutex); 222 if (bad && bad - 1 == cfile->reopens) 223 cfile->invalidHandle = true; 224 } 225 226 static void cifs_invalidate_bad_handle(struct cifs_io_request *req) 227 { 228 if (!cifs_bad_handle_pending(req)) 229 return; 230 guard(mutex)(&req->cfile->fh_mutex); 231 __cifs_invalidate_bad_handle(req); 232 } 233 234 static bool cifs_try_invalidate_bad_handle(struct cifs_io_request *req) 235 { 236 if (!cifs_bad_handle_pending(req)) 237 return true; 238 if (!mutex_trylock(&req->cfile->fh_mutex)) 239 return false; 240 __cifs_invalidate_bad_handle(req); 241 mutex_unlock(&req->cfile->fh_mutex); 242 return true; 243 } 244 245 static int cifs_reopen_failed(struct cifs_io_request *req) 246 { 247 if (READ_ONCE(req->reopen_gave_up)) 248 return -EIO; 249 return READ_ONCE(req->reopen_err); 250 } 251 252 static int cifs_reopen_writeback(struct cifs_io_subrequest *wdata) 253 { 254 struct cifs_io_request *req = wdata->req; 255 struct cifsFileInfo *cfile = req->cfile; 256 int rc; 257 258 if (cifs_io_aborted(wdata)) 259 return -EIO; 260 261 guard(mutex)(&req->reopen_lock); 262 if (!cifs_bad_handle_pending(req) && !cfile->invalidHandle) 263 return 0; 264 265 rc = cifs_reopen_failed(req); 266 if (rc) 267 return rc; 268 269 rc = req->reopen_pass_err; 270 if (!rc) { 271 if (!cifs_try_invalidate_bad_handle(req)) 272 rc = -EAGAIN; 273 else if (cfile->invalidHandle) 274 rc = cifs_reopen_file(cfile, false, 275 CIFS_REOPEN_BOUND | 276 CIFS_REOPEN_NOWAIT); 277 if (!rc) 278 return 0; 279 if (!cifs_writeback_transient(rc)) { 280 WRITE_ONCE(req->reopen_err, rc); 281 return rc; 282 } 283 req->reopen_pass_err = rc; 284 } 285 286 if (!cifs_io_should_replay(wdata)) { 287 WRITE_ONCE(req->reopen_gave_up, true); 288 return -EIO; 289 } 290 291 trace_netfs_sreq(&wdata->subreq, netfs_sreq_trace_io_retry_needed); 292 __set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags); 293 return rc; 294 } 295 296 /* 297 * Prepare a subrequest to upload to the server. We need to allocate credits 298 * so that we know the maximum amount of data that we can include in it. 299 */ 300 static void cifs_prepare_write(struct netfs_io_subrequest *subreq) 301 { 302 struct cifs_io_subrequest *wdata = 303 container_of(subreq, struct cifs_io_subrequest, subreq); 304 struct cifs_io_request *req = wdata->req; 305 struct netfs_io_stream *stream = &req->rreq.io_streams[subreq->stream_nr]; 306 struct TCP_Server_Info *server; 307 struct cifsFileInfo *open_file = req->cfile; 308 struct cifs_sb_info *cifs_sb = CIFS_SB(wdata->rreq->inode->i_sb); 309 bool writeback = subreq->rreq->origin == NETFS_WRITEBACK; 310 bool reprepared = wdata->have_xid; 311 size_t wsize = req->rreq.wsize; 312 int rc; 313 314 if (!wdata->have_xid) { 315 wdata->xid = get_xid(); 316 wdata->have_xid = true; 317 } 318 319 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); 320 wdata->server = server; 321 wdata->deferred = false; 322 323 if (reprepared) { 324 req->retry_deadline = wdata->retry_deadline; 325 req->cur_sleep = wdata->cur_sleep; 326 req->replay = wdata->replay; 327 } else if (subreq->retry_count) { 328 wdata->retry_deadline = req->retry_deadline; 329 wdata->cur_sleep = req->cur_sleep; 330 wdata->replay = req->replay; 331 } 332 333 if (cifs_io_aborted(wdata) || 334 (reprepared && cifs_io_stopped(wdata))) { 335 subreq->error = -EIO; 336 return netfs_prepare_write_failed(subreq); 337 } 338 339 if (cifs_sb->ctx->wsize == 0) 340 cifs_negotiate_wsize(server, cifs_sb->ctx, 341 tlink_tcon(req->cfile->tlink)); 342 343 if (writeback && 344 (cifs_bad_handle_pending(req) || open_file->invalidHandle)) { 345 rc = cifs_reopen_failed(req); 346 if (rc) { 347 subreq->error = rc; 348 return netfs_prepare_write_failed(subreq); 349 } 350 goto defer; 351 } 352 353 if (cifs_writeback_dying(subreq)) 354 goto defer; 355 356 if (!writeback) { 357 cifs_invalidate_bad_handle(req); 358 if (open_file->invalidHandle) { 359 rc = cifs_reopen_file_retry(open_file, false, wdata); 360 if (rc < 0) { 361 subreq->error = rc; 362 return netfs_prepare_write_failed(subreq); 363 } 364 } 365 } 366 367 rc = server->ops->wait_mtu_credits(server, wsize, &stream->sreq_max_len, 368 &wdata->credits, writeback); 369 if (rc < 0) { 370 if (cifs_writeback_dying(subreq) || 371 (writeback && rc == -EAGAIN)) 372 goto defer; 373 subreq->error = cifs_writeback_error(subreq, rc); 374 return netfs_prepare_write_failed(subreq); 375 } 376 goto out; 377 378 defer: 379 wdata->deferred = true; 380 wdata->credits = (struct cifs_credits){}; 381 stream->sreq_max_len = min_t(size_t, wsize, SMB2_MAX_BUFFER_SIZE); 382 out: 383 wdata->credits.rreq_debug_id = subreq->rreq->debug_id; 384 wdata->credits.rreq_debug_index = subreq->debug_index; 385 wdata->credits.in_flight_check = 1; 386 trace_smb3_rw_credits(wdata->rreq->debug_id, 387 wdata->subreq.debug_index, 388 wdata->credits.value, 389 server->credits, server->in_flight, 390 wdata->credits.value, 391 cifs_trace_rw_credits_write_prepare); 392 393 #ifdef CONFIG_CIFS_SMB_DIRECT 394 if (server->smbd_conn) { 395 const struct smbdirect_socket_parameters *sp = 396 smbd_get_parameters(server->smbd_conn); 397 398 stream->sreq_max_segs = sp->max_frmr_depth; 399 } 400 #endif 401 } 402 403 /* 404 * Issue a subrequest to upload to the server. 405 */ 406 static void __cifs_issue_write(struct netfs_io_subrequest *subreq) 407 { 408 struct cifs_io_subrequest *wdata = 409 container_of(subreq, struct cifs_io_subrequest, subreq); 410 struct cifs_sb_info *sbi = CIFS_SB(subreq->rreq->inode->i_sb); 411 int rc; 412 413 if (cifs_forced_shutdown(sbi)) { 414 rc = smb_EIO(smb_eio_trace_forced_shutdown); 415 goto fail; 416 } 417 418 rc = -EIO; 419 if (cifs_io_aborted(wdata)) 420 goto fail; 421 422 rc = adjust_credits(wdata->server, wdata, cifs_trace_rw_credits_issue_write_adjust); 423 if (rc) 424 goto fail; 425 426 rc = -EAGAIN; 427 if (wdata->req->cfile->invalidHandle || 428 (wdata->credits.value && 429 wdata->credits.instance != 430 READ_ONCE(wdata->server->reconnect_instance))) 431 goto fail; 432 433 /* pairs with smp_store_release() in cifs_reopen_file() */ 434 wdata->reopens = smp_load_acquire(&wdata->req->cfile->reopens); 435 wdata->server->ops->async_writev(wdata); 436 out: 437 return; 438 439 fail: 440 if (rc == -EAGAIN) { 441 trace_netfs_sreq(subreq, netfs_sreq_trace_retry); 442 __set_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags); 443 } else { 444 trace_netfs_sreq(subreq, netfs_sreq_trace_fail); 445 } 446 add_credits_and_wake_if(wdata->server, &wdata->credits, 0); 447 cifs_write_subrequest_terminated(wdata, rc); 448 goto out; 449 } 450 451 static void cifs_issue_write_worker(struct work_struct *work) 452 { 453 struct cifs_io_subrequest *wdata; 454 struct cifsFileInfo *cfile; 455 int rc = 0; 456 457 wdata = container_of(work, struct cifs_io_subrequest, subreq.work); 458 cfile = wdata->req->cfile; 459 if (cifs_bad_handle_pending(wdata->req) || cfile->invalidHandle) 460 rc = cifs_reopen_writeback(wdata); 461 if (rc) { 462 add_credits_and_wake_if(wdata->server, &wdata->credits, 0); 463 cifs_write_subrequest_terminated(wdata, rc); 464 return; 465 } 466 __cifs_issue_write(&wdata->subreq); 467 } 468 469 static void cifs_issue_write(struct netfs_io_subrequest *subreq) 470 { 471 struct cifs_io_subrequest *wdata = 472 container_of(subreq, struct cifs_io_subrequest, subreq); 473 struct cifs_io_request *req = wdata->req; 474 475 if (wdata->cur_sleep > req->slept) { 476 msleep(wdata->cur_sleep - req->slept); 477 req->slept = wdata->cur_sleep; 478 } 479 480 if (unlikely(wdata->deferred || cifs_writeback_dying(subreq))) { 481 subreq->work.func = cifs_issue_write_worker; 482 WARN_ON_ONCE(!queue_work(cifs_writeback_wq, &subreq->work)); 483 return; 484 } 485 __cifs_issue_write(subreq); 486 } 487 488 static void cifs_share_retry_state(struct netfs_io_subrequest *from, 489 struct netfs_io_subrequest *stop, 490 struct list_head *head) 491 { 492 struct netfs_io_subrequest *subreq = from; 493 struct cifs_io_subrequest *wdata; 494 unsigned long deadline = 0; 495 unsigned int sleep = 0; 496 bool replay = false; 497 498 list_for_each_entry_from(subreq, head, rreq_link) { 499 if (subreq == stop) 500 break; 501 wdata = container_of(subreq, struct cifs_io_subrequest, subreq); 502 if (wdata->retry_deadline && 503 (!deadline || time_before(wdata->retry_deadline, deadline))) 504 deadline = wdata->retry_deadline; 505 sleep = max(sleep, wdata->cur_sleep); 506 replay |= wdata->replay; 507 } 508 509 subreq = from; 510 list_for_each_entry_from(subreq, head, rreq_link) { 511 if (subreq == stop) 512 break; 513 wdata = container_of(subreq, struct cifs_io_subrequest, subreq); 514 wdata->retry_deadline = deadline; 515 wdata->cur_sleep = sleep; 516 wdata->replay = replay; 517 } 518 } 519 520 static void cifs_retry_request(struct netfs_io_request *wreq, 521 struct netfs_io_stream *stream) 522 { 523 struct netfs_io_subrequest *subreq, *from = NULL; 524 struct cifs_io_request *req = 525 container_of(wreq, struct cifs_io_request, rreq); 526 uoff_t end = 0; 527 528 req->slept = 0; 529 req->reopen_pass_err = 0; 530 if (!stream) 531 return; 532 533 guard(spinlock)(&wreq->lock); 534 list_for_each_entry(subreq, &stream->subrequests, rreq_link) { 535 if (from && test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags) && 536 !test_bit(NETFS_SREQ_BOUNDARY, &subreq->flags) && 537 subreq->start + subreq->transferred == end) { 538 end += subreq->len; 539 continue; 540 } 541 if (from) 542 cifs_share_retry_state(from, subreq, 543 &stream->subrequests); 544 from = NULL; 545 if (test_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags)) { 546 from = subreq; 547 end = subreq->start + subreq->len; 548 } 549 } 550 if (from) 551 cifs_share_retry_state(from, NULL, &stream->subrequests); 552 } 553 554 static void cifs_netfs_invalidate_cache(struct netfs_io_request *wreq) 555 { 556 cifs_invalidate_cache(wreq->inode, 0); 557 } 558 559 /* 560 * Negotiate the size of a read operation on behalf of the netfs library. 561 */ 562 static int cifs_prepare_read(struct netfs_io_subrequest *subreq) 563 { 564 struct netfs_io_request *rreq = subreq->rreq; 565 struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq); 566 struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq); 567 struct TCP_Server_Info *server; 568 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb); 569 bool reprepared = rdata->have_xid; 570 size_t size; 571 int rc = 0; 572 573 if (!rdata->have_xid) { 574 rdata->xid = get_xid(); 575 rdata->have_xid = true; 576 } 577 578 server = cifs_pick_channel(tlink_tcon(req->cfile->tlink)->ses); 579 rdata->server = server; 580 581 if (cifs_io_aborted(rdata) || 582 (reprepared && cifs_io_stopped(rdata))) 583 return -EIO; 584 585 if (cifs_sb->ctx->rsize == 0) 586 cifs_negotiate_rsize(server, cifs_sb->ctx, 587 tlink_tcon(req->cfile->tlink)); 588 589 if (req->cfile->invalidHandle) { 590 rc = cifs_reopen_file_retry(req->cfile, true, rdata); 591 if (rc) 592 return rc; 593 } 594 595 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize, 596 &size, &rdata->credits, false); 597 if (rc) 598 return rc; 599 600 rreq->io_streams[0].sreq_max_len = size; 601 602 rdata->credits.in_flight_check = 1; 603 rdata->credits.rreq_debug_id = rreq->debug_id; 604 rdata->credits.rreq_debug_index = subreq->debug_index; 605 606 trace_smb3_rw_credits(rdata->rreq->debug_id, 607 rdata->subreq.debug_index, 608 rdata->credits.value, 609 server->credits, server->in_flight, 0, 610 cifs_trace_rw_credits_read_submit); 611 612 #ifdef CONFIG_CIFS_SMB_DIRECT 613 if (server->smbd_conn) { 614 const struct smbdirect_socket_parameters *sp = 615 smbd_get_parameters(server->smbd_conn); 616 617 rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth; 618 } 619 #endif 620 return 0; 621 } 622 623 /* 624 * Issue a read operation on behalf of the netfs helper functions. We're asked 625 * to make a read of a certain size at a point in the file. We are permitted 626 * to only read a portion of that, but as long as we read something, the netfs 627 * helper will call us again so that we can issue another read. 628 */ 629 static void cifs_issue_read(struct netfs_io_subrequest *subreq) 630 { 631 struct netfs_io_request *rreq = subreq->rreq; 632 struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq); 633 struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq); 634 struct TCP_Server_Info *server = rdata->server; 635 int rc = 0; 636 637 cifs_dbg(FYI, "%s: op=%08x[%x] mapping=%p len=%zu/%zu\n", 638 __func__, rreq->debug_id, subreq->debug_index, rreq->mapping, 639 subreq->transferred, subreq->len); 640 641 rc = -EIO; 642 if (cifs_io_aborted(rdata)) 643 goto failed; 644 645 rc = adjust_credits(server, rdata, cifs_trace_rw_credits_issue_read_adjust); 646 if (rc) 647 goto not_sent; 648 649 if (rdata->replay && rdata->cur_sleep) { 650 msleep(rdata->cur_sleep); 651 rc = -EIO; 652 if (cifs_io_aborted(rdata)) 653 goto failed; 654 } 655 656 rc = -EAGAIN; 657 if (req->cfile->invalidHandle || 658 (rdata->credits.value && 659 rdata->credits.instance != READ_ONCE(server->reconnect_instance))) 660 goto not_sent; 661 662 if (subreq->rreq->origin != NETFS_UNBUFFERED_READ && 663 subreq->rreq->origin != NETFS_DIO_READ) 664 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); 665 666 trace_netfs_sreq(subreq, netfs_sreq_trace_submit); 667 rc = rdata->server->ops->async_readv(rdata); 668 if (rc) 669 goto failed; 670 return; 671 672 not_sent: 673 if (rc == -EAGAIN) { 674 trace_netfs_sreq(subreq, netfs_sreq_trace_retry); 675 __set_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags); 676 } 677 failed: 678 add_credits_and_wake_if(rdata->server, &rdata->credits, 0); 679 subreq->error = rc; 680 netfs_read_subreq_terminated(subreq); 681 } 682 683 /* 684 * Writeback calls this when it finds a folio that needs uploading. This isn't 685 * called if writeback only has copy-to-cache to deal with. 686 */ 687 static void cifs_begin_writeback(struct netfs_io_request *wreq) 688 { 689 struct cifs_io_request *req = container_of(wreq, struct cifs_io_request, rreq); 690 int ret; 691 692 ret = cifs_get_writable_file(CIFS_I(wreq->inode), 693 FIND_NO_REOPEN | FIND_NO_DIRECT, 694 &req->cfile); 695 if (ret) { 696 ret = cifs_get_writable_file(CIFS_I(wreq->inode), 697 FIND_NO_REOPEN, &req->cfile); 698 } 699 if (ret) { 700 cifs_dbg(VFS, "%s: couldn't find writable handle: %d\n", 701 __func__, ret); 702 mapping_set_error(wreq->mapping, -EIO); 703 return; 704 } 705 706 wreq->io_streams[0].avail = true; 707 } 708 709 /* 710 * Initialise a request. 711 */ 712 static int cifs_init_request(struct netfs_io_request *rreq, struct file *file) 713 { 714 struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq); 715 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode); 716 struct cifsFileInfo *open_file = NULL; 717 718 rreq->rsize = cifs_sb->ctx->rsize; 719 rreq->wsize = cifs_sb->ctx->wsize; 720 req->pid = current->tgid; // Ummm... This may be a workqueue 721 req->umount_aborts = atomic_read(&cifs_sb->umount_aborts); 722 mutex_init(&req->reopen_lock); 723 724 if (file) { 725 open_file = file->private_data; 726 rreq->netfs_priv = file->private_data; 727 req->cfile = cifsFileInfo_get(open_file); 728 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_RWPIDFORWARD) 729 req->pid = req->cfile->pid; 730 } else if (rreq->origin != NETFS_WRITEBACK) { 731 WARN_ON_ONCE(1); 732 return smb_EIO1(smb_eio_trace_not_netfs_writeback, rreq->origin); 733 } 734 735 atomic_inc(&cifs_sb->outstanding_rreq); 736 return 0; 737 } 738 739 /* 740 * Completion of a request operation. 741 */ 742 static void cifs_rreq_done(struct netfs_io_request *rreq) 743 { 744 struct timespec64 atime, mtime; 745 struct inode *inode = rreq->inode; 746 747 /* we do not want atime to be less than mtime, it broke some apps */ 748 atime = inode_set_atime_to_ts(inode, current_time(inode)); 749 mtime = inode_get_mtime(inode); 750 if (timespec64_compare(&atime, &mtime) < 0) 751 inode_set_atime_to_ts(inode, inode_get_mtime(inode)); 752 } 753 754 static void cifs_free_request(struct netfs_io_request *rreq) 755 { 756 struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq); 757 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb); 758 759 if (req->cfile) 760 cifsFileInfo_put(req->cfile); 761 762 if (atomic_dec_and_test(&cifs_sb->outstanding_rreq)) 763 wake_up_var(&cifs_sb->outstanding_rreq); 764 } 765 766 static void cifs_free_subrequest(struct netfs_io_subrequest *subreq) 767 { 768 struct cifs_io_subrequest *rdata = 769 container_of(subreq, struct cifs_io_subrequest, subreq); 770 int rc = subreq->error; 771 772 if (rdata->subreq.source == NETFS_DOWNLOAD_FROM_SERVER) { 773 #ifdef CONFIG_CIFS_SMB_DIRECT 774 if (rdata->mr) { 775 smbd_deregister_mr(rdata->mr); 776 rdata->mr = NULL; 777 } 778 #endif 779 } 780 781 if (rdata->credits.value != 0) { 782 trace_smb3_rw_credits(rdata->rreq->debug_id, 783 rdata->subreq.debug_index, 784 rdata->credits.value, 785 rdata->server ? rdata->server->credits : 0, 786 rdata->server ? rdata->server->in_flight : 0, 787 -rdata->credits.value, 788 cifs_trace_rw_credits_free_subreq); 789 if (rdata->server) 790 add_credits_and_wake_if(rdata->server, &rdata->credits, 0); 791 else 792 rdata->credits.value = 0; 793 } 794 795 if (rdata->have_xid) 796 free_xid(rdata->xid); 797 } 798 799 const struct netfs_request_ops cifs_req_ops = { 800 .request_pool = &cifs_io_request_pool, 801 .subrequest_pool = &cifs_io_subrequest_pool, 802 .init_request = cifs_init_request, 803 .free_request = cifs_free_request, 804 .free_subrequest = cifs_free_subrequest, 805 .prepare_read = cifs_prepare_read, 806 .issue_read = cifs_issue_read, 807 .done = cifs_rreq_done, 808 .begin_writeback = cifs_begin_writeback, 809 .prepare_write = cifs_prepare_write, 810 .issue_write = cifs_issue_write, 811 .retry_request = cifs_retry_request, 812 .invalidate_cache = cifs_netfs_invalidate_cache, 813 }; 814 815 /* 816 * Mark as invalid, all open files on tree connections since they 817 * were closed when session to server was lost. 818 */ 819 void 820 cifs_mark_open_files_invalid(struct cifs_tcon *tcon) 821 { 822 struct cifsFileInfo *open_file = NULL; 823 struct list_head *tmp; 824 struct list_head *tmp1; 825 826 /* only send once per connect */ 827 spin_lock(&tcon->tc_lock); 828 if (tcon->need_reconnect) 829 tcon->status = TID_NEED_RECON; 830 831 if (tcon->status != TID_NEED_RECON) { 832 spin_unlock(&tcon->tc_lock); 833 return; 834 } 835 tcon->status = TID_IN_FILES_INVALIDATE; 836 spin_unlock(&tcon->tc_lock); 837 838 /* list all files open on tree connection and mark them invalid */ 839 spin_lock(&tcon->open_file_lock); 840 list_for_each_safe(tmp, tmp1, &tcon->openFileList) { 841 open_file = list_entry(tmp, struct cifsFileInfo, tlist); 842 open_file->invalidHandle = true; 843 open_file->oplock_break_cancelled = true; 844 } 845 spin_unlock(&tcon->open_file_lock); 846 847 invalidate_all_cached_dirs(tcon, true); 848 spin_lock(&tcon->tc_lock); 849 if (tcon->status == TID_IN_FILES_INVALIDATE) 850 tcon->status = TID_NEED_TCON; 851 spin_unlock(&tcon->tc_lock); 852 853 /* 854 * BB Add call to evict_inodes(sb) for all superblocks mounted 855 * to this tcon. 856 */ 857 } 858 859 static inline int cifs_convert_flags(unsigned int oflags, int rdwr_for_fscache) 860 { 861 int flags = 0; 862 863 if (oflags & O_TMPFILE) 864 flags |= DELETE; 865 866 if ((oflags & O_ACCMODE) == O_RDONLY) 867 return flags | GENERIC_READ; 868 if ((oflags & O_ACCMODE) == O_WRONLY) { 869 return flags | (rdwr_for_fscache == 1 ? 870 (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE); 871 } 872 if ((oflags & O_ACCMODE) == O_RDWR) { 873 /* GENERIC_ALL is too much permission to request 874 can cause unnecessary access denied on create */ 875 /* return GENERIC_ALL; */ 876 return flags | GENERIC_READ | GENERIC_WRITE; 877 } 878 879 return flags | READ_CONTROL | FILE_WRITE_ATTRIBUTES | 880 FILE_READ_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | 881 FILE_WRITE_DATA | FILE_READ_DATA; 882 } 883 884 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 885 static u32 cifs_posix_convert_flags(unsigned int flags) 886 { 887 u32 posix_flags = 0; 888 889 if ((flags & O_ACCMODE) == O_RDONLY) 890 posix_flags = SMB_O_RDONLY; 891 else if ((flags & O_ACCMODE) == O_WRONLY) 892 posix_flags = SMB_O_WRONLY; 893 else if ((flags & O_ACCMODE) == O_RDWR) 894 posix_flags = SMB_O_RDWR; 895 896 if (flags & O_CREAT) { 897 posix_flags |= SMB_O_CREAT; 898 if (flags & O_EXCL) 899 posix_flags |= SMB_O_EXCL; 900 } else if (flags & O_EXCL) 901 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", 902 current->comm, current->tgid); 903 904 if (flags & O_TRUNC) 905 posix_flags |= SMB_O_TRUNC; 906 /* be safe and imply O_SYNC for O_DSYNC */ 907 if (flags & O_DSYNC) 908 posix_flags |= SMB_O_SYNC; 909 if (flags & O_DIRECTORY) 910 posix_flags |= SMB_O_DIRECTORY; 911 if (flags & O_NOFOLLOW) 912 posix_flags |= SMB_O_NOFOLLOW; 913 if (flags & O_DIRECT) 914 posix_flags |= SMB_O_DIRECT; 915 916 return posix_flags; 917 } 918 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 919 920 static inline int cifs_get_disposition(unsigned int flags) 921 { 922 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 923 return FILE_CREATE; 924 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 925 return FILE_OVERWRITE_IF; 926 else if ((flags & O_CREAT) == O_CREAT) 927 return FILE_OPEN_IF; 928 else if ((flags & O_TRUNC) == O_TRUNC) 929 return FILE_OVERWRITE; 930 else 931 return FILE_OPEN; 932 } 933 934 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 935 int cifs_posix_open(const char *full_path, struct inode **pinode, 936 struct super_block *sb, int mode, unsigned int f_flags, 937 __u32 *poplock, __u16 *pnetfid, unsigned int xid) 938 { 939 int rc; 940 FILE_UNIX_BASIC_INFO *presp_data; 941 __u32 posix_flags = 0; 942 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 943 struct cifs_fattr fattr; 944 struct tcon_link *tlink; 945 struct cifs_tcon *tcon; 946 947 cifs_dbg(FYI, "posix open %s\n", full_path); 948 949 presp_data = kzalloc_obj(FILE_UNIX_BASIC_INFO); 950 if (presp_data == NULL) 951 return -ENOMEM; 952 953 tlink = cifs_sb_tlink(cifs_sb); 954 if (IS_ERR(tlink)) { 955 rc = PTR_ERR(tlink); 956 goto posix_open_ret; 957 } 958 959 tcon = tlink_tcon(tlink); 960 mode &= ~current_umask(); 961 962 posix_flags = cifs_posix_convert_flags(f_flags); 963 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, 964 poplock, full_path, cifs_remap(cifs_sb)); 965 cifs_put_tlink(tlink); 966 967 if (rc) 968 goto posix_open_ret; 969 970 if (presp_data->Type == cpu_to_le32(-1)) 971 goto posix_open_ret; /* open ok, caller does qpathinfo */ 972 973 if (!pinode) 974 goto posix_open_ret; /* caller does not need info */ 975 976 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); 977 978 /* get new inode and set it up */ 979 if (*pinode == NULL) { 980 cifs_fill_uniqueid(sb, &fattr); 981 *pinode = cifs_iget(sb, &fattr); 982 if (!*pinode) { 983 rc = -ENOMEM; 984 goto posix_open_ret; 985 } 986 } else { 987 cifs_revalidate_mapping(*pinode); 988 rc = cifs_fattr_to_inode(*pinode, &fattr, false); 989 } 990 991 posix_open_ret: 992 kfree(presp_data); 993 return rc; 994 } 995 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 996 997 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, 998 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, 999 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf) 1000 { 1001 int rc; 1002 int desired_access; 1003 int disposition; 1004 int create_options = CREATE_NOT_DIR; 1005 struct TCP_Server_Info *server = tcon->ses->server; 1006 struct cifs_open_parms oparms; 1007 int rdwr_for_fscache = 0; 1008 1009 if (!server->ops->open) 1010 return -ENOSYS; 1011 1012 /* If we're caching, we need to be able to fill in around partial writes. */ 1013 if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY) 1014 rdwr_for_fscache = 1; 1015 1016 desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache); 1017 1018 /********************************************************************* 1019 * open flag mapping table: 1020 * 1021 * POSIX Flag CIFS Disposition 1022 * ---------- ---------------- 1023 * O_CREAT FILE_OPEN_IF 1024 * O_CREAT | O_EXCL FILE_CREATE 1025 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF 1026 * O_TRUNC FILE_OVERWRITE 1027 * none of the above FILE_OPEN 1028 * 1029 * Note that there is not a direct match between disposition 1030 * FILE_SUPERSEDE (ie create whether or not file exists although 1031 * O_CREAT | O_TRUNC is similar but truncates the existing 1032 * file rather than creating a new file as FILE_SUPERSEDE does 1033 * (which uses the attributes / metadata passed in on open call) 1034 *? 1035 *? O_SYNC is a reasonable match to CIFS writethrough flag 1036 *? and the read write flags match reasonably. O_LARGEFILE 1037 *? is irrelevant because largefile support is always used 1038 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, 1039 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation 1040 *********************************************************************/ 1041 1042 disposition = cifs_get_disposition(f_flags); 1043 /* BB pass O_SYNC flag through on file attributes .. BB */ 1044 create_options |= cifs_open_create_options(f_flags, create_options); 1045 1046 retry_open: 1047 oparms = (struct cifs_open_parms) { 1048 .tcon = tcon, 1049 .cifs_sb = cifs_sb, 1050 .desired_access = desired_access, 1051 .create_options = cifs_create_options(cifs_sb, create_options), 1052 .disposition = disposition, 1053 .path = full_path, 1054 .fid = fid, 1055 }; 1056 1057 rc = server->ops->open(xid, &oparms, oplock, buf); 1058 if (rc) { 1059 if (rc == -EACCES && rdwr_for_fscache == 1) { 1060 desired_access = cifs_convert_flags(f_flags, 0); 1061 rdwr_for_fscache = 2; 1062 goto retry_open; 1063 } 1064 return rc; 1065 } 1066 if (rdwr_for_fscache == 2) 1067 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 1068 1069 if (server->vals->protocol_id > SMB20_PROT_ID && 1070 *oplock != SMB2_OPLOCK_LEVEL_NONE) 1071 CIFS_I(inode)->lease_granted = true; 1072 1073 /* TODO: Add support for calling posix query info but with passing in fid */ 1074 if (tcon->unix_ext) 1075 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, 1076 xid); 1077 else 1078 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, 1079 xid, fid); 1080 1081 if (rc) { 1082 server->ops->close(xid, tcon, fid); 1083 if (rc == -ESTALE) 1084 rc = -EOPENSTALE; 1085 } 1086 1087 return rc; 1088 } 1089 1090 static bool 1091 cifs_has_mand_locks(struct cifsInodeInfo *cinode) 1092 { 1093 struct cifs_fid_locks *cur; 1094 bool has_locks = false; 1095 1096 down_read(&cinode->lock_sem); 1097 list_for_each_entry(cur, &cinode->llist, llist) { 1098 if (!list_empty(&cur->locks)) { 1099 has_locks = true; 1100 break; 1101 } 1102 } 1103 up_read(&cinode->lock_sem); 1104 return has_locks; 1105 } 1106 1107 void 1108 cifs_down_write(struct rw_semaphore *sem) 1109 { 1110 while (!down_write_trylock(sem)) 1111 msleep(10); 1112 } 1113 1114 static void cifsFileInfo_put_work(struct work_struct *work); 1115 void serverclose_work(struct work_struct *work); 1116 1117 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, 1118 struct tcon_link *tlink, __u32 oplock, 1119 const char *symlink_target) 1120 { 1121 struct dentry *dentry = file_dentry(file); 1122 struct inode *inode = d_inode(dentry); 1123 struct cifsInodeInfo *cinode = CIFS_I(inode); 1124 struct cifsFileInfo *cfile; 1125 struct cifs_fid_locks *fdlocks; 1126 struct cifs_tcon *tcon = tlink_tcon(tlink); 1127 struct TCP_Server_Info *server = tcon->ses->server; 1128 1129 cfile = kzalloc_obj(struct cifsFileInfo); 1130 if (cfile == NULL) 1131 return cfile; 1132 1133 fdlocks = kzalloc_obj(struct cifs_fid_locks); 1134 if (!fdlocks) { 1135 kfree(cfile); 1136 return NULL; 1137 } 1138 1139 if (symlink_target) { 1140 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL); 1141 if (!cfile->symlink_target) { 1142 kfree(fdlocks); 1143 kfree(cfile); 1144 return NULL; 1145 } 1146 } 1147 1148 INIT_LIST_HEAD(&fdlocks->locks); 1149 fdlocks->cfile = cfile; 1150 cfile->llist = fdlocks; 1151 1152 cfile->count = 1; 1153 cfile->pid = current->tgid; 1154 cfile->uid = current_fsuid(); 1155 cfile->dentry = dget(dentry); 1156 cfile->f_flags = file->f_flags; 1157 cfile->invalidHandle = false; 1158 cfile->deferred_close_scheduled = false; 1159 cfile->status_file_deleted = file->f_flags & O_TMPFILE; 1160 cfile->tlink = cifs_get_tlink(tlink); 1161 INIT_WORK(&cfile->oplock_break, cifs_oplock_break); 1162 INIT_WORK(&cfile->put, cifsFileInfo_put_work); 1163 INIT_WORK(&cfile->serverclose, serverclose_work); 1164 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close); 1165 mutex_init(&cfile->fh_mutex); 1166 spin_lock_init(&cfile->file_info_lock); 1167 1168 /* 1169 * If the server returned a read oplock and we have mandatory brlocks, 1170 * set oplock level to None. 1171 */ 1172 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 1173 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 1174 oplock = 0; 1175 } 1176 1177 cifs_down_write(&cinode->lock_sem); 1178 list_add(&fdlocks->llist, &cinode->llist); 1179 up_write(&cinode->lock_sem); 1180 1181 spin_lock(&tcon->open_file_lock); 1182 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) 1183 oplock = fid->pending_open->oplock; 1184 list_del(&fid->pending_open->olist); 1185 1186 list_add(&cfile->tlist, &tcon->openFileList); 1187 atomic_inc(&tcon->num_local_opens); 1188 1189 /* if readable file instance put first in list*/ 1190 spin_lock(&cinode->open_file_lock); 1191 if (file->f_flags & O_TMPFILE) 1192 set_bit(CIFS_INO_TMPFILE, &cinode->flags); 1193 fid->purge_cache = false; 1194 server->ops->set_fid(cfile, fid, oplock); 1195 1196 if (file->f_mode & FMODE_READ) 1197 list_add(&cfile->flist, &cinode->openFileList); 1198 else 1199 list_add_tail(&cfile->flist, &cinode->openFileList); 1200 spin_unlock(&cinode->open_file_lock); 1201 spin_unlock(&tcon->open_file_lock); 1202 1203 if (fid->purge_cache) 1204 cifs_zap_mapping(inode); 1205 1206 file->private_data = cfile; 1207 return cfile; 1208 } 1209 1210 struct cifsFileInfo * 1211 cifsFileInfo_get(struct cifsFileInfo *cifs_file) 1212 { 1213 spin_lock(&cifs_file->file_info_lock); 1214 cifsFileInfo_get_locked(cifs_file); 1215 spin_unlock(&cifs_file->file_info_lock); 1216 return cifs_file; 1217 } 1218 1219 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file) 1220 { 1221 struct inode *inode = d_inode(cifs_file->dentry); 1222 struct cifsInodeInfo *cifsi = CIFS_I(inode); 1223 struct cifsLockInfo *li, *tmp; 1224 1225 /* 1226 * Delete any outstanding lock records. We'll lose them when the file 1227 * is closed anyway. 1228 */ 1229 cifs_down_write(&cifsi->lock_sem); 1230 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { 1231 list_del(&li->llist); 1232 cifs_del_lock_waiters(li); 1233 kfree(li); 1234 } 1235 list_del(&cifs_file->llist->llist); 1236 kfree(cifs_file->llist); 1237 up_write(&cifsi->lock_sem); 1238 1239 cifs_put_tlink(cifs_file->tlink); 1240 dput(cifs_file->dentry); 1241 kfree(cifs_file->symlink_target); 1242 kfree(cifs_file); 1243 } 1244 1245 static void cifsFileInfo_put_work(struct work_struct *work) 1246 { 1247 struct cifsFileInfo *cifs_file = container_of(work, 1248 struct cifsFileInfo, put); 1249 1250 cifsFileInfo_put_final(cifs_file); 1251 } 1252 1253 void serverclose_work(struct work_struct *work) 1254 { 1255 struct cifsFileInfo *cifs_file = container_of(work, 1256 struct cifsFileInfo, serverclose); 1257 1258 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 1259 1260 struct TCP_Server_Info *server = tcon->ses->server; 1261 int rc = 0; 1262 int retries = 0; 1263 int MAX_RETRIES = 4; 1264 1265 do { 1266 if (server->ops->close_getattr) 1267 rc = server->ops->close_getattr(0, tcon, cifs_file); 1268 else if (server->ops->close) 1269 rc = server->ops->close(0, tcon, &cifs_file->fid); 1270 1271 if (rc == -EBUSY || rc == -EAGAIN) { 1272 retries++; 1273 msleep(250); 1274 } 1275 } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES) 1276 ); 1277 1278 if (retries == MAX_RETRIES) 1279 pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES); 1280 1281 if (cifs_file->offload) 1282 queue_work(fileinfo_put_wq, &cifs_file->put); 1283 else 1284 cifsFileInfo_put_final(cifs_file); 1285 } 1286 1287 /** 1288 * cifsFileInfo_put - release a reference of file priv data 1289 * 1290 * Always potentially wait for oplock handler. See _cifsFileInfo_put(). 1291 * 1292 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 1293 */ 1294 void cifsFileInfo_put(struct cifsFileInfo *cifs_file) 1295 { 1296 _cifsFileInfo_put(cifs_file, true, true); 1297 } 1298 1299 /** 1300 * _cifsFileInfo_put - release a reference of file priv data 1301 * 1302 * This may involve closing the filehandle @cifs_file out on the 1303 * server. Must be called without holding tcon->open_file_lock, 1304 * cinode->open_file_lock and cifs_file->file_info_lock. 1305 * 1306 * If @wait_for_oplock_handler is true and we are releasing the last 1307 * reference, wait for any running oplock break handler of the file 1308 * and cancel any pending one. 1309 * 1310 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 1311 * @wait_oplock_handler: must be false if called from oplock_break_handler 1312 * @offload: not offloaded on close and oplock breaks 1313 * 1314 */ 1315 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, 1316 bool wait_oplock_handler, bool offload) 1317 { 1318 struct inode *inode = d_inode(cifs_file->dentry); 1319 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 1320 struct TCP_Server_Info *server = tcon->ses->server; 1321 struct cifsInodeInfo *cifsi = CIFS_I(inode); 1322 struct super_block *sb = inode->i_sb; 1323 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 1324 struct cifs_fid fid = {}; 1325 struct cifs_pending_open open; 1326 bool oplock_break_cancelled; 1327 bool serverclose_offloaded = false; 1328 1329 spin_lock(&tcon->open_file_lock); 1330 spin_lock(&cifsi->open_file_lock); 1331 spin_lock(&cifs_file->file_info_lock); 1332 1333 cifs_file->offload = offload; 1334 if (--cifs_file->count > 0) { 1335 spin_unlock(&cifs_file->file_info_lock); 1336 spin_unlock(&cifsi->open_file_lock); 1337 spin_unlock(&tcon->open_file_lock); 1338 return; 1339 } 1340 spin_unlock(&cifs_file->file_info_lock); 1341 1342 if (server->ops->get_lease_key) 1343 server->ops->get_lease_key(inode, &fid); 1344 1345 /* store open in pending opens to make sure we don't miss lease break */ 1346 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); 1347 1348 /* remove it from the lists */ 1349 list_del(&cifs_file->flist); 1350 list_del(&cifs_file->tlist); 1351 atomic_dec(&tcon->num_local_opens); 1352 1353 if (list_empty(&cifsi->openFileList)) { 1354 cifs_dbg(FYI, "closing last open instance for inode %p\n", 1355 d_inode(cifs_file->dentry)); 1356 /* 1357 * In strict cache mode we need invalidate mapping on the last 1358 * close because it may cause a error when we open this file 1359 * again and get at least level II oplock. 1360 */ 1361 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_STRICT_IO) 1362 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); 1363 cifs_set_oplock_level(cifsi, 0); 1364 } 1365 1366 if (OPEN_FMODE(cifs_file->f_flags) & FMODE_WRITE) { 1367 /* Stamp while open_file_lock is held; covers all close paths 1368 * including background I/O. Pairs with smp_load_acquire() in 1369 * is_size_safe_to_change(). 1370 */ 1371 smp_store_release(&cifsi->time_last_write, jiffies); 1372 } 1373 1374 spin_unlock(&cifsi->open_file_lock); 1375 spin_unlock(&tcon->open_file_lock); 1376 1377 oplock_break_cancelled = wait_oplock_handler ? 1378 cancel_work_sync(&cifs_file->oplock_break) : false; 1379 1380 if (!tcon->need_reconnect && !cifs_file->invalidHandle) { 1381 struct TCP_Server_Info *server = tcon->ses->server; 1382 unsigned int xid; 1383 int rc = 0; 1384 1385 xid = get_xid(); 1386 if (server->ops->close_getattr) 1387 rc = server->ops->close_getattr(xid, tcon, cifs_file); 1388 else if (server->ops->close) 1389 rc = server->ops->close(xid, tcon, &cifs_file->fid); 1390 _free_xid(xid); 1391 1392 if (rc == -EBUSY || rc == -EAGAIN) { 1393 // Server close failed, hence offloading it as an async op 1394 queue_work(serverclose_wq, &cifs_file->serverclose); 1395 serverclose_offloaded = true; 1396 } 1397 } 1398 1399 if (oplock_break_cancelled) 1400 cifs_done_oplock_break(cifsi); 1401 1402 cifs_del_pending_open(&open); 1403 1404 // if serverclose has been offloaded to wq (on failure), it will 1405 // handle offloading put as well. If serverclose not offloaded, 1406 // we need to handle offloading put here. 1407 if (!serverclose_offloaded) { 1408 if (offload) 1409 queue_work(fileinfo_put_wq, &cifs_file->put); 1410 else 1411 cifsFileInfo_put_final(cifs_file); 1412 } 1413 } 1414 1415 int cifs_file_flush(const unsigned int xid, struct inode *inode, 1416 struct cifsFileInfo *cfile) 1417 { 1418 struct cifs_sb_info *cifs_sb = CIFS_SB(inode); 1419 struct cifs_tcon *tcon; 1420 int rc; 1421 1422 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOSSYNC) 1423 return 0; 1424 1425 if (cfile && (OPEN_FMODE(cfile->f_flags) & FMODE_WRITE)) { 1426 tcon = tlink_tcon(cfile->tlink); 1427 return tcon->ses->server->ops->flush(xid, tcon, 1428 &cfile->fid); 1429 } 1430 rc = cifs_get_writable_file(CIFS_I(inode), FIND_ANY, &cfile); 1431 if (!rc) { 1432 tcon = tlink_tcon(cfile->tlink); 1433 rc = tcon->ses->server->ops->flush(xid, tcon, &cfile->fid); 1434 cifsFileInfo_put(cfile); 1435 } else if (rc == -EBADF) { 1436 rc = 0; 1437 } 1438 return rc; 1439 } 1440 1441 static int cifs_do_truncate(const unsigned int xid, struct dentry *dentry) 1442 { 1443 struct cifsInodeInfo *cinode = CIFS_I(d_inode(dentry)); 1444 struct inode *inode = d_inode(dentry); 1445 struct cifsFileInfo *cfile = NULL; 1446 struct TCP_Server_Info *server; 1447 struct cifs_tcon *tcon; 1448 int rc; 1449 1450 rc = inode_lock_killable(inode); 1451 if (rc) 1452 return -ERESTARTSYS; 1453 1454 filemap_invalidate_lock(inode->i_mapping); 1455 1456 rc = filemap_write_and_wait(inode->i_mapping); 1457 if (is_interrupt_error(rc)) { 1458 rc = -ERESTARTSYS; 1459 goto out; 1460 } 1461 mapping_set_error(inode->i_mapping, rc); 1462 1463 netfs_wait_for_outstanding_io(inode); 1464 1465 cfile = find_writable_file(cinode, FIND_FSUID_ONLY); 1466 rc = cifs_file_flush(xid, inode, cfile); 1467 if (!rc) { 1468 if (cfile) { 1469 struct netfs_inode *ictx = netfs_inode(inode); 1470 loff_t old_size = i_size_read(inode); 1471 1472 tcon = tlink_tcon(cfile->tlink); 1473 server = tcon->ses->server; 1474 netfs_wb_begin(ictx, false); 1475 rc = server->ops->set_file_size(xid, tcon, 1476 cfile, 0, false); 1477 if (!rc) { 1478 netfs_resize_file(&cinode->netfs, 0, true); 1479 cifs_setsize(inode, old_size, 0); 1480 cifs_invalidate_cache(inode, 0); 1481 } 1482 netfs_wb_end(ictx); 1483 } else { 1484 /* 1485 * No cached handle; evict stale pages so they can't 1486 * be served after the file is later extended; let 1487 * the server's O_TRUNC open response set the i_size 1488 */ 1489 truncate_inode_pages(inode->i_mapping, 0); 1490 cifs_invalidate_cache(inode, 0); 1491 } 1492 } 1493 1494 out: 1495 filemap_invalidate_unlock(inode->i_mapping); 1496 inode_unlock(inode); 1497 if (cfile) 1498 cifsFileInfo_put(cfile); 1499 return rc; 1500 } 1501 1502 int cifs_open(struct inode *inode, struct file *file) 1503 1504 { 1505 struct cifs_sb_info *cifs_sb = CIFS_SB(inode); 1506 struct cifs_open_info_data data = {}; 1507 struct cifsFileInfo *cfile = NULL; 1508 struct TCP_Server_Info *server; 1509 struct cifs_pending_open open; 1510 bool posix_open_ok = false; 1511 struct cifs_fid fid = {}; 1512 struct tcon_link *tlink; 1513 struct cifs_tcon *tcon; 1514 const char *full_path; 1515 unsigned int sbflags; 1516 int rc = -EACCES; 1517 unsigned int xid; 1518 __u32 oplock; 1519 void *page; 1520 1521 xid = get_xid(); 1522 1523 if (unlikely(cifs_forced_shutdown(cifs_sb))) { 1524 free_xid(xid); 1525 return smb_EIO(smb_eio_trace_forced_shutdown); 1526 } 1527 1528 tlink = cifs_sb_tlink(cifs_sb); 1529 if (IS_ERR(tlink)) { 1530 free_xid(xid); 1531 return PTR_ERR(tlink); 1532 } 1533 tcon = tlink_tcon(tlink); 1534 server = tcon->ses->server; 1535 1536 page = alloc_dentry_path(); 1537 full_path = build_path_from_dentry(file_dentry(file), page); 1538 if (IS_ERR(full_path)) { 1539 rc = PTR_ERR(full_path); 1540 goto out; 1541 } 1542 1543 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", 1544 inode, file->f_flags, full_path); 1545 1546 sbflags = cifs_sb_flags(cifs_sb); 1547 if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) { 1548 if (sbflags & CIFS_MOUNT_NO_BRL) 1549 file->f_op = &cifs_file_direct_nobrl_ops; 1550 else 1551 file->f_op = &cifs_file_direct_ops; 1552 } 1553 1554 if (file->f_flags & O_TRUNC) { 1555 rc = cifs_do_truncate(xid, file_dentry(file)); 1556 if (rc) 1557 goto out; 1558 } 1559 1560 /* Get the cached handle as SMB2 close is deferred */ 1561 if (OPEN_FMODE(file->f_flags) & FMODE_WRITE) { 1562 rc = __cifs_get_writable_file(CIFS_I(inode), 1563 FIND_FSUID_ONLY | 1564 FIND_NO_PENDING_DELETE | 1565 FIND_OPEN_FLAGS, 1566 file->f_flags, &cfile); 1567 } else { 1568 cfile = __find_readable_file(CIFS_I(inode), 1569 FIND_NO_PENDING_DELETE | 1570 FIND_OPEN_FLAGS, 1571 file->f_flags); 1572 rc = cfile ? 0 : -ENOENT; 1573 } 1574 if (rc == 0) { 1575 trace_smb3_open_cached(xid, tcon->tid, tcon->ses->Suid, 1576 cfile->fid.persistent_fid, 1577 file->f_flags, cfile->f_flags); 1578 file->private_data = cfile; 1579 spin_lock(&CIFS_I(inode)->deferred_lock); 1580 cifs_del_deferred_close(cfile); 1581 spin_unlock(&CIFS_I(inode)->deferred_lock); 1582 goto use_cache; 1583 } 1584 /* hard link on the deferred close file */ 1585 rc = cifs_get_hardlink_path(tcon, inode, file); 1586 if (rc) 1587 cifs_close_deferred_file(CIFS_I(inode)); 1588 1589 if (server->oplocks) 1590 oplock = REQ_OPLOCK; 1591 else 1592 oplock = 0; 1593 1594 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1595 if (!tcon->broken_posix_open && tcon->unix_ext && 1596 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & 1597 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 1598 /* can not refresh inode info since size could be stale */ 1599 rc = cifs_posix_open(full_path, &inode, inode->i_sb, 1600 cifs_sb->ctx->file_mode /* ignored */, 1601 file->f_flags, &oplock, &fid.netfid, xid); 1602 if (rc == 0) { 1603 cifs_dbg(FYI, "posix open succeeded\n"); 1604 posix_open_ok = true; 1605 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 1606 if (tcon->ses->serverNOS) 1607 cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n", 1608 tcon->ses->ip_addr, 1609 tcon->ses->serverNOS); 1610 tcon->broken_posix_open = true; 1611 } else if ((rc != -EIO) && (rc != -EREMOTE) && 1612 (rc != -EOPNOTSUPP)) /* path not found or net err */ 1613 goto out; 1614 /* 1615 * Else fallthrough to retry open the old way on network i/o 1616 * or DFS errors. 1617 */ 1618 } 1619 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1620 1621 if (server->ops->get_lease_key) 1622 server->ops->get_lease_key(inode, &fid); 1623 1624 cifs_add_pending_open(&fid, tlink, &open); 1625 1626 if (!posix_open_ok) { 1627 if (server->ops->get_lease_key) 1628 server->ops->get_lease_key(inode, &fid); 1629 1630 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid, 1631 xid, &data); 1632 if (rc) { 1633 cifs_del_pending_open(&open); 1634 goto out; 1635 } 1636 } 1637 1638 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target); 1639 if (cfile == NULL) { 1640 if (server->ops->close) 1641 server->ops->close(xid, tcon, &fid); 1642 cifs_del_pending_open(&open); 1643 rc = -ENOMEM; 1644 goto out; 1645 } 1646 1647 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1648 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { 1649 /* 1650 * Time to set mode which we can not set earlier due to 1651 * problems creating new read-only files. 1652 */ 1653 struct cifs_unix_set_info_args args = { 1654 .mode = inode->i_mode, 1655 .uid = INVALID_UID, /* no change */ 1656 .gid = INVALID_GID, /* no change */ 1657 .ctime = NO_CHANGE_64, 1658 .atime = NO_CHANGE_64, 1659 .mtime = NO_CHANGE_64, 1660 .device = 0, 1661 }; 1662 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, 1663 cfile->pid); 1664 } 1665 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1666 1667 use_cache: 1668 fscache_use_cookie(cifs_inode_cookie(file_inode(file)), 1669 file->f_mode & FMODE_WRITE); 1670 if (!(file->f_flags & O_DIRECT)) 1671 goto out; 1672 if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY) 1673 goto out; 1674 cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE); 1675 1676 out: 1677 free_dentry_path(page); 1678 free_xid(xid); 1679 cifs_put_tlink(tlink); 1680 cifs_free_open_info(&data); 1681 return rc; 1682 } 1683 1684 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1685 static int cifs_push_posix_locks(struct cifsFileInfo *cfile); 1686 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1687 1688 /* 1689 * Try to reacquire byte range locks that were released when session 1690 * to server was lost. 1691 */ 1692 static int 1693 cifs_relock_file(struct cifsFileInfo *cfile) 1694 { 1695 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1696 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1697 int rc = 0; 1698 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1699 struct cifs_sb_info *cifs_sb = CIFS_SB(cinode); 1700 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1701 1702 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); 1703 if (cinode->can_cache_brlcks) { 1704 /* can cache locks - no need to relock */ 1705 up_read(&cinode->lock_sem); 1706 return rc; 1707 } 1708 1709 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1710 if (cap_unix(tcon->ses) && 1711 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 1712 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0)) 1713 rc = cifs_push_posix_locks(cfile); 1714 else 1715 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1716 rc = tcon->ses->server->ops->push_mand_locks(cfile); 1717 1718 up_read(&cinode->lock_sem); 1719 return rc; 1720 } 1721 1722 static bool reopen_aborted(struct cifs_tcon *tcon, int aborts) 1723 { 1724 struct TCP_Server_Info *server; 1725 1726 if (atomic_read(&tcon->umount_aborts) == aborts) 1727 return false; 1728 server = cifs_pick_channel(tcon->ses); 1729 return server && READ_ONCE(server->tcpStatus) == CifsNeedReconnect; 1730 } 1731 1732 static int 1733 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush, unsigned int flags) 1734 { 1735 int rc = -EACCES; 1736 unsigned int xid; 1737 __u32 oplock; 1738 struct cifs_sb_info *cifs_sb; 1739 struct cifs_tcon *tcon; 1740 struct TCP_Server_Info *server; 1741 struct cifsInodeInfo *cinode; 1742 struct inode *inode; 1743 void *page; 1744 const char *full_path; 1745 int desired_access; 1746 int disposition = FILE_OPEN; 1747 int create_options = CREATE_NOT_DIR; 1748 struct cifs_open_parms oparms; 1749 int rdwr_for_fscache = 0; 1750 int aborts; 1751 bool fresh; 1752 1753 tcon = tlink_tcon(cfile->tlink); 1754 aborts = atomic_read(&tcon->umount_aborts); 1755 xid = get_xid(); 1756 if (!(flags & CIFS_REOPEN_NOWAIT)) { 1757 mutex_lock(&cfile->fh_mutex); 1758 } else if (!mutex_trylock(&cfile->fh_mutex)) { 1759 free_xid(xid); 1760 return -EAGAIN; 1761 } 1762 if (!cfile->invalidHandle) { 1763 mutex_unlock(&cfile->fh_mutex); 1764 free_xid(xid); 1765 return 0; 1766 } 1767 if (reopen_aborted(tcon, aborts)) { 1768 mutex_unlock(&cfile->fh_mutex); 1769 free_xid(xid); 1770 return -EHOSTDOWN; 1771 } 1772 1773 inode = d_inode(cfile->dentry); 1774 cifs_sb = CIFS_SB(inode->i_sb); 1775 server = tcon->ses->server; 1776 1777 /* 1778 * Can not grab rename sem here because various ops, including those 1779 * that already have the rename sem can end up causing writepage to get 1780 * called and if the server was down that means we end up here, and we 1781 * can never tell if the caller already has the rename_sem. 1782 */ 1783 page = alloc_dentry_path(); 1784 full_path = build_path_from_dentry(cfile->dentry, page); 1785 if (IS_ERR(full_path)) { 1786 mutex_unlock(&cfile->fh_mutex); 1787 free_dentry_path(page); 1788 free_xid(xid); 1789 return PTR_ERR(full_path); 1790 } 1791 1792 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", 1793 inode, cfile->f_flags, full_path); 1794 1795 if (tcon->ses->server->oplocks) 1796 oplock = REQ_OPLOCK; 1797 else 1798 oplock = 0; 1799 1800 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1801 if (tcon->unix_ext && cap_unix(tcon->ses) && 1802 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 1803 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 1804 /* 1805 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the 1806 * original open. Must mask them off for a reopen. 1807 */ 1808 unsigned int oflags = cfile->f_flags & 1809 ~(O_CREAT | O_EXCL | O_TRUNC); 1810 1811 rc = cifs_posix_open(full_path, NULL, inode->i_sb, 1812 cifs_sb->ctx->file_mode /* ignored */, 1813 oflags, &oplock, &cfile->fid.netfid, xid); 1814 if (rc == 0) { 1815 cifs_dbg(FYI, "posix reopen succeeded\n"); 1816 oparms.reconnect = true; 1817 goto reopen_success; 1818 } 1819 /* 1820 * fallthrough to retry open the old way on errors, especially 1821 * in the reconnect path it is important to retry hard 1822 */ 1823 } 1824 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1825 1826 /* If we're caching, we need to be able to fill in around partial writes. */ 1827 if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY) 1828 rdwr_for_fscache = 1; 1829 1830 if (cfile->reopen_replay && rdwr_for_fscache == 1 && 1831 !(cfile->reopen_access & GENERIC_READ)) 1832 rdwr_for_fscache = 2; 1833 1834 desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache); 1835 create_options |= cifs_open_create_options(cfile->f_flags, 1836 create_options); 1837 1838 if (server->ops->get_lease_key) 1839 server->ops->get_lease_key(inode, &cfile->fid); 1840 1841 retry_open: 1842 fresh = cfile->reopen_fresh; 1843 oparms = (struct cifs_open_parms) { 1844 .tcon = tcon, 1845 .cifs_sb = cifs_sb, 1846 .desired_access = cfile->reopen_replay ? 1847 cfile->reopen_access : desired_access, 1848 .create_options = cifs_create_options(cifs_sb, create_options), 1849 .disposition = disposition, 1850 .path = full_path, 1851 .fid = &cfile->fid, 1852 .reconnect = !fresh, 1853 .replay = cfile->reopen_replay, 1854 .pending = cfile->reopen_replay, 1855 .no_replay = !!(flags & CIFS_REOPEN_BOUND), 1856 .no_reconnect_wait = !!(flags & CIFS_REOPEN_NOWAIT), 1857 }; 1858 1859 /* 1860 * Can not refresh inode by passing in file_info buf to be returned by 1861 * ops->open and then calling get_inode_info with returned buf since 1862 * file might have write behind data that needs to be flushed and server 1863 * version of file size can be stale. If we knew for sure that inode was 1864 * not dirty locally we could do this. 1865 */ 1866 if (reopen_aborted(tcon, aborts)) 1867 rc = -EHOSTDOWN; 1868 else 1869 rc = server->ops->open(xid, &oparms, &oplock, NULL); 1870 if (rc == -ENOENT && !fresh && !oparms.reconnect && !oparms.pending) { 1871 /* durable handle timeout is expired - open the file again */ 1872 fresh = true; 1873 oparms.desired_access = desired_access; 1874 oparms.replay = false; 1875 if (reopen_aborted(tcon, aborts)) 1876 rc = -EHOSTDOWN; 1877 else 1878 rc = server->ops->open(xid, &oparms, &oplock, NULL); 1879 } 1880 cfile->reopen_fresh = rc && fresh; 1881 cfile->reopen_replay = rc && oparms.pending; 1882 if (cfile->reopen_replay) 1883 cfile->reopen_access = oparms.desired_access; 1884 /* indicate that we need to relock the file */ 1885 if (fresh) 1886 oparms.reconnect = true; 1887 if (rc == -EACCES && rdwr_for_fscache == 1 && !oparms.pending) { 1888 desired_access = cifs_convert_flags(cfile->f_flags, 0); 1889 rdwr_for_fscache = 2; 1890 goto retry_open; 1891 } 1892 1893 if (rc) { 1894 mutex_unlock(&cfile->fh_mutex); 1895 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); 1896 cifs_dbg(FYI, "oplock: %d\n", oplock); 1897 goto reopen_error_exit; 1898 } 1899 1900 if (rdwr_for_fscache == 2) 1901 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 1902 1903 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1904 reopen_success: 1905 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1906 /* pairs with smp_load_acquire() in __cifs_issue_write() */ 1907 smp_store_release(&cfile->reopens, cfile->reopens + 1); 1908 cfile->invalidHandle = false; 1909 mutex_unlock(&cfile->fh_mutex); 1910 cinode = CIFS_I(inode); 1911 1912 if (can_flush) { 1913 rc = filemap_write_and_wait(inode->i_mapping); 1914 if (!is_interrupt_error(rc)) 1915 mapping_set_error(inode->i_mapping, rc); 1916 1917 if (tcon->posix_extensions) { 1918 rc = smb311_posix_get_inode_info(&inode, full_path, 1919 NULL, inode->i_sb, xid); 1920 } else if (tcon->unix_ext) { 1921 rc = cifs_get_inode_info_unix(&inode, full_path, 1922 inode->i_sb, xid); 1923 } else { 1924 rc = cifs_get_inode_info(&inode, full_path, NULL, 1925 inode->i_sb, xid, NULL); 1926 } 1927 } 1928 /* 1929 * Else we are writing out data to server already and could deadlock if 1930 * we tried to flush data, and since we do not know if we have data that 1931 * would invalidate the current end of file on the server we can not go 1932 * to the server to get the new inode info. 1933 */ 1934 1935 /* 1936 * If the server returned a read oplock and we have mandatory brlocks, 1937 * set oplock level to None. 1938 */ 1939 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 1940 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 1941 oplock = 0; 1942 } 1943 1944 scoped_guard(spinlock, &cinode->open_file_lock) 1945 server->ops->set_fid(cfile, &cfile->fid, oplock); 1946 if (oparms.reconnect) 1947 cifs_relock_file(cfile); 1948 1949 reopen_error_exit: 1950 free_dentry_path(page); 1951 free_xid(xid); 1952 return rc; 1953 } 1954 1955 void smb2_deferred_work_close(struct work_struct *work) 1956 { 1957 struct cifsFileInfo *cfile = container_of(work, 1958 struct cifsFileInfo, deferred.work); 1959 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1960 1961 spin_lock(&cinode->deferred_lock); 1962 cifs_del_deferred_close(cfile); 1963 cfile->deferred_close_scheduled = false; 1964 spin_unlock(&cinode->deferred_lock); 1965 _cifsFileInfo_put(cfile, true, false); 1966 } 1967 1968 static bool 1969 smb2_can_defer_close(struct inode *inode, struct cifs_deferred_close *dclose) 1970 { 1971 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1972 struct cifsInodeInfo *cinode = CIFS_I(inode); 1973 unsigned int oplock = READ_ONCE(cinode->oplock); 1974 1975 return cifs_sb->ctx->closetimeo && cinode->lease_granted && dclose && 1976 (oplock == CIFS_CACHE_RHW_FLG || oplock == CIFS_CACHE_RH_FLG) && 1977 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags); 1978 1979 } 1980 1981 int cifs_close(struct inode *inode, struct file *file) 1982 { 1983 struct cifsFileInfo *cfile; 1984 struct cifsInodeInfo *cinode = CIFS_I(inode); 1985 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1986 struct cifs_deferred_close *dclose; 1987 struct cifs_tcon *tcon; 1988 1989 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE); 1990 1991 if (file->private_data != NULL) { 1992 cfile = file->private_data; 1993 file->private_data = NULL; 1994 dclose = kmalloc_obj(struct cifs_deferred_close); 1995 if ((cfile->status_file_deleted == false) && 1996 (smb2_can_defer_close(inode, dclose))) { 1997 if (test_and_clear_bit(NETFS_ICTX_MODIFIED_ATTR, &cinode->netfs.flags)) { 1998 inode_set_mtime_to_ts(inode, 1999 inode_set_ctime_current(inode)); 2000 } 2001 spin_lock(&cinode->deferred_lock); 2002 cifs_add_deferred_close(cfile, dclose); 2003 if (cfile->deferred_close_scheduled && 2004 delayed_work_pending(&cfile->deferred)) { 2005 /* 2006 * If there is no pending work, mod_delayed_work queues new work. 2007 * So, Increase the ref count to avoid use-after-free. 2008 */ 2009 if (!mod_delayed_work(deferredclose_wq, 2010 &cfile->deferred, cifs_sb->ctx->closetimeo)) 2011 cifsFileInfo_get(cfile); 2012 } else { 2013 /* Deferred close for files */ 2014 tcon = tlink_tcon(cfile->tlink); 2015 trace_smb3_close_cached(tcon->tid, tcon->ses->Suid, 2016 cfile->fid.persistent_fid, 2017 cifs_sb->ctx->closetimeo); 2018 /* 2019 * Each queued execution owns one reference. 2020 * If nothing was queued, the reference of 2021 * the closing file is dropped below. 2022 */ 2023 if (queue_delayed_work(deferredclose_wq, 2024 &cfile->deferred, 2025 cifs_sb->ctx->closetimeo)) { 2026 cfile->deferred_close_scheduled = true; 2027 spin_unlock(&cinode->deferred_lock); 2028 return 0; 2029 } 2030 } 2031 spin_unlock(&cinode->deferred_lock); 2032 _cifsFileInfo_put(cfile, true, false); 2033 } else { 2034 _cifsFileInfo_put(cfile, true, false); 2035 kfree(dclose); 2036 } 2037 } 2038 2039 /* return code from the ->release op is always ignored */ 2040 return 0; 2041 } 2042 2043 bool 2044 cifs_reopen_persistent_handles(struct cifs_tcon *tcon) 2045 { 2046 struct cifsFileInfo *open_file, *tmp; 2047 struct cifs_sb_info *cifs_sb; 2048 LIST_HEAD(tmp_list); 2049 bool retry = false; 2050 int rc; 2051 2052 if (!tcon->use_persistent || !tcon->need_reopen_files) 2053 return false; 2054 2055 tcon->need_reopen_files = false; 2056 2057 cifs_dbg(FYI, "Reopen persistent handles\n"); 2058 2059 /* list all files open on tree connection, reopen resilient handles */ 2060 spin_lock(&tcon->open_file_lock); 2061 list_for_each_entry(open_file, &tcon->openFileList, tlist) { 2062 if (!open_file->invalidHandle) 2063 continue; 2064 cifs_sb = CIFS_SB(open_file->dentry); 2065 cifsFileInfo_get(open_file); 2066 atomic_inc(&cifs_sb->outstanding_rreq); 2067 list_add_tail(&open_file->rlist, &tmp_list); 2068 } 2069 spin_unlock(&tcon->open_file_lock); 2070 2071 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) { 2072 cifs_sb = CIFS_SB(open_file->dentry); 2073 /* do not flush */ 2074 rc = cifs_reopen_file(open_file, false, 2075 CIFS_REOPEN_BOUND | CIFS_REOPEN_NOWAIT); 2076 if (rc) { 2077 tcon->need_reopen_files = true; 2078 retry |= is_replayable_error(rc); 2079 } 2080 list_del_init(&open_file->rlist); 2081 cifsFileInfo_put(open_file); 2082 if (atomic_dec_and_test(&cifs_sb->outstanding_rreq)) 2083 wake_up_var(&cifs_sb->outstanding_rreq); 2084 } 2085 return retry; 2086 } 2087 2088 int cifs_closedir(struct inode *inode, struct file *file) 2089 { 2090 int rc = 0; 2091 unsigned int xid; 2092 struct cifsFileInfo *cfile = file->private_data; 2093 struct cifs_tcon *tcon; 2094 struct TCP_Server_Info *server; 2095 char *buf; 2096 2097 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); 2098 2099 if (cfile == NULL) 2100 return rc; 2101 2102 xid = get_xid(); 2103 tcon = tlink_tcon(cfile->tlink); 2104 server = tcon->ses->server; 2105 2106 cifs_dbg(FYI, "Freeing private data in close dir\n"); 2107 spin_lock(&cfile->file_info_lock); 2108 if (server->ops->dir_needs_close(cfile)) { 2109 cfile->invalidHandle = true; 2110 spin_unlock(&cfile->file_info_lock); 2111 if (server->ops->close_dir) 2112 rc = server->ops->close_dir(xid, tcon, &cfile->fid); 2113 else 2114 rc = -ENOSYS; 2115 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); 2116 /* not much we can do if it fails anyway, ignore rc */ 2117 rc = 0; 2118 } else 2119 spin_unlock(&cfile->file_info_lock); 2120 2121 buf = cfile->srch_inf.ntwrk_buf_start; 2122 if (buf) { 2123 cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); 2124 cfile->srch_inf.ntwrk_buf_start = NULL; 2125 if (cfile->srch_inf.smallBuf) 2126 cifs_small_buf_release(buf); 2127 else if (cfile->srch_inf.is_dynamic_buf) 2128 kfree(buf); 2129 else 2130 cifs_buf_release(buf); 2131 } 2132 2133 cifs_put_tlink(cfile->tlink); 2134 kfree(file->private_data); 2135 file->private_data = NULL; 2136 /* BB can we lock the filestruct while this is going on? */ 2137 free_xid(xid); 2138 return rc; 2139 } 2140 2141 static struct cifsLockInfo * 2142 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags) 2143 { 2144 struct cifsLockInfo *lock = 2145 kmalloc_obj(struct cifsLockInfo); 2146 if (!lock) 2147 return lock; 2148 lock->offset = offset; 2149 lock->length = length; 2150 lock->type = type; 2151 lock->pid = current->tgid; 2152 lock->flags = flags; 2153 INIT_LIST_HEAD(&lock->blist); 2154 init_waitqueue_head(&lock->block_q); 2155 return lock; 2156 } 2157 2158 void 2159 cifs_del_lock_waiters(struct cifsLockInfo *lock) 2160 { 2161 struct cifsLockInfo *li, *tmp; 2162 list_for_each_entry_safe(li, tmp, &lock->blist, blist) { 2163 list_del_init(&li->blist); 2164 wake_up(&li->block_q); 2165 } 2166 } 2167 2168 #define CIFS_LOCK_OP 0 2169 #define CIFS_READ_OP 1 2170 #define CIFS_WRITE_OP 2 2171 2172 /* @rw_check : 0 - no op, 1 - read, 2 - write */ 2173 static bool 2174 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, 2175 __u64 length, __u8 type, __u16 flags, 2176 struct cifsFileInfo *cfile, 2177 struct cifsLockInfo **conf_lock, int rw_check) 2178 { 2179 struct cifsLockInfo *li; 2180 struct cifsFileInfo *cur_cfile = fdlocks->cfile; 2181 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 2182 2183 list_for_each_entry(li, &fdlocks->locks, llist) { 2184 if (offset + length <= li->offset || 2185 offset >= li->offset + li->length) 2186 continue; 2187 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && 2188 server->ops->compare_fids(cfile, cur_cfile)) { 2189 /* shared lock prevents write op through the same fid */ 2190 if (!(li->type & server->vals->shared_lock_type) || 2191 rw_check != CIFS_WRITE_OP) 2192 continue; 2193 } 2194 if ((type & server->vals->shared_lock_type) && 2195 ((server->ops->compare_fids(cfile, cur_cfile) && 2196 current->tgid == li->pid) || type == li->type)) 2197 continue; 2198 if (rw_check == CIFS_LOCK_OP && 2199 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) && 2200 server->ops->compare_fids(cfile, cur_cfile)) 2201 continue; 2202 if (conf_lock) 2203 *conf_lock = li; 2204 trace_smb3_lock_conflict(cfile->fid.persistent_fid, 2205 offset, length, type, 2206 li->offset, li->length, li->type, li->pid); 2207 return true; 2208 } 2209 return false; 2210 } 2211 2212 bool 2213 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 2214 __u8 type, __u16 flags, 2215 struct cifsLockInfo **conf_lock, int rw_check) 2216 { 2217 bool rc = false; 2218 struct cifs_fid_locks *cur; 2219 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2220 2221 list_for_each_entry(cur, &cinode->llist, llist) { 2222 rc = cifs_find_fid_lock_conflict(cur, offset, length, type, 2223 flags, cfile, conf_lock, 2224 rw_check); 2225 if (rc) 2226 break; 2227 } 2228 2229 return rc; 2230 } 2231 2232 /* 2233 * Check if there is another lock that prevents us to set the lock (mandatory 2234 * style). If such a lock exists, update the flock structure with its 2235 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 2236 * or leave it the same if we can't. Returns 0 if we don't need to request to 2237 * the server or 1 otherwise. 2238 */ 2239 static int 2240 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 2241 __u8 type, struct file_lock *flock) 2242 { 2243 int rc = 0; 2244 struct cifsLockInfo *conf_lock; 2245 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2246 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 2247 bool exist; 2248 2249 down_read(&cinode->lock_sem); 2250 2251 exist = cifs_find_lock_conflict(cfile, offset, length, type, 2252 flock->c.flc_flags, &conf_lock, 2253 CIFS_LOCK_OP); 2254 if (exist) { 2255 flock->fl_start = conf_lock->offset; 2256 flock->fl_end = conf_lock->offset + conf_lock->length - 1; 2257 flock->c.flc_pid = conf_lock->pid; 2258 if (conf_lock->type & server->vals->shared_lock_type) 2259 flock->c.flc_type = F_RDLCK; 2260 else 2261 flock->c.flc_type = F_WRLCK; 2262 } else if (!cinode->can_cache_brlcks) 2263 rc = 1; 2264 else 2265 flock->c.flc_type = F_UNLCK; 2266 2267 up_read(&cinode->lock_sem); 2268 return rc; 2269 } 2270 2271 static void 2272 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) 2273 { 2274 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2275 cifs_down_write(&cinode->lock_sem); 2276 list_add_tail(&lock->llist, &cfile->llist->locks); 2277 up_write(&cinode->lock_sem); 2278 } 2279 2280 /* 2281 * Set the byte-range lock (mandatory style). Returns: 2282 * 1) 0, if we set the lock and don't need to request to the server; 2283 * 2) 1, if no locks prevent us but we need to request to the server; 2284 * 3) -EACCES, if there is a lock that prevents us and wait is false. 2285 */ 2286 static int 2287 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, 2288 bool wait, unsigned int xid) 2289 { 2290 struct cifsLockInfo *conf_lock; 2291 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2292 bool exist; 2293 int rc = 0; 2294 2295 try_again: 2296 exist = false; 2297 cifs_down_write(&cinode->lock_sem); 2298 2299 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, 2300 lock->type, lock->flags, &conf_lock, 2301 CIFS_LOCK_OP); 2302 if (!exist && cinode->can_cache_brlcks) { 2303 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2304 2305 list_add_tail(&lock->llist, &cfile->llist->locks); 2306 trace_smb3_lock_cached(xid, cfile->fid.persistent_fid, 2307 tcon->tid, tcon->ses->Suid, 2308 lock->offset, lock->length, 2309 lock->type, 1, 0); 2310 up_write(&cinode->lock_sem); 2311 return rc; 2312 } 2313 2314 if (!exist) 2315 rc = 1; 2316 else if (!wait) 2317 rc = -EACCES; 2318 else { 2319 list_add_tail(&lock->blist, &conf_lock->blist); 2320 up_write(&cinode->lock_sem); 2321 rc = wait_event_interruptible(lock->block_q, 2322 (lock->blist.prev == &lock->blist) && 2323 (lock->blist.next == &lock->blist)); 2324 if (!rc) 2325 goto try_again; 2326 cifs_down_write(&cinode->lock_sem); 2327 list_del_init(&lock->blist); 2328 } 2329 2330 up_write(&cinode->lock_sem); 2331 return rc; 2332 } 2333 2334 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2335 /* 2336 * Check if there is another lock that prevents us to set the lock (posix 2337 * style). If such a lock exists, update the flock structure with its 2338 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 2339 * or leave it the same if we can't. Returns 0 if we don't need to request to 2340 * the server or 1 otherwise. 2341 */ 2342 static int 2343 cifs_posix_lock_test(struct file *file, struct file_lock *flock) 2344 { 2345 int rc = 0; 2346 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 2347 unsigned char saved_type = flock->c.flc_type; 2348 2349 if ((flock->c.flc_flags & FL_POSIX) == 0) 2350 return 1; 2351 2352 down_read(&cinode->lock_sem); 2353 posix_test_lock(file, flock); 2354 2355 if (lock_is_unlock(flock) && !cinode->can_cache_brlcks) { 2356 flock->c.flc_type = saved_type; 2357 rc = 1; 2358 } 2359 2360 up_read(&cinode->lock_sem); 2361 return rc; 2362 } 2363 2364 /* 2365 * Set the byte-range lock (posix style). Returns: 2366 * 1) <0, if the error occurs while setting the lock; 2367 * 2) 0, if we set the lock and don't need to request to the server; 2368 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock; 2369 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server. 2370 */ 2371 static int 2372 cifs_posix_lock_set(struct file *file, struct file_lock *flock) 2373 { 2374 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 2375 int rc = FILE_LOCK_DEFERRED + 1; 2376 2377 if ((flock->c.flc_flags & FL_POSIX) == 0) 2378 return rc; 2379 2380 cifs_down_write(&cinode->lock_sem); 2381 if (!cinode->can_cache_brlcks) { 2382 up_write(&cinode->lock_sem); 2383 return rc; 2384 } 2385 2386 rc = posix_lock_file(file, flock, NULL); 2387 up_write(&cinode->lock_sem); 2388 return rc; 2389 } 2390 2391 int 2392 cifs_push_mandatory_locks(struct cifsFileInfo *cfile) 2393 { 2394 unsigned int xid; 2395 int rc = 0, stored_rc; 2396 struct cifsLockInfo *li, *tmp; 2397 struct cifs_tcon *tcon; 2398 unsigned int num, max_num, max_buf; 2399 LOCKING_ANDX_RANGE *buf, *cur; 2400 static const int types[] = { 2401 LOCKING_ANDX_LARGE_FILES, 2402 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 2403 }; 2404 int i; 2405 2406 xid = get_xid(); 2407 tcon = tlink_tcon(cfile->tlink); 2408 2409 /* 2410 * Accessing maxBuf is racy with cifs_reconnect - need to store value 2411 * and check it before using. 2412 */ 2413 max_buf = tcon->ses->server->maxBuf; 2414 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { 2415 free_xid(xid); 2416 return -EINVAL; 2417 } 2418 2419 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 2420 PAGE_SIZE); 2421 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 2422 PAGE_SIZE); 2423 max_num = (max_buf - sizeof(struct smb_hdr)) / 2424 sizeof(LOCKING_ANDX_RANGE); 2425 buf = kzalloc_objs(LOCKING_ANDX_RANGE, max_num); 2426 if (!buf) { 2427 free_xid(xid); 2428 return -ENOMEM; 2429 } 2430 2431 for (i = 0; i < 2; i++) { 2432 cur = buf; 2433 num = 0; 2434 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 2435 if (li->type != types[i]) 2436 continue; 2437 cur->Pid = cpu_to_le16(li->pid); 2438 cur->LengthLow = cpu_to_le32((u32)li->length); 2439 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 2440 cur->OffsetLow = cpu_to_le32((u32)li->offset); 2441 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 2442 if (++num == max_num) { 2443 stored_rc = cifs_lockv(xid, tcon, 2444 cfile->fid.netfid, 2445 (__u8)li->type, 0, num, 2446 buf); 2447 if (stored_rc) 2448 rc = stored_rc; 2449 cur = buf; 2450 num = 0; 2451 } else 2452 cur++; 2453 } 2454 2455 if (num) { 2456 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 2457 (__u8)types[i], 0, num, buf); 2458 if (stored_rc) 2459 rc = stored_rc; 2460 } 2461 } 2462 2463 kfree(buf); 2464 free_xid(xid); 2465 return rc; 2466 } 2467 2468 static __u32 2469 hash_lockowner(fl_owner_t owner) 2470 { 2471 return cifs_lock_secret ^ hash32_ptr((const void *)owner); 2472 } 2473 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2474 2475 struct lock_to_push { 2476 struct list_head llist; 2477 __u64 offset; 2478 __u64 length; 2479 __u32 pid; 2480 __u16 netfid; 2481 __u8 type; 2482 }; 2483 2484 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2485 static int 2486 cifs_push_posix_locks(struct cifsFileInfo *cfile) 2487 { 2488 struct inode *inode = d_inode(cfile->dentry); 2489 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2490 struct file_lock *flock; 2491 struct file_lock_context *flctx = locks_inode_context(inode); 2492 unsigned int count = 0, i; 2493 int rc = 0, xid, type; 2494 struct list_head locks_to_send, *el; 2495 struct lock_to_push *lck, *tmp; 2496 __u64 length; 2497 2498 xid = get_xid(); 2499 2500 if (!flctx) 2501 goto out; 2502 2503 spin_lock(&flctx->flc_lock); 2504 list_for_each(el, &flctx->flc_posix) { 2505 count++; 2506 } 2507 spin_unlock(&flctx->flc_lock); 2508 2509 INIT_LIST_HEAD(&locks_to_send); 2510 2511 /* 2512 * Allocating count locks is enough because no FL_POSIX locks can be 2513 * added to the list while we are holding cinode->lock_sem that 2514 * protects locking operations of this inode. 2515 */ 2516 for (i = 0; i < count; i++) { 2517 lck = kmalloc_obj(struct lock_to_push); 2518 if (!lck) { 2519 rc = -ENOMEM; 2520 goto err_out; 2521 } 2522 list_add_tail(&lck->llist, &locks_to_send); 2523 } 2524 2525 el = locks_to_send.next; 2526 spin_lock(&flctx->flc_lock); 2527 for_each_file_lock(flock, &flctx->flc_posix) { 2528 unsigned char ftype = flock->c.flc_type; 2529 2530 if (el == &locks_to_send) { 2531 /* 2532 * The list ended. We don't have enough allocated 2533 * structures - something is really wrong. 2534 */ 2535 cifs_dbg(VFS, "Can't push all brlocks!\n"); 2536 break; 2537 } 2538 length = cifs_flock_len(flock); 2539 if (ftype == F_RDLCK || ftype == F_SHLCK) 2540 type = CIFS_RDLCK; 2541 else 2542 type = CIFS_WRLCK; 2543 lck = list_entry(el, struct lock_to_push, llist); 2544 lck->pid = hash_lockowner(flock->c.flc_owner); 2545 lck->netfid = cfile->fid.netfid; 2546 lck->length = length; 2547 lck->type = type; 2548 lck->offset = flock->fl_start; 2549 } 2550 spin_unlock(&flctx->flc_lock); 2551 2552 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 2553 int stored_rc; 2554 2555 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, 2556 lck->offset, lck->length, NULL, 2557 lck->type, 0); 2558 if (stored_rc) 2559 rc = stored_rc; 2560 list_del(&lck->llist); 2561 kfree(lck); 2562 } 2563 2564 out: 2565 free_xid(xid); 2566 return rc; 2567 err_out: 2568 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 2569 list_del(&lck->llist); 2570 kfree(lck); 2571 } 2572 goto out; 2573 } 2574 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2575 2576 static int 2577 cifs_push_locks(struct cifsFileInfo *cfile) 2578 { 2579 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2580 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2581 int rc = 0; 2582 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2583 struct cifs_sb_info *cifs_sb = CIFS_SB(cinode); 2584 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2585 2586 /* we are going to update can_cache_brlcks here - need a write access */ 2587 cifs_down_write(&cinode->lock_sem); 2588 if (!cinode->can_cache_brlcks) { 2589 up_write(&cinode->lock_sem); 2590 return rc; 2591 } 2592 2593 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2594 if (cap_unix(tcon->ses) && 2595 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2596 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2597 rc = cifs_push_posix_locks(cfile); 2598 else 2599 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2600 rc = tcon->ses->server->ops->push_mand_locks(cfile); 2601 2602 cinode->can_cache_brlcks = false; 2603 up_write(&cinode->lock_sem); 2604 return rc; 2605 } 2606 2607 static void 2608 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, 2609 bool *wait_flag, struct TCP_Server_Info *server) 2610 { 2611 if (flock->c.flc_flags & FL_POSIX) 2612 cifs_dbg(FYI, "Posix\n"); 2613 if (flock->c.flc_flags & FL_FLOCK) 2614 cifs_dbg(FYI, "Flock\n"); 2615 if (flock->c.flc_flags & FL_SLEEP) { 2616 cifs_dbg(FYI, "Blocking lock\n"); 2617 *wait_flag = true; 2618 } 2619 if (flock->c.flc_flags & FL_ACCESS) 2620 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); 2621 if (flock->c.flc_flags & FL_LEASE) 2622 cifs_dbg(FYI, "Lease on file - not implemented yet\n"); 2623 if (flock->c.flc_flags & 2624 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | 2625 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK))) 2626 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", 2627 flock->c.flc_flags); 2628 2629 *type = server->vals->large_lock_type; 2630 if (lock_is_write(flock)) { 2631 cifs_dbg(FYI, "F_WRLCK\n"); 2632 *type |= server->vals->exclusive_lock_type; 2633 *lock = 1; 2634 } else if (lock_is_unlock(flock)) { 2635 cifs_dbg(FYI, "F_UNLCK\n"); 2636 *type |= server->vals->unlock_lock_type; 2637 *unlock = 1; 2638 /* Check if unlock includes more than one lock range */ 2639 } else if (lock_is_read(flock)) { 2640 cifs_dbg(FYI, "F_RDLCK\n"); 2641 *type |= server->vals->shared_lock_type; 2642 *lock = 1; 2643 } else if (flock->c.flc_type == F_EXLCK) { 2644 cifs_dbg(FYI, "F_EXLCK\n"); 2645 *type |= server->vals->exclusive_lock_type; 2646 *lock = 1; 2647 } else if (flock->c.flc_type == F_SHLCK) { 2648 cifs_dbg(FYI, "F_SHLCK\n"); 2649 *type |= server->vals->shared_lock_type; 2650 *lock = 1; 2651 } else 2652 cifs_dbg(FYI, "Unknown type of lock\n"); 2653 } 2654 2655 static int 2656 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, 2657 bool wait_flag, bool posix_lck, unsigned int xid) 2658 { 2659 int rc = 0; 2660 __u64 length = cifs_flock_len(flock); 2661 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2662 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2663 struct TCP_Server_Info *server = tcon->ses->server; 2664 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2665 __u16 netfid = cfile->fid.netfid; 2666 2667 if (posix_lck) { 2668 int posix_lock_type; 2669 2670 rc = cifs_posix_lock_test(file, flock); 2671 if (!rc) 2672 return rc; 2673 2674 if (type & server->vals->shared_lock_type) 2675 posix_lock_type = CIFS_RDLCK; 2676 else 2677 posix_lock_type = CIFS_WRLCK; 2678 rc = CIFSSMBPosixLock(xid, tcon, netfid, 2679 hash_lockowner(flock->c.flc_owner), 2680 flock->fl_start, length, flock, 2681 posix_lock_type, wait_flag); 2682 return rc; 2683 } 2684 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2685 2686 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); 2687 if (!rc) 2688 return rc; 2689 2690 /* BB we could chain these into one lock request BB */ 2691 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, 2692 1, 0, false); 2693 if (rc == 0) { 2694 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2695 type, 0, 1, false); 2696 flock->c.flc_type = F_UNLCK; 2697 if (rc != 0) 2698 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 2699 rc); 2700 return 0; 2701 } 2702 2703 if (type & server->vals->shared_lock_type) { 2704 flock->c.flc_type = F_WRLCK; 2705 return 0; 2706 } 2707 2708 type &= ~server->vals->exclusive_lock_type; 2709 2710 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2711 type | server->vals->shared_lock_type, 2712 1, 0, false); 2713 if (rc == 0) { 2714 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2715 type | server->vals->shared_lock_type, 0, 1, false); 2716 flock->c.flc_type = F_RDLCK; 2717 if (rc != 0) 2718 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 2719 rc); 2720 } else 2721 flock->c.flc_type = F_WRLCK; 2722 2723 return 0; 2724 } 2725 2726 void 2727 cifs_move_llist(struct list_head *source, struct list_head *dest) 2728 { 2729 struct list_head *li, *tmp; 2730 list_for_each_safe(li, tmp, source) 2731 list_move(li, dest); 2732 } 2733 2734 int 2735 cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode, 2736 struct file *file) 2737 { 2738 struct cifsFileInfo *open_file = NULL; 2739 struct cifsInodeInfo *cinode = CIFS_I(inode); 2740 int rc = 0; 2741 2742 spin_lock(&tcon->open_file_lock); 2743 spin_lock(&cinode->open_file_lock); 2744 2745 list_for_each_entry(open_file, &cinode->openFileList, flist) { 2746 if (open_file->dentry != file_dentry(file)) { 2747 rc = -EINVAL; 2748 break; 2749 } 2750 } 2751 2752 spin_unlock(&cinode->open_file_lock); 2753 spin_unlock(&tcon->open_file_lock); 2754 return rc; 2755 } 2756 2757 void 2758 cifs_free_llist(struct list_head *llist) 2759 { 2760 struct cifsLockInfo *li, *tmp; 2761 list_for_each_entry_safe(li, tmp, llist, llist) { 2762 cifs_del_lock_waiters(li); 2763 list_del(&li->llist); 2764 kfree(li); 2765 } 2766 } 2767 2768 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2769 int 2770 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, 2771 unsigned int xid) 2772 { 2773 int rc = 0, stored_rc; 2774 static const int types[] = { 2775 LOCKING_ANDX_LARGE_FILES, 2776 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 2777 }; 2778 unsigned int i; 2779 unsigned int max_num, num, max_buf; 2780 LOCKING_ANDX_RANGE *buf, *cur; 2781 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2782 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2783 struct cifsLockInfo *li, *tmp; 2784 __u64 length = cifs_flock_len(flock); 2785 LIST_HEAD(tmp_llist); 2786 2787 /* 2788 * Accessing maxBuf is racy with cifs_reconnect - need to store value 2789 * and check it before using. 2790 */ 2791 max_buf = tcon->ses->server->maxBuf; 2792 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) 2793 return -EINVAL; 2794 2795 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 2796 PAGE_SIZE); 2797 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 2798 PAGE_SIZE); 2799 max_num = (max_buf - sizeof(struct smb_hdr)) / 2800 sizeof(LOCKING_ANDX_RANGE); 2801 buf = kzalloc_objs(LOCKING_ANDX_RANGE, max_num); 2802 if (!buf) 2803 return -ENOMEM; 2804 2805 cifs_down_write(&cinode->lock_sem); 2806 for (i = 0; i < 2; i++) { 2807 cur = buf; 2808 num = 0; 2809 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 2810 if (flock->fl_start > li->offset || 2811 (flock->fl_start + length) < 2812 (li->offset + li->length)) 2813 continue; 2814 if (current->tgid != li->pid) 2815 continue; 2816 if (types[i] != li->type) 2817 continue; 2818 if (cinode->can_cache_brlcks) { 2819 /* 2820 * We can cache brlock requests - simply remove 2821 * a lock from the file's list. 2822 */ 2823 list_del(&li->llist); 2824 cifs_del_lock_waiters(li); 2825 kfree(li); 2826 continue; 2827 } 2828 cur->Pid = cpu_to_le16(li->pid); 2829 cur->LengthLow = cpu_to_le32((u32)li->length); 2830 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 2831 cur->OffsetLow = cpu_to_le32((u32)li->offset); 2832 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 2833 /* 2834 * We need to save a lock here to let us add it again to 2835 * the file's list if the unlock range request fails on 2836 * the server. 2837 */ 2838 list_move(&li->llist, &tmp_llist); 2839 if (++num == max_num) { 2840 stored_rc = cifs_lockv(xid, tcon, 2841 cfile->fid.netfid, 2842 li->type, num, 0, buf); 2843 if (stored_rc) { 2844 /* 2845 * We failed on the unlock range 2846 * request - add all locks from the tmp 2847 * list to the head of the file's list. 2848 */ 2849 cifs_move_llist(&tmp_llist, 2850 &cfile->llist->locks); 2851 rc = stored_rc; 2852 } else 2853 /* 2854 * The unlock range request succeed - 2855 * free the tmp list. 2856 */ 2857 cifs_free_llist(&tmp_llist); 2858 cur = buf; 2859 num = 0; 2860 } else 2861 cur++; 2862 } 2863 if (num) { 2864 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 2865 types[i], num, 0, buf); 2866 if (stored_rc) { 2867 cifs_move_llist(&tmp_llist, 2868 &cfile->llist->locks); 2869 rc = stored_rc; 2870 } else 2871 cifs_free_llist(&tmp_llist); 2872 } 2873 } 2874 2875 up_write(&cinode->lock_sem); 2876 kfree(buf); 2877 return rc; 2878 } 2879 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2880 2881 static int 2882 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, 2883 bool wait_flag, bool posix_lck, int lock, int unlock, 2884 unsigned int xid) 2885 { 2886 int rc = 0; 2887 __u64 length = cifs_flock_len(flock); 2888 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2889 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2890 struct TCP_Server_Info *server = tcon->ses->server; 2891 struct inode *inode = d_inode(cfile->dentry); 2892 2893 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2894 if (posix_lck) { 2895 int posix_lock_type; 2896 2897 rc = cifs_posix_lock_set(file, flock); 2898 if (rc <= FILE_LOCK_DEFERRED) 2899 return rc; 2900 2901 if (type & server->vals->shared_lock_type) 2902 posix_lock_type = CIFS_RDLCK; 2903 else 2904 posix_lock_type = CIFS_WRLCK; 2905 2906 if (unlock == 1) 2907 posix_lock_type = CIFS_UNLCK; 2908 2909 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, 2910 hash_lockowner(flock->c.flc_owner), 2911 flock->fl_start, length, 2912 NULL, posix_lock_type, wait_flag); 2913 goto out; 2914 } 2915 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2916 if (lock) { 2917 struct cifsLockInfo *lock; 2918 2919 lock = cifs_lock_init(flock->fl_start, length, type, 2920 flock->c.flc_flags); 2921 if (!lock) 2922 return -ENOMEM; 2923 2924 rc = cifs_lock_add_if(cfile, lock, wait_flag, xid); 2925 if (rc < 0) { 2926 kfree(lock); 2927 return rc; 2928 } 2929 if (!rc) 2930 goto out; 2931 2932 /* 2933 * Windows 7 server can delay breaking lease from read to None 2934 * if we set a byte-range lock on a file - break it explicitly 2935 * before sending the lock to the server to be sure the next 2936 * read won't conflict with non-overlapted locks due to 2937 * pagereading. 2938 */ 2939 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && 2940 CIFS_CACHE_READ(CIFS_I(inode))) { 2941 cifs_zap_mapping(inode); 2942 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", 2943 inode); 2944 cifs_reset_oplock(CIFS_I(inode)); 2945 } 2946 2947 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2948 type, 1, 0, wait_flag); 2949 if (rc) { 2950 kfree(lock); 2951 return rc; 2952 } 2953 2954 cifs_lock_add(cfile, lock); 2955 } else if (unlock) 2956 rc = server->ops->mand_unlock_range(cfile, flock, xid); 2957 2958 out: 2959 if ((flock->c.flc_flags & FL_POSIX) || (flock->c.flc_flags & FL_FLOCK)) { 2960 /* 2961 * If this is a request to remove all locks because we 2962 * are closing the file, it doesn't matter if the 2963 * unlocking failed as both cifs.ko and the SMB server 2964 * remove the lock on file close 2965 */ 2966 if (rc) { 2967 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); 2968 if (!(flock->c.flc_flags & FL_CLOSE)) 2969 return rc; 2970 } 2971 rc = locks_lock_file_wait(file, flock); 2972 } 2973 return rc; 2974 } 2975 2976 int cifs_flock(struct file *file, int cmd, struct file_lock *fl) 2977 { 2978 int rc, xid; 2979 int lock = 0, unlock = 0; 2980 bool wait_flag = false; 2981 bool posix_lck = false; 2982 struct cifs_sb_info *cifs_sb; 2983 struct cifs_tcon *tcon; 2984 struct cifsFileInfo *cfile; 2985 __u32 type; 2986 2987 xid = get_xid(); 2988 2989 if (!(fl->c.flc_flags & FL_FLOCK)) { 2990 rc = -ENOLCK; 2991 free_xid(xid); 2992 return rc; 2993 } 2994 2995 cfile = (struct cifsFileInfo *)file->private_data; 2996 tcon = tlink_tcon(cfile->tlink); 2997 2998 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag, 2999 tcon->ses->server); 3000 cifs_sb = CIFS_SB(file); 3001 3002 if (cap_unix(tcon->ses) && 3003 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 3004 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0)) 3005 posix_lck = true; 3006 3007 if (!lock && !unlock) { 3008 /* 3009 * if no lock or unlock then nothing to do since we do not 3010 * know what it is 3011 */ 3012 rc = -EOPNOTSUPP; 3013 free_xid(xid); 3014 return rc; 3015 } 3016 3017 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock, 3018 xid); 3019 free_xid(xid); 3020 return rc; 3021 3022 3023 } 3024 3025 int cifs_lock(struct file *file, int cmd, struct file_lock *flock) 3026 { 3027 struct cifs_sb_info *cifs_sb = CIFS_SB(file); 3028 struct cifsFileInfo *cfile; 3029 int lock = 0, unlock = 0; 3030 bool wait_flag = false; 3031 bool posix_lck = false; 3032 struct cifs_tcon *tcon; 3033 __u32 type; 3034 int rc, xid; 3035 3036 rc = -EACCES; 3037 xid = get_xid(); 3038 3039 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd, 3040 flock->c.flc_flags, flock->c.flc_type, 3041 (long long)flock->fl_start, 3042 (long long)flock->fl_end); 3043 3044 cfile = (struct cifsFileInfo *)file->private_data; 3045 tcon = tlink_tcon(cfile->tlink); 3046 3047 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, 3048 tcon->ses->server); 3049 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags); 3050 3051 if (cap_unix(tcon->ses) && 3052 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 3053 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0)) 3054 posix_lck = true; 3055 /* 3056 * BB add code here to normalize offset and length to account for 3057 * negative length which we can not accept over the wire. 3058 */ 3059 if (IS_GETLK(cmd)) { 3060 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); 3061 free_xid(xid); 3062 return rc; 3063 } 3064 3065 if (!lock && !unlock) { 3066 /* 3067 * if no lock or unlock then nothing to do since we do not 3068 * know what it is 3069 */ 3070 free_xid(xid); 3071 return -EOPNOTSUPP; 3072 } 3073 3074 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, 3075 xid); 3076 free_xid(xid); 3077 return rc; 3078 } 3079 3080 static void cifs_update_i_blocks_for_write(struct inode *inode, loff_t start, 3081 loff_t end) 3082 { 3083 struct cifsInodeInfo *cinode = CIFS_I(inode); 3084 u64 allocated_end = CIFS_INO_BYTES(inode->i_blocks); 3085 u64 blocks; 3086 3087 if (cinode->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) 3088 return; 3089 3090 /* 3091 * Grow the local estimate only across the currently known allocated 3092 * prefix. A write beyond that may leave a hole. 3093 */ 3094 if ((u64)start > allocated_end) 3095 return; 3096 3097 blocks = CIFS_INO_BLOCKS(end); 3098 if ((u64)inode->i_blocks < blocks) 3099 inode->i_blocks = blocks; 3100 } 3101 3102 static void cifs_update_i_blocks_after_write(struct kiocb *iocb, 3103 ssize_t written) 3104 { 3105 struct inode *inode = file_inode(iocb->ki_filp); 3106 loff_t end = iocb->ki_pos; 3107 3108 if (written <= 0) 3109 return; 3110 3111 spin_lock(&inode->i_lock); 3112 cifs_update_i_blocks_for_write(inode, end - written, end); 3113 spin_unlock(&inode->i_lock); 3114 } 3115 3116 void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata, ssize_t result) 3117 { 3118 struct netfs_io_request *wreq = wdata->rreq; 3119 struct inode *inode = wreq->inode; 3120 struct netfs_inode *ictx = netfs_inode(inode); 3121 loff_t wrend; 3122 3123 if (result > 0) { 3124 spin_lock(&inode->i_lock); 3125 3126 wrend = wdata->subreq.start + wdata->subreq.transferred + result; 3127 3128 if (wrend > ictx->_zero_point && 3129 (wdata->rreq->origin == NETFS_UNBUFFERED_WRITE || 3130 wdata->rreq->origin == NETFS_DIO_WRITE)) 3131 netfs_write_zero_point(inode, wrend); 3132 if (wrend > ictx->_remote_i_size) 3133 netfs_resize_file(ictx, wrend, true); 3134 cifs_update_i_blocks_for_write(inode, wdata->subreq.start, 3135 wrend); 3136 3137 spin_unlock(&inode->i_lock); 3138 } 3139 3140 if (wreq->origin == NETFS_WRITEBACK && 3141 !test_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags) && 3142 cifs_writeback_transient(result) && cifs_io_should_replay(wdata)) { 3143 if (result == -EBADF) 3144 cifs_mark_bad_handle(wdata->req, wdata->reopens); 3145 wdata->replay = true; 3146 trace_netfs_sreq(&wdata->subreq, 3147 netfs_sreq_trace_io_retry_needed); 3148 __set_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags); 3149 } 3150 3151 if (result < 0 && 3152 !test_bit(NETFS_SREQ_NEED_RETRY, &wdata->subreq.flags)) 3153 result = cifs_writeback_error(&wdata->subreq, result); 3154 netfs_write_subrequest_terminated(&wdata->subreq, result); 3155 } 3156 3157 static bool open_flags_match(struct cifsInodeInfo *cinode, 3158 unsigned int oflags, unsigned int cflags) 3159 { 3160 struct inode *inode = &cinode->netfs.inode; 3161 int crw = 0, orw = 0; 3162 3163 oflags &= ~(O_CREAT | O_EXCL | O_TRUNC); 3164 cflags &= ~(O_CREAT | O_EXCL | O_TRUNC); 3165 3166 if (cifs_fscache_enabled(inode)) { 3167 if (OPEN_FMODE(cflags) & FMODE_WRITE) 3168 crw = 1; 3169 if (OPEN_FMODE(oflags) & FMODE_WRITE) 3170 orw = 1; 3171 } 3172 if (cifs_convert_flags(oflags, orw) != cifs_convert_flags(cflags, crw)) 3173 return false; 3174 3175 return (oflags & (O_SYNC | O_DIRECT)) == (cflags & (O_SYNC | O_DIRECT)); 3176 } 3177 3178 struct cifsFileInfo *__find_readable_file(struct cifsInodeInfo *cifs_inode, 3179 unsigned int find_flags, 3180 unsigned int open_flags) 3181 { 3182 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode); 3183 bool fsuid_only = find_flags & FIND_FSUID_ONLY; 3184 struct cifsFileInfo *open_file = NULL; 3185 3186 /* only filter by fsuid on multiuser mounts */ 3187 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MULTIUSER)) 3188 fsuid_only = false; 3189 3190 spin_lock(&cifs_inode->open_file_lock); 3191 /* we could simply get the first_list_entry since write-only entries 3192 are always at the end of the list but since the first entry might 3193 have a close pending, we go through the whole list */ 3194 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 3195 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 3196 continue; 3197 if ((find_flags & FIND_NO_PENDING_DELETE) && 3198 open_file->status_file_deleted) 3199 continue; 3200 if ((find_flags & FIND_OPEN_FLAGS) && 3201 !open_flags_match(cifs_inode, open_flags, 3202 open_file->f_flags)) 3203 continue; 3204 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { 3205 if ((!open_file->invalidHandle)) { 3206 /* found a good file */ 3207 /* lock it so it will not be closed on us */ 3208 cifsFileInfo_get(open_file); 3209 spin_unlock(&cifs_inode->open_file_lock); 3210 return open_file; 3211 } /* else might as well continue, and look for 3212 another, or simply have the caller reopen it 3213 again rather than trying to fix this handle */ 3214 } else /* write only file */ 3215 break; /* write only files are last so must be done */ 3216 } 3217 spin_unlock(&cifs_inode->open_file_lock); 3218 return NULL; 3219 } 3220 3221 /* Return -EBADF if no handle is found and general rc otherwise */ 3222 int __cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, 3223 unsigned int find_flags, unsigned int open_flags, 3224 struct cifsFileInfo **ret_file) 3225 { 3226 struct cifsFileInfo *open_file, *inv_file = NULL; 3227 bool fsuid_only, with_delete; 3228 struct cifs_sb_info *cifs_sb; 3229 bool any_available = false; 3230 unsigned int refind = 0; 3231 *ret_file = NULL; 3232 int rc = -EBADF; 3233 3234 /* 3235 * Having a null inode here (because mapping->host was set to zero by 3236 * the VFS or MM) should not happen but we had reports of on oops (due 3237 * to it being zero) during stress testcases so we need to check for it 3238 */ 3239 3240 if (cifs_inode == NULL) { 3241 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); 3242 dump_stack(); 3243 return rc; 3244 } 3245 3246 if (test_bit(CIFS_INO_TMPFILE, &cifs_inode->flags)) 3247 find_flags &= FIND_NO_REOPEN | FIND_NO_DIRECT; 3248 3249 cifs_sb = CIFS_SB(cifs_inode); 3250 3251 with_delete = find_flags & FIND_WITH_DELETE; 3252 fsuid_only = find_flags & FIND_FSUID_ONLY; 3253 /* only filter by fsuid on multiuser mounts */ 3254 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MULTIUSER)) 3255 fsuid_only = false; 3256 3257 spin_lock(&cifs_inode->open_file_lock); 3258 refind_writable: 3259 if (refind > MAX_REOPEN_ATT) { 3260 spin_unlock(&cifs_inode->open_file_lock); 3261 return rc; 3262 } 3263 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 3264 if (!any_available && open_file->pid != current->tgid) 3265 continue; 3266 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 3267 continue; 3268 if (with_delete && !(open_file->fid.access & DELETE)) 3269 continue; 3270 if ((find_flags & FIND_NO_PENDING_DELETE) && 3271 open_file->status_file_deleted) 3272 continue; 3273 if ((find_flags & FIND_OPEN_FLAGS) && 3274 !open_flags_match(cifs_inode, open_flags, 3275 open_file->f_flags)) 3276 continue; 3277 if ((find_flags & FIND_NO_DIRECT) && 3278 (open_file->f_flags & O_DIRECT)) 3279 continue; 3280 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 3281 if (!open_file->invalidHandle) { 3282 /* found a good writable file */ 3283 cifsFileInfo_get(open_file); 3284 spin_unlock(&cifs_inode->open_file_lock); 3285 *ret_file = open_file; 3286 return 0; 3287 } else { 3288 if (!inv_file) 3289 inv_file = open_file; 3290 } 3291 } 3292 } 3293 /* couldn't find usable FH with same pid, try any available */ 3294 if (!any_available) { 3295 any_available = true; 3296 goto refind_writable; 3297 } 3298 3299 if (inv_file) { 3300 any_available = false; 3301 cifsFileInfo_get(inv_file); 3302 } 3303 3304 spin_unlock(&cifs_inode->open_file_lock); 3305 3306 if (inv_file) { 3307 if (find_flags & FIND_NO_REOPEN) { 3308 *ret_file = inv_file; 3309 return 0; 3310 } 3311 rc = cifs_reopen_file(inv_file, false, 0); 3312 if (!rc) { 3313 *ret_file = inv_file; 3314 return 0; 3315 } 3316 3317 spin_lock(&cifs_inode->open_file_lock); 3318 list_move_tail(&inv_file->flist, &cifs_inode->openFileList); 3319 spin_unlock(&cifs_inode->open_file_lock); 3320 cifsFileInfo_put(inv_file); 3321 ++refind; 3322 inv_file = NULL; 3323 spin_lock(&cifs_inode->open_file_lock); 3324 goto refind_writable; 3325 } 3326 3327 return rc; 3328 } 3329 3330 struct cifsFileInfo * 3331 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags) 3332 { 3333 struct cifsFileInfo *cfile; 3334 int rc; 3335 3336 rc = cifs_get_writable_file(cifs_inode, flags, &cfile); 3337 if (rc) 3338 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc); 3339 3340 return cfile; 3341 } 3342 3343 int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name, 3344 struct inode *inode, int flags, 3345 struct cifsFileInfo **ret_file) 3346 { 3347 struct cifsFileInfo *cfile; 3348 void *page; 3349 3350 *ret_file = NULL; 3351 3352 if (inode) 3353 return cifs_get_writable_file(CIFS_I(inode), flags, ret_file); 3354 3355 page = alloc_dentry_path(); 3356 spin_lock(&tcon->open_file_lock); 3357 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 3358 struct cifsInodeInfo *cinode; 3359 const char *full_path = build_path_from_dentry(cfile->dentry, page); 3360 if (IS_ERR(full_path)) { 3361 spin_unlock(&tcon->open_file_lock); 3362 free_dentry_path(page); 3363 return PTR_ERR(full_path); 3364 } 3365 if (strcmp(full_path, name)) 3366 continue; 3367 3368 cinode = CIFS_I(d_inode(cfile->dentry)); 3369 spin_unlock(&tcon->open_file_lock); 3370 free_dentry_path(page); 3371 return cifs_get_writable_file(cinode, flags, ret_file); 3372 } 3373 3374 spin_unlock(&tcon->open_file_lock); 3375 free_dentry_path(page); 3376 return -ENOENT; 3377 } 3378 3379 int 3380 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name, 3381 struct cifsFileInfo **ret_file) 3382 { 3383 struct cifsFileInfo *cfile; 3384 void *page = alloc_dentry_path(); 3385 3386 *ret_file = NULL; 3387 3388 spin_lock(&tcon->open_file_lock); 3389 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 3390 struct cifsInodeInfo *cinode; 3391 const char *full_path = build_path_from_dentry(cfile->dentry, page); 3392 if (IS_ERR(full_path)) { 3393 spin_unlock(&tcon->open_file_lock); 3394 free_dentry_path(page); 3395 return PTR_ERR(full_path); 3396 } 3397 if (strcmp(full_path, name)) 3398 continue; 3399 3400 cinode = CIFS_I(d_inode(cfile->dentry)); 3401 spin_unlock(&tcon->open_file_lock); 3402 free_dentry_path(page); 3403 *ret_file = find_readable_file(cinode, FIND_ANY); 3404 return *ret_file ? 0 : -ENOENT; 3405 } 3406 3407 spin_unlock(&tcon->open_file_lock); 3408 free_dentry_path(page); 3409 return -ENOENT; 3410 } 3411 3412 static int cifs_wait_writeback_killable(struct address_space *mapping, 3413 loff_t start, loff_t end) 3414 { 3415 pgoff_t end_index = end >> PAGE_SHIFT; 3416 pgoff_t index = start >> PAGE_SHIFT; 3417 struct folio_batch fbatch; 3418 int rc = 0; 3419 3420 folio_batch_init(&fbatch); 3421 while (!rc && 3422 filemap_get_folios_tag(mapping, &index, end_index, 3423 PAGECACHE_TAG_WRITEBACK, &fbatch)) { 3424 for (unsigned int i = 0; 3425 !rc && i < folio_batch_count(&fbatch); i++) 3426 rc = folio_wait_writeback_killable(fbatch.folios[i]); 3427 folio_batch_release(&fbatch); 3428 cond_resched(); 3429 } 3430 return rc; 3431 } 3432 3433 static int cifs_file_write_and_wait_killable(struct file *file, 3434 loff_t start, loff_t end) 3435 { 3436 struct address_space *mapping = file->f_mapping; 3437 int rc, rc2; 3438 3439 if (end < start) 3440 return 0; 3441 3442 rc = filemap_fdatawrite_range(mapping, start, end); 3443 if (cifs_wait_writeback_killable(mapping, start, end)) 3444 return -EINTR; 3445 rc2 = file_check_and_advance_wb_err(file); 3446 if (!rc) 3447 rc = rc2; 3448 return rc; 3449 } 3450 3451 /* 3452 * Flush data on a strict file. 3453 */ 3454 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, 3455 int datasync) 3456 { 3457 struct cifsFileInfo *smbfile = file->private_data; 3458 struct inode *inode = file_inode(file); 3459 unsigned int xid; 3460 int rc; 3461 3462 rc = cifs_file_write_and_wait_killable(file, start, end); 3463 if (rc) { 3464 trace_cifs_fsync_err(inode->i_ino, rc); 3465 return rc; 3466 } 3467 3468 cifs_dbg(FYI, "%s: name=%pD datasync=0x%x\n", __func__, file, datasync); 3469 3470 if (!CIFS_CACHE_READ(CIFS_I(inode))) { 3471 rc = cifs_zap_mapping(inode); 3472 cifs_dbg(FYI, "%s: invalidate mapping: rc = %d\n", __func__, rc); 3473 } 3474 3475 xid = get_xid(); 3476 rc = cifs_file_flush(xid, inode, smbfile); 3477 free_xid(xid); 3478 return rc; 3479 } 3480 3481 /* 3482 * Flush data on a non-strict data. 3483 */ 3484 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 3485 { 3486 unsigned int xid; 3487 int rc = 0; 3488 struct cifs_tcon *tcon; 3489 struct TCP_Server_Info *server; 3490 struct cifsFileInfo *smbfile = file->private_data; 3491 struct inode *inode = file_inode(file); 3492 struct cifs_sb_info *cifs_sb = CIFS_SB(file); 3493 3494 rc = cifs_file_write_and_wait_killable(file, start, end); 3495 if (rc) { 3496 trace_cifs_fsync_err(file_inode(file)->i_ino, rc); 3497 return rc; 3498 } 3499 3500 xid = get_xid(); 3501 3502 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", 3503 file, datasync); 3504 3505 tcon = tlink_tcon(smbfile->tlink); 3506 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOSSYNC)) { 3507 server = tcon->ses->server; 3508 if (server->ops->flush == NULL) { 3509 rc = -ENOSYS; 3510 goto fsync_exit; 3511 } 3512 3513 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { 3514 smbfile = find_writable_file(CIFS_I(inode), FIND_ANY); 3515 if (smbfile) { 3516 rc = server->ops->flush(xid, tcon, &smbfile->fid); 3517 cifsFileInfo_put(smbfile); 3518 } else 3519 cifs_dbg(FYI, "ignore fsync for file not open for write\n"); 3520 } else 3521 rc = server->ops->flush(xid, tcon, &smbfile->fid); 3522 } 3523 3524 fsync_exit: 3525 free_xid(xid); 3526 return rc; 3527 } 3528 3529 /* 3530 * As file closes, flush all cached write data for this inode checking 3531 * for write behind errors. 3532 */ 3533 int cifs_flush(struct file *file, fl_owner_t id) 3534 { 3535 struct address_space *mapping = file->f_mapping; 3536 struct inode *inode = file_inode(file); 3537 int rc = 0, rc2; 3538 3539 if (file->f_mode & FMODE_WRITE) { 3540 rc = filemap_fdatawrite(mapping); 3541 if (fatal_signal_pending(current) || 3542 cifs_wait_writeback_killable(mapping, 0, LLONG_MAX)) 3543 return 0; 3544 rc2 = filemap_check_errors(mapping); 3545 if (!rc) 3546 rc = rc2; 3547 } 3548 3549 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); 3550 if (rc) { 3551 /* get more nuanced writeback errors */ 3552 rc = filemap_check_wb_err(file->f_mapping, 0); 3553 trace_cifs_flush_err(inode->i_ino, rc); 3554 } 3555 return rc; 3556 } 3557 3558 static ssize_t 3559 cifs_writev(struct kiocb *iocb, struct iov_iter *from) 3560 { 3561 struct file *file = iocb->ki_filp; 3562 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 3563 struct inode *inode = file->f_mapping->host; 3564 struct cifsInodeInfo *cinode = CIFS_I(inode); 3565 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 3566 struct cifs_sb_info *cifs_sb = CIFS_SB(inode); 3567 ssize_t rc; 3568 3569 rc = netfs_start_io_write(inode); 3570 if (rc < 0) 3571 return rc; 3572 3573 /* 3574 * We need to hold the sem to be sure nobody modifies lock list 3575 * with a brlock that prevents writing. 3576 */ 3577 down_read(&cinode->lock_sem); 3578 3579 rc = generic_write_checks(iocb, from); 3580 if (rc <= 0) 3581 goto out; 3582 3583 if ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) && 3584 (cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), 3585 server->vals->exclusive_lock_type, 0, 3586 NULL, CIFS_WRITE_OP))) { 3587 rc = -EACCES; 3588 goto out; 3589 } 3590 3591 rc = netfs_buffered_write_iter_locked(iocb, from, NULL); 3592 cifs_update_i_blocks_after_write(iocb, rc); 3593 3594 out: 3595 up_read(&cinode->lock_sem); 3596 netfs_end_io_write(inode); 3597 if (rc > 0) 3598 rc = generic_write_sync(iocb, rc); 3599 return rc; 3600 } 3601 3602 ssize_t 3603 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) 3604 { 3605 struct inode *inode = file_inode(iocb->ki_filp); 3606 struct cifsInodeInfo *cinode = CIFS_I(inode); 3607 struct cifs_sb_info *cifs_sb = CIFS_SB(inode); 3608 struct cifsFileInfo *cfile = (struct cifsFileInfo *) 3609 iocb->ki_filp->private_data; 3610 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 3611 ssize_t written; 3612 3613 written = cifs_get_writer(cinode); 3614 if (written) 3615 return written; 3616 3617 if (CIFS_CACHE_WRITE(cinode)) { 3618 if (cap_unix(tcon->ses) && 3619 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 3620 ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0)) { 3621 written = netfs_file_write_iter(iocb, from); 3622 cifs_update_i_blocks_after_write(iocb, written); 3623 goto out; 3624 } 3625 written = cifs_writev(iocb, from); 3626 goto out; 3627 } 3628 /* 3629 * For non-oplocked files in strict cache mode we need to write the data 3630 * to the server exactly from the pos to pos+len-1 rather than flush all 3631 * affected pages because it may cause a error with mandatory locks on 3632 * these pages but not on the region from pos to ppos+len-1. 3633 */ 3634 written = netfs_file_write_iter(iocb, from); 3635 cifs_update_i_blocks_after_write(iocb, written); 3636 if (CIFS_CACHE_READ(cinode)) { 3637 /* 3638 * We have read level caching and we have just sent a write 3639 * request to the server thus making data in the cache stale. 3640 * Zap the cache and set oplock/lease level to NONE to avoid 3641 * reading stale data from the cache. All subsequent read 3642 * operations will read new data from the server. 3643 */ 3644 cifs_zap_mapping(inode); 3645 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", 3646 inode); 3647 cifs_reset_oplock(cinode); 3648 } 3649 out: 3650 cifs_put_writer(cinode); 3651 return written; 3652 } 3653 3654 ssize_t cifs_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) 3655 { 3656 ssize_t written; 3657 3658 written = netfs_file_write_iter(iocb, from); 3659 cifs_update_i_blocks_after_write(iocb, written); 3660 return written; 3661 } 3662 3663 ssize_t cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter) 3664 { 3665 ssize_t rc; 3666 struct inode *inode = file_inode(iocb->ki_filp); 3667 3668 if (iocb->ki_flags & IOCB_DIRECT) 3669 return netfs_unbuffered_read_iter(iocb, iter); 3670 3671 rc = cifs_revalidate_mapping(inode); 3672 if (rc) 3673 return rc; 3674 3675 return netfs_file_read_iter(iocb, iter); 3676 } 3677 3678 ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 3679 { 3680 struct inode *inode = file_inode(iocb->ki_filp); 3681 struct cifsInodeInfo *cinode = CIFS_I(inode); 3682 ssize_t written; 3683 int rc; 3684 3685 if (iocb->ki_filp->f_flags & O_DIRECT) { 3686 written = netfs_unbuffered_write_iter(iocb, from); 3687 cifs_update_i_blocks_after_write(iocb, written); 3688 if (written > 0 && CIFS_CACHE_READ(cinode)) { 3689 cifs_zap_mapping(inode); 3690 cifs_dbg(FYI, 3691 "Set no oplock for inode=%p after a write operation\n", 3692 inode); 3693 cifs_reset_oplock(cinode); 3694 } 3695 return written; 3696 } 3697 3698 written = cifs_get_writer(cinode); 3699 if (written) 3700 return written; 3701 3702 written = netfs_file_write_iter(iocb, from); 3703 cifs_update_i_blocks_after_write(iocb, written); 3704 3705 if (!CIFS_CACHE_WRITE(CIFS_I(inode))) { 3706 rc = filemap_fdatawrite(inode->i_mapping); 3707 if (rc) 3708 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n", 3709 rc, inode); 3710 } 3711 3712 cifs_put_writer(cinode); 3713 return written; 3714 } 3715 3716 ssize_t 3717 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) 3718 { 3719 struct inode *inode = file_inode(iocb->ki_filp); 3720 struct cifsInodeInfo *cinode = CIFS_I(inode); 3721 struct cifs_sb_info *cifs_sb = CIFS_SB(inode); 3722 struct cifsFileInfo *cfile = (struct cifsFileInfo *) 3723 iocb->ki_filp->private_data; 3724 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 3725 int rc = -EACCES; 3726 3727 /* 3728 * In strict cache mode we need to read from the server all the time 3729 * if we don't have level II oplock because the server can delay mtime 3730 * change - so we can't make a decision about inode invalidating. 3731 * And we can also fail with pagereading if there are mandatory locks 3732 * on pages affected by this read but not on the region from pos to 3733 * pos+len-1. 3734 */ 3735 if (!CIFS_CACHE_READ(cinode)) 3736 return netfs_unbuffered_read_iter(iocb, to); 3737 3738 if ((cifs_sb_flags(cifs_sb) & CIFS_MOUNT_NOPOSIXBRL) == 0) { 3739 if (iocb->ki_flags & IOCB_DIRECT) 3740 return netfs_unbuffered_read_iter(iocb, to); 3741 return netfs_buffered_read_iter(iocb, to); 3742 } 3743 3744 /* 3745 * We need to hold the sem to be sure nobody modifies lock list 3746 * with a brlock that prevents reading. 3747 */ 3748 if (iocb->ki_flags & IOCB_DIRECT) { 3749 rc = netfs_start_io_direct(inode); 3750 if (rc < 0) 3751 goto out; 3752 rc = -EACCES; 3753 down_read(&cinode->lock_sem); 3754 if (!cifs_find_lock_conflict( 3755 cfile, iocb->ki_pos, iov_iter_count(to), 3756 tcon->ses->server->vals->shared_lock_type, 3757 0, NULL, CIFS_READ_OP)) 3758 rc = netfs_unbuffered_read_iter_locked(iocb, to); 3759 up_read(&cinode->lock_sem); 3760 netfs_end_io_direct(inode); 3761 } else { 3762 rc = netfs_start_io_read(inode); 3763 if (rc < 0) 3764 goto out; 3765 rc = -EACCES; 3766 down_read(&cinode->lock_sem); 3767 if (!cifs_find_lock_conflict( 3768 cfile, iocb->ki_pos, iov_iter_count(to), 3769 tcon->ses->server->vals->shared_lock_type, 3770 0, NULL, CIFS_READ_OP)) 3771 rc = filemap_read(iocb, to, 0); 3772 up_read(&cinode->lock_sem); 3773 netfs_end_io_read(inode); 3774 } 3775 out: 3776 return rc; 3777 } 3778 3779 static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf) 3780 { 3781 return netfs_page_mkwrite(vmf, NULL); 3782 } 3783 3784 static const struct vm_operations_struct cifs_file_vm_ops = { 3785 .fault = filemap_fault, 3786 .map_pages = filemap_map_pages, 3787 .page_mkwrite = cifs_page_mkwrite, 3788 }; 3789 3790 int cifs_file_strict_mmap_prepare(struct vm_area_desc *desc) 3791 { 3792 int xid, rc = 0; 3793 struct inode *inode = file_inode(desc->file); 3794 3795 xid = get_xid(); 3796 3797 if (!CIFS_CACHE_READ(CIFS_I(inode))) 3798 rc = cifs_zap_mapping(inode); 3799 if (!rc) 3800 rc = generic_file_mmap_prepare(desc); 3801 if (!rc) 3802 desc->vm_ops = &cifs_file_vm_ops; 3803 3804 free_xid(xid); 3805 return rc; 3806 } 3807 3808 int cifs_file_mmap_prepare(struct vm_area_desc *desc) 3809 { 3810 int rc, xid; 3811 3812 xid = get_xid(); 3813 3814 rc = cifs_revalidate_file(desc->file); 3815 if (rc) 3816 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", 3817 rc); 3818 if (!rc) 3819 rc = generic_file_mmap_prepare(desc); 3820 if (!rc) 3821 desc->vm_ops = &cifs_file_vm_ops; 3822 3823 free_xid(xid); 3824 return rc; 3825 } 3826 3827 static int is_inode_writable(struct cifsInodeInfo *cifs_inode) 3828 { 3829 struct cifsFileInfo *open_file; 3830 3831 spin_lock(&cifs_inode->open_file_lock); 3832 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 3833 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 3834 spin_unlock(&cifs_inode->open_file_lock); 3835 return 1; 3836 } 3837 } 3838 spin_unlock(&cifs_inode->open_file_lock); 3839 return 0; 3840 } 3841 3842 /* We do not want to update the file size from server for inodes 3843 open for write - to avoid races with writepage extending 3844 the file - in the future we could consider allowing 3845 refreshing the inode only on increases in the file size 3846 but this is tricky to do without racing with writebehind 3847 page caching in the current Linux kernel design */ 3848 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file, 3849 bool from_readdir) 3850 { 3851 struct cifs_sb_info *cifs_sb; 3852 unsigned long tlw; 3853 3854 if (!cifsInode) 3855 return true; 3856 3857 cifs_sb = CIFS_SB(cifsInode); 3858 3859 if (is_inode_writable(cifsInode) || 3860 ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) { 3861 /* This inode is open for write at least once */ 3862 3863 /* 3864 * Readdir data is unreliable when we have writable handles or 3865 * an exclusive lease -- never allow it to change i_size, even 3866 * on direct-IO mounts where the server's directory metadata 3867 * can still lag behind the actual file state. 3868 */ 3869 if (from_readdir) 3870 return false; 3871 3872 if (cifs_sb_flags(cifs_sb) & CIFS_MOUNT_DIRECT_IO) { 3873 /* since no page cache to corrupt on directio 3874 we can change size safely */ 3875 return true; 3876 } 3877 3878 if (i_size_read(&cifsInode->netfs.inode) < end_of_file) 3879 return true; 3880 3881 return false; 3882 } 3883 3884 /* 3885 * No writable handles open. Check whether we are within the attribute 3886 * cache validity window of a recent local modification. 3887 * 3888 * For the close() path: _cifsFileInfo_put() stamps time_last_write 3889 * (via smp_store_release()) before releasing open_file_lock. That 3890 * spin_unlock() is a store-release that pairs with the spin_lock() 3891 * (load-acquire) in is_inode_writable() above, so if 3892 * is_inode_writable() returned false the smp_load_acquire() below is 3893 * guaranteed to observe any time_last_write update from a concurrent 3894 * close(), covering all close paths including background I/O. 3895 * 3896 * For the setattr/truncate paths: those callers use smp_store_release() 3897 * directly; the smp_load_acquire() below pairs with that store. There 3898 * is no shared lock between setattr and readdir, so this relies on 3899 * acquire-release semantics alone. The store propagation latency on 3900 * weakly-ordered architectures (nanoseconds) is negligible relative to 3901 * the acregmax window (seconds) and the readdir RPC round-trip 3902 * (milliseconds), making this a sound design choice in practice. 3903 * 3904 * time_last_write == 0 means the inode has never been written locally; 3905 * skip the window check to avoid false positives near boot time when 3906 * jiffies is still close to INITIAL_JIFFIES on 32-bit systems. 3907 */ 3908 if (from_readdir) { 3909 /* Pairs with smp_store_release() in _cifsFileInfo_put() and setattr. */ 3910 tlw = smp_load_acquire(&cifsInode->time_last_write); 3911 if (tlw && time_before(jiffies, tlw + cifs_sb->ctx->acregmax)) 3912 return false; 3913 } 3914 3915 return true; 3916 } 3917 3918 void cifs_oplock_break(struct work_struct *work) 3919 { 3920 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, 3921 oplock_break); 3922 struct inode *inode = d_inode(cfile->dentry); 3923 struct super_block *sb = inode->i_sb; 3924 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 3925 struct cifsInodeInfo *cinode = CIFS_I(inode); 3926 bool cache_read, cache_write, cache_handle; 3927 struct cifs_tcon *tcon; 3928 struct TCP_Server_Info *server; 3929 struct tcon_link *tlink; 3930 unsigned int oplock; 3931 int rc = 0; 3932 bool purge_cache = false, oplock_break_cancelled; 3933 __u64 persistent_fid, volatile_fid; 3934 __u16 net_fid; 3935 3936 /* 3937 * Dropping the handle reference below also drops the dentry 3938 * reference that pins this inode, and cinode is still needed to 3939 * acknowledge the break and to report it as complete. Hold the inode 3940 * until that is done. 3941 */ 3942 ihold(inode); 3943 3944 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, 3945 TASK_UNINTERRUPTIBLE); 3946 3947 tlink = cifs_get_tlink(cfile->tlink); 3948 if (IS_ERR_OR_NULL(tlink)) { 3949 /* drop the reference taken when the break was queued */ 3950 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false); 3951 goto out; 3952 } 3953 tcon = tlink_tcon(tlink); 3954 server = tcon->ses->server; 3955 3956 scoped_guard(spinlock, &cinode->open_file_lock) { 3957 unsigned int sbflags = cifs_sb_flags(cifs_sb); 3958 3959 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level, 3960 cfile->oplock_epoch, &purge_cache); 3961 oplock = READ_ONCE(cinode->oplock); 3962 cache_read = (oplock & CIFS_CACHE_READ_FLG) || 3963 (sbflags & CIFS_MOUNT_RO_CACHE); 3964 cache_write = (oplock & CIFS_CACHE_WRITE_FLG) || 3965 (sbflags & CIFS_MOUNT_RW_CACHE); 3966 cache_handle = oplock & CIFS_CACHE_HANDLE_FLG; 3967 } 3968 3969 if (!cache_write && cache_read && cifs_has_mand_locks(cinode)) { 3970 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", 3971 inode); 3972 cifs_reset_oplock(cinode); 3973 oplock = 0; 3974 cache_read = cache_write = cache_handle = false; 3975 } 3976 3977 if (S_ISREG(inode->i_mode)) { 3978 if (cache_read) 3979 break_lease(inode, O_RDONLY); 3980 else 3981 break_lease(inode, O_WRONLY); 3982 rc = filemap_fdatawrite(inode->i_mapping); 3983 if (!cache_read || purge_cache) { 3984 rc = filemap_fdatawait(inode->i_mapping); 3985 mapping_set_error(inode->i_mapping, rc); 3986 cifs_zap_mapping(inode); 3987 } 3988 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); 3989 if (cache_write) 3990 goto oplock_break_ack; 3991 } 3992 3993 rc = cifs_push_locks(cfile); 3994 if (rc) 3995 cifs_dbg(VFS, "Push locks rc = %d\n", rc); 3996 3997 oplock_break_ack: 3998 /* 3999 * When oplock break is received and there are no active 4000 * file handles but cached, then schedule deferred close immediately. 4001 * So, new open will not use cached handle. 4002 */ 4003 4004 if (!cache_handle && !list_empty(&cinode->deferred_closes)) 4005 cifs_close_deferred_file(cinode); 4006 4007 persistent_fid = cfile->fid.persistent_fid; 4008 volatile_fid = cfile->fid.volatile_fid; 4009 net_fid = cfile->fid.netfid; 4010 oplock_break_cancelled = cfile->oplock_break_cancelled; 4011 4012 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false); 4013 /* 4014 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require 4015 * an acknowledgment to be sent when the file has already been closed. 4016 */ 4017 spin_lock(&cinode->open_file_lock); 4018 /* check list empty since can race with kill_sb calling tree disconnect */ 4019 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) { 4020 spin_unlock(&cinode->open_file_lock); 4021 rc = server->ops->oplock_response(tcon, persistent_fid, 4022 volatile_fid, net_fid, 4023 cinode, oplock); 4024 cifs_dbg(FYI, "Oplock release rc = %d\n", rc); 4025 } else 4026 spin_unlock(&cinode->open_file_lock); 4027 4028 cifs_put_tlink(tlink); 4029 out: 4030 cifs_done_oplock_break(cinode); 4031 iput(inode); 4032 if (atomic_dec_and_test(&cifs_sb->outstanding_rreq)) 4033 wake_up_var(&cifs_sb->outstanding_rreq); 4034 } 4035 4036 #ifdef CONFIG_SWAP 4037 static void cifs_swap_submit_write(struct swap_io_ctx *ctx) 4038 { 4039 struct swap_iocb *sio = ctx->sio; 4040 struct iov_iter iter; 4041 int ret; 4042 4043 swap_fs_prepare_rw(ctx, WRITE, &iter); 4044 ret = netfs_unbuffered_write_iter_locked(&sio->iocb, &iter, NULL); 4045 if (ret != -EIOCBQUEUED) 4046 sio->iocb.ki_complete(&sio->iocb, ret); 4047 } 4048 4049 static void cifs_swap_submit_read(struct swap_io_ctx *ctx) 4050 { 4051 struct swap_iocb *sio = ctx->sio; 4052 struct iov_iter iter; 4053 int ret; 4054 4055 swap_fs_prepare_rw(ctx, READ, &iter); 4056 ret = netfs_unbuffered_read_iter_locked(&sio->iocb, &iter); 4057 if (ret != -EIOCBQUEUED) 4058 sio->iocb.ki_complete(&sio->iocb, ret); 4059 } 4060 4061 static const struct swap_ops cifs_swap_ops = { 4062 .flags = SWAP_OPS_F_REQUIRE_NOFS, 4063 .submit_write = cifs_swap_submit_write, 4064 .submit_read = cifs_swap_submit_read, 4065 .can_merge = swap_fs_can_merge, 4066 }; 4067 4068 static int cifs_swap_activate(struct swap_info_struct *sis, 4069 struct file *swap_file, sector_t *span) 4070 { 4071 struct cifsFileInfo *cfile = swap_file->private_data; 4072 struct inode *inode = swap_file->f_mapping->host; 4073 unsigned long blocks; 4074 long long isize; 4075 4076 cifs_dbg(FYI, "swap activate\n"); 4077 4078 if (swap_file->f_mapping->a_ops != &cifs_addr_ops) 4079 /* Cannot support swap */ 4080 return -EINVAL; 4081 4082 spin_lock(&inode->i_lock); 4083 blocks = inode->i_blocks; 4084 isize = inode->i_size; 4085 spin_unlock(&inode->i_lock); 4086 if (blocks*512 < isize) { 4087 pr_warn("swap activate: swapfile has holes\n"); 4088 return -EINVAL; 4089 } 4090 *span = sis->pages; 4091 4092 pr_warn_once("Swap support over SMB3 is experimental\n"); 4093 4094 /* 4095 * TODO: consider adding ACL (or documenting how) to prevent other 4096 * users (on this or other systems) from reading it 4097 */ 4098 4099 4100 /* TODO: add sk_set_memalloc(inet) or similar */ 4101 4102 if (cfile) 4103 cfile->swapfile = true; 4104 /* 4105 * TODO: Since file already open, we can't open with DENY_ALL here 4106 * but we could add call to grab a byte range lock to prevent others 4107 * from reading or writing the file 4108 */ 4109 return swap_fs_activate(sis, &cifs_swap_ops); 4110 } 4111 4112 static void cifs_swap_deactivate(struct file *file) 4113 { 4114 struct cifsFileInfo *cfile = file->private_data; 4115 4116 cifs_dbg(FYI, "swap deactivate\n"); 4117 4118 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */ 4119 4120 if (cfile) 4121 cfile->swapfile = false; 4122 4123 /* do we need to unpin (or unlock) the file */ 4124 } 4125 #else 4126 #define cifs_swap_activate NULL 4127 #define cifs_swap_deactivate NULL 4128 #endif /* CONFIG_SWAP */ 4129 4130 const struct address_space_operations cifs_addr_ops = { 4131 .read_folio = netfs_read_folio, 4132 .readahead = netfs_readahead, 4133 .writepages = netfs_writepages, 4134 .dirty_folio = netfs_dirty_folio, 4135 .release_folio = netfs_release_folio, 4136 .direct_IO = noop_direct_IO, 4137 .invalidate_folio = netfs_invalidate_folio, 4138 .migrate_folio = filemap_migrate_folio, 4139 /* 4140 * TODO: investigate and if useful we could add an is_dirty_writeback 4141 * helper if needed 4142 */ 4143 .swap_activate = cifs_swap_activate, 4144 .swap_deactivate = cifs_swap_deactivate, 4145 }; 4146 4147 /* 4148 * cifs_readahead requires the server to support a buffer large enough to 4149 * contain the header plus one complete page of data. Otherwise, we need 4150 * to leave cifs_readahead out of the address space operations. 4151 */ 4152 const struct address_space_operations cifs_addr_ops_smallbuf = { 4153 .read_folio = netfs_read_folio, 4154 .writepages = netfs_writepages, 4155 .dirty_folio = netfs_dirty_folio, 4156 .release_folio = netfs_release_folio, 4157 .invalidate_folio = netfs_invalidate_folio, 4158 .migrate_folio = filemap_migrate_folio, 4159 }; 4160