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