FILE
file.c
File preview with syntax highlighting.
linux-6.19.14/fs/smb/client/file.c
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/pagevec.h> 19#include <linux/writeback.h> 20#include <linux/task_io_accounting_ops.h> 21#include <linux/delay.h> 22#include <linux/mount.h> 23#include <linux/slab.h> 24#include <linux/swap.h> 25#include <linux/mm.h> 26#include <asm/div64.h> 27#include "cifsfs.h" 28#include "cifspdu.h" 29#include "cifsglob.h" 30#include "cifsproto.h" 31#include "smb2proto.h" 32#include "cifs_unicode.h" 33#include "cifs_debug.h" 34#include "cifs_fs_sb.h" 35#include "fscache.h" 36#include "smbdirect.h" 37#include "fs_context.h" 38#include "cifs_ioctl.h" 39#include "cached_dir.h" 40#include <trace/events/netfs.h> 41 42static int cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush); 43 44/* 45 * Prepare a subrequest to upload to the server. We need to allocate credits 46 * so that we know the maximum amount of data that we can include in it. 47 */ 48static void cifs_prepare_write(struct netfs_io_subrequest *subreq) 49{ 50 struct cifs_io_subrequest *wdata = 51 container_of(subreq, struct cifs_io_subrequest, subreq); 52 struct cifs_io_request *req = wdata->req; 53 struct netfs_io_stream *stream = &req->rreq.io_streams[subreq->stream_nr]; 54 struct TCP_Server_Info *server; 55 struct cifsFileInfo *open_file = req->cfile; 56 struct cifs_sb_info *cifs_sb = CIFS_SB(wdata->rreq->inode->i_sb); 57 size_t wsize = req->rreq.wsize; 58 int rc; 59 60 if (!wdata->have_xid) { 61 wdata->xid = get_xid(); 62 wdata->have_xid = true; 63 } 64 65 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); 66 wdata->server = server; 67 68 if (cifs_sb->ctx->wsize == 0) 69 cifs_negotiate_wsize(server, cifs_sb->ctx, 70 tlink_tcon(req->cfile->tlink)); 71 72retry: 73 if (open_file->invalidHandle) { 74 rc = cifs_reopen_file(open_file, false); 75 if (rc < 0) { 76 if (rc == -EAGAIN) 77 goto retry; 78 subreq->error = rc; 79 return netfs_prepare_write_failed(subreq); 80 } 81 } 82 83 rc = server->ops->wait_mtu_credits(server, wsize, &stream->sreq_max_len, 84 &wdata->credits); 85 if (rc < 0) { 86 subreq->error = rc; 87 return netfs_prepare_write_failed(subreq); 88 } 89 90 wdata->credits.rreq_debug_id = subreq->rreq->debug_id; 91 wdata->credits.rreq_debug_index = subreq->debug_index; 92 wdata->credits.in_flight_check = 1; 93 trace_smb3_rw_credits(wdata->rreq->debug_id, 94 wdata->subreq.debug_index, 95 wdata->credits.value, 96 server->credits, server->in_flight, 97 wdata->credits.value, 98 cifs_trace_rw_credits_write_prepare); 99 100#ifdef CONFIG_CIFS_SMB_DIRECT 101 if (server->smbd_conn) { 102 const struct smbdirect_socket_parameters *sp = 103 smbd_get_parameters(server->smbd_conn); 104 105 stream->sreq_max_segs = sp->max_frmr_depth; 106 } 107#endif 108} 109 110/* 111 * Issue a subrequest to upload to the server. 112 */ 113static void cifs_issue_write(struct netfs_io_subrequest *subreq) 114{ 115 struct cifs_io_subrequest *wdata = 116 container_of(subreq, struct cifs_io_subrequest, subreq); 117 struct cifs_sb_info *sbi = CIFS_SB(subreq->rreq->inode->i_sb); 118 int rc; 119 120 if (cifs_forced_shutdown(sbi)) { 121 rc = smb_EIO(smb_eio_trace_forced_shutdown); 122 goto fail; 123 } 124 125 rc = adjust_credits(wdata->server, wdata, cifs_trace_rw_credits_issue_write_adjust); 126 if (rc) 127 goto fail; 128 129 rc = -EAGAIN; 130 if (wdata->req->cfile->invalidHandle) 131 goto fail; 132 133 wdata->server->ops->async_writev(wdata); 134out: 135 return; 136 137fail: 138 if (rc == -EAGAIN) 139 trace_netfs_sreq(subreq, netfs_sreq_trace_retry); 140 else 141 trace_netfs_sreq(subreq, netfs_sreq_trace_fail); 142 add_credits_and_wake_if(wdata->server, &wdata->credits, 0); 143 cifs_write_subrequest_terminated(wdata, rc); 144 goto out; 145} 146 147static void cifs_netfs_invalidate_cache(struct netfs_io_request *wreq) 148{ 149 cifs_invalidate_cache(wreq->inode, 0); 150} 151 152/* 153 * Negotiate the size of a read operation on behalf of the netfs library. 154 */ 155static int cifs_prepare_read(struct netfs_io_subrequest *subreq) 156{ 157 struct netfs_io_request *rreq = subreq->rreq; 158 struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq); 159 struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq); 160 struct TCP_Server_Info *server; 161 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb); 162 size_t size; 163 int rc = 0; 164 165 if (!rdata->have_xid) { 166 rdata->xid = get_xid(); 167 rdata->have_xid = true; 168 } 169 170 server = cifs_pick_channel(tlink_tcon(req->cfile->tlink)->ses); 171 rdata->server = server; 172 173 if (cifs_sb->ctx->rsize == 0) 174 cifs_negotiate_rsize(server, cifs_sb->ctx, 175 tlink_tcon(req->cfile->tlink)); 176 177 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize, 178 &size, &rdata->credits); 179 if (rc) 180 return rc; 181 182 rreq->io_streams[0].sreq_max_len = size; 183 184 rdata->credits.in_flight_check = 1; 185 rdata->credits.rreq_debug_id = rreq->debug_id; 186 rdata->credits.rreq_debug_index = subreq->debug_index; 187 188 trace_smb3_rw_credits(rdata->rreq->debug_id, 189 rdata->subreq.debug_index, 190 rdata->credits.value, 191 server->credits, server->in_flight, 0, 192 cifs_trace_rw_credits_read_submit); 193 194#ifdef CONFIG_CIFS_SMB_DIRECT 195 if (server->smbd_conn) { 196 const struct smbdirect_socket_parameters *sp = 197 smbd_get_parameters(server->smbd_conn); 198 199 rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth; 200 } 201#endif 202 return 0; 203} 204 205/* 206 * Issue a read operation on behalf of the netfs helper functions. We're asked 207 * to make a read of a certain size at a point in the file. We are permitted 208 * to only read a portion of that, but as long as we read something, the netfs 209 * helper will call us again so that we can issue another read. 210 */ 211static void cifs_issue_read(struct netfs_io_subrequest *subreq) 212{ 213 struct netfs_io_request *rreq = subreq->rreq; 214 struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq); 215 struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq); 216 struct TCP_Server_Info *server = rdata->server; 217 int rc = 0; 218 219 cifs_dbg(FYI, "%s: op=%08x[%x] mapping=%p len=%zu/%zu\n", 220 __func__, rreq->debug_id, subreq->debug_index, rreq->mapping, 221 subreq->transferred, subreq->len); 222 223 rc = adjust_credits(server, rdata, cifs_trace_rw_credits_issue_read_adjust); 224 if (rc) 225 goto failed; 226 227 if (req->cfile->invalidHandle) { 228 do { 229 rc = cifs_reopen_file(req->cfile, true); 230 } while (rc == -EAGAIN); 231 if (rc) 232 goto failed; 233 } 234 235 if (subreq->rreq->origin != NETFS_UNBUFFERED_READ && 236 subreq->rreq->origin != NETFS_DIO_READ) 237 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); 238 239 trace_netfs_sreq(subreq, netfs_sreq_trace_submit); 240 rc = rdata->server->ops->async_readv(rdata); 241 if (rc) 242 goto failed; 243 return; 244 245failed: 246 subreq->error = rc; 247 netfs_read_subreq_terminated(subreq); 248} 249 250/* 251 * Writeback calls this when it finds a folio that needs uploading. This isn't 252 * called if writeback only has copy-to-cache to deal with. 253 */ 254static void cifs_begin_writeback(struct netfs_io_request *wreq) 255{ 256 struct cifs_io_request *req = container_of(wreq, struct cifs_io_request, rreq); 257 int ret; 258 259 ret = cifs_get_writable_file(CIFS_I(wreq->inode), FIND_WR_ANY, &req->cfile); 260 if (ret) { 261 cifs_dbg(VFS, "No writable handle in writepages ret=%d\n", ret); 262 return; 263 } 264 265 wreq->io_streams[0].avail = true; 266} 267 268/* 269 * Initialise a request. 270 */ 271static int cifs_init_request(struct netfs_io_request *rreq, struct file *file) 272{ 273 struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq); 274 struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb); 275 struct cifsFileInfo *open_file = NULL; 276 277 rreq->rsize = cifs_sb->ctx->rsize; 278 rreq->wsize = cifs_sb->ctx->wsize; 279 req->pid = current->tgid; // Ummm... This may be a workqueue 280 281 if (file) { 282 open_file = file->private_data; 283 rreq->netfs_priv = file->private_data; 284 req->cfile = cifsFileInfo_get(open_file); 285 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) 286 req->pid = req->cfile->pid; 287 } else if (rreq->origin != NETFS_WRITEBACK) { 288 WARN_ON_ONCE(1); 289 return smb_EIO1(smb_eio_trace_not_netfs_writeback, rreq->origin); 290 } 291 292 return 0; 293} 294 295/* 296 * Completion of a request operation. 297 */ 298static void cifs_rreq_done(struct netfs_io_request *rreq) 299{ 300 struct timespec64 atime, mtime; 301 struct inode *inode = rreq->inode; 302 303 /* we do not want atime to be less than mtime, it broke some apps */ 304 atime = inode_set_atime_to_ts(inode, current_time(inode)); 305 mtime = inode_get_mtime(inode); 306 if (timespec64_compare(&atime, &mtime)) 307 inode_set_atime_to_ts(inode, inode_get_mtime(inode)); 308} 309 310static void cifs_free_request(struct netfs_io_request *rreq) 311{ 312 struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq); 313 314 if (req->cfile) 315 cifsFileInfo_put(req->cfile); 316} 317 318static void cifs_free_subrequest(struct netfs_io_subrequest *subreq) 319{ 320 struct cifs_io_subrequest *rdata = 321 container_of(subreq, struct cifs_io_subrequest, subreq); 322 int rc = subreq->error; 323 324 if (rdata->subreq.source == NETFS_DOWNLOAD_FROM_SERVER) { 325#ifdef CONFIG_CIFS_SMB_DIRECT 326 if (rdata->mr) { 327 smbd_deregister_mr(rdata->mr); 328 rdata->mr = NULL; 329 } 330#endif 331 } 332 333 if (rdata->credits.value != 0) { 334 trace_smb3_rw_credits(rdata->rreq->debug_id, 335 rdata->subreq.debug_index, 336 rdata->credits.value, 337 rdata->server ? rdata->server->credits : 0, 338 rdata->server ? rdata->server->in_flight : 0, 339 -rdata->credits.value, 340 cifs_trace_rw_credits_free_subreq); 341 if (rdata->server) 342 add_credits_and_wake_if(rdata->server, &rdata->credits, 0); 343 else 344 rdata->credits.value = 0; 345 } 346 347 if (rdata->have_xid) 348 free_xid(rdata->xid); 349} 350 351const struct netfs_request_ops cifs_req_ops = { 352 .request_pool = &cifs_io_request_pool, 353 .subrequest_pool = &cifs_io_subrequest_pool, 354 .init_request = cifs_init_request, 355 .free_request = cifs_free_request, 356 .free_subrequest = cifs_free_subrequest, 357 .prepare_read = cifs_prepare_read, 358 .issue_read = cifs_issue_read, 359 .done = cifs_rreq_done, 360 .begin_writeback = cifs_begin_writeback, 361 .prepare_write = cifs_prepare_write, 362 .issue_write = cifs_issue_write, 363 .invalidate_cache = cifs_netfs_invalidate_cache, 364}; 365 366/* 367 * Mark as invalid, all open files on tree connections since they 368 * were closed when session to server was lost. 369 */ 370void 371cifs_mark_open_files_invalid(struct cifs_tcon *tcon) 372{ 373 struct cifsFileInfo *open_file = NULL; 374 struct list_head *tmp; 375 struct list_head *tmp1; 376 377 /* only send once per connect */ 378 spin_lock(&tcon->tc_lock); 379 if (tcon->need_reconnect) 380 tcon->status = TID_NEED_RECON; 381 382 if (tcon->status != TID_NEED_RECON) { 383 spin_unlock(&tcon->tc_lock); 384 return; 385 } 386 tcon->status = TID_IN_FILES_INVALIDATE; 387 spin_unlock(&tcon->tc_lock); 388 389 /* list all files open on tree connection and mark them invalid */ 390 spin_lock(&tcon->open_file_lock); 391 list_for_each_safe(tmp, tmp1, &tcon->openFileList) { 392 open_file = list_entry(tmp, struct cifsFileInfo, tlist); 393 open_file->invalidHandle = true; 394 open_file->oplock_break_cancelled = true; 395 } 396 spin_unlock(&tcon->open_file_lock); 397 398 invalidate_all_cached_dirs(tcon); 399 spin_lock(&tcon->tc_lock); 400 if (tcon->status == TID_IN_FILES_INVALIDATE) 401 tcon->status = TID_NEED_TCON; 402 spin_unlock(&tcon->tc_lock); 403 404 /* 405 * BB Add call to evict_inodes(sb) for all superblocks mounted 406 * to this tcon. 407 */ 408} 409 410static inline int cifs_convert_flags(unsigned int flags, int rdwr_for_fscache) 411{ 412 if ((flags & O_ACCMODE) == O_RDONLY) 413 return GENERIC_READ; 414 else if ((flags & O_ACCMODE) == O_WRONLY) 415 return rdwr_for_fscache == 1 ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE; 416 else if ((flags & O_ACCMODE) == O_RDWR) { 417 /* GENERIC_ALL is too much permission to request 418 can cause unnecessary access denied on create */ 419 /* return GENERIC_ALL; */ 420 return (GENERIC_READ | GENERIC_WRITE); 421 } 422 423 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | 424 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | 425 FILE_READ_DATA); 426} 427 428#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 429static u32 cifs_posix_convert_flags(unsigned int flags) 430{ 431 u32 posix_flags = 0; 432 433 if ((flags & O_ACCMODE) == O_RDONLY) 434 posix_flags = SMB_O_RDONLY; 435 else if ((flags & O_ACCMODE) == O_WRONLY) 436 posix_flags = SMB_O_WRONLY; 437 else if ((flags & O_ACCMODE) == O_RDWR) 438 posix_flags = SMB_O_RDWR; 439 440 if (flags & O_CREAT) { 441 posix_flags |= SMB_O_CREAT; 442 if (flags & O_EXCL) 443 posix_flags |= SMB_O_EXCL; 444 } else if (flags & O_EXCL) 445 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", 446 current->comm, current->tgid); 447 448 if (flags & O_TRUNC) 449 posix_flags |= SMB_O_TRUNC; 450 /* be safe and imply O_SYNC for O_DSYNC */ 451 if (flags & O_DSYNC) 452 posix_flags |= SMB_O_SYNC; 453 if (flags & O_DIRECTORY) 454 posix_flags |= SMB_O_DIRECTORY; 455 if (flags & O_NOFOLLOW) 456 posix_flags |= SMB_O_NOFOLLOW; 457 if (flags & O_DIRECT) 458 posix_flags |= SMB_O_DIRECT; 459 460 return posix_flags; 461} 462#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 463 464static inline int cifs_get_disposition(unsigned int flags) 465{ 466 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 467 return FILE_CREATE; 468 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 469 return FILE_OVERWRITE_IF; 470 else if ((flags & O_CREAT) == O_CREAT) 471 return FILE_OPEN_IF; 472 else if ((flags & O_TRUNC) == O_TRUNC) 473 return FILE_OVERWRITE; 474 else 475 return FILE_OPEN; 476} 477 478#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 479int cifs_posix_open(const char *full_path, struct inode **pinode, 480 struct super_block *sb, int mode, unsigned int f_flags, 481 __u32 *poplock, __u16 *pnetfid, unsigned int xid) 482{ 483 int rc; 484 FILE_UNIX_BASIC_INFO *presp_data; 485 __u32 posix_flags = 0; 486 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 487 struct cifs_fattr fattr; 488 struct tcon_link *tlink; 489 struct cifs_tcon *tcon; 490 491 cifs_dbg(FYI, "posix open %s\n", full_path); 492 493 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); 494 if (presp_data == NULL) 495 return -ENOMEM; 496 497 tlink = cifs_sb_tlink(cifs_sb); 498 if (IS_ERR(tlink)) { 499 rc = PTR_ERR(tlink); 500 goto posix_open_ret; 501 } 502 503 tcon = tlink_tcon(tlink); 504 mode &= ~current_umask(); 505 506 posix_flags = cifs_posix_convert_flags(f_flags); 507 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, 508 poplock, full_path, cifs_sb->local_nls, 509 cifs_remap(cifs_sb)); 510 cifs_put_tlink(tlink); 511 512 if (rc) 513 goto posix_open_ret; 514 515 if (presp_data->Type == cpu_to_le32(-1)) 516 goto posix_open_ret; /* open ok, caller does qpathinfo */ 517 518 if (!pinode) 519 goto posix_open_ret; /* caller does not need info */ 520 521 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); 522 523 /* get new inode and set it up */ 524 if (*pinode == NULL) { 525 cifs_fill_uniqueid(sb, &fattr); 526 *pinode = cifs_iget(sb, &fattr); 527 if (!*pinode) { 528 rc = -ENOMEM; 529 goto posix_open_ret; 530 } 531 } else { 532 cifs_revalidate_mapping(*pinode); 533 rc = cifs_fattr_to_inode(*pinode, &fattr, false); 534 } 535 536posix_open_ret: 537 kfree(presp_data); 538 return rc; 539} 540#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 541 542static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, 543 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, 544 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf) 545{ 546 int rc; 547 int desired_access; 548 int disposition; 549 int create_options = CREATE_NOT_DIR; 550 struct TCP_Server_Info *server = tcon->ses->server; 551 struct cifs_open_parms oparms; 552 int rdwr_for_fscache = 0; 553 554 if (!server->ops->open) 555 return -ENOSYS; 556 557 /* If we're caching, we need to be able to fill in around partial writes. */ 558 if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY) 559 rdwr_for_fscache = 1; 560 561 desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache); 562 563/********************************************************************* 564 * open flag mapping table: 565 * 566 * POSIX Flag CIFS Disposition 567 * ---------- ---------------- 568 * O_CREAT FILE_OPEN_IF 569 * O_CREAT | O_EXCL FILE_CREATE 570 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF 571 * O_TRUNC FILE_OVERWRITE 572 * none of the above FILE_OPEN 573 * 574 * Note that there is not a direct match between disposition 575 * FILE_SUPERSEDE (ie create whether or not file exists although 576 * O_CREAT | O_TRUNC is similar but truncates the existing 577 * file rather than creating a new file as FILE_SUPERSEDE does 578 * (which uses the attributes / metadata passed in on open call) 579 *? 580 *? O_SYNC is a reasonable match to CIFS writethrough flag 581 *? and the read write flags match reasonably. O_LARGEFILE 582 *? is irrelevant because largefile support is always used 583 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, 584 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation 585 *********************************************************************/ 586 587 disposition = cifs_get_disposition(f_flags); 588 /* BB pass O_SYNC flag through on file attributes .. BB */ 589 create_options |= cifs_open_create_options(f_flags, create_options); 590 591retry_open: 592 oparms = (struct cifs_open_parms) { 593 .tcon = tcon, 594 .cifs_sb = cifs_sb, 595 .desired_access = desired_access, 596 .create_options = cifs_create_options(cifs_sb, create_options), 597 .disposition = disposition, 598 .path = full_path, 599 .fid = fid, 600 }; 601 602 rc = server->ops->open(xid, &oparms, oplock, buf); 603 if (rc) { 604 if (rc == -EACCES && rdwr_for_fscache == 1) { 605 desired_access = cifs_convert_flags(f_flags, 0); 606 rdwr_for_fscache = 2; 607 goto retry_open; 608 } 609 return rc; 610 } 611 if (rdwr_for_fscache == 2) 612 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 613 614 /* TODO: Add support for calling posix query info but with passing in fid */ 615 if (tcon->unix_ext) 616 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, 617 xid); 618 else 619 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, 620 xid, fid); 621 622 if (rc) { 623 server->ops->close(xid, tcon, fid); 624 if (rc == -ESTALE) 625 rc = -EOPENSTALE; 626 } 627 628 return rc; 629} 630 631static bool 632cifs_has_mand_locks(struct cifsInodeInfo *cinode) 633{ 634 struct cifs_fid_locks *cur; 635 bool has_locks = false; 636 637 down_read(&cinode->lock_sem); 638 list_for_each_entry(cur, &cinode->llist, llist) { 639 if (!list_empty(&cur->locks)) { 640 has_locks = true; 641 break; 642 } 643 } 644 up_read(&cinode->lock_sem); 645 return has_locks; 646} 647 648void 649cifs_down_write(struct rw_semaphore *sem) 650{ 651 while (!down_write_trylock(sem)) 652 msleep(10); 653} 654 655static void cifsFileInfo_put_work(struct work_struct *work); 656void serverclose_work(struct work_struct *work); 657 658struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, 659 struct tcon_link *tlink, __u32 oplock, 660 const char *symlink_target) 661{ 662 struct dentry *dentry = file_dentry(file); 663 struct inode *inode = d_inode(dentry); 664 struct cifsInodeInfo *cinode = CIFS_I(inode); 665 struct cifsFileInfo *cfile; 666 struct cifs_fid_locks *fdlocks; 667 struct cifs_tcon *tcon = tlink_tcon(tlink); 668 struct TCP_Server_Info *server = tcon->ses->server; 669 670 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 671 if (cfile == NULL) 672 return cfile; 673 674 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); 675 if (!fdlocks) { 676 kfree(cfile); 677 return NULL; 678 } 679 680 if (symlink_target) { 681 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL); 682 if (!cfile->symlink_target) { 683 kfree(fdlocks); 684 kfree(cfile); 685 return NULL; 686 } 687 } 688 689 INIT_LIST_HEAD(&fdlocks->locks); 690 fdlocks->cfile = cfile; 691 cfile->llist = fdlocks; 692 693 cfile->count = 1; 694 cfile->pid = current->tgid; 695 cfile->uid = current_fsuid(); 696 cfile->dentry = dget(dentry); 697 cfile->f_flags = file->f_flags; 698 cfile->invalidHandle = false; 699 cfile->deferred_close_scheduled = false; 700 cfile->tlink = cifs_get_tlink(tlink); 701 INIT_WORK(&cfile->oplock_break, cifs_oplock_break); 702 INIT_WORK(&cfile->put, cifsFileInfo_put_work); 703 INIT_WORK(&cfile->serverclose, serverclose_work); 704 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close); 705 mutex_init(&cfile->fh_mutex); 706 spin_lock_init(&cfile->file_info_lock); 707 708 /* 709 * If the server returned a read oplock and we have mandatory brlocks, 710 * set oplock level to None. 711 */ 712 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 713 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 714 oplock = 0; 715 } 716 717 cifs_down_write(&cinode->lock_sem); 718 list_add(&fdlocks->llist, &cinode->llist); 719 up_write(&cinode->lock_sem); 720 721 spin_lock(&tcon->open_file_lock); 722 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) 723 oplock = fid->pending_open->oplock; 724 list_del(&fid->pending_open->olist); 725 726 fid->purge_cache = false; 727 server->ops->set_fid(cfile, fid, oplock); 728 729 list_add(&cfile->tlist, &tcon->openFileList); 730 atomic_inc(&tcon->num_local_opens); 731 732 /* if readable file instance put first in list*/ 733 spin_lock(&cinode->open_file_lock); 734 if (file->f_mode & FMODE_READ) 735 list_add(&cfile->flist, &cinode->openFileList); 736 else 737 list_add_tail(&cfile->flist, &cinode->openFileList); 738 spin_unlock(&cinode->open_file_lock); 739 spin_unlock(&tcon->open_file_lock); 740 741 if (fid->purge_cache) 742 cifs_zap_mapping(inode); 743 744 file->private_data = cfile; 745 return cfile; 746} 747 748struct cifsFileInfo * 749cifsFileInfo_get(struct cifsFileInfo *cifs_file) 750{ 751 spin_lock(&cifs_file->file_info_lock); 752 cifsFileInfo_get_locked(cifs_file); 753 spin_unlock(&cifs_file->file_info_lock); 754 return cifs_file; 755} 756 757static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file) 758{ 759 struct inode *inode = d_inode(cifs_file->dentry); 760 struct cifsInodeInfo *cifsi = CIFS_I(inode); 761 struct cifsLockInfo *li, *tmp; 762 763 /* 764 * Delete any outstanding lock records. We'll lose them when the file 765 * is closed anyway. 766 */ 767 cifs_down_write(&cifsi->lock_sem); 768 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { 769 list_del(&li->llist); 770 cifs_del_lock_waiters(li); 771 kfree(li); 772 } 773 list_del(&cifs_file->llist->llist); 774 kfree(cifs_file->llist); 775 up_write(&cifsi->lock_sem); 776 777 cifs_put_tlink(cifs_file->tlink); 778 dput(cifs_file->dentry); 779 kfree(cifs_file->symlink_target); 780 kfree(cifs_file); 781} 782 783static void cifsFileInfo_put_work(struct work_struct *work) 784{ 785 struct cifsFileInfo *cifs_file = container_of(work, 786 struct cifsFileInfo, put); 787 788 cifsFileInfo_put_final(cifs_file); 789} 790 791void serverclose_work(struct work_struct *work) 792{ 793 struct cifsFileInfo *cifs_file = container_of(work, 794 struct cifsFileInfo, serverclose); 795 796 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 797 798 struct TCP_Server_Info *server = tcon->ses->server; 799 int rc = 0; 800 int retries = 0; 801 int MAX_RETRIES = 4; 802 803 do { 804 if (server->ops->close_getattr) 805 rc = server->ops->close_getattr(0, tcon, cifs_file); 806 else if (server->ops->close) 807 rc = server->ops->close(0, tcon, &cifs_file->fid); 808 809 if (rc == -EBUSY || rc == -EAGAIN) { 810 retries++; 811 msleep(250); 812 } 813 } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES) 814 ); 815 816 if (retries == MAX_RETRIES) 817 pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES); 818 819 if (cifs_file->offload) 820 queue_work(fileinfo_put_wq, &cifs_file->put); 821 else 822 cifsFileInfo_put_final(cifs_file); 823} 824 825/** 826 * cifsFileInfo_put - release a reference of file priv data 827 * 828 * Always potentially wait for oplock handler. See _cifsFileInfo_put(). 829 * 830 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 831 */ 832void cifsFileInfo_put(struct cifsFileInfo *cifs_file) 833{ 834 _cifsFileInfo_put(cifs_file, true, true); 835} 836 837/** 838 * _cifsFileInfo_put - release a reference of file priv data 839 * 840 * This may involve closing the filehandle @cifs_file out on the 841 * server. Must be called without holding tcon->open_file_lock, 842 * cinode->open_file_lock and cifs_file->file_info_lock. 843 * 844 * If @wait_for_oplock_handler is true and we are releasing the last 845 * reference, wait for any running oplock break handler of the file 846 * and cancel any pending one. 847 * 848 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 849 * @wait_oplock_handler: must be false if called from oplock_break_handler 850 * @offload: not offloaded on close and oplock breaks 851 * 852 */ 853void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, 854 bool wait_oplock_handler, bool offload) 855{ 856 struct inode *inode = d_inode(cifs_file->dentry); 857 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 858 struct TCP_Server_Info *server = tcon->ses->server; 859 struct cifsInodeInfo *cifsi = CIFS_I(inode); 860 struct super_block *sb = inode->i_sb; 861 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 862 struct cifs_fid fid = {}; 863 struct cifs_pending_open open; 864 bool oplock_break_cancelled; 865 bool serverclose_offloaded = false; 866 867 spin_lock(&tcon->open_file_lock); 868 spin_lock(&cifsi->open_file_lock); 869 spin_lock(&cifs_file->file_info_lock); 870 871 cifs_file->offload = offload; 872 if (--cifs_file->count > 0) { 873 spin_unlock(&cifs_file->file_info_lock); 874 spin_unlock(&cifsi->open_file_lock); 875 spin_unlock(&tcon->open_file_lock); 876 return; 877 } 878 spin_unlock(&cifs_file->file_info_lock); 879 880 if (server->ops->get_lease_key) 881 server->ops->get_lease_key(inode, &fid); 882 883 /* store open in pending opens to make sure we don't miss lease break */ 884 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); 885 886 /* remove it from the lists */ 887 list_del(&cifs_file->flist); 888 list_del(&cifs_file->tlist); 889 atomic_dec(&tcon->num_local_opens); 890 891 if (list_empty(&cifsi->openFileList)) { 892 cifs_dbg(FYI, "closing last open instance for inode %p\n", 893 d_inode(cifs_file->dentry)); 894 /* 895 * In strict cache mode we need invalidate mapping on the last 896 * close because it may cause a error when we open this file 897 * again and get at least level II oplock. 898 */ 899 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) 900 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); 901 cifs_set_oplock_level(cifsi, 0); 902 } 903 904 spin_unlock(&cifsi->open_file_lock); 905 spin_unlock(&tcon->open_file_lock); 906 907 oplock_break_cancelled = wait_oplock_handler ? 908 cancel_work_sync(&cifs_file->oplock_break) : false; 909 910 if (!tcon->need_reconnect && !cifs_file->invalidHandle) { 911 struct TCP_Server_Info *server = tcon->ses->server; 912 unsigned int xid; 913 int rc = 0; 914 915 xid = get_xid(); 916 if (server->ops->close_getattr) 917 rc = server->ops->close_getattr(xid, tcon, cifs_file); 918 else if (server->ops->close) 919 rc = server->ops->close(xid, tcon, &cifs_file->fid); 920 _free_xid(xid); 921 922 if (rc == -EBUSY || rc == -EAGAIN) { 923 // Server close failed, hence offloading it as an async op 924 queue_work(serverclose_wq, &cifs_file->serverclose); 925 serverclose_offloaded = true; 926 } 927 } 928 929 if (oplock_break_cancelled) 930 cifs_done_oplock_break(cifsi); 931 932 cifs_del_pending_open(&open); 933 934 // if serverclose has been offloaded to wq (on failure), it will 935 // handle offloading put as well. If serverclose not offloaded, 936 // we need to handle offloading put here. 937 if (!serverclose_offloaded) { 938 if (offload) 939 queue_work(fileinfo_put_wq, &cifs_file->put); 940 else 941 cifsFileInfo_put_final(cifs_file); 942 } 943} 944 945int cifs_file_flush(const unsigned int xid, struct inode *inode, 946 struct cifsFileInfo *cfile) 947{ 948 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 949 struct cifs_tcon *tcon; 950 int rc; 951 952 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC) 953 return 0; 954 955 if (cfile && (OPEN_FMODE(cfile->f_flags) & FMODE_WRITE)) { 956 tcon = tlink_tcon(cfile->tlink); 957 return tcon->ses->server->ops->flush(xid, tcon, 958 &cfile->fid); 959 } 960 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile); 961 if (!rc) { 962 tcon = tlink_tcon(cfile->tlink); 963 rc = tcon->ses->server->ops->flush(xid, tcon, &cfile->fid); 964 cifsFileInfo_put(cfile); 965 } else if (rc == -EBADF) { 966 rc = 0; 967 } 968 return rc; 969} 970 971static int cifs_do_truncate(const unsigned int xid, struct dentry *dentry) 972{ 973 struct cifsInodeInfo *cinode = CIFS_I(d_inode(dentry)); 974 struct inode *inode = d_inode(dentry); 975 struct cifsFileInfo *cfile = NULL; 976 struct TCP_Server_Info *server; 977 struct cifs_tcon *tcon; 978 int rc; 979 980 rc = filemap_write_and_wait(inode->i_mapping); 981 if (is_interrupt_error(rc)) 982 return -ERESTARTSYS; 983 mapping_set_error(inode->i_mapping, rc); 984 985 cfile = find_writable_file(cinode, FIND_WR_FSUID_ONLY); 986 rc = cifs_file_flush(xid, inode, cfile); 987 if (!rc) { 988 if (cfile) { 989 tcon = tlink_tcon(cfile->tlink); 990 server = tcon->ses->server; 991 rc = server->ops->set_file_size(xid, tcon, 992 cfile, 0, false); 993 } 994 if (!rc) { 995 netfs_resize_file(&cinode->netfs, 0, true); 996 cifs_setsize(inode, 0); 997 } 998 } 999 if (cfile) 1000 cifsFileInfo_put(cfile); 1001 return rc; 1002} 1003 1004int cifs_open(struct inode *inode, struct file *file) 1005 1006{ 1007 int rc = -EACCES; 1008 unsigned int xid; 1009 __u32 oplock; 1010 struct cifs_sb_info *cifs_sb; 1011 struct TCP_Server_Info *server; 1012 struct cifs_tcon *tcon; 1013 struct tcon_link *tlink; 1014 struct cifsFileInfo *cfile = NULL; 1015 void *page; 1016 const char *full_path; 1017 bool posix_open_ok = false; 1018 struct cifs_fid fid = {}; 1019 struct cifs_pending_open open; 1020 struct cifs_open_info_data data = {}; 1021 1022 xid = get_xid(); 1023 1024 cifs_sb = CIFS_SB(inode->i_sb); 1025 if (unlikely(cifs_forced_shutdown(cifs_sb))) { 1026 free_xid(xid); 1027 return smb_EIO(smb_eio_trace_forced_shutdown); 1028 } 1029 1030 tlink = cifs_sb_tlink(cifs_sb); 1031 if (IS_ERR(tlink)) { 1032 free_xid(xid); 1033 return PTR_ERR(tlink); 1034 } 1035 tcon = tlink_tcon(tlink); 1036 server = tcon->ses->server; 1037 1038 page = alloc_dentry_path(); 1039 full_path = build_path_from_dentry(file_dentry(file), page); 1040 if (IS_ERR(full_path)) { 1041 rc = PTR_ERR(full_path); 1042 goto out; 1043 } 1044 1045 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", 1046 inode, file->f_flags, full_path); 1047 1048 if (file->f_flags & O_DIRECT && 1049 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { 1050 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 1051 file->f_op = &cifs_file_direct_nobrl_ops; 1052 else 1053 file->f_op = &cifs_file_direct_ops; 1054 } 1055 1056 if (file->f_flags & O_TRUNC) { 1057 rc = cifs_do_truncate(xid, file_dentry(file)); 1058 if (rc) 1059 goto out; 1060 } 1061 1062 /* Get the cached handle as SMB2 close is deferred */ 1063 if (OPEN_FMODE(file->f_flags) & FMODE_WRITE) { 1064 rc = cifs_get_writable_path(tcon, full_path, 1065 FIND_WR_FSUID_ONLY | 1066 FIND_WR_NO_PENDING_DELETE, 1067 &cfile); 1068 } else { 1069 rc = cifs_get_readable_path(tcon, full_path, &cfile); 1070 } 1071 if (rc == 0) { 1072 unsigned int oflags = file->f_flags & ~(O_CREAT|O_EXCL|O_TRUNC); 1073 unsigned int cflags = cfile->f_flags & ~(O_CREAT|O_EXCL|O_TRUNC); 1074 1075 if (cifs_convert_flags(oflags, 0) == cifs_convert_flags(cflags, 0) && 1076 (oflags & (O_SYNC|O_DIRECT)) == (cflags & (O_SYNC|O_DIRECT))) { 1077 file->private_data = cfile; 1078 spin_lock(&CIFS_I(inode)->deferred_lock); 1079 cifs_del_deferred_close(cfile); 1080 spin_unlock(&CIFS_I(inode)->deferred_lock); 1081 goto use_cache; 1082 } 1083 _cifsFileInfo_put(cfile, true, false); 1084 } else { 1085 /* hard link on the defeered close file */ 1086 rc = cifs_get_hardlink_path(tcon, inode, file); 1087 if (rc) 1088 cifs_close_deferred_file(CIFS_I(inode)); 1089 } 1090 1091 if (server->oplocks) 1092 oplock = REQ_OPLOCK; 1093 else 1094 oplock = 0; 1095 1096#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1097 if (!tcon->broken_posix_open && tcon->unix_ext && 1098 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & 1099 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 1100 /* can not refresh inode info since size could be stale */ 1101 rc = cifs_posix_open(full_path, &inode, inode->i_sb, 1102 cifs_sb->ctx->file_mode /* ignored */, 1103 file->f_flags, &oplock, &fid.netfid, xid); 1104 if (rc == 0) { 1105 cifs_dbg(FYI, "posix open succeeded\n"); 1106 posix_open_ok = true; 1107 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 1108 if (tcon->ses->serverNOS) 1109 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", 1110 tcon->ses->ip_addr, 1111 tcon->ses->serverNOS); 1112 tcon->broken_posix_open = true; 1113 } else if ((rc != -EIO) && (rc != -EREMOTE) && 1114 (rc != -EOPNOTSUPP)) /* path not found or net err */ 1115 goto out; 1116 /* 1117 * Else fallthrough to retry open the old way on network i/o 1118 * or DFS errors. 1119 */ 1120 } 1121#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1122 1123 if (server->ops->get_lease_key) 1124 server->ops->get_lease_key(inode, &fid); 1125 1126 cifs_add_pending_open(&fid, tlink, &open); 1127 1128 if (!posix_open_ok) { 1129 if (server->ops->get_lease_key) 1130 server->ops->get_lease_key(inode, &fid); 1131 1132 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid, 1133 xid, &data); 1134 if (rc) { 1135 cifs_del_pending_open(&open); 1136 goto out; 1137 } 1138 } 1139 1140 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target); 1141 if (cfile == NULL) { 1142 if (server->ops->close) 1143 server->ops->close(xid, tcon, &fid); 1144 cifs_del_pending_open(&open); 1145 rc = -ENOMEM; 1146 goto out; 1147 } 1148 1149#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1150 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { 1151 /* 1152 * Time to set mode which we can not set earlier due to 1153 * problems creating new read-only files. 1154 */ 1155 struct cifs_unix_set_info_args args = { 1156 .mode = inode->i_mode, 1157 .uid = INVALID_UID, /* no change */ 1158 .gid = INVALID_GID, /* no change */ 1159 .ctime = NO_CHANGE_64, 1160 .atime = NO_CHANGE_64, 1161 .mtime = NO_CHANGE_64, 1162 .device = 0, 1163 }; 1164 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, 1165 cfile->pid); 1166 } 1167#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1168 1169use_cache: 1170 fscache_use_cookie(cifs_inode_cookie(file_inode(file)), 1171 file->f_mode & FMODE_WRITE); 1172 if (!(file->f_flags & O_DIRECT)) 1173 goto out; 1174 if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY) 1175 goto out; 1176 cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE); 1177 1178out: 1179 free_dentry_path(page); 1180 free_xid(xid); 1181 cifs_put_tlink(tlink); 1182 cifs_free_open_info(&data); 1183 return rc; 1184} 1185 1186#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1187static int cifs_push_posix_locks(struct cifsFileInfo *cfile); 1188#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1189 1190/* 1191 * Try to reacquire byte range locks that were released when session 1192 * to server was lost. 1193 */ 1194static int 1195cifs_relock_file(struct cifsFileInfo *cfile) 1196{ 1197 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1198 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1199 int rc = 0; 1200#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1201 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 1202#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1203 1204 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); 1205 if (cinode->can_cache_brlcks) { 1206 /* can cache locks - no need to relock */ 1207 up_read(&cinode->lock_sem); 1208 return rc; 1209 } 1210 1211#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1212 if (cap_unix(tcon->ses) && 1213 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 1214 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 1215 rc = cifs_push_posix_locks(cfile); 1216 else 1217#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1218 rc = tcon->ses->server->ops->push_mand_locks(cfile); 1219 1220 up_read(&cinode->lock_sem); 1221 return rc; 1222} 1223 1224static int 1225cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) 1226{ 1227 int rc = -EACCES; 1228 unsigned int xid; 1229 __u32 oplock; 1230 struct cifs_sb_info *cifs_sb; 1231 struct cifs_tcon *tcon; 1232 struct TCP_Server_Info *server; 1233 struct cifsInodeInfo *cinode; 1234 struct inode *inode; 1235 void *page; 1236 const char *full_path; 1237 int desired_access; 1238 int disposition = FILE_OPEN; 1239 int create_options = CREATE_NOT_DIR; 1240 struct cifs_open_parms oparms; 1241 int rdwr_for_fscache = 0; 1242 1243 xid = get_xid(); 1244 mutex_lock(&cfile->fh_mutex); 1245 if (!cfile->invalidHandle) { 1246 mutex_unlock(&cfile->fh_mutex); 1247 free_xid(xid); 1248 return 0; 1249 } 1250 1251 inode = d_inode(cfile->dentry); 1252 cifs_sb = CIFS_SB(inode->i_sb); 1253 tcon = tlink_tcon(cfile->tlink); 1254 server = tcon->ses->server; 1255 1256 /* 1257 * Can not grab rename sem here because various ops, including those 1258 * that already have the rename sem can end up causing writepage to get 1259 * called and if the server was down that means we end up here, and we 1260 * can never tell if the caller already has the rename_sem. 1261 */ 1262 page = alloc_dentry_path(); 1263 full_path = build_path_from_dentry(cfile->dentry, page); 1264 if (IS_ERR(full_path)) { 1265 mutex_unlock(&cfile->fh_mutex); 1266 free_dentry_path(page); 1267 free_xid(xid); 1268 return PTR_ERR(full_path); 1269 } 1270 1271 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", 1272 inode, cfile->f_flags, full_path); 1273 1274 if (tcon->ses->server->oplocks) 1275 oplock = REQ_OPLOCK; 1276 else 1277 oplock = 0; 1278 1279#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1280 if (tcon->unix_ext && cap_unix(tcon->ses) && 1281 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 1282 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 1283 /* 1284 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the 1285 * original open. Must mask them off for a reopen. 1286 */ 1287 unsigned int oflags = cfile->f_flags & 1288 ~(O_CREAT | O_EXCL | O_TRUNC); 1289 1290 rc = cifs_posix_open(full_path, NULL, inode->i_sb, 1291 cifs_sb->ctx->file_mode /* ignored */, 1292 oflags, &oplock, &cfile->fid.netfid, xid); 1293 if (rc == 0) { 1294 cifs_dbg(FYI, "posix reopen succeeded\n"); 1295 oparms.reconnect = true; 1296 goto reopen_success; 1297 } 1298 /* 1299 * fallthrough to retry open the old way on errors, especially 1300 * in the reconnect path it is important to retry hard 1301 */ 1302 } 1303#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1304 1305 /* If we're caching, we need to be able to fill in around partial writes. */ 1306 if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY) 1307 rdwr_for_fscache = 1; 1308 1309 desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache); 1310 create_options |= cifs_open_create_options(cfile->f_flags, 1311 create_options); 1312 1313 if (server->ops->get_lease_key) 1314 server->ops->get_lease_key(inode, &cfile->fid); 1315 1316retry_open: 1317 oparms = (struct cifs_open_parms) { 1318 .tcon = tcon, 1319 .cifs_sb = cifs_sb, 1320 .desired_access = desired_access, 1321 .create_options = cifs_create_options(cifs_sb, create_options), 1322 .disposition = disposition, 1323 .path = full_path, 1324 .fid = &cfile->fid, 1325 .reconnect = true, 1326 }; 1327 1328 /* 1329 * Can not refresh inode by passing in file_info buf to be returned by 1330 * ops->open and then calling get_inode_info with returned buf since 1331 * file might have write behind data that needs to be flushed and server 1332 * version of file size can be stale. If we knew for sure that inode was 1333 * not dirty locally we could do this. 1334 */ 1335 rc = server->ops->open(xid, &oparms, &oplock, NULL); 1336 if (rc == -ENOENT && oparms.reconnect == false) { 1337 /* durable handle timeout is expired - open the file again */ 1338 rc = server->ops->open(xid, &oparms, &oplock, NULL); 1339 /* indicate that we need to relock the file */ 1340 oparms.reconnect = true; 1341 } 1342 if (rc == -EACCES && rdwr_for_fscache == 1) { 1343 desired_access = cifs_convert_flags(cfile->f_flags, 0); 1344 rdwr_for_fscache = 2; 1345 goto retry_open; 1346 } 1347 1348 if (rc) { 1349 mutex_unlock(&cfile->fh_mutex); 1350 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); 1351 cifs_dbg(FYI, "oplock: %d\n", oplock); 1352 goto reopen_error_exit; 1353 } 1354 1355 if (rdwr_for_fscache == 2) 1356 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 1357 1358#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1359reopen_success: 1360#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1361 cfile->invalidHandle = false; 1362 mutex_unlock(&cfile->fh_mutex); 1363 cinode = CIFS_I(inode); 1364 1365 if (can_flush) { 1366 rc = filemap_write_and_wait(inode->i_mapping); 1367 if (!is_interrupt_error(rc)) 1368 mapping_set_error(inode->i_mapping, rc); 1369 1370 if (tcon->posix_extensions) { 1371 rc = smb311_posix_get_inode_info(&inode, full_path, 1372 NULL, inode->i_sb, xid); 1373 } else if (tcon->unix_ext) { 1374 rc = cifs_get_inode_info_unix(&inode, full_path, 1375 inode->i_sb, xid); 1376 } else { 1377 rc = cifs_get_inode_info(&inode, full_path, NULL, 1378 inode->i_sb, xid, NULL); 1379 } 1380 } 1381 /* 1382 * Else we are writing out data to server already and could deadlock if 1383 * we tried to flush data, and since we do not know if we have data that 1384 * would invalidate the current end of file on the server we can not go 1385 * to the server to get the new inode info. 1386 */ 1387 1388 /* 1389 * If the server returned a read oplock and we have mandatory brlocks, 1390 * set oplock level to None. 1391 */ 1392 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 1393 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 1394 oplock = 0; 1395 } 1396 1397 server->ops->set_fid(cfile, &cfile->fid, oplock); 1398 if (oparms.reconnect) 1399 cifs_relock_file(cfile); 1400 1401reopen_error_exit: 1402 free_dentry_path(page); 1403 free_xid(xid); 1404 return rc; 1405} 1406 1407void smb2_deferred_work_close(struct work_struct *work) 1408{ 1409 struct cifsFileInfo *cfile = container_of(work, 1410 struct cifsFileInfo, deferred.work); 1411 1412 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 1413 cifs_del_deferred_close(cfile); 1414 cfile->deferred_close_scheduled = false; 1415 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 1416 _cifsFileInfo_put(cfile, true, false); 1417} 1418 1419static bool 1420smb2_can_defer_close(struct inode *inode, struct cifs_deferred_close *dclose) 1421{ 1422 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1423 struct cifsInodeInfo *cinode = CIFS_I(inode); 1424 1425 return (cifs_sb->ctx->closetimeo && cinode->lease_granted && dclose && 1426 (cinode->oplock == CIFS_CACHE_RHW_FLG || 1427 cinode->oplock == CIFS_CACHE_RH_FLG) && 1428 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags)); 1429 1430} 1431 1432int cifs_close(struct inode *inode, struct file *file) 1433{ 1434 struct cifsFileInfo *cfile; 1435 struct cifsInodeInfo *cinode = CIFS_I(inode); 1436 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1437 struct cifs_deferred_close *dclose; 1438 1439 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE); 1440 1441 if (file->private_data != NULL) { 1442 cfile = file->private_data; 1443 file->private_data = NULL; 1444 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL); 1445 if ((cfile->status_file_deleted == false) && 1446 (smb2_can_defer_close(inode, dclose))) { 1447 if (test_and_clear_bit(NETFS_ICTX_MODIFIED_ATTR, &cinode->netfs.flags)) { 1448 inode_set_mtime_to_ts(inode, 1449 inode_set_ctime_current(inode)); 1450 } 1451 spin_lock(&cinode->deferred_lock); 1452 cifs_add_deferred_close(cfile, dclose); 1453 if (cfile->deferred_close_scheduled && 1454 delayed_work_pending(&cfile->deferred)) { 1455 /* 1456 * If there is no pending work, mod_delayed_work queues new work. 1457 * So, Increase the ref count to avoid use-after-free. 1458 */ 1459 if (!mod_delayed_work(deferredclose_wq, 1460 &cfile->deferred, cifs_sb->ctx->closetimeo)) 1461 cifsFileInfo_get(cfile); 1462 } else { 1463 /* Deferred close for files */ 1464 queue_delayed_work(deferredclose_wq, 1465 &cfile->deferred, cifs_sb->ctx->closetimeo); 1466 cfile->deferred_close_scheduled = true; 1467 spin_unlock(&cinode->deferred_lock); 1468 return 0; 1469 } 1470 spin_unlock(&cinode->deferred_lock); 1471 _cifsFileInfo_put(cfile, true, false); 1472 } else { 1473 _cifsFileInfo_put(cfile, true, false); 1474 kfree(dclose); 1475 } 1476 } 1477 1478 /* return code from the ->release op is always ignored */ 1479 return 0; 1480} 1481 1482void 1483cifs_reopen_persistent_handles(struct cifs_tcon *tcon) 1484{ 1485 struct cifsFileInfo *open_file, *tmp; 1486 LIST_HEAD(tmp_list); 1487 1488 if (!tcon->use_persistent || !tcon->need_reopen_files) 1489 return; 1490 1491 tcon->need_reopen_files = false; 1492 1493 cifs_dbg(FYI, "Reopen persistent handles\n"); 1494 1495 /* list all files open on tree connection, reopen resilient handles */ 1496 spin_lock(&tcon->open_file_lock); 1497 list_for_each_entry(open_file, &tcon->openFileList, tlist) { 1498 if (!open_file->invalidHandle) 1499 continue; 1500 cifsFileInfo_get(open_file); 1501 list_add_tail(&open_file->rlist, &tmp_list); 1502 } 1503 spin_unlock(&tcon->open_file_lock); 1504 1505 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) { 1506 if (cifs_reopen_file(open_file, false /* do not flush */)) 1507 tcon->need_reopen_files = true; 1508 list_del_init(&open_file->rlist); 1509 cifsFileInfo_put(open_file); 1510 } 1511} 1512 1513int cifs_closedir(struct inode *inode, struct file *file) 1514{ 1515 int rc = 0; 1516 unsigned int xid; 1517 struct cifsFileInfo *cfile = file->private_data; 1518 struct cifs_tcon *tcon; 1519 struct TCP_Server_Info *server; 1520 char *buf; 1521 1522 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); 1523 1524 if (cfile == NULL) 1525 return rc; 1526 1527 xid = get_xid(); 1528 tcon = tlink_tcon(cfile->tlink); 1529 server = tcon->ses->server; 1530 1531 cifs_dbg(FYI, "Freeing private data in close dir\n"); 1532 spin_lock(&cfile->file_info_lock); 1533 if (server->ops->dir_needs_close(cfile)) { 1534 cfile->invalidHandle = true; 1535 spin_unlock(&cfile->file_info_lock); 1536 if (server->ops->close_dir) 1537 rc = server->ops->close_dir(xid, tcon, &cfile->fid); 1538 else 1539 rc = -ENOSYS; 1540 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); 1541 /* not much we can do if it fails anyway, ignore rc */ 1542 rc = 0; 1543 } else 1544 spin_unlock(&cfile->file_info_lock); 1545 1546 buf = cfile->srch_inf.ntwrk_buf_start; 1547 if (buf) { 1548 cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); 1549 cfile->srch_inf.ntwrk_buf_start = NULL; 1550 if (cfile->srch_inf.smallBuf) 1551 cifs_small_buf_release(buf); 1552 else 1553 cifs_buf_release(buf); 1554 } 1555 1556 cifs_put_tlink(cfile->tlink); 1557 kfree(file->private_data); 1558 file->private_data = NULL; 1559 /* BB can we lock the filestruct while this is going on? */ 1560 free_xid(xid); 1561 return rc; 1562} 1563 1564static struct cifsLockInfo * 1565cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags) 1566{ 1567 struct cifsLockInfo *lock = 1568 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); 1569 if (!lock) 1570 return lock; 1571 lock->offset = offset; 1572 lock->length = length; 1573 lock->type = type; 1574 lock->pid = current->tgid; 1575 lock->flags = flags; 1576 INIT_LIST_HEAD(&lock->blist); 1577 init_waitqueue_head(&lock->block_q); 1578 return lock; 1579} 1580 1581void 1582cifs_del_lock_waiters(struct cifsLockInfo *lock) 1583{ 1584 struct cifsLockInfo *li, *tmp; 1585 list_for_each_entry_safe(li, tmp, &lock->blist, blist) { 1586 list_del_init(&li->blist); 1587 wake_up(&li->block_q); 1588 } 1589} 1590 1591#define CIFS_LOCK_OP 0 1592#define CIFS_READ_OP 1 1593#define CIFS_WRITE_OP 2 1594 1595/* @rw_check : 0 - no op, 1 - read, 2 - write */ 1596static bool 1597cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, 1598 __u64 length, __u8 type, __u16 flags, 1599 struct cifsFileInfo *cfile, 1600 struct cifsLockInfo **conf_lock, int rw_check) 1601{ 1602 struct cifsLockInfo *li; 1603 struct cifsFileInfo *cur_cfile = fdlocks->cfile; 1604 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 1605 1606 list_for_each_entry(li, &fdlocks->locks, llist) { 1607 if (offset + length <= li->offset || 1608 offset >= li->offset + li->length) 1609 continue; 1610 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && 1611 server->ops->compare_fids(cfile, cur_cfile)) { 1612 /* shared lock prevents write op through the same fid */ 1613 if (!(li->type & server->vals->shared_lock_type) || 1614 rw_check != CIFS_WRITE_OP) 1615 continue; 1616 } 1617 if ((type & server->vals->shared_lock_type) && 1618 ((server->ops->compare_fids(cfile, cur_cfile) && 1619 current->tgid == li->pid) || type == li->type)) 1620 continue; 1621 if (rw_check == CIFS_LOCK_OP && 1622 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) && 1623 server->ops->compare_fids(cfile, cur_cfile)) 1624 continue; 1625 if (conf_lock) 1626 *conf_lock = li; 1627 return true; 1628 } 1629 return false; 1630} 1631 1632bool 1633cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 1634 __u8 type, __u16 flags, 1635 struct cifsLockInfo **conf_lock, int rw_check) 1636{ 1637 bool rc = false; 1638 struct cifs_fid_locks *cur; 1639 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1640 1641 list_for_each_entry(cur, &cinode->llist, llist) { 1642 rc = cifs_find_fid_lock_conflict(cur, offset, length, type, 1643 flags, cfile, conf_lock, 1644 rw_check); 1645 if (rc) 1646 break; 1647 } 1648 1649 return rc; 1650} 1651 1652/* 1653 * Check if there is another lock that prevents us to set the lock (mandatory 1654 * style). If such a lock exists, update the flock structure with its 1655 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 1656 * or leave it the same if we can't. Returns 0 if we don't need to request to 1657 * the server or 1 otherwise. 1658 */ 1659static int 1660cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 1661 __u8 type, struct file_lock *flock) 1662{ 1663 int rc = 0; 1664 struct cifsLockInfo *conf_lock; 1665 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1666 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 1667 bool exist; 1668 1669 down_read(&cinode->lock_sem); 1670 1671 exist = cifs_find_lock_conflict(cfile, offset, length, type, 1672 flock->c.flc_flags, &conf_lock, 1673 CIFS_LOCK_OP); 1674 if (exist) { 1675 flock->fl_start = conf_lock->offset; 1676 flock->fl_end = conf_lock->offset + conf_lock->length - 1; 1677 flock->c.flc_pid = conf_lock->pid; 1678 if (conf_lock->type & server->vals->shared_lock_type) 1679 flock->c.flc_type = F_RDLCK; 1680 else 1681 flock->c.flc_type = F_WRLCK; 1682 } else if (!cinode->can_cache_brlcks) 1683 rc = 1; 1684 else 1685 flock->c.flc_type = F_UNLCK; 1686 1687 up_read(&cinode->lock_sem); 1688 return rc; 1689} 1690 1691static void 1692cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) 1693{ 1694 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1695 cifs_down_write(&cinode->lock_sem); 1696 list_add_tail(&lock->llist, &cfile->llist->locks); 1697 up_write(&cinode->lock_sem); 1698} 1699 1700/* 1701 * Set the byte-range lock (mandatory style). Returns: 1702 * 1) 0, if we set the lock and don't need to request to the server; 1703 * 2) 1, if no locks prevent us but we need to request to the server; 1704 * 3) -EACCES, if there is a lock that prevents us and wait is false. 1705 */ 1706static int 1707cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, 1708 bool wait) 1709{ 1710 struct cifsLockInfo *conf_lock; 1711 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1712 bool exist; 1713 int rc = 0; 1714 1715try_again: 1716 exist = false; 1717 cifs_down_write(&cinode->lock_sem); 1718 1719 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, 1720 lock->type, lock->flags, &conf_lock, 1721 CIFS_LOCK_OP); 1722 if (!exist && cinode->can_cache_brlcks) { 1723 list_add_tail(&lock->llist, &cfile->llist->locks); 1724 up_write(&cinode->lock_sem); 1725 return rc; 1726 } 1727 1728 if (!exist) 1729 rc = 1; 1730 else if (!wait) 1731 rc = -EACCES; 1732 else { 1733 list_add_tail(&lock->blist, &conf_lock->blist); 1734 up_write(&cinode->lock_sem); 1735 rc = wait_event_interruptible(lock->block_q, 1736 (lock->blist.prev == &lock->blist) && 1737 (lock->blist.next == &lock->blist)); 1738 if (!rc) 1739 goto try_again; 1740 cifs_down_write(&cinode->lock_sem); 1741 list_del_init(&lock->blist); 1742 } 1743 1744 up_write(&cinode->lock_sem); 1745 return rc; 1746} 1747 1748#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1749/* 1750 * Check if there is another lock that prevents us to set the lock (posix 1751 * style). If such a lock exists, update the flock structure with its 1752 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 1753 * or leave it the same if we can't. Returns 0 if we don't need to request to 1754 * the server or 1 otherwise. 1755 */ 1756static int 1757cifs_posix_lock_test(struct file *file, struct file_lock *flock) 1758{ 1759 int rc = 0; 1760 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 1761 unsigned char saved_type = flock->c.flc_type; 1762 1763 if ((flock->c.flc_flags & FL_POSIX) == 0) 1764 return 1; 1765 1766 down_read(&cinode->lock_sem); 1767 posix_test_lock(file, flock); 1768 1769 if (lock_is_unlock(flock) && !cinode->can_cache_brlcks) { 1770 flock->c.flc_type = saved_type; 1771 rc = 1; 1772 } 1773 1774 up_read(&cinode->lock_sem); 1775 return rc; 1776} 1777 1778/* 1779 * Set the byte-range lock (posix style). Returns: 1780 * 1) <0, if the error occurs while setting the lock; 1781 * 2) 0, if we set the lock and don't need to request to the server; 1782 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock; 1783 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server. 1784 */ 1785static int 1786cifs_posix_lock_set(struct file *file, struct file_lock *flock) 1787{ 1788 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 1789 int rc = FILE_LOCK_DEFERRED + 1; 1790 1791 if ((flock->c.flc_flags & FL_POSIX) == 0) 1792 return rc; 1793 1794 cifs_down_write(&cinode->lock_sem); 1795 if (!cinode->can_cache_brlcks) { 1796 up_write(&cinode->lock_sem); 1797 return rc; 1798 } 1799 1800 rc = posix_lock_file(file, flock, NULL); 1801 up_write(&cinode->lock_sem); 1802 return rc; 1803} 1804 1805int 1806cifs_push_mandatory_locks(struct cifsFileInfo *cfile) 1807{ 1808 unsigned int xid; 1809 int rc = 0, stored_rc; 1810 struct cifsLockInfo *li, *tmp; 1811 struct cifs_tcon *tcon; 1812 unsigned int num, max_num, max_buf; 1813 LOCKING_ANDX_RANGE *buf, *cur; 1814 static const int types[] = { 1815 LOCKING_ANDX_LARGE_FILES, 1816 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 1817 }; 1818 int i; 1819 1820 xid = get_xid(); 1821 tcon = tlink_tcon(cfile->tlink); 1822 1823 /* 1824 * Accessing maxBuf is racy with cifs_reconnect - need to store value 1825 * and check it before using. 1826 */ 1827 max_buf = tcon->ses->server->maxBuf; 1828 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { 1829 free_xid(xid); 1830 return -EINVAL; 1831 } 1832 1833 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 1834 PAGE_SIZE); 1835 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 1836 PAGE_SIZE); 1837 max_num = (max_buf - sizeof(struct smb_hdr)) / 1838 sizeof(LOCKING_ANDX_RANGE); 1839 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); 1840 if (!buf) { 1841 free_xid(xid); 1842 return -ENOMEM; 1843 } 1844 1845 for (i = 0; i < 2; i++) { 1846 cur = buf; 1847 num = 0; 1848 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 1849 if (li->type != types[i]) 1850 continue; 1851 cur->Pid = cpu_to_le16(li->pid); 1852 cur->LengthLow = cpu_to_le32((u32)li->length); 1853 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 1854 cur->OffsetLow = cpu_to_le32((u32)li->offset); 1855 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 1856 if (++num == max_num) { 1857 stored_rc = cifs_lockv(xid, tcon, 1858 cfile->fid.netfid, 1859 (__u8)li->type, 0, num, 1860 buf); 1861 if (stored_rc) 1862 rc = stored_rc; 1863 cur = buf; 1864 num = 0; 1865 } else 1866 cur++; 1867 } 1868 1869 if (num) { 1870 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 1871 (__u8)types[i], 0, num, buf); 1872 if (stored_rc) 1873 rc = stored_rc; 1874 } 1875 } 1876 1877 kfree(buf); 1878 free_xid(xid); 1879 return rc; 1880} 1881 1882static __u32 1883hash_lockowner(fl_owner_t owner) 1884{ 1885 return cifs_lock_secret ^ hash32_ptr((const void *)owner); 1886} 1887#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1888 1889struct lock_to_push { 1890 struct list_head llist; 1891 __u64 offset; 1892 __u64 length; 1893 __u32 pid; 1894 __u16 netfid; 1895 __u8 type; 1896}; 1897 1898#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1899static int 1900cifs_push_posix_locks(struct cifsFileInfo *cfile) 1901{ 1902 struct inode *inode = d_inode(cfile->dentry); 1903 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1904 struct file_lock *flock; 1905 struct file_lock_context *flctx = locks_inode_context(inode); 1906 unsigned int count = 0, i; 1907 int rc = 0, xid, type; 1908 struct list_head locks_to_send, *el; 1909 struct lock_to_push *lck, *tmp; 1910 __u64 length; 1911 1912 xid = get_xid(); 1913 1914 if (!flctx) 1915 goto out; 1916 1917 spin_lock(&flctx->flc_lock); 1918 list_for_each(el, &flctx->flc_posix) { 1919 count++; 1920 } 1921 spin_unlock(&flctx->flc_lock); 1922 1923 INIT_LIST_HEAD(&locks_to_send); 1924 1925 /* 1926 * Allocating count locks is enough because no FL_POSIX locks can be 1927 * added to the list while we are holding cinode->lock_sem that 1928 * protects locking operations of this inode. 1929 */ 1930 for (i = 0; i < count; i++) { 1931 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); 1932 if (!lck) { 1933 rc = -ENOMEM; 1934 goto err_out; 1935 } 1936 list_add_tail(&lck->llist, &locks_to_send); 1937 } 1938 1939 el = locks_to_send.next; 1940 spin_lock(&flctx->flc_lock); 1941 for_each_file_lock(flock, &flctx->flc_posix) { 1942 unsigned char ftype = flock->c.flc_type; 1943 1944 if (el == &locks_to_send) { 1945 /* 1946 * The list ended. We don't have enough allocated 1947 * structures - something is really wrong. 1948 */ 1949 cifs_dbg(VFS, "Can't push all brlocks!\n"); 1950 break; 1951 } 1952 length = cifs_flock_len(flock); 1953 if (ftype == F_RDLCK || ftype == F_SHLCK) 1954 type = CIFS_RDLCK; 1955 else 1956 type = CIFS_WRLCK; 1957 lck = list_entry(el, struct lock_to_push, llist); 1958 lck->pid = hash_lockowner(flock->c.flc_owner); 1959 lck->netfid = cfile->fid.netfid; 1960 lck->length = length; 1961 lck->type = type; 1962 lck->offset = flock->fl_start; 1963 } 1964 spin_unlock(&flctx->flc_lock); 1965 1966 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 1967 int stored_rc; 1968 1969 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, 1970 lck->offset, lck->length, NULL, 1971 lck->type, 0); 1972 if (stored_rc) 1973 rc = stored_rc; 1974 list_del(&lck->llist); 1975 kfree(lck); 1976 } 1977 1978out: 1979 free_xid(xid); 1980 return rc; 1981err_out: 1982 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 1983 list_del(&lck->llist); 1984 kfree(lck); 1985 } 1986 goto out; 1987} 1988#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1989 1990static int 1991cifs_push_locks(struct cifsFileInfo *cfile) 1992{ 1993 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1994 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1995 int rc = 0; 1996#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1997 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 1998#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1999 2000 /* we are going to update can_cache_brlcks here - need a write access */ 2001 cifs_down_write(&cinode->lock_sem); 2002 if (!cinode->can_cache_brlcks) { 2003 up_write(&cinode->lock_sem); 2004 return rc; 2005 } 2006 2007#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2008 if (cap_unix(tcon->ses) && 2009 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2010 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2011 rc = cifs_push_posix_locks(cfile); 2012 else 2013#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2014 rc = tcon->ses->server->ops->push_mand_locks(cfile); 2015 2016 cinode->can_cache_brlcks = false; 2017 up_write(&cinode->lock_sem); 2018 return rc; 2019} 2020 2021static void 2022cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, 2023 bool *wait_flag, struct TCP_Server_Info *server) 2024{ 2025 if (flock->c.flc_flags & FL_POSIX) 2026 cifs_dbg(FYI, "Posix\n"); 2027 if (flock->c.flc_flags & FL_FLOCK) 2028 cifs_dbg(FYI, "Flock\n"); 2029 if (flock->c.flc_flags & FL_SLEEP) { 2030 cifs_dbg(FYI, "Blocking lock\n"); 2031 *wait_flag = true; 2032 } 2033 if (flock->c.flc_flags & FL_ACCESS) 2034 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); 2035 if (flock->c.flc_flags & FL_LEASE) 2036 cifs_dbg(FYI, "Lease on file - not implemented yet\n"); 2037 if (flock->c.flc_flags & 2038 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | 2039 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK))) 2040 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", 2041 flock->c.flc_flags); 2042 2043 *type = server->vals->large_lock_type; 2044 if (lock_is_write(flock)) { 2045 cifs_dbg(FYI, "F_WRLCK\n"); 2046 *type |= server->vals->exclusive_lock_type; 2047 *lock = 1; 2048 } else if (lock_is_unlock(flock)) { 2049 cifs_dbg(FYI, "F_UNLCK\n"); 2050 *type |= server->vals->unlock_lock_type; 2051 *unlock = 1; 2052 /* Check if unlock includes more than one lock range */ 2053 } else if (lock_is_read(flock)) { 2054 cifs_dbg(FYI, "F_RDLCK\n"); 2055 *type |= server->vals->shared_lock_type; 2056 *lock = 1; 2057 } else if (flock->c.flc_type == F_EXLCK) { 2058 cifs_dbg(FYI, "F_EXLCK\n"); 2059 *type |= server->vals->exclusive_lock_type; 2060 *lock = 1; 2061 } else if (flock->c.flc_type == F_SHLCK) { 2062 cifs_dbg(FYI, "F_SHLCK\n"); 2063 *type |= server->vals->shared_lock_type; 2064 *lock = 1; 2065 } else 2066 cifs_dbg(FYI, "Unknown type of lock\n"); 2067} 2068 2069static int 2070cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, 2071 bool wait_flag, bool posix_lck, unsigned int xid) 2072{ 2073 int rc = 0; 2074 __u64 length = cifs_flock_len(flock); 2075 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2076 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2077 struct TCP_Server_Info *server = tcon->ses->server; 2078#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2079 __u16 netfid = cfile->fid.netfid; 2080 2081 if (posix_lck) { 2082 int posix_lock_type; 2083 2084 rc = cifs_posix_lock_test(file, flock); 2085 if (!rc) 2086 return rc; 2087 2088 if (type & server->vals->shared_lock_type) 2089 posix_lock_type = CIFS_RDLCK; 2090 else 2091 posix_lock_type = CIFS_WRLCK; 2092 rc = CIFSSMBPosixLock(xid, tcon, netfid, 2093 hash_lockowner(flock->c.flc_owner), 2094 flock->fl_start, length, flock, 2095 posix_lock_type, wait_flag); 2096 return rc; 2097 } 2098#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2099 2100 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); 2101 if (!rc) 2102 return rc; 2103 2104 /* BB we could chain these into one lock request BB */ 2105 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, 2106 1, 0, false); 2107 if (rc == 0) { 2108 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2109 type, 0, 1, false); 2110 flock->c.flc_type = F_UNLCK; 2111 if (rc != 0) 2112 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 2113 rc); 2114 return 0; 2115 } 2116 2117 if (type & server->vals->shared_lock_type) { 2118 flock->c.flc_type = F_WRLCK; 2119 return 0; 2120 } 2121 2122 type &= ~server->vals->exclusive_lock_type; 2123 2124 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2125 type | server->vals->shared_lock_type, 2126 1, 0, false); 2127 if (rc == 0) { 2128 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2129 type | server->vals->shared_lock_type, 0, 1, false); 2130 flock->c.flc_type = F_RDLCK; 2131 if (rc != 0) 2132 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 2133 rc); 2134 } else 2135 flock->c.flc_type = F_WRLCK; 2136 2137 return 0; 2138} 2139 2140void 2141cifs_move_llist(struct list_head *source, struct list_head *dest) 2142{ 2143 struct list_head *li, *tmp; 2144 list_for_each_safe(li, tmp, source) 2145 list_move(li, dest); 2146} 2147 2148int 2149cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode, 2150 struct file *file) 2151{ 2152 struct cifsFileInfo *open_file = NULL; 2153 struct cifsInodeInfo *cinode = CIFS_I(inode); 2154 int rc = 0; 2155 2156 spin_lock(&tcon->open_file_lock); 2157 spin_lock(&cinode->open_file_lock); 2158 2159 list_for_each_entry(open_file, &cinode->openFileList, flist) { 2160 if (file->f_flags == open_file->f_flags) { 2161 rc = -EINVAL; 2162 break; 2163 } 2164 } 2165 2166 spin_unlock(&cinode->open_file_lock); 2167 spin_unlock(&tcon->open_file_lock); 2168 return rc; 2169} 2170 2171void 2172cifs_free_llist(struct list_head *llist) 2173{ 2174 struct cifsLockInfo *li, *tmp; 2175 list_for_each_entry_safe(li, tmp, llist, llist) { 2176 cifs_del_lock_waiters(li); 2177 list_del(&li->llist); 2178 kfree(li); 2179 } 2180} 2181 2182#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2183int 2184cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, 2185 unsigned int xid) 2186{ 2187 int rc = 0, stored_rc; 2188 static const int types[] = { 2189 LOCKING_ANDX_LARGE_FILES, 2190 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 2191 }; 2192 unsigned int i; 2193 unsigned int max_num, num, max_buf; 2194 LOCKING_ANDX_RANGE *buf, *cur; 2195 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2196 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2197 struct cifsLockInfo *li, *tmp; 2198 __u64 length = cifs_flock_len(flock); 2199 LIST_HEAD(tmp_llist); 2200 2201 /* 2202 * Accessing maxBuf is racy with cifs_reconnect - need to store value 2203 * and check it before using. 2204 */ 2205 max_buf = tcon->ses->server->maxBuf; 2206 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) 2207 return -EINVAL; 2208 2209 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 2210 PAGE_SIZE); 2211 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 2212 PAGE_SIZE); 2213 max_num = (max_buf - sizeof(struct smb_hdr)) / 2214 sizeof(LOCKING_ANDX_RANGE); 2215 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); 2216 if (!buf) 2217 return -ENOMEM; 2218 2219 cifs_down_write(&cinode->lock_sem); 2220 for (i = 0; i < 2; i++) { 2221 cur = buf; 2222 num = 0; 2223 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 2224 if (flock->fl_start > li->offset || 2225 (flock->fl_start + length) < 2226 (li->offset + li->length)) 2227 continue; 2228 if (current->tgid != li->pid) 2229 continue; 2230 if (types[i] != li->type) 2231 continue; 2232 if (cinode->can_cache_brlcks) { 2233 /* 2234 * We can cache brlock requests - simply remove 2235 * a lock from the file's list. 2236 */ 2237 list_del(&li->llist); 2238 cifs_del_lock_waiters(li); 2239 kfree(li); 2240 continue; 2241 } 2242 cur->Pid = cpu_to_le16(li->pid); 2243 cur->LengthLow = cpu_to_le32((u32)li->length); 2244 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 2245 cur->OffsetLow = cpu_to_le32((u32)li->offset); 2246 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 2247 /* 2248 * We need to save a lock here to let us add it again to 2249 * the file's list if the unlock range request fails on 2250 * the server. 2251 */ 2252 list_move(&li->llist, &tmp_llist); 2253 if (++num == max_num) { 2254 stored_rc = cifs_lockv(xid, tcon, 2255 cfile->fid.netfid, 2256 li->type, num, 0, buf); 2257 if (stored_rc) { 2258 /* 2259 * We failed on the unlock range 2260 * request - add all locks from the tmp 2261 * list to the head of the file's list. 2262 */ 2263 cifs_move_llist(&tmp_llist, 2264 &cfile->llist->locks); 2265 rc = stored_rc; 2266 } else 2267 /* 2268 * The unlock range request succeed - 2269 * free the tmp list. 2270 */ 2271 cifs_free_llist(&tmp_llist); 2272 cur = buf; 2273 num = 0; 2274 } else 2275 cur++; 2276 } 2277 if (num) { 2278 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 2279 types[i], num, 0, buf); 2280 if (stored_rc) { 2281 cifs_move_llist(&tmp_llist, 2282 &cfile->llist->locks); 2283 rc = stored_rc; 2284 } else 2285 cifs_free_llist(&tmp_llist); 2286 } 2287 } 2288 2289 up_write(&cinode->lock_sem); 2290 kfree(buf); 2291 return rc; 2292} 2293#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2294 2295static int 2296cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, 2297 bool wait_flag, bool posix_lck, int lock, int unlock, 2298 unsigned int xid) 2299{ 2300 int rc = 0; 2301 __u64 length = cifs_flock_len(flock); 2302 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2303 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2304 struct TCP_Server_Info *server = tcon->ses->server; 2305 struct inode *inode = d_inode(cfile->dentry); 2306 2307#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2308 if (posix_lck) { 2309 int posix_lock_type; 2310 2311 rc = cifs_posix_lock_set(file, flock); 2312 if (rc <= FILE_LOCK_DEFERRED) 2313 return rc; 2314 2315 if (type & server->vals->shared_lock_type) 2316 posix_lock_type = CIFS_RDLCK; 2317 else 2318 posix_lock_type = CIFS_WRLCK; 2319 2320 if (unlock == 1) 2321 posix_lock_type = CIFS_UNLCK; 2322 2323 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, 2324 hash_lockowner(flock->c.flc_owner), 2325 flock->fl_start, length, 2326 NULL, posix_lock_type, wait_flag); 2327 goto out; 2328 } 2329#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2330 if (lock) { 2331 struct cifsLockInfo *lock; 2332 2333 lock = cifs_lock_init(flock->fl_start, length, type, 2334 flock->c.flc_flags); 2335 if (!lock) 2336 return -ENOMEM; 2337 2338 rc = cifs_lock_add_if(cfile, lock, wait_flag); 2339 if (rc < 0) { 2340 kfree(lock); 2341 return rc; 2342 } 2343 if (!rc) 2344 goto out; 2345 2346 /* 2347 * Windows 7 server can delay breaking lease from read to None 2348 * if we set a byte-range lock on a file - break it explicitly 2349 * before sending the lock to the server to be sure the next 2350 * read won't conflict with non-overlapted locks due to 2351 * pagereading. 2352 */ 2353 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && 2354 CIFS_CACHE_READ(CIFS_I(inode))) { 2355 cifs_zap_mapping(inode); 2356 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", 2357 inode); 2358 CIFS_I(inode)->oplock = 0; 2359 } 2360 2361 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2362 type, 1, 0, wait_flag); 2363 if (rc) { 2364 kfree(lock); 2365 return rc; 2366 } 2367 2368 cifs_lock_add(cfile, lock); 2369 } else if (unlock) 2370 rc = server->ops->mand_unlock_range(cfile, flock, xid); 2371 2372out: 2373 if ((flock->c.flc_flags & FL_POSIX) || (flock->c.flc_flags & FL_FLOCK)) { 2374 /* 2375 * If this is a request to remove all locks because we 2376 * are closing the file, it doesn't matter if the 2377 * unlocking failed as both cifs.ko and the SMB server 2378 * remove the lock on file close 2379 */ 2380 if (rc) { 2381 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); 2382 if (!(flock->c.flc_flags & FL_CLOSE)) 2383 return rc; 2384 } 2385 rc = locks_lock_file_wait(file, flock); 2386 } 2387 return rc; 2388} 2389 2390int cifs_flock(struct file *file, int cmd, struct file_lock *fl) 2391{ 2392 int rc, xid; 2393 int lock = 0, unlock = 0; 2394 bool wait_flag = false; 2395 bool posix_lck = false; 2396 struct cifs_sb_info *cifs_sb; 2397 struct cifs_tcon *tcon; 2398 struct cifsFileInfo *cfile; 2399 __u32 type; 2400 2401 xid = get_xid(); 2402 2403 if (!(fl->c.flc_flags & FL_FLOCK)) { 2404 rc = -ENOLCK; 2405 free_xid(xid); 2406 return rc; 2407 } 2408 2409 cfile = (struct cifsFileInfo *)file->private_data; 2410 tcon = tlink_tcon(cfile->tlink); 2411 2412 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag, 2413 tcon->ses->server); 2414 cifs_sb = CIFS_FILE_SB(file); 2415 2416 if (cap_unix(tcon->ses) && 2417 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2418 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2419 posix_lck = true; 2420 2421 if (!lock && !unlock) { 2422 /* 2423 * if no lock or unlock then nothing to do since we do not 2424 * know what it is 2425 */ 2426 rc = -EOPNOTSUPP; 2427 free_xid(xid); 2428 return rc; 2429 } 2430 2431 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock, 2432 xid); 2433 free_xid(xid); 2434 return rc; 2435 2436 2437} 2438 2439int cifs_lock(struct file *file, int cmd, struct file_lock *flock) 2440{ 2441 int rc, xid; 2442 int lock = 0, unlock = 0; 2443 bool wait_flag = false; 2444 bool posix_lck = false; 2445 struct cifs_sb_info *cifs_sb; 2446 struct cifs_tcon *tcon; 2447 struct cifsFileInfo *cfile; 2448 __u32 type; 2449 2450 rc = -EACCES; 2451 xid = get_xid(); 2452 2453 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd, 2454 flock->c.flc_flags, flock->c.flc_type, 2455 (long long)flock->fl_start, 2456 (long long)flock->fl_end); 2457 2458 cfile = (struct cifsFileInfo *)file->private_data; 2459 tcon = tlink_tcon(cfile->tlink); 2460 2461 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, 2462 tcon->ses->server); 2463 cifs_sb = CIFS_FILE_SB(file); 2464 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags); 2465 2466 if (cap_unix(tcon->ses) && 2467 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2468 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2469 posix_lck = true; 2470 /* 2471 * BB add code here to normalize offset and length to account for 2472 * negative length which we can not accept over the wire. 2473 */ 2474 if (IS_GETLK(cmd)) { 2475 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); 2476 free_xid(xid); 2477 return rc; 2478 } 2479 2480 if (!lock && !unlock) { 2481 /* 2482 * if no lock or unlock then nothing to do since we do not 2483 * know what it is 2484 */ 2485 free_xid(xid); 2486 return -EOPNOTSUPP; 2487 } 2488 2489 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, 2490 xid); 2491 free_xid(xid); 2492 return rc; 2493} 2494 2495void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata, ssize_t result) 2496{ 2497 struct netfs_io_request *wreq = wdata->rreq; 2498 struct netfs_inode *ictx = netfs_inode(wreq->inode); 2499 loff_t wrend; 2500 2501 if (result > 0) { 2502 wrend = wdata->subreq.start + wdata->subreq.transferred + result; 2503 2504 if (wrend > ictx->zero_point && 2505 (wdata->rreq->origin == NETFS_UNBUFFERED_WRITE || 2506 wdata->rreq->origin == NETFS_DIO_WRITE)) 2507 ictx->zero_point = wrend; 2508 if (wrend > ictx->remote_i_size) 2509 netfs_resize_file(ictx, wrend, true); 2510 } 2511 2512 netfs_write_subrequest_terminated(&wdata->subreq, result); 2513} 2514 2515struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, 2516 bool fsuid_only) 2517{ 2518 struct cifsFileInfo *open_file = NULL; 2519 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb); 2520 2521 /* only filter by fsuid on multiuser mounts */ 2522 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 2523 fsuid_only = false; 2524 2525 spin_lock(&cifs_inode->open_file_lock); 2526 /* we could simply get the first_list_entry since write-only entries 2527 are always at the end of the list but since the first entry might 2528 have a close pending, we go through the whole list */ 2529 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 2530 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 2531 continue; 2532 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { 2533 if ((!open_file->invalidHandle)) { 2534 /* found a good file */ 2535 /* lock it so it will not be closed on us */ 2536 cifsFileInfo_get(open_file); 2537 spin_unlock(&cifs_inode->open_file_lock); 2538 return open_file; 2539 } /* else might as well continue, and look for 2540 another, or simply have the caller reopen it 2541 again rather than trying to fix this handle */ 2542 } else /* write only file */ 2543 break; /* write only files are last so must be done */ 2544 } 2545 spin_unlock(&cifs_inode->open_file_lock); 2546 return NULL; 2547} 2548 2549/* Return -EBADF if no handle is found and general rc otherwise */ 2550int 2551cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags, 2552 struct cifsFileInfo **ret_file) 2553{ 2554 struct cifsFileInfo *open_file, *inv_file = NULL; 2555 struct cifs_sb_info *cifs_sb; 2556 bool any_available = false; 2557 int rc = -EBADF; 2558 unsigned int refind = 0; 2559 bool fsuid_only = flags & FIND_WR_FSUID_ONLY; 2560 bool with_delete = flags & FIND_WR_WITH_DELETE; 2561 *ret_file = NULL; 2562 2563 /* 2564 * Having a null inode here (because mapping->host was set to zero by 2565 * the VFS or MM) should not happen but we had reports of on oops (due 2566 * to it being zero) during stress testcases so we need to check for it 2567 */ 2568 2569 if (cifs_inode == NULL) { 2570 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); 2571 dump_stack(); 2572 return rc; 2573 } 2574 2575 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb); 2576 2577 /* only filter by fsuid on multiuser mounts */ 2578 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 2579 fsuid_only = false; 2580 2581 spin_lock(&cifs_inode->open_file_lock); 2582refind_writable: 2583 if (refind > MAX_REOPEN_ATT) { 2584 spin_unlock(&cifs_inode->open_file_lock); 2585 return rc; 2586 } 2587 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 2588 if (!any_available && open_file->pid != current->tgid) 2589 continue; 2590 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 2591 continue; 2592 if (with_delete && !(open_file->fid.access & DELETE)) 2593 continue; 2594 if ((flags & FIND_WR_NO_PENDING_DELETE) && 2595 open_file->status_file_deleted) 2596 continue; 2597 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 2598 if (!open_file->invalidHandle) { 2599 /* found a good writable file */ 2600 cifsFileInfo_get(open_file); 2601 spin_unlock(&cifs_inode->open_file_lock); 2602 *ret_file = open_file; 2603 return 0; 2604 } else { 2605 if (!inv_file) 2606 inv_file = open_file; 2607 } 2608 } 2609 } 2610 /* couldn't find usable FH with same pid, try any available */ 2611 if (!any_available) { 2612 any_available = true; 2613 goto refind_writable; 2614 } 2615 2616 if (inv_file) { 2617 any_available = false; 2618 cifsFileInfo_get(inv_file); 2619 } 2620 2621 spin_unlock(&cifs_inode->open_file_lock); 2622 2623 if (inv_file) { 2624 rc = cifs_reopen_file(inv_file, false); 2625 if (!rc) { 2626 *ret_file = inv_file; 2627 return 0; 2628 } 2629 2630 spin_lock(&cifs_inode->open_file_lock); 2631 list_move_tail(&inv_file->flist, &cifs_inode->openFileList); 2632 spin_unlock(&cifs_inode->open_file_lock); 2633 cifsFileInfo_put(inv_file); 2634 ++refind; 2635 inv_file = NULL; 2636 spin_lock(&cifs_inode->open_file_lock); 2637 goto refind_writable; 2638 } 2639 2640 return rc; 2641} 2642 2643struct cifsFileInfo * 2644find_writable_file(struct cifsInodeInfo *cifs_inode, int flags) 2645{ 2646 struct cifsFileInfo *cfile; 2647 int rc; 2648 2649 rc = cifs_get_writable_file(cifs_inode, flags, &cfile); 2650 if (rc) 2651 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc); 2652 2653 return cfile; 2654} 2655 2656int 2657cifs_get_writable_path(struct cifs_tcon *tcon, const char *name, 2658 int flags, 2659 struct cifsFileInfo **ret_file) 2660{ 2661 struct cifsFileInfo *cfile; 2662 void *page = alloc_dentry_path(); 2663 2664 *ret_file = NULL; 2665 2666 spin_lock(&tcon->open_file_lock); 2667 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 2668 struct cifsInodeInfo *cinode; 2669 const char *full_path = build_path_from_dentry(cfile->dentry, page); 2670 if (IS_ERR(full_path)) { 2671 spin_unlock(&tcon->open_file_lock); 2672 free_dentry_path(page); 2673 return PTR_ERR(full_path); 2674 } 2675 if (strcmp(full_path, name)) 2676 continue; 2677 2678 cinode = CIFS_I(d_inode(cfile->dentry)); 2679 spin_unlock(&tcon->open_file_lock); 2680 free_dentry_path(page); 2681 return cifs_get_writable_file(cinode, flags, ret_file); 2682 } 2683 2684 spin_unlock(&tcon->open_file_lock); 2685 free_dentry_path(page); 2686 return -ENOENT; 2687} 2688 2689int 2690cifs_get_readable_path(struct cifs_tcon *tcon, const char *name, 2691 struct cifsFileInfo **ret_file) 2692{ 2693 struct cifsFileInfo *cfile; 2694 void *page = alloc_dentry_path(); 2695 2696 *ret_file = NULL; 2697 2698 spin_lock(&tcon->open_file_lock); 2699 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 2700 struct cifsInodeInfo *cinode; 2701 const char *full_path = build_path_from_dentry(cfile->dentry, page); 2702 if (IS_ERR(full_path)) { 2703 spin_unlock(&tcon->open_file_lock); 2704 free_dentry_path(page); 2705 return PTR_ERR(full_path); 2706 } 2707 if (strcmp(full_path, name)) 2708 continue; 2709 2710 cinode = CIFS_I(d_inode(cfile->dentry)); 2711 spin_unlock(&tcon->open_file_lock); 2712 free_dentry_path(page); 2713 *ret_file = find_readable_file(cinode, 0); 2714 if (*ret_file) { 2715 spin_lock(&cinode->open_file_lock); 2716 if ((*ret_file)->status_file_deleted) { 2717 spin_unlock(&cinode->open_file_lock); 2718 cifsFileInfo_put(*ret_file); 2719 *ret_file = NULL; 2720 } else { 2721 spin_unlock(&cinode->open_file_lock); 2722 } 2723 } 2724 return *ret_file ? 0 : -ENOENT; 2725 } 2726 2727 spin_unlock(&tcon->open_file_lock); 2728 free_dentry_path(page); 2729 return -ENOENT; 2730} 2731 2732/* 2733 * Flush data on a strict file. 2734 */ 2735int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, 2736 int datasync) 2737{ 2738 struct cifsFileInfo *smbfile = file->private_data; 2739 struct inode *inode = file_inode(file); 2740 unsigned int xid; 2741 int rc; 2742 2743 rc = file_write_and_wait_range(file, start, end); 2744 if (rc) { 2745 trace_cifs_fsync_err(inode->i_ino, rc); 2746 return rc; 2747 } 2748 2749 cifs_dbg(FYI, "%s: name=%pD datasync=0x%x\n", __func__, file, datasync); 2750 2751 if (!CIFS_CACHE_READ(CIFS_I(inode))) { 2752 rc = cifs_zap_mapping(inode); 2753 cifs_dbg(FYI, "%s: invalidate mapping: rc = %d\n", __func__, rc); 2754 } 2755 2756 xid = get_xid(); 2757 rc = cifs_file_flush(xid, inode, smbfile); 2758 free_xid(xid); 2759 return rc; 2760} 2761 2762/* 2763 * Flush data on a non-strict data. 2764 */ 2765int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 2766{ 2767 unsigned int xid; 2768 int rc = 0; 2769 struct cifs_tcon *tcon; 2770 struct TCP_Server_Info *server; 2771 struct cifsFileInfo *smbfile = file->private_data; 2772 struct inode *inode = file_inode(file); 2773 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); 2774 2775 rc = file_write_and_wait_range(file, start, end); 2776 if (rc) { 2777 trace_cifs_fsync_err(file_inode(file)->i_ino, rc); 2778 return rc; 2779 } 2780 2781 xid = get_xid(); 2782 2783 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", 2784 file, datasync); 2785 2786 tcon = tlink_tcon(smbfile->tlink); 2787 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { 2788 server = tcon->ses->server; 2789 if (server->ops->flush == NULL) { 2790 rc = -ENOSYS; 2791 goto fsync_exit; 2792 } 2793 2794 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { 2795 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); 2796 if (smbfile) { 2797 rc = server->ops->flush(xid, tcon, &smbfile->fid); 2798 cifsFileInfo_put(smbfile); 2799 } else 2800 cifs_dbg(FYI, "ignore fsync for file not open for write\n"); 2801 } else 2802 rc = server->ops->flush(xid, tcon, &smbfile->fid); 2803 } 2804 2805fsync_exit: 2806 free_xid(xid); 2807 return rc; 2808} 2809 2810/* 2811 * As file closes, flush all cached write data for this inode checking 2812 * for write behind errors. 2813 */ 2814int cifs_flush(struct file *file, fl_owner_t id) 2815{ 2816 struct inode *inode = file_inode(file); 2817 int rc = 0; 2818 2819 if (file->f_mode & FMODE_WRITE) 2820 rc = filemap_write_and_wait(inode->i_mapping); 2821 2822 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); 2823 if (rc) { 2824 /* get more nuanced writeback errors */ 2825 rc = filemap_check_wb_err(file->f_mapping, 0); 2826 trace_cifs_flush_err(inode->i_ino, rc); 2827 } 2828 return rc; 2829} 2830 2831static ssize_t 2832cifs_writev(struct kiocb *iocb, struct iov_iter *from) 2833{ 2834 struct file *file = iocb->ki_filp; 2835 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2836 struct inode *inode = file->f_mapping->host; 2837 struct cifsInodeInfo *cinode = CIFS_I(inode); 2838 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 2839 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 2840 ssize_t rc; 2841 2842 rc = netfs_start_io_write(inode); 2843 if (rc < 0) 2844 return rc; 2845 2846 /* 2847 * We need to hold the sem to be sure nobody modifies lock list 2848 * with a brlock that prevents writing. 2849 */ 2850 down_read(&cinode->lock_sem); 2851 2852 rc = generic_write_checks(iocb, from); 2853 if (rc <= 0) 2854 goto out; 2855 2856 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) && 2857 (cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), 2858 server->vals->exclusive_lock_type, 0, 2859 NULL, CIFS_WRITE_OP))) { 2860 rc = -EACCES; 2861 goto out; 2862 } 2863 2864 rc = netfs_buffered_write_iter_locked(iocb, from, NULL); 2865 2866out: 2867 up_read(&cinode->lock_sem); 2868 netfs_end_io_write(inode); 2869 if (rc > 0) 2870 rc = generic_write_sync(iocb, rc); 2871 return rc; 2872} 2873 2874ssize_t 2875cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) 2876{ 2877 struct inode *inode = file_inode(iocb->ki_filp); 2878 struct cifsInodeInfo *cinode = CIFS_I(inode); 2879 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 2880 struct cifsFileInfo *cfile = (struct cifsFileInfo *) 2881 iocb->ki_filp->private_data; 2882 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2883 ssize_t written; 2884 2885 written = cifs_get_writer(cinode); 2886 if (written) 2887 return written; 2888 2889 if (CIFS_CACHE_WRITE(cinode)) { 2890 if (cap_unix(tcon->ses) && 2891 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2892 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) { 2893 written = netfs_file_write_iter(iocb, from); 2894 goto out; 2895 } 2896 written = cifs_writev(iocb, from); 2897 goto out; 2898 } 2899 /* 2900 * For non-oplocked files in strict cache mode we need to write the data 2901 * to the server exactly from the pos to pos+len-1 rather than flush all 2902 * affected pages because it may cause a error with mandatory locks on 2903 * these pages but not on the region from pos to ppos+len-1. 2904 */ 2905 written = netfs_file_write_iter(iocb, from); 2906 if (CIFS_CACHE_READ(cinode)) { 2907 /* 2908 * We have read level caching and we have just sent a write 2909 * request to the server thus making data in the cache stale. 2910 * Zap the cache and set oplock/lease level to NONE to avoid 2911 * reading stale data from the cache. All subsequent read 2912 * operations will read new data from the server. 2913 */ 2914 cifs_zap_mapping(inode); 2915 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", 2916 inode); 2917 cinode->oplock = 0; 2918 } 2919out: 2920 cifs_put_writer(cinode); 2921 return written; 2922} 2923 2924ssize_t cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter) 2925{ 2926 ssize_t rc; 2927 struct inode *inode = file_inode(iocb->ki_filp); 2928 2929 if (iocb->ki_flags & IOCB_DIRECT) 2930 return netfs_unbuffered_read_iter(iocb, iter); 2931 2932 rc = cifs_revalidate_mapping(inode); 2933 if (rc) 2934 return rc; 2935 2936 return netfs_file_read_iter(iocb, iter); 2937} 2938 2939ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 2940{ 2941 struct inode *inode = file_inode(iocb->ki_filp); 2942 struct cifsInodeInfo *cinode = CIFS_I(inode); 2943 ssize_t written; 2944 int rc; 2945 2946 if (iocb->ki_filp->f_flags & O_DIRECT) { 2947 written = netfs_unbuffered_write_iter(iocb, from); 2948 if (written > 0 && CIFS_CACHE_READ(cinode)) { 2949 cifs_zap_mapping(inode); 2950 cifs_dbg(FYI, 2951 "Set no oplock for inode=%p after a write operation\n", 2952 inode); 2953 cinode->oplock = 0; 2954 } 2955 return written; 2956 } 2957 2958 written = cifs_get_writer(cinode); 2959 if (written) 2960 return written; 2961 2962 written = netfs_file_write_iter(iocb, from); 2963 2964 if (!CIFS_CACHE_WRITE(CIFS_I(inode))) { 2965 rc = filemap_fdatawrite(inode->i_mapping); 2966 if (rc) 2967 cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n", 2968 rc, inode); 2969 } 2970 2971 cifs_put_writer(cinode); 2972 return written; 2973} 2974 2975ssize_t 2976cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) 2977{ 2978 struct inode *inode = file_inode(iocb->ki_filp); 2979 struct cifsInodeInfo *cinode = CIFS_I(inode); 2980 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 2981 struct cifsFileInfo *cfile = (struct cifsFileInfo *) 2982 iocb->ki_filp->private_data; 2983 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2984 int rc = -EACCES; 2985 2986 /* 2987 * In strict cache mode we need to read from the server all the time 2988 * if we don't have level II oplock because the server can delay mtime 2989 * change - so we can't make a decision about inode invalidating. 2990 * And we can also fail with pagereading if there are mandatory locks 2991 * on pages affected by this read but not on the region from pos to 2992 * pos+len-1. 2993 */ 2994 if (!CIFS_CACHE_READ(cinode)) 2995 return netfs_unbuffered_read_iter(iocb, to); 2996 2997 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0) { 2998 if (iocb->ki_flags & IOCB_DIRECT) 2999 return netfs_unbuffered_read_iter(iocb, to); 3000 return netfs_buffered_read_iter(iocb, to); 3001 } 3002 3003 /* 3004 * We need to hold the sem to be sure nobody modifies lock list 3005 * with a brlock that prevents reading. 3006 */ 3007 if (iocb->ki_flags & IOCB_DIRECT) { 3008 rc = netfs_start_io_direct(inode); 3009 if (rc < 0) 3010 goto out; 3011 rc = -EACCES; 3012 down_read(&cinode->lock_sem); 3013 if (!cifs_find_lock_conflict( 3014 cfile, iocb->ki_pos, iov_iter_count(to), 3015 tcon->ses->server->vals->shared_lock_type, 3016 0, NULL, CIFS_READ_OP)) 3017 rc = netfs_unbuffered_read_iter_locked(iocb, to); 3018 up_read(&cinode->lock_sem); 3019 netfs_end_io_direct(inode); 3020 } else { 3021 rc = netfs_start_io_read(inode); 3022 if (rc < 0) 3023 goto out; 3024 rc = -EACCES; 3025 down_read(&cinode->lock_sem); 3026 if (!cifs_find_lock_conflict( 3027 cfile, iocb->ki_pos, iov_iter_count(to), 3028 tcon->ses->server->vals->shared_lock_type, 3029 0, NULL, CIFS_READ_OP)) 3030 rc = filemap_read(iocb, to, 0); 3031 up_read(&cinode->lock_sem); 3032 netfs_end_io_read(inode); 3033 } 3034out: 3035 return rc; 3036} 3037 3038static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf) 3039{ 3040 return netfs_page_mkwrite(vmf, NULL); 3041} 3042 3043static const struct vm_operations_struct cifs_file_vm_ops = { 3044 .fault = filemap_fault, 3045 .map_pages = filemap_map_pages, 3046 .page_mkwrite = cifs_page_mkwrite, 3047}; 3048 3049int cifs_file_strict_mmap_prepare(struct vm_area_desc *desc) 3050{ 3051 int xid, rc = 0; 3052 struct inode *inode = file_inode(desc->file); 3053 3054 xid = get_xid(); 3055 3056 if (!CIFS_CACHE_READ(CIFS_I(inode))) 3057 rc = cifs_zap_mapping(inode); 3058 if (!rc) 3059 rc = generic_file_mmap_prepare(desc); 3060 if (!rc) 3061 desc->vm_ops = &cifs_file_vm_ops; 3062 3063 free_xid(xid); 3064 return rc; 3065} 3066 3067int cifs_file_mmap_prepare(struct vm_area_desc *desc) 3068{ 3069 int rc, xid; 3070 3071 xid = get_xid(); 3072 3073 rc = cifs_revalidate_file(desc->file); 3074 if (rc) 3075 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", 3076 rc); 3077 if (!rc) 3078 rc = generic_file_mmap_prepare(desc); 3079 if (!rc) 3080 desc->vm_ops = &cifs_file_vm_ops; 3081 3082 free_xid(xid); 3083 return rc; 3084} 3085 3086static int is_inode_writable(struct cifsInodeInfo *cifs_inode) 3087{ 3088 struct cifsFileInfo *open_file; 3089 3090 spin_lock(&cifs_inode->open_file_lock); 3091 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 3092 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 3093 spin_unlock(&cifs_inode->open_file_lock); 3094 return 1; 3095 } 3096 } 3097 spin_unlock(&cifs_inode->open_file_lock); 3098 return 0; 3099} 3100 3101/* We do not want to update the file size from server for inodes 3102 open for write - to avoid races with writepage extending 3103 the file - in the future we could consider allowing 3104 refreshing the inode only on increases in the file size 3105 but this is tricky to do without racing with writebehind 3106 page caching in the current Linux kernel design */ 3107bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file, 3108 bool from_readdir) 3109{ 3110 if (!cifsInode) 3111 return true; 3112 3113 if (is_inode_writable(cifsInode) || 3114 ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) { 3115 /* This inode is open for write at least once */ 3116 struct cifs_sb_info *cifs_sb; 3117 3118 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb); 3119 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 3120 /* since no page cache to corrupt on directio 3121 we can change size safely */ 3122 return true; 3123 } 3124 3125 if (i_size_read(&cifsInode->netfs.inode) < end_of_file) 3126 return true; 3127 3128 return false; 3129 } else 3130 return true; 3131} 3132 3133void cifs_oplock_break(struct work_struct *work) 3134{ 3135 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, 3136 oplock_break); 3137 struct inode *inode = d_inode(cfile->dentry); 3138 struct super_block *sb = inode->i_sb; 3139 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 3140 struct cifsInodeInfo *cinode = CIFS_I(inode); 3141 struct cifs_tcon *tcon; 3142 struct TCP_Server_Info *server; 3143 struct tcon_link *tlink; 3144 int rc = 0; 3145 bool purge_cache = false, oplock_break_cancelled; 3146 __u64 persistent_fid, volatile_fid; 3147 __u16 net_fid; 3148 3149 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, 3150 TASK_UNINTERRUPTIBLE); 3151 3152 tlink = cifs_sb_tlink(cifs_sb); 3153 if (IS_ERR(tlink)) 3154 goto out; 3155 tcon = tlink_tcon(tlink); 3156 server = tcon->ses->server; 3157 3158 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level, 3159 cfile->oplock_epoch, &purge_cache); 3160 3161 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) && 3162 cifs_has_mand_locks(cinode)) { 3163 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", 3164 inode); 3165 cinode->oplock = 0; 3166 } 3167 3168 if (S_ISREG(inode->i_mode)) { 3169 if (CIFS_CACHE_READ(cinode)) 3170 break_lease(inode, O_RDONLY); 3171 else 3172 break_lease(inode, O_WRONLY); 3173 rc = filemap_fdatawrite(inode->i_mapping); 3174 if (!CIFS_CACHE_READ(cinode) || purge_cache) { 3175 rc = filemap_fdatawait(inode->i_mapping); 3176 mapping_set_error(inode->i_mapping, rc); 3177 cifs_zap_mapping(inode); 3178 } 3179 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); 3180 if (CIFS_CACHE_WRITE(cinode)) 3181 goto oplock_break_ack; 3182 } 3183 3184 rc = cifs_push_locks(cfile); 3185 if (rc) 3186 cifs_dbg(VFS, "Push locks rc = %d\n", rc); 3187 3188oplock_break_ack: 3189 /* 3190 * When oplock break is received and there are no active 3191 * file handles but cached, then schedule deferred close immediately. 3192 * So, new open will not use cached handle. 3193 */ 3194 3195 if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes)) 3196 cifs_close_deferred_file(cinode); 3197 3198 persistent_fid = cfile->fid.persistent_fid; 3199 volatile_fid = cfile->fid.volatile_fid; 3200 net_fid = cfile->fid.netfid; 3201 oplock_break_cancelled = cfile->oplock_break_cancelled; 3202 3203 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false); 3204 /* 3205 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require 3206 * an acknowledgment to be sent when the file has already been closed. 3207 */ 3208 spin_lock(&cinode->open_file_lock); 3209 /* check list empty since can race with kill_sb calling tree disconnect */ 3210 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) { 3211 spin_unlock(&cinode->open_file_lock); 3212 rc = server->ops->oplock_response(tcon, persistent_fid, 3213 volatile_fid, net_fid, cinode); 3214 cifs_dbg(FYI, "Oplock release rc = %d\n", rc); 3215 } else 3216 spin_unlock(&cinode->open_file_lock); 3217 3218 cifs_put_tlink(tlink); 3219out: 3220 cifs_done_oplock_break(cinode); 3221} 3222 3223static int cifs_swap_activate(struct swap_info_struct *sis, 3224 struct file *swap_file, sector_t *span) 3225{ 3226 struct cifsFileInfo *cfile = swap_file->private_data; 3227 struct inode *inode = swap_file->f_mapping->host; 3228 unsigned long blocks; 3229 long long isize; 3230 3231 cifs_dbg(FYI, "swap activate\n"); 3232 3233 if (!swap_file->f_mapping->a_ops->swap_rw) 3234 /* Cannot support swap */ 3235 return -EINVAL; 3236 3237 spin_lock(&inode->i_lock); 3238 blocks = inode->i_blocks; 3239 isize = inode->i_size; 3240 spin_unlock(&inode->i_lock); 3241 if (blocks*512 < isize) { 3242 pr_warn("swap activate: swapfile has holes\n"); 3243 return -EINVAL; 3244 } 3245 *span = sis->pages; 3246 3247 pr_warn_once("Swap support over SMB3 is experimental\n"); 3248 3249 /* 3250 * TODO: consider adding ACL (or documenting how) to prevent other 3251 * users (on this or other systems) from reading it 3252 */ 3253 3254 3255 /* TODO: add sk_set_memalloc(inet) or similar */ 3256 3257 if (cfile) 3258 cfile->swapfile = true; 3259 /* 3260 * TODO: Since file already open, we can't open with DENY_ALL here 3261 * but we could add call to grab a byte range lock to prevent others 3262 * from reading or writing the file 3263 */ 3264 3265 sis->flags |= SWP_FS_OPS; 3266 return add_swap_extent(sis, 0, sis->max, 0); 3267} 3268 3269static void cifs_swap_deactivate(struct file *file) 3270{ 3271 struct cifsFileInfo *cfile = file->private_data; 3272 3273 cifs_dbg(FYI, "swap deactivate\n"); 3274 3275 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */ 3276 3277 if (cfile) 3278 cfile->swapfile = false; 3279 3280 /* do we need to unpin (or unlock) the file */ 3281} 3282 3283/** 3284 * cifs_swap_rw - SMB3 address space operation for swap I/O 3285 * @iocb: target I/O control block 3286 * @iter: I/O buffer 3287 * 3288 * Perform IO to the swap-file. This is much like direct IO. 3289 */ 3290static int cifs_swap_rw(struct kiocb *iocb, struct iov_iter *iter) 3291{ 3292 ssize_t ret; 3293 3294 if (iov_iter_rw(iter) == READ) 3295 ret = netfs_unbuffered_read_iter_locked(iocb, iter); 3296 else 3297 ret = netfs_unbuffered_write_iter_locked(iocb, iter, NULL); 3298 if (ret < 0) 3299 return ret; 3300 return 0; 3301} 3302 3303const struct address_space_operations cifs_addr_ops = { 3304 .read_folio = netfs_read_folio, 3305 .readahead = netfs_readahead, 3306 .writepages = netfs_writepages, 3307 .dirty_folio = netfs_dirty_folio, 3308 .release_folio = netfs_release_folio, 3309 .direct_IO = noop_direct_IO, 3310 .invalidate_folio = netfs_invalidate_folio, 3311 .migrate_folio = filemap_migrate_folio, 3312 /* 3313 * TODO: investigate and if useful we could add an is_dirty_writeback 3314 * helper if needed 3315 */ 3316 .swap_activate = cifs_swap_activate, 3317 .swap_deactivate = cifs_swap_deactivate, 3318 .swap_rw = cifs_swap_rw, 3319}; 3320 3321/* 3322 * cifs_readahead requires the server to support a buffer large enough to 3323 * contain the header plus one complete page of data. Otherwise, we need 3324 * to leave cifs_readahead out of the address space operations. 3325 */ 3326const struct address_space_operations cifs_addr_ops_smallbuf = { 3327 .read_folio = netfs_read_folio, 3328 .writepages = netfs_writepages, 3329 .dirty_folio = netfs_dirty_folio, 3330 .release_folio = netfs_release_folio, 3331 .invalidate_folio = netfs_invalidate_folio, 3332 .migrate_folio = filemap_migrate_folio, 3333};
~/
Read-only file preview.