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