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