1 // SPDX-License-Identifier: LGPL-2.1 1 // SPDX-License-Identifier: LGPL-2.1 2 /* 2 /* 3 * 3 * 4 * vfs operations that deal with files 4 * vfs operations that deal with files 5 * 5 * 6 * Copyright (C) International Business Mach 6 * Copyright (C) International Business Machines Corp., 2002,2010 7 * Author(s): Steve French (sfrench@us.ibm.c 7 * Author(s): Steve French (sfrench@us.ibm.com) 8 * Jeremy Allison (jra@samba.org) 8 * Jeremy Allison (jra@samba.org) 9 * 9 * 10 */ 10 */ 11 #include <linux/fs.h> 11 #include <linux/fs.h> 12 #include <linux/filelock.h> << 13 #include <linux/backing-dev.h> 12 #include <linux/backing-dev.h> 14 #include <linux/stat.h> 13 #include <linux/stat.h> 15 #include <linux/fcntl.h> 14 #include <linux/fcntl.h> 16 #include <linux/pagemap.h> 15 #include <linux/pagemap.h> 17 #include <linux/pagevec.h> 16 #include <linux/pagevec.h> 18 #include <linux/writeback.h> 17 #include <linux/writeback.h> 19 #include <linux/task_io_accounting_ops.h> 18 #include <linux/task_io_accounting_ops.h> 20 #include <linux/delay.h> 19 #include <linux/delay.h> 21 #include <linux/mount.h> 20 #include <linux/mount.h> 22 #include <linux/slab.h> 21 #include <linux/slab.h> 23 #include <linux/swap.h> 22 #include <linux/swap.h> 24 #include <linux/mm.h> 23 #include <linux/mm.h> 25 #include <asm/div64.h> 24 #include <asm/div64.h> 26 #include "cifsfs.h" 25 #include "cifsfs.h" 27 #include "cifspdu.h" 26 #include "cifspdu.h" 28 #include "cifsglob.h" 27 #include "cifsglob.h" 29 #include "cifsproto.h" 28 #include "cifsproto.h" 30 #include "smb2proto.h" 29 #include "smb2proto.h" 31 #include "cifs_unicode.h" 30 #include "cifs_unicode.h" 32 #include "cifs_debug.h" 31 #include "cifs_debug.h" 33 #include "cifs_fs_sb.h" 32 #include "cifs_fs_sb.h" 34 #include "fscache.h" 33 #include "fscache.h" 35 #include "smbdirect.h" 34 #include "smbdirect.h" 36 #include "fs_context.h" 35 #include "fs_context.h" 37 #include "cifs_ioctl.h" 36 #include "cifs_ioctl.h" 38 #include "cached_dir.h" 37 #include "cached_dir.h" 39 #include <trace/events/netfs.h> << 40 << 41 static int cifs_reopen_file(struct cifsFileInf << 42 << 43 /* << 44 * Prepare a subrequest to upload to the serve << 45 * so that we know the maximum amount of data << 46 */ << 47 static void cifs_prepare_write(struct netfs_io << 48 { << 49 struct cifs_io_subrequest *wdata = << 50 container_of(subreq, struct ci << 51 struct cifs_io_request *req = wdata->r << 52 struct netfs_io_stream *stream = &req- << 53 struct TCP_Server_Info *server; << 54 struct cifsFileInfo *open_file = req-> << 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( << 64 wdata->server = server; << 65 << 66 retry: << 67 if (open_file->invalidHandle) { << 68 rc = cifs_reopen_file(open_fil << 69 if (rc < 0) { << 70 if (rc == -EAGAIN) << 71 goto retry; << 72 subreq->error = rc; << 73 return netfs_prepare_w << 74 } << 75 } << 76 << 77 rc = server->ops->wait_mtu_credits(ser << 78 &wd << 79 if (rc < 0) { << 80 subreq->error = rc; << 81 return netfs_prepare_write_fai << 82 } << 83 << 84 wdata->credits.rreq_debug_id = subreq- << 85 wdata->credits.rreq_debug_index = subr << 86 wdata->credits.in_flight_check = 1; << 87 trace_smb3_rw_credits(wdata->rreq->deb << 88 wdata->subreq.de << 89 wdata->credits.v << 90 server->credits, << 91 wdata->credits.v << 92 cifs_trace_rw_cr << 93 << 94 #ifdef CONFIG_CIFS_SMB_DIRECT << 95 if (server->smbd_conn) << 96 stream->sreq_max_segs = server << 97 #endif << 98 } << 99 << 100 /* << 101 * Issue a subrequest to upload to the server. << 102 */ << 103 static void cifs_issue_write(struct netfs_io_s << 104 { << 105 struct cifs_io_subrequest *wdata = << 106 container_of(subreq, struct ci << 107 struct cifs_sb_info *sbi = CIFS_SB(sub << 108 int rc; << 109 << 110 if (cifs_forced_shutdown(sbi)) { << 111 rc = -EIO; << 112 goto fail; << 113 } << 114 << 115 rc = adjust_credits(wdata->server, wda << 116 if (rc) << 117 goto fail; << 118 << 119 rc = -EAGAIN; << 120 if (wdata->req->cfile->invalidHandle) << 121 goto fail; << 122 << 123 wdata->server->ops->async_writev(wdata << 124 out: << 125 return; << 126 << 127 fail: << 128 if (rc == -EAGAIN) << 129 trace_netfs_sreq(subreq, netfs << 130 else << 131 trace_netfs_sreq(subreq, netfs << 132 add_credits_and_wake_if(wdata->server, << 133 cifs_write_subrequest_terminated(wdata << 134 goto out; << 135 } << 136 << 137 static void cifs_netfs_invalidate_cache(struct << 138 { << 139 cifs_invalidate_cache(wreq->inode, 0); << 140 } << 141 << 142 /* << 143 * Negotiate the size of a read operation on b << 144 */ << 145 static int cifs_prepare_read(struct netfs_io_s << 146 { << 147 struct netfs_io_request *rreq = subreq << 148 struct cifs_io_subrequest *rdata = con << 149 struct cifs_io_request *req = containe << 150 struct TCP_Server_Info *server = req-> << 151 struct cifs_sb_info *cifs_sb = CIFS_SB << 152 size_t size; << 153 int rc = 0; << 154 << 155 if (!rdata->have_xid) { << 156 rdata->xid = get_xid(); << 157 rdata->have_xid = true; << 158 } << 159 rdata->server = server; << 160 << 161 if (cifs_sb->ctx->rsize == 0) << 162 cifs_sb->ctx->rsize = << 163 server->ops->negotiate << 164 << 165 << 166 rc = server->ops->wait_mtu_credits(ser << 167 &si << 168 if (rc) << 169 return rc; << 170 << 171 rreq->io_streams[0].sreq_max_len = siz << 172 << 173 rdata->credits.in_flight_check = 1; << 174 rdata->credits.rreq_debug_id = rreq->d << 175 rdata->credits.rreq_debug_index = subr << 176 << 177 trace_smb3_rw_credits(rdata->rreq->deb << 178 rdata->subreq.de << 179 rdata->credits.v << 180 server->credits, << 181 cifs_trace_rw_cr << 182 << 183 #ifdef CONFIG_CIFS_SMB_DIRECT << 184 if (server->smbd_conn) << 185 rreq->io_streams[0].sreq_max_s << 186 #endif << 187 return 0; << 188 } << 189 << 190 /* << 191 * Issue a read operation on behalf of the net << 192 * to make a read of a certain size at a point << 193 * to only read a portion of that, but as long << 194 * helper will call us again so that we can is << 195 */ << 196 static void cifs_issue_read(struct netfs_io_su << 197 { << 198 struct netfs_io_request *rreq = subreq << 199 struct cifs_io_subrequest *rdata = con << 200 struct cifs_io_request *req = containe << 201 struct TCP_Server_Info *server = req-> << 202 int rc = 0; << 203 << 204 cifs_dbg(FYI, "%s: op=%08x[%x] mapping << 205 __func__, rreq->debug_id, sub << 206 subreq->transferred, subreq-> << 207 << 208 rc = adjust_credits(server, rdata, cif << 209 if (rc) << 210 goto failed; << 211 << 212 if (req->cfile->invalidHandle) { << 213 do { << 214 rc = cifs_reopen_file( << 215 } while (rc == -EAGAIN); << 216 if (rc) << 217 goto failed; << 218 } << 219 << 220 if (subreq->rreq->origin != NETFS_DIO_ << 221 __set_bit(NETFS_SREQ_CLEAR_TAI << 222 << 223 trace_netfs_sreq(subreq, netfs_sreq_tr << 224 rc = rdata->server->ops->async_readv(r << 225 if (rc) << 226 goto failed; << 227 return; << 228 << 229 failed: << 230 netfs_read_subreq_terminated(subreq, r << 231 } << 232 << 233 /* << 234 * Writeback calls this when it finds a folio << 235 * called if writeback only has copy-to-cache << 236 */ << 237 static void cifs_begin_writeback(struct netfs_ << 238 { << 239 struct cifs_io_request *req = containe << 240 int ret; << 241 << 242 ret = cifs_get_writable_file(CIFS_I(wr << 243 if (ret) { << 244 cifs_dbg(VFS, "No writable han << 245 return; << 246 } << 247 << 248 wreq->io_streams[0].avail = true; << 249 } << 250 << 251 /* << 252 * Initialise a request. << 253 */ << 254 static int cifs_init_request(struct netfs_io_r << 255 { << 256 struct cifs_io_request *req = containe << 257 struct cifs_sb_info *cifs_sb = CIFS_SB << 258 struct cifsFileInfo *open_file = NULL; << 259 << 260 rreq->rsize = cifs_sb->ctx->rsize; << 261 rreq->wsize = cifs_sb->ctx->wsize; << 262 req->pid = current->tgid; // Ummm... << 263 << 264 if (file) { << 265 open_file = file->private_data << 266 rreq->netfs_priv = file->priva << 267 req->cfile = cifsFileInfo_get( << 268 req->server = cifs_pick_channe << 269 if (cifs_sb->mnt_cifs_flags & << 270 req->pid = req->cfile- << 271 } else if (rreq->origin != NETFS_WRITE << 272 WARN_ON_ONCE(1); << 273 return -EIO; << 274 } << 275 << 276 return 0; << 277 } << 278 << 279 /* << 280 * Completion of a request operation. << 281 */ << 282 static void cifs_rreq_done(struct netfs_io_req << 283 { << 284 struct timespec64 atime, mtime; << 285 struct inode *inode = rreq->inode; << 286 << 287 /* we do not want atime to be less tha << 288 atime = inode_set_atime_to_ts(inode, c << 289 mtime = inode_get_mtime(inode); << 290 if (timespec64_compare(&atime, &mtime) << 291 inode_set_atime_to_ts(inode, i << 292 } << 293 << 294 static void cifs_free_request(struct netfs_io_ << 295 { << 296 struct cifs_io_request *req = containe << 297 << 298 if (req->cfile) << 299 cifsFileInfo_put(req->cfile); << 300 } << 301 << 302 static void cifs_free_subrequest(struct netfs_ << 303 { << 304 struct cifs_io_subrequest *rdata = << 305 container_of(subreq, struct ci << 306 int rc = subreq->error; << 307 << 308 if (rdata->subreq.source == NETFS_DOWN << 309 #ifdef CONFIG_CIFS_SMB_DIRECT << 310 if (rdata->mr) { << 311 smbd_deregister_mr(rda << 312 rdata->mr = NULL; << 313 } << 314 #endif << 315 } << 316 << 317 if (rdata->credits.value != 0) { << 318 trace_smb3_rw_credits(rdata->r << 319 rdata->s << 320 rdata->c << 321 rdata->s << 322 rdata->s << 323 -rdata-> << 324 cifs_tra << 325 if (rdata->server) << 326 add_credits_and_wake_i << 327 else << 328 rdata->credits.value = << 329 } << 330 << 331 if (rdata->have_xid) << 332 free_xid(rdata->xid); << 333 } << 334 << 335 const struct netfs_request_ops cifs_req_ops = << 336 .request_pool = &cifs_io_req << 337 .subrequest_pool = &cifs_io_sub << 338 .init_request = cifs_init_re << 339 .free_request = cifs_free_re << 340 .free_subrequest = cifs_free_su << 341 .prepare_read = cifs_prepare << 342 .issue_read = cifs_issue_r << 343 .done = cifs_rreq_do << 344 .begin_writeback = cifs_begin_w << 345 .prepare_write = cifs_prepare << 346 .issue_write = cifs_issue_w << 347 .invalidate_cache = cifs_netfs_i << 348 }; << 349 38 350 /* 39 /* 351 * Mark as invalid, all open files on tree con 40 * Mark as invalid, all open files on tree connections since they 352 * were closed when session to server was lost 41 * were closed when session to server was lost. 353 */ 42 */ 354 void 43 void 355 cifs_mark_open_files_invalid(struct cifs_tcon 44 cifs_mark_open_files_invalid(struct cifs_tcon *tcon) 356 { 45 { 357 struct cifsFileInfo *open_file = NULL; 46 struct cifsFileInfo *open_file = NULL; 358 struct list_head *tmp; 47 struct list_head *tmp; 359 struct list_head *tmp1; 48 struct list_head *tmp1; 360 49 361 /* only send once per connect */ 50 /* only send once per connect */ 362 spin_lock(&tcon->tc_lock); !! 51 spin_lock(&tcon->ses->ses_lock); 363 if (tcon->need_reconnect) !! 52 if ((tcon->ses->ses_status != SES_GOOD) || (tcon->status != TID_NEED_RECON)) { 364 tcon->status = TID_NEED_RECON; !! 53 spin_unlock(&tcon->ses->ses_lock); 365 << 366 if (tcon->status != TID_NEED_RECON) { << 367 spin_unlock(&tcon->tc_lock); << 368 return; 54 return; 369 } 55 } 370 tcon->status = TID_IN_FILES_INVALIDATE 56 tcon->status = TID_IN_FILES_INVALIDATE; 371 spin_unlock(&tcon->tc_lock); !! 57 spin_unlock(&tcon->ses->ses_lock); 372 58 373 /* list all files open on tree connect 59 /* list all files open on tree connection and mark them invalid */ 374 spin_lock(&tcon->open_file_lock); 60 spin_lock(&tcon->open_file_lock); 375 list_for_each_safe(tmp, tmp1, &tcon->o 61 list_for_each_safe(tmp, tmp1, &tcon->openFileList) { 376 open_file = list_entry(tmp, st 62 open_file = list_entry(tmp, struct cifsFileInfo, tlist); 377 open_file->invalidHandle = tru 63 open_file->invalidHandle = true; 378 open_file->oplock_break_cancel 64 open_file->oplock_break_cancelled = true; 379 } 65 } 380 spin_unlock(&tcon->open_file_lock); 66 spin_unlock(&tcon->open_file_lock); 381 67 382 invalidate_all_cached_dirs(tcon); 68 invalidate_all_cached_dirs(tcon); 383 spin_lock(&tcon->tc_lock); 69 spin_lock(&tcon->tc_lock); 384 if (tcon->status == TID_IN_FILES_INVAL 70 if (tcon->status == TID_IN_FILES_INVALIDATE) 385 tcon->status = TID_NEED_TCON; 71 tcon->status = TID_NEED_TCON; 386 spin_unlock(&tcon->tc_lock); 72 spin_unlock(&tcon->tc_lock); 387 73 388 /* 74 /* 389 * BB Add call to invalidate_inodes(sb 75 * BB Add call to invalidate_inodes(sb) for all superblocks mounted 390 * to this tcon. 76 * to this tcon. 391 */ 77 */ 392 } 78 } 393 79 394 static inline int cifs_convert_flags(unsigned 80 static inline int cifs_convert_flags(unsigned int flags, int rdwr_for_fscache) 395 { 81 { 396 if ((flags & O_ACCMODE) == O_RDONLY) 82 if ((flags & O_ACCMODE) == O_RDONLY) 397 return GENERIC_READ; 83 return GENERIC_READ; 398 else if ((flags & O_ACCMODE) == O_WRON 84 else if ((flags & O_ACCMODE) == O_WRONLY) 399 return rdwr_for_fscache == 1 ? 85 return rdwr_for_fscache == 1 ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE; 400 else if ((flags & O_ACCMODE) == O_RDWR 86 else if ((flags & O_ACCMODE) == O_RDWR) { 401 /* GENERIC_ALL is too much per 87 /* GENERIC_ALL is too much permission to request 402 can cause unnecessary acces 88 can cause unnecessary access denied on create */ 403 /* return GENERIC_ALL; */ 89 /* return GENERIC_ALL; */ 404 return (GENERIC_READ | GENERIC 90 return (GENERIC_READ | GENERIC_WRITE); 405 } 91 } 406 92 407 return (READ_CONTROL | FILE_WRITE_ATTR 93 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | 408 FILE_WRITE_EA | FILE_APPEND_DA 94 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | 409 FILE_READ_DATA); 95 FILE_READ_DATA); 410 } 96 } 411 97 412 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 98 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 413 static u32 cifs_posix_convert_flags(unsigned i 99 static u32 cifs_posix_convert_flags(unsigned int flags) 414 { 100 { 415 u32 posix_flags = 0; 101 u32 posix_flags = 0; 416 102 417 if ((flags & O_ACCMODE) == O_RDONLY) 103 if ((flags & O_ACCMODE) == O_RDONLY) 418 posix_flags = SMB_O_RDONLY; 104 posix_flags = SMB_O_RDONLY; 419 else if ((flags & O_ACCMODE) == O_WRON 105 else if ((flags & O_ACCMODE) == O_WRONLY) 420 posix_flags = SMB_O_WRONLY; 106 posix_flags = SMB_O_WRONLY; 421 else if ((flags & O_ACCMODE) == O_RDWR 107 else if ((flags & O_ACCMODE) == O_RDWR) 422 posix_flags = SMB_O_RDWR; 108 posix_flags = SMB_O_RDWR; 423 109 424 if (flags & O_CREAT) { 110 if (flags & O_CREAT) { 425 posix_flags |= SMB_O_CREAT; 111 posix_flags |= SMB_O_CREAT; 426 if (flags & O_EXCL) 112 if (flags & O_EXCL) 427 posix_flags |= SMB_O_E 113 posix_flags |= SMB_O_EXCL; 428 } else if (flags & O_EXCL) 114 } else if (flags & O_EXCL) 429 cifs_dbg(FYI, "Application %s 115 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", 430 current->comm, curren 116 current->comm, current->tgid); 431 117 432 if (flags & O_TRUNC) 118 if (flags & O_TRUNC) 433 posix_flags |= SMB_O_TRUNC; 119 posix_flags |= SMB_O_TRUNC; 434 /* be safe and imply O_SYNC for O_DSYN 120 /* be safe and imply O_SYNC for O_DSYNC */ 435 if (flags & O_DSYNC) 121 if (flags & O_DSYNC) 436 posix_flags |= SMB_O_SYNC; 122 posix_flags |= SMB_O_SYNC; 437 if (flags & O_DIRECTORY) 123 if (flags & O_DIRECTORY) 438 posix_flags |= SMB_O_DIRECTORY 124 posix_flags |= SMB_O_DIRECTORY; 439 if (flags & O_NOFOLLOW) 125 if (flags & O_NOFOLLOW) 440 posix_flags |= SMB_O_NOFOLLOW; 126 posix_flags |= SMB_O_NOFOLLOW; 441 if (flags & O_DIRECT) 127 if (flags & O_DIRECT) 442 posix_flags |= SMB_O_DIRECT; 128 posix_flags |= SMB_O_DIRECT; 443 129 444 return posix_flags; 130 return posix_flags; 445 } 131 } 446 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 132 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 447 133 448 static inline int cifs_get_disposition(unsigne 134 static inline int cifs_get_disposition(unsigned int flags) 449 { 135 { 450 if ((flags & (O_CREAT | O_EXCL)) == (O 136 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 451 return FILE_CREATE; 137 return FILE_CREATE; 452 else if ((flags & (O_CREAT | O_TRUNC)) 138 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 453 return FILE_OVERWRITE_IF; 139 return FILE_OVERWRITE_IF; 454 else if ((flags & O_CREAT) == O_CREAT) 140 else if ((flags & O_CREAT) == O_CREAT) 455 return FILE_OPEN_IF; 141 return FILE_OPEN_IF; 456 else if ((flags & O_TRUNC) == O_TRUNC) 142 else if ((flags & O_TRUNC) == O_TRUNC) 457 return FILE_OVERWRITE; 143 return FILE_OVERWRITE; 458 else 144 else 459 return FILE_OPEN; 145 return FILE_OPEN; 460 } 146 } 461 147 462 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 148 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 463 int cifs_posix_open(const char *full_path, str 149 int cifs_posix_open(const char *full_path, struct inode **pinode, 464 struct super_block *sb 150 struct super_block *sb, int mode, unsigned int f_flags, 465 __u32 *poplock, __u16 151 __u32 *poplock, __u16 *pnetfid, unsigned int xid) 466 { 152 { 467 int rc; 153 int rc; 468 FILE_UNIX_BASIC_INFO *presp_data; 154 FILE_UNIX_BASIC_INFO *presp_data; 469 __u32 posix_flags = 0; 155 __u32 posix_flags = 0; 470 struct cifs_sb_info *cifs_sb = CIFS_SB 156 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 471 struct cifs_fattr fattr; 157 struct cifs_fattr fattr; 472 struct tcon_link *tlink; 158 struct tcon_link *tlink; 473 struct cifs_tcon *tcon; 159 struct cifs_tcon *tcon; 474 160 475 cifs_dbg(FYI, "posix open %s\n", full_ 161 cifs_dbg(FYI, "posix open %s\n", full_path); 476 162 477 presp_data = kzalloc(sizeof(FILE_UNIX_ 163 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); 478 if (presp_data == NULL) 164 if (presp_data == NULL) 479 return -ENOMEM; 165 return -ENOMEM; 480 166 481 tlink = cifs_sb_tlink(cifs_sb); 167 tlink = cifs_sb_tlink(cifs_sb); 482 if (IS_ERR(tlink)) { 168 if (IS_ERR(tlink)) { 483 rc = PTR_ERR(tlink); 169 rc = PTR_ERR(tlink); 484 goto posix_open_ret; 170 goto posix_open_ret; 485 } 171 } 486 172 487 tcon = tlink_tcon(tlink); 173 tcon = tlink_tcon(tlink); 488 mode &= ~current_umask(); 174 mode &= ~current_umask(); 489 175 490 posix_flags = cifs_posix_convert_flags 176 posix_flags = cifs_posix_convert_flags(f_flags); 491 rc = CIFSPOSIXCreate(xid, tcon, posix_ 177 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, 492 poplock, full_pat 178 poplock, full_path, cifs_sb->local_nls, 493 cifs_remap(cifs_s 179 cifs_remap(cifs_sb)); 494 cifs_put_tlink(tlink); 180 cifs_put_tlink(tlink); 495 181 496 if (rc) 182 if (rc) 497 goto posix_open_ret; 183 goto posix_open_ret; 498 184 499 if (presp_data->Type == cpu_to_le32(-1 185 if (presp_data->Type == cpu_to_le32(-1)) 500 goto posix_open_ret; /* open o 186 goto posix_open_ret; /* open ok, caller does qpathinfo */ 501 187 502 if (!pinode) 188 if (!pinode) 503 goto posix_open_ret; /* caller 189 goto posix_open_ret; /* caller does not need info */ 504 190 505 cifs_unix_basic_to_fattr(&fattr, presp 191 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); 506 192 507 /* get new inode and set it up */ 193 /* get new inode and set it up */ 508 if (*pinode == NULL) { 194 if (*pinode == NULL) { 509 cifs_fill_uniqueid(sb, &fattr) 195 cifs_fill_uniqueid(sb, &fattr); 510 *pinode = cifs_iget(sb, &fattr 196 *pinode = cifs_iget(sb, &fattr); 511 if (!*pinode) { 197 if (!*pinode) { 512 rc = -ENOMEM; 198 rc = -ENOMEM; 513 goto posix_open_ret; 199 goto posix_open_ret; 514 } 200 } 515 } else { 201 } else { 516 cifs_revalidate_mapping(*pinod 202 cifs_revalidate_mapping(*pinode); 517 rc = cifs_fattr_to_inode(*pino !! 203 rc = cifs_fattr_to_inode(*pinode, &fattr); 518 } 204 } 519 205 520 posix_open_ret: 206 posix_open_ret: 521 kfree(presp_data); 207 kfree(presp_data); 522 return rc; 208 return rc; 523 } 209 } 524 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 210 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 525 211 526 static int cifs_nt_open(const char *full_path, 212 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, 527 struct cifs_tcon *tcon 213 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, 528 struct cifs_fid *fid, 214 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf) 529 { 215 { 530 int rc; 216 int rc; 531 int desired_access; 217 int desired_access; 532 int disposition; 218 int disposition; 533 int create_options = CREATE_NOT_DIR; 219 int create_options = CREATE_NOT_DIR; 534 struct TCP_Server_Info *server = tcon- 220 struct TCP_Server_Info *server = tcon->ses->server; 535 struct cifs_open_parms oparms; 221 struct cifs_open_parms oparms; 536 int rdwr_for_fscache = 0; 222 int rdwr_for_fscache = 0; 537 223 538 if (!server->ops->open) 224 if (!server->ops->open) 539 return -ENOSYS; 225 return -ENOSYS; 540 226 541 /* If we're caching, we need to be abl 227 /* If we're caching, we need to be able to fill in around partial writes. */ 542 if (cifs_fscache_enabled(inode) && (f_ 228 if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY) 543 rdwr_for_fscache = 1; 229 rdwr_for_fscache = 1; 544 230 545 desired_access = cifs_convert_flags(f_ 231 desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache); 546 232 547 /********************************************* 233 /********************************************************************* 548 * open flag mapping table: 234 * open flag mapping table: 549 * 235 * 550 * POSIX Flag CIFS Disposition 236 * POSIX Flag CIFS Disposition 551 * ---------- ---------------- 237 * ---------- ---------------- 552 * O_CREAT FILE_OPEN_IF 238 * O_CREAT FILE_OPEN_IF 553 * O_CREAT | O_EXCL FILE_CREATE 239 * O_CREAT | O_EXCL FILE_CREATE 554 * O_CREAT | O_TRUNC FILE_OVERWRITE_I 240 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF 555 * O_TRUNC FILE_OVERWRITE 241 * O_TRUNC FILE_OVERWRITE 556 * none of the above FILE_OPEN 242 * none of the above FILE_OPEN 557 * 243 * 558 * Note that there is not a direct match 244 * Note that there is not a direct match between disposition 559 * FILE_SUPERSEDE (ie create whether or n 245 * FILE_SUPERSEDE (ie create whether or not file exists although 560 * O_CREAT | O_TRUNC is similar but trunc 246 * O_CREAT | O_TRUNC is similar but truncates the existing 561 * file rather than creating a new file a 247 * file rather than creating a new file as FILE_SUPERSEDE does 562 * (which uses the attributes / metadata 248 * (which uses the attributes / metadata passed in on open call) 563 *? 249 *? 564 *? O_SYNC is a reasonable match to CIFS writ 250 *? O_SYNC is a reasonable match to CIFS writethrough flag 565 *? and the read write flags match reasonably 251 *? and the read write flags match reasonably. O_LARGEFILE 566 *? is irrelevant because largefile support i 252 *? is irrelevant because largefile support is always used 567 *? by this client. Flags O_APPEND, O_DIRECT, 253 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, 568 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need 254 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation 569 ********************************************* 255 *********************************************************************/ 570 256 571 disposition = cifs_get_disposition(f_f 257 disposition = cifs_get_disposition(f_flags); 572 258 573 /* BB pass O_SYNC flag through on file 259 /* BB pass O_SYNC flag through on file attributes .. BB */ 574 260 575 /* O_SYNC also has bit for O_DSYNC so 261 /* O_SYNC also has bit for O_DSYNC so following check picks up either */ 576 if (f_flags & O_SYNC) 262 if (f_flags & O_SYNC) 577 create_options |= CREATE_WRITE 263 create_options |= CREATE_WRITE_THROUGH; 578 264 579 if (f_flags & O_DIRECT) 265 if (f_flags & O_DIRECT) 580 create_options |= CREATE_NO_BU 266 create_options |= CREATE_NO_BUFFER; 581 267 582 retry_open: 268 retry_open: 583 oparms = (struct cifs_open_parms) { 269 oparms = (struct cifs_open_parms) { 584 .tcon = tcon, 270 .tcon = tcon, 585 .cifs_sb = cifs_sb, 271 .cifs_sb = cifs_sb, 586 .desired_access = desired_acce 272 .desired_access = desired_access, 587 .create_options = cifs_create_ 273 .create_options = cifs_create_options(cifs_sb, create_options), 588 .disposition = disposition, 274 .disposition = disposition, 589 .path = full_path, 275 .path = full_path, 590 .fid = fid, 276 .fid = fid, 591 }; 277 }; 592 278 593 rc = server->ops->open(xid, &oparms, o 279 rc = server->ops->open(xid, &oparms, oplock, buf); 594 if (rc) { 280 if (rc) { 595 if (rc == -EACCES && rdwr_for_ 281 if (rc == -EACCES && rdwr_for_fscache == 1) { 596 desired_access = cifs_ 282 desired_access = cifs_convert_flags(f_flags, 0); 597 rdwr_for_fscache = 2; 283 rdwr_for_fscache = 2; 598 goto retry_open; 284 goto retry_open; 599 } 285 } 600 return rc; 286 return rc; 601 } 287 } 602 if (rdwr_for_fscache == 2) 288 if (rdwr_for_fscache == 2) 603 cifs_invalidate_cache(inode, F 289 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 604 290 605 /* TODO: Add support for calling posix 291 /* TODO: Add support for calling posix query info but with passing in fid */ 606 if (tcon->unix_ext) 292 if (tcon->unix_ext) 607 rc = cifs_get_inode_info_unix( 293 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, 608 294 xid); 609 else 295 else 610 rc = cifs_get_inode_info(&inod 296 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, 611 xid, 297 xid, fid); 612 298 613 if (rc) { 299 if (rc) { 614 server->ops->close(xid, tcon, 300 server->ops->close(xid, tcon, fid); 615 if (rc == -ESTALE) 301 if (rc == -ESTALE) 616 rc = -EOPENSTALE; 302 rc = -EOPENSTALE; 617 } 303 } 618 304 619 return rc; 305 return rc; 620 } 306 } 621 307 622 static bool 308 static bool 623 cifs_has_mand_locks(struct cifsInodeInfo *cino 309 cifs_has_mand_locks(struct cifsInodeInfo *cinode) 624 { 310 { 625 struct cifs_fid_locks *cur; 311 struct cifs_fid_locks *cur; 626 bool has_locks = false; 312 bool has_locks = false; 627 313 628 down_read(&cinode->lock_sem); 314 down_read(&cinode->lock_sem); 629 list_for_each_entry(cur, &cinode->llis 315 list_for_each_entry(cur, &cinode->llist, llist) { 630 if (!list_empty(&cur->locks)) 316 if (!list_empty(&cur->locks)) { 631 has_locks = true; 317 has_locks = true; 632 break; 318 break; 633 } 319 } 634 } 320 } 635 up_read(&cinode->lock_sem); 321 up_read(&cinode->lock_sem); 636 return has_locks; 322 return has_locks; 637 } 323 } 638 324 639 void 325 void 640 cifs_down_write(struct rw_semaphore *sem) 326 cifs_down_write(struct rw_semaphore *sem) 641 { 327 { 642 while (!down_write_trylock(sem)) 328 while (!down_write_trylock(sem)) 643 msleep(10); 329 msleep(10); 644 } 330 } 645 331 646 static void cifsFileInfo_put_work(struct work_ 332 static void cifsFileInfo_put_work(struct work_struct *work); 647 void serverclose_work(struct work_struct *work 333 void serverclose_work(struct work_struct *work); 648 334 649 struct cifsFileInfo *cifs_new_fileinfo(struct 335 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, 650 struct 336 struct tcon_link *tlink, __u32 oplock, 651 const c 337 const char *symlink_target) 652 { 338 { 653 struct dentry *dentry = file_dentry(fi 339 struct dentry *dentry = file_dentry(file); 654 struct inode *inode = d_inode(dentry); 340 struct inode *inode = d_inode(dentry); 655 struct cifsInodeInfo *cinode = CIFS_I( 341 struct cifsInodeInfo *cinode = CIFS_I(inode); 656 struct cifsFileInfo *cfile; 342 struct cifsFileInfo *cfile; 657 struct cifs_fid_locks *fdlocks; 343 struct cifs_fid_locks *fdlocks; 658 struct cifs_tcon *tcon = tlink_tcon(tl 344 struct cifs_tcon *tcon = tlink_tcon(tlink); 659 struct TCP_Server_Info *server = tcon- 345 struct TCP_Server_Info *server = tcon->ses->server; 660 346 661 cfile = kzalloc(sizeof(struct cifsFile 347 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 662 if (cfile == NULL) 348 if (cfile == NULL) 663 return cfile; 349 return cfile; 664 350 665 fdlocks = kzalloc(sizeof(struct cifs_f 351 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); 666 if (!fdlocks) { 352 if (!fdlocks) { 667 kfree(cfile); 353 kfree(cfile); 668 return NULL; 354 return NULL; 669 } 355 } 670 356 671 if (symlink_target) { 357 if (symlink_target) { 672 cfile->symlink_target = kstrdu 358 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL); 673 if (!cfile->symlink_target) { 359 if (!cfile->symlink_target) { 674 kfree(fdlocks); 360 kfree(fdlocks); 675 kfree(cfile); 361 kfree(cfile); 676 return NULL; 362 return NULL; 677 } 363 } 678 } 364 } 679 365 680 INIT_LIST_HEAD(&fdlocks->locks); 366 INIT_LIST_HEAD(&fdlocks->locks); 681 fdlocks->cfile = cfile; 367 fdlocks->cfile = cfile; 682 cfile->llist = fdlocks; 368 cfile->llist = fdlocks; 683 369 684 cfile->count = 1; 370 cfile->count = 1; 685 cfile->pid = current->tgid; 371 cfile->pid = current->tgid; 686 cfile->uid = current_fsuid(); 372 cfile->uid = current_fsuid(); 687 cfile->dentry = dget(dentry); 373 cfile->dentry = dget(dentry); 688 cfile->f_flags = file->f_flags; 374 cfile->f_flags = file->f_flags; 689 cfile->invalidHandle = false; 375 cfile->invalidHandle = false; 690 cfile->deferred_close_scheduled = fals 376 cfile->deferred_close_scheduled = false; 691 cfile->tlink = cifs_get_tlink(tlink); 377 cfile->tlink = cifs_get_tlink(tlink); 692 INIT_WORK(&cfile->oplock_break, cifs_o 378 INIT_WORK(&cfile->oplock_break, cifs_oplock_break); 693 INIT_WORK(&cfile->put, cifsFileInfo_pu 379 INIT_WORK(&cfile->put, cifsFileInfo_put_work); 694 INIT_WORK(&cfile->serverclose, serverc 380 INIT_WORK(&cfile->serverclose, serverclose_work); 695 INIT_DELAYED_WORK(&cfile->deferred, sm 381 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close); 696 mutex_init(&cfile->fh_mutex); 382 mutex_init(&cfile->fh_mutex); 697 spin_lock_init(&cfile->file_info_lock) 383 spin_lock_init(&cfile->file_info_lock); 698 384 699 cifs_sb_active(inode->i_sb); 385 cifs_sb_active(inode->i_sb); 700 386 701 /* 387 /* 702 * If the server returned a read oploc 388 * If the server returned a read oplock and we have mandatory brlocks, 703 * set oplock level to None. 389 * set oplock level to None. 704 */ 390 */ 705 if (server->ops->is_read_op(oplock) && 391 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 706 cifs_dbg(FYI, "Reset oplock va 392 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 707 oplock = 0; 393 oplock = 0; 708 } 394 } 709 395 710 cifs_down_write(&cinode->lock_sem); 396 cifs_down_write(&cinode->lock_sem); 711 list_add(&fdlocks->llist, &cinode->lli 397 list_add(&fdlocks->llist, &cinode->llist); 712 up_write(&cinode->lock_sem); 398 up_write(&cinode->lock_sem); 713 399 714 spin_lock(&tcon->open_file_lock); 400 spin_lock(&tcon->open_file_lock); 715 if (fid->pending_open->oplock != CIFS_ 401 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) 716 oplock = fid->pending_open->op 402 oplock = fid->pending_open->oplock; 717 list_del(&fid->pending_open->olist); 403 list_del(&fid->pending_open->olist); 718 404 719 fid->purge_cache = false; 405 fid->purge_cache = false; 720 server->ops->set_fid(cfile, fid, oploc 406 server->ops->set_fid(cfile, fid, oplock); 721 407 722 list_add(&cfile->tlist, &tcon->openFil 408 list_add(&cfile->tlist, &tcon->openFileList); 723 atomic_inc(&tcon->num_local_opens); 409 atomic_inc(&tcon->num_local_opens); 724 410 725 /* if readable file instance put first 411 /* if readable file instance put first in list*/ 726 spin_lock(&cinode->open_file_lock); 412 spin_lock(&cinode->open_file_lock); 727 if (file->f_mode & FMODE_READ) 413 if (file->f_mode & FMODE_READ) 728 list_add(&cfile->flist, &cinod 414 list_add(&cfile->flist, &cinode->openFileList); 729 else 415 else 730 list_add_tail(&cfile->flist, & 416 list_add_tail(&cfile->flist, &cinode->openFileList); 731 spin_unlock(&cinode->open_file_lock); 417 spin_unlock(&cinode->open_file_lock); 732 spin_unlock(&tcon->open_file_lock); 418 spin_unlock(&tcon->open_file_lock); 733 419 734 if (fid->purge_cache) 420 if (fid->purge_cache) 735 cifs_zap_mapping(inode); 421 cifs_zap_mapping(inode); 736 422 737 file->private_data = cfile; 423 file->private_data = cfile; 738 return cfile; 424 return cfile; 739 } 425 } 740 426 741 struct cifsFileInfo * 427 struct cifsFileInfo * 742 cifsFileInfo_get(struct cifsFileInfo *cifs_fil 428 cifsFileInfo_get(struct cifsFileInfo *cifs_file) 743 { 429 { 744 spin_lock(&cifs_file->file_info_lock); 430 spin_lock(&cifs_file->file_info_lock); 745 cifsFileInfo_get_locked(cifs_file); 431 cifsFileInfo_get_locked(cifs_file); 746 spin_unlock(&cifs_file->file_info_lock 432 spin_unlock(&cifs_file->file_info_lock); 747 return cifs_file; 433 return cifs_file; 748 } 434 } 749 435 750 static void cifsFileInfo_put_final(struct cifs 436 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file) 751 { 437 { 752 struct inode *inode = d_inode(cifs_fil 438 struct inode *inode = d_inode(cifs_file->dentry); 753 struct cifsInodeInfo *cifsi = CIFS_I(i 439 struct cifsInodeInfo *cifsi = CIFS_I(inode); 754 struct cifsLockInfo *li, *tmp; 440 struct cifsLockInfo *li, *tmp; 755 struct super_block *sb = inode->i_sb; 441 struct super_block *sb = inode->i_sb; 756 442 757 /* 443 /* 758 * Delete any outstanding lock records 444 * Delete any outstanding lock records. We'll lose them when the file 759 * is closed anyway. 445 * is closed anyway. 760 */ 446 */ 761 cifs_down_write(&cifsi->lock_sem); 447 cifs_down_write(&cifsi->lock_sem); 762 list_for_each_entry_safe(li, tmp, &cif 448 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { 763 list_del(&li->llist); 449 list_del(&li->llist); 764 cifs_del_lock_waiters(li); 450 cifs_del_lock_waiters(li); 765 kfree(li); 451 kfree(li); 766 } 452 } 767 list_del(&cifs_file->llist->llist); 453 list_del(&cifs_file->llist->llist); 768 kfree(cifs_file->llist); 454 kfree(cifs_file->llist); 769 up_write(&cifsi->lock_sem); 455 up_write(&cifsi->lock_sem); 770 456 771 cifs_put_tlink(cifs_file->tlink); 457 cifs_put_tlink(cifs_file->tlink); 772 dput(cifs_file->dentry); 458 dput(cifs_file->dentry); 773 cifs_sb_deactive(sb); 459 cifs_sb_deactive(sb); 774 kfree(cifs_file->symlink_target); 460 kfree(cifs_file->symlink_target); 775 kfree(cifs_file); 461 kfree(cifs_file); 776 } 462 } 777 463 778 static void cifsFileInfo_put_work(struct work_ 464 static void cifsFileInfo_put_work(struct work_struct *work) 779 { 465 { 780 struct cifsFileInfo *cifs_file = conta 466 struct cifsFileInfo *cifs_file = container_of(work, 781 struct cifsFileInfo, p 467 struct cifsFileInfo, put); 782 468 783 cifsFileInfo_put_final(cifs_file); 469 cifsFileInfo_put_final(cifs_file); 784 } 470 } 785 471 786 void serverclose_work(struct work_struct *work 472 void serverclose_work(struct work_struct *work) 787 { 473 { 788 struct cifsFileInfo *cifs_file = conta 474 struct cifsFileInfo *cifs_file = container_of(work, 789 struct cifsFileInfo, s 475 struct cifsFileInfo, serverclose); 790 476 791 struct cifs_tcon *tcon = tlink_tcon(ci 477 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 792 478 793 struct TCP_Server_Info *server = tcon- 479 struct TCP_Server_Info *server = tcon->ses->server; 794 int rc = 0; 480 int rc = 0; 795 int retries = 0; 481 int retries = 0; 796 int MAX_RETRIES = 4; 482 int MAX_RETRIES = 4; 797 483 798 do { 484 do { 799 if (server->ops->close_getattr 485 if (server->ops->close_getattr) 800 rc = server->ops->clos 486 rc = server->ops->close_getattr(0, tcon, cifs_file); 801 else if (server->ops->close) 487 else if (server->ops->close) 802 rc = server->ops->clos 488 rc = server->ops->close(0, tcon, &cifs_file->fid); 803 489 804 if (rc == -EBUSY || rc == -EAG 490 if (rc == -EBUSY || rc == -EAGAIN) { 805 retries++; 491 retries++; 806 msleep(250); 492 msleep(250); 807 } 493 } 808 } while ((rc == -EBUSY || rc == -EAGAI 494 } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES) 809 ); 495 ); 810 496 811 if (retries == MAX_RETRIES) 497 if (retries == MAX_RETRIES) 812 pr_warn("Serverclose failed %d 498 pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES); 813 499 814 if (cifs_file->offload) 500 if (cifs_file->offload) 815 queue_work(fileinfo_put_wq, &c 501 queue_work(fileinfo_put_wq, &cifs_file->put); 816 else 502 else 817 cifsFileInfo_put_final(cifs_fi 503 cifsFileInfo_put_final(cifs_file); 818 } 504 } 819 505 820 /** 506 /** 821 * cifsFileInfo_put - release a reference of f 507 * cifsFileInfo_put - release a reference of file priv data 822 * 508 * 823 * Always potentially wait for oplock handler. 509 * Always potentially wait for oplock handler. See _cifsFileInfo_put(). 824 * 510 * 825 * @cifs_file: cifs/smb3 specific info (eg re 511 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 826 */ 512 */ 827 void cifsFileInfo_put(struct cifsFileInfo *cif 513 void cifsFileInfo_put(struct cifsFileInfo *cifs_file) 828 { 514 { 829 _cifsFileInfo_put(cifs_file, true, tru 515 _cifsFileInfo_put(cifs_file, true, true); 830 } 516 } 831 517 832 /** 518 /** 833 * _cifsFileInfo_put - release a reference of 519 * _cifsFileInfo_put - release a reference of file priv data 834 * 520 * 835 * This may involve closing the filehandle @ci 521 * This may involve closing the filehandle @cifs_file out on the 836 * server. Must be called without holding tcon 522 * server. Must be called without holding tcon->open_file_lock, 837 * cinode->open_file_lock and cifs_file->file_ 523 * cinode->open_file_lock and cifs_file->file_info_lock. 838 * 524 * 839 * If @wait_for_oplock_handler is true and we 525 * If @wait_for_oplock_handler is true and we are releasing the last 840 * reference, wait for any running oplock brea 526 * reference, wait for any running oplock break handler of the file 841 * and cancel any pending one. 527 * and cancel any pending one. 842 * 528 * 843 * @cifs_file: cifs/smb3 specific info (eg re 529 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 844 * @wait_oplock_handler: must be false if call 530 * @wait_oplock_handler: must be false if called from oplock_break_handler 845 * @offload: not offloaded on close and opl 531 * @offload: not offloaded on close and oplock breaks 846 * 532 * 847 */ 533 */ 848 void _cifsFileInfo_put(struct cifsFileInfo *ci 534 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, 849 bool wait_oplock_handle 535 bool wait_oplock_handler, bool offload) 850 { 536 { 851 struct inode *inode = d_inode(cifs_fil 537 struct inode *inode = d_inode(cifs_file->dentry); 852 struct cifs_tcon *tcon = tlink_tcon(ci 538 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 853 struct TCP_Server_Info *server = tcon- 539 struct TCP_Server_Info *server = tcon->ses->server; 854 struct cifsInodeInfo *cifsi = CIFS_I(i 540 struct cifsInodeInfo *cifsi = CIFS_I(inode); 855 struct super_block *sb = inode->i_sb; 541 struct super_block *sb = inode->i_sb; 856 struct cifs_sb_info *cifs_sb = CIFS_SB 542 struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 857 struct cifs_fid fid = {}; 543 struct cifs_fid fid = {}; 858 struct cifs_pending_open open; 544 struct cifs_pending_open open; 859 bool oplock_break_cancelled; 545 bool oplock_break_cancelled; 860 bool serverclose_offloaded = false; 546 bool serverclose_offloaded = false; 861 547 862 spin_lock(&tcon->open_file_lock); 548 spin_lock(&tcon->open_file_lock); 863 spin_lock(&cifsi->open_file_lock); 549 spin_lock(&cifsi->open_file_lock); 864 spin_lock(&cifs_file->file_info_lock); 550 spin_lock(&cifs_file->file_info_lock); 865 551 866 cifs_file->offload = offload; 552 cifs_file->offload = offload; 867 if (--cifs_file->count > 0) { 553 if (--cifs_file->count > 0) { 868 spin_unlock(&cifs_file->file_i 554 spin_unlock(&cifs_file->file_info_lock); 869 spin_unlock(&cifsi->open_file_ 555 spin_unlock(&cifsi->open_file_lock); 870 spin_unlock(&tcon->open_file_l 556 spin_unlock(&tcon->open_file_lock); 871 return; 557 return; 872 } 558 } 873 spin_unlock(&cifs_file->file_info_lock 559 spin_unlock(&cifs_file->file_info_lock); 874 560 875 if (server->ops->get_lease_key) 561 if (server->ops->get_lease_key) 876 server->ops->get_lease_key(ino 562 server->ops->get_lease_key(inode, &fid); 877 563 878 /* store open in pending opens to make 564 /* store open in pending opens to make sure we don't miss lease break */ 879 cifs_add_pending_open_locked(&fid, cif 565 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); 880 566 881 /* remove it from the lists */ 567 /* remove it from the lists */ 882 list_del(&cifs_file->flist); 568 list_del(&cifs_file->flist); 883 list_del(&cifs_file->tlist); 569 list_del(&cifs_file->tlist); 884 atomic_dec(&tcon->num_local_opens); 570 atomic_dec(&tcon->num_local_opens); 885 571 886 if (list_empty(&cifsi->openFileList)) 572 if (list_empty(&cifsi->openFileList)) { 887 cifs_dbg(FYI, "closing last op 573 cifs_dbg(FYI, "closing last open instance for inode %p\n", 888 d_inode(cifs_file->de 574 d_inode(cifs_file->dentry)); 889 /* 575 /* 890 * In strict cache mode we nee 576 * In strict cache mode we need invalidate mapping on the last 891 * close because it may cause 577 * close because it may cause a error when we open this file 892 * again and get at least leve 578 * again and get at least level II oplock. 893 */ 579 */ 894 if (cifs_sb->mnt_cifs_flags & 580 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) 895 set_bit(CIFS_INO_INVAL 581 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); 896 cifs_set_oplock_level(cifsi, 0 582 cifs_set_oplock_level(cifsi, 0); 897 } 583 } 898 584 899 spin_unlock(&cifsi->open_file_lock); 585 spin_unlock(&cifsi->open_file_lock); 900 spin_unlock(&tcon->open_file_lock); 586 spin_unlock(&tcon->open_file_lock); 901 587 902 oplock_break_cancelled = wait_oplock_h 588 oplock_break_cancelled = wait_oplock_handler ? 903 cancel_work_sync(&cifs_file->o 589 cancel_work_sync(&cifs_file->oplock_break) : false; 904 590 905 if (!tcon->need_reconnect && !cifs_fil 591 if (!tcon->need_reconnect && !cifs_file->invalidHandle) { 906 struct TCP_Server_Info *server 592 struct TCP_Server_Info *server = tcon->ses->server; 907 unsigned int xid; 593 unsigned int xid; 908 int rc = 0; 594 int rc = 0; 909 595 910 xid = get_xid(); 596 xid = get_xid(); 911 if (server->ops->close_getattr 597 if (server->ops->close_getattr) 912 rc = server->ops->clos 598 rc = server->ops->close_getattr(xid, tcon, cifs_file); 913 else if (server->ops->close) 599 else if (server->ops->close) 914 rc = server->ops->clos 600 rc = server->ops->close(xid, tcon, &cifs_file->fid); 915 _free_xid(xid); 601 _free_xid(xid); 916 602 917 if (rc == -EBUSY || rc == -EAG 603 if (rc == -EBUSY || rc == -EAGAIN) { 918 // Server close failed 604 // Server close failed, hence offloading it as an async op 919 queue_work(serverclose 605 queue_work(serverclose_wq, &cifs_file->serverclose); 920 serverclose_offloaded 606 serverclose_offloaded = true; 921 } 607 } 922 } 608 } 923 609 924 if (oplock_break_cancelled) 610 if (oplock_break_cancelled) 925 cifs_done_oplock_break(cifsi); 611 cifs_done_oplock_break(cifsi); 926 612 927 cifs_del_pending_open(&open); 613 cifs_del_pending_open(&open); 928 614 929 // if serverclose has been offloaded t 615 // if serverclose has been offloaded to wq (on failure), it will 930 // handle offloading put as well. If s 616 // handle offloading put as well. If serverclose not offloaded, 931 // we need to handle offloading put he 617 // we need to handle offloading put here. 932 if (!serverclose_offloaded) { 618 if (!serverclose_offloaded) { 933 if (offload) 619 if (offload) 934 queue_work(fileinfo_pu 620 queue_work(fileinfo_put_wq, &cifs_file->put); 935 else 621 else 936 cifsFileInfo_put_final 622 cifsFileInfo_put_final(cifs_file); 937 } 623 } 938 } 624 } 939 625 940 int cifs_open(struct inode *inode, struct file 626 int cifs_open(struct inode *inode, struct file *file) 941 627 942 { 628 { 943 int rc = -EACCES; 629 int rc = -EACCES; 944 unsigned int xid; 630 unsigned int xid; 945 __u32 oplock; 631 __u32 oplock; 946 struct cifs_sb_info *cifs_sb; 632 struct cifs_sb_info *cifs_sb; 947 struct TCP_Server_Info *server; 633 struct TCP_Server_Info *server; 948 struct cifs_tcon *tcon; 634 struct cifs_tcon *tcon; 949 struct tcon_link *tlink; 635 struct tcon_link *tlink; 950 struct cifsFileInfo *cfile = NULL; 636 struct cifsFileInfo *cfile = NULL; 951 void *page; 637 void *page; 952 const char *full_path; 638 const char *full_path; 953 bool posix_open_ok = false; 639 bool posix_open_ok = false; 954 struct cifs_fid fid = {}; 640 struct cifs_fid fid = {}; 955 struct cifs_pending_open open; 641 struct cifs_pending_open open; 956 struct cifs_open_info_data data = {}; 642 struct cifs_open_info_data data = {}; 957 643 958 xid = get_xid(); 644 xid = get_xid(); 959 645 960 cifs_sb = CIFS_SB(inode->i_sb); 646 cifs_sb = CIFS_SB(inode->i_sb); 961 if (unlikely(cifs_forced_shutdown(cifs 647 if (unlikely(cifs_forced_shutdown(cifs_sb))) { 962 free_xid(xid); 648 free_xid(xid); 963 return -EIO; 649 return -EIO; 964 } 650 } 965 651 966 tlink = cifs_sb_tlink(cifs_sb); 652 tlink = cifs_sb_tlink(cifs_sb); 967 if (IS_ERR(tlink)) { 653 if (IS_ERR(tlink)) { 968 free_xid(xid); 654 free_xid(xid); 969 return PTR_ERR(tlink); 655 return PTR_ERR(tlink); 970 } 656 } 971 tcon = tlink_tcon(tlink); 657 tcon = tlink_tcon(tlink); 972 server = tcon->ses->server; 658 server = tcon->ses->server; 973 659 974 page = alloc_dentry_path(); 660 page = alloc_dentry_path(); 975 full_path = build_path_from_dentry(fil 661 full_path = build_path_from_dentry(file_dentry(file), page); 976 if (IS_ERR(full_path)) { 662 if (IS_ERR(full_path)) { 977 rc = PTR_ERR(full_path); 663 rc = PTR_ERR(full_path); 978 goto out; 664 goto out; 979 } 665 } 980 666 981 cifs_dbg(FYI, "inode = 0x%p file flags 667 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", 982 inode, file->f_flags, full_pa 668 inode, file->f_flags, full_path); 983 669 984 if (file->f_flags & O_DIRECT && 670 if (file->f_flags & O_DIRECT && 985 cifs_sb->mnt_cifs_flags & CIFS_MOU 671 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { 986 if (cifs_sb->mnt_cifs_flags & 672 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 987 file->f_op = &cifs_fil 673 file->f_op = &cifs_file_direct_nobrl_ops; 988 else 674 else 989 file->f_op = &cifs_fil 675 file->f_op = &cifs_file_direct_ops; 990 } 676 } 991 677 992 /* Get the cached handle as SMB2 close 678 /* Get the cached handle as SMB2 close is deferred */ 993 rc = cifs_get_readable_path(tcon, full 679 rc = cifs_get_readable_path(tcon, full_path, &cfile); 994 if (rc == 0) { 680 if (rc == 0) { 995 if (file->f_flags == cfile->f_ 681 if (file->f_flags == cfile->f_flags) { 996 file->private_data = c 682 file->private_data = cfile; 997 spin_lock(&CIFS_I(inod 683 spin_lock(&CIFS_I(inode)->deferred_lock); 998 cifs_del_deferred_clos 684 cifs_del_deferred_close(cfile); 999 spin_unlock(&CIFS_I(in 685 spin_unlock(&CIFS_I(inode)->deferred_lock); 1000 goto use_cache; 686 goto use_cache; 1001 } else { 687 } else { 1002 _cifsFileInfo_put(cfi 688 _cifsFileInfo_put(cfile, true, false); 1003 } 689 } 1004 } 690 } 1005 691 1006 if (server->oplocks) 692 if (server->oplocks) 1007 oplock = REQ_OPLOCK; 693 oplock = REQ_OPLOCK; 1008 else 694 else 1009 oplock = 0; 695 oplock = 0; 1010 696 1011 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 697 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1012 if (!tcon->broken_posix_open && tcon- 698 if (!tcon->broken_posix_open && tcon->unix_ext && 1013 cap_unix(tcon->ses) && (CIFS_UNIX 699 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & 1014 le64_to_cpu(t 700 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 1015 /* can not refresh inode info 701 /* can not refresh inode info since size could be stale */ 1016 rc = cifs_posix_open(full_pat 702 rc = cifs_posix_open(full_path, &inode, inode->i_sb, 1017 cifs_sb->ctx- 703 cifs_sb->ctx->file_mode /* ignored */, 1018 file->f_flags 704 file->f_flags, &oplock, &fid.netfid, xid); 1019 if (rc == 0) { 705 if (rc == 0) { 1020 cifs_dbg(FYI, "posix 706 cifs_dbg(FYI, "posix open succeeded\n"); 1021 posix_open_ok = true; 707 posix_open_ok = true; 1022 } else if ((rc == -EINVAL) || 708 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 1023 if (tcon->ses->server 709 if (tcon->ses->serverNOS) 1024 cifs_dbg(VFS, 710 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", 1025 tcon 711 tcon->ses->ip_addr, 1026 tcon 712 tcon->ses->serverNOS); 1027 tcon->broken_posix_op 713 tcon->broken_posix_open = true; 1028 } else if ((rc != -EIO) && (r 714 } else if ((rc != -EIO) && (rc != -EREMOTE) && 1029 (rc != -EOPNOTSUPP)) 715 (rc != -EOPNOTSUPP)) /* path not found or net err */ 1030 goto out; 716 goto out; 1031 /* 717 /* 1032 * Else fallthrough to retry 718 * Else fallthrough to retry open the old way on network i/o 1033 * or DFS errors. 719 * or DFS errors. 1034 */ 720 */ 1035 } 721 } 1036 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 722 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1037 723 1038 if (server->ops->get_lease_key) 724 if (server->ops->get_lease_key) 1039 server->ops->get_lease_key(in 725 server->ops->get_lease_key(inode, &fid); 1040 726 1041 cifs_add_pending_open(&fid, tlink, &o 727 cifs_add_pending_open(&fid, tlink, &open); 1042 728 1043 if (!posix_open_ok) { 729 if (!posix_open_ok) { 1044 if (server->ops->get_lease_ke 730 if (server->ops->get_lease_key) 1045 server->ops->get_leas 731 server->ops->get_lease_key(inode, &fid); 1046 732 1047 rc = cifs_nt_open(full_path, 733 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid, 1048 xid, &data) 734 xid, &data); 1049 if (rc) { 735 if (rc) { 1050 cifs_del_pending_open 736 cifs_del_pending_open(&open); 1051 goto out; 737 goto out; 1052 } 738 } 1053 } 739 } 1054 740 1055 cfile = cifs_new_fileinfo(&fid, file, 741 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target); 1056 if (cfile == NULL) { 742 if (cfile == NULL) { 1057 if (server->ops->close) 743 if (server->ops->close) 1058 server->ops->close(xi 744 server->ops->close(xid, tcon, &fid); 1059 cifs_del_pending_open(&open); 745 cifs_del_pending_open(&open); 1060 rc = -ENOMEM; 746 rc = -ENOMEM; 1061 goto out; 747 goto out; 1062 } 748 } 1063 749 1064 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 750 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1065 if ((oplock & CIFS_CREATE_ACTION) && 751 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { 1066 /* 752 /* 1067 * Time to set mode which we 753 * Time to set mode which we can not set earlier due to 1068 * problems creating new read 754 * problems creating new read-only files. 1069 */ 755 */ 1070 struct cifs_unix_set_info_arg 756 struct cifs_unix_set_info_args args = { 1071 .mode = inode->i_mo 757 .mode = inode->i_mode, 1072 .uid = INVALID_UID 758 .uid = INVALID_UID, /* no change */ 1073 .gid = INVALID_GID 759 .gid = INVALID_GID, /* no change */ 1074 .ctime = NO_CHANGE_6 760 .ctime = NO_CHANGE_64, 1075 .atime = NO_CHANGE_6 761 .atime = NO_CHANGE_64, 1076 .mtime = NO_CHANGE_6 762 .mtime = NO_CHANGE_64, 1077 .device = 0, 763 .device = 0, 1078 }; 764 }; 1079 CIFSSMBUnixSetFileInfo(xid, t 765 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, 1080 cfile- 766 cfile->pid); 1081 } 767 } 1082 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 768 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1083 769 1084 use_cache: 770 use_cache: 1085 fscache_use_cookie(cifs_inode_cookie( 771 fscache_use_cookie(cifs_inode_cookie(file_inode(file)), 1086 file->f_mode & FMO 772 file->f_mode & FMODE_WRITE); 1087 if (!(file->f_flags & O_DIRECT)) 773 if (!(file->f_flags & O_DIRECT)) 1088 goto out; 774 goto out; 1089 if ((file->f_flags & (O_ACCMODE | O_A 775 if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY) 1090 goto out; 776 goto out; 1091 cifs_invalidate_cache(file_inode(file 777 cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE); 1092 778 1093 out: 779 out: 1094 free_dentry_path(page); 780 free_dentry_path(page); 1095 free_xid(xid); 781 free_xid(xid); 1096 cifs_put_tlink(tlink); 782 cifs_put_tlink(tlink); 1097 cifs_free_open_info(&data); 783 cifs_free_open_info(&data); 1098 return rc; 784 return rc; 1099 } 785 } 1100 786 1101 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 787 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1102 static int cifs_push_posix_locks(struct cifsF 788 static int cifs_push_posix_locks(struct cifsFileInfo *cfile); 1103 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 789 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1104 790 1105 /* 791 /* 1106 * Try to reacquire byte range locks that wer 792 * Try to reacquire byte range locks that were released when session 1107 * to server was lost. 793 * to server was lost. 1108 */ 794 */ 1109 static int 795 static int 1110 cifs_relock_file(struct cifsFileInfo *cfile) 796 cifs_relock_file(struct cifsFileInfo *cfile) 1111 { 797 { 1112 struct cifsInodeInfo *cinode = CIFS_I 798 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1113 struct cifs_tcon *tcon = tlink_tcon(c 799 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1114 int rc = 0; 800 int rc = 0; 1115 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 801 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1116 struct cifs_sb_info *cifs_sb = CIFS_S 802 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 1117 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 803 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1118 804 1119 down_read_nested(&cinode->lock_sem, S 805 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); 1120 if (cinode->can_cache_brlcks) { 806 if (cinode->can_cache_brlcks) { 1121 /* can cache locks - no need 807 /* can cache locks - no need to relock */ 1122 up_read(&cinode->lock_sem); 808 up_read(&cinode->lock_sem); 1123 return rc; 809 return rc; 1124 } 810 } 1125 811 1126 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 812 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1127 if (cap_unix(tcon->ses) && 813 if (cap_unix(tcon->ses) && 1128 (CIFS_UNIX_FCNTL_CAP & le64_to_cp 814 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 1129 ((cifs_sb->mnt_cifs_flags & CIFS_ 815 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 1130 rc = cifs_push_posix_locks(cf 816 rc = cifs_push_posix_locks(cfile); 1131 else 817 else 1132 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 818 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1133 rc = tcon->ses->server->ops-> 819 rc = tcon->ses->server->ops->push_mand_locks(cfile); 1134 820 1135 up_read(&cinode->lock_sem); 821 up_read(&cinode->lock_sem); 1136 return rc; 822 return rc; 1137 } 823 } 1138 824 1139 static int 825 static int 1140 cifs_reopen_file(struct cifsFileInfo *cfile, 826 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) 1141 { 827 { 1142 int rc = -EACCES; 828 int rc = -EACCES; 1143 unsigned int xid; 829 unsigned int xid; 1144 __u32 oplock; 830 __u32 oplock; 1145 struct cifs_sb_info *cifs_sb; 831 struct cifs_sb_info *cifs_sb; 1146 struct cifs_tcon *tcon; 832 struct cifs_tcon *tcon; 1147 struct TCP_Server_Info *server; 833 struct TCP_Server_Info *server; 1148 struct cifsInodeInfo *cinode; 834 struct cifsInodeInfo *cinode; 1149 struct inode *inode; 835 struct inode *inode; 1150 void *page; 836 void *page; 1151 const char *full_path; 837 const char *full_path; 1152 int desired_access; 838 int desired_access; 1153 int disposition = FILE_OPEN; 839 int disposition = FILE_OPEN; 1154 int create_options = CREATE_NOT_DIR; 840 int create_options = CREATE_NOT_DIR; 1155 struct cifs_open_parms oparms; 841 struct cifs_open_parms oparms; 1156 int rdwr_for_fscache = 0; 842 int rdwr_for_fscache = 0; 1157 843 1158 xid = get_xid(); 844 xid = get_xid(); 1159 mutex_lock(&cfile->fh_mutex); 845 mutex_lock(&cfile->fh_mutex); 1160 if (!cfile->invalidHandle) { 846 if (!cfile->invalidHandle) { 1161 mutex_unlock(&cfile->fh_mutex 847 mutex_unlock(&cfile->fh_mutex); 1162 free_xid(xid); 848 free_xid(xid); 1163 return 0; 849 return 0; 1164 } 850 } 1165 851 1166 inode = d_inode(cfile->dentry); 852 inode = d_inode(cfile->dentry); 1167 cifs_sb = CIFS_SB(inode->i_sb); 853 cifs_sb = CIFS_SB(inode->i_sb); 1168 tcon = tlink_tcon(cfile->tlink); 854 tcon = tlink_tcon(cfile->tlink); 1169 server = tcon->ses->server; 855 server = tcon->ses->server; 1170 856 1171 /* 857 /* 1172 * Can not grab rename sem here becau 858 * Can not grab rename sem here because various ops, including those 1173 * that already have the rename sem c 859 * that already have the rename sem can end up causing writepage to get 1174 * called and if the server was down 860 * called and if the server was down that means we end up here, and we 1175 * can never tell if the caller alrea 861 * can never tell if the caller already has the rename_sem. 1176 */ 862 */ 1177 page = alloc_dentry_path(); 863 page = alloc_dentry_path(); 1178 full_path = build_path_from_dentry(cf 864 full_path = build_path_from_dentry(cfile->dentry, page); 1179 if (IS_ERR(full_path)) { 865 if (IS_ERR(full_path)) { 1180 mutex_unlock(&cfile->fh_mutex 866 mutex_unlock(&cfile->fh_mutex); 1181 free_dentry_path(page); 867 free_dentry_path(page); 1182 free_xid(xid); 868 free_xid(xid); 1183 return PTR_ERR(full_path); 869 return PTR_ERR(full_path); 1184 } 870 } 1185 871 1186 cifs_dbg(FYI, "inode = 0x%p file flag 872 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", 1187 inode, cfile->f_flags, full_ 873 inode, cfile->f_flags, full_path); 1188 874 1189 if (tcon->ses->server->oplocks) 875 if (tcon->ses->server->oplocks) 1190 oplock = REQ_OPLOCK; 876 oplock = REQ_OPLOCK; 1191 else 877 else 1192 oplock = 0; 878 oplock = 0; 1193 879 1194 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 880 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1195 if (tcon->unix_ext && cap_unix(tcon-> 881 if (tcon->unix_ext && cap_unix(tcon->ses) && 1196 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 882 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 1197 le64_to_cpu(t 883 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 1198 /* 884 /* 1199 * O_CREAT, O_EXCL and O_TRUN 885 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the 1200 * original open. Must mask t 886 * original open. Must mask them off for a reopen. 1201 */ 887 */ 1202 unsigned int oflags = cfile-> 888 unsigned int oflags = cfile->f_flags & 1203 889 ~(O_CREAT | O_EXCL | O_TRUNC); 1204 890 1205 rc = cifs_posix_open(full_pat 891 rc = cifs_posix_open(full_path, NULL, inode->i_sb, 1206 cifs_sb- 892 cifs_sb->ctx->file_mode /* ignored */, 1207 oflags, 893 oflags, &oplock, &cfile->fid.netfid, xid); 1208 if (rc == 0) { 894 if (rc == 0) { 1209 cifs_dbg(FYI, "posix 895 cifs_dbg(FYI, "posix reopen succeeded\n"); 1210 oparms.reconnect = tr 896 oparms.reconnect = true; 1211 goto reopen_success; 897 goto reopen_success; 1212 } 898 } 1213 /* 899 /* 1214 * fallthrough to retry open 900 * fallthrough to retry open the old way on errors, especially 1215 * in the reconnect path it i 901 * in the reconnect path it is important to retry hard 1216 */ 902 */ 1217 } 903 } 1218 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 904 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1219 905 1220 /* If we're caching, we need to be ab 906 /* If we're caching, we need to be able to fill in around partial writes. */ 1221 if (cifs_fscache_enabled(inode) && (c 907 if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY) 1222 rdwr_for_fscache = 1; 908 rdwr_for_fscache = 1; 1223 909 1224 desired_access = cifs_convert_flags(c 910 desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache); 1225 911 1226 /* O_SYNC also has bit for O_DSYNC so 912 /* O_SYNC also has bit for O_DSYNC so following check picks up either */ 1227 if (cfile->f_flags & O_SYNC) 913 if (cfile->f_flags & O_SYNC) 1228 create_options |= CREATE_WRIT 914 create_options |= CREATE_WRITE_THROUGH; 1229 915 1230 if (cfile->f_flags & O_DIRECT) 916 if (cfile->f_flags & O_DIRECT) 1231 create_options |= CREATE_NO_B 917 create_options |= CREATE_NO_BUFFER; 1232 918 1233 if (server->ops->get_lease_key) 919 if (server->ops->get_lease_key) 1234 server->ops->get_lease_key(in 920 server->ops->get_lease_key(inode, &cfile->fid); 1235 921 1236 retry_open: 922 retry_open: 1237 oparms = (struct cifs_open_parms) { 923 oparms = (struct cifs_open_parms) { 1238 .tcon = tcon, 924 .tcon = tcon, 1239 .cifs_sb = cifs_sb, 925 .cifs_sb = cifs_sb, 1240 .desired_access = desired_acc 926 .desired_access = desired_access, 1241 .create_options = cifs_create 927 .create_options = cifs_create_options(cifs_sb, create_options), 1242 .disposition = disposition, 928 .disposition = disposition, 1243 .path = full_path, 929 .path = full_path, 1244 .fid = &cfile->fid, 930 .fid = &cfile->fid, 1245 .reconnect = true, 931 .reconnect = true, 1246 }; 932 }; 1247 933 1248 /* 934 /* 1249 * Can not refresh inode by passing i 935 * Can not refresh inode by passing in file_info buf to be returned by 1250 * ops->open and then calling get_ino 936 * ops->open and then calling get_inode_info with returned buf since 1251 * file might have write behind data 937 * file might have write behind data that needs to be flushed and server 1252 * version of file size can be stale. 938 * version of file size can be stale. If we knew for sure that inode was 1253 * not dirty locally we could do this 939 * not dirty locally we could do this. 1254 */ 940 */ 1255 rc = server->ops->open(xid, &oparms, 941 rc = server->ops->open(xid, &oparms, &oplock, NULL); 1256 if (rc == -ENOENT && oparms.reconnect 942 if (rc == -ENOENT && oparms.reconnect == false) { 1257 /* durable handle timeout is 943 /* durable handle timeout is expired - open the file again */ 1258 rc = server->ops->open(xid, & 944 rc = server->ops->open(xid, &oparms, &oplock, NULL); 1259 /* indicate that we need to r 945 /* indicate that we need to relock the file */ 1260 oparms.reconnect = true; 946 oparms.reconnect = true; 1261 } 947 } 1262 if (rc == -EACCES && rdwr_for_fscache 948 if (rc == -EACCES && rdwr_for_fscache == 1) { 1263 desired_access = cifs_convert 949 desired_access = cifs_convert_flags(cfile->f_flags, 0); 1264 rdwr_for_fscache = 2; 950 rdwr_for_fscache = 2; 1265 goto retry_open; 951 goto retry_open; 1266 } 952 } 1267 953 1268 if (rc) { 954 if (rc) { 1269 mutex_unlock(&cfile->fh_mutex 955 mutex_unlock(&cfile->fh_mutex); 1270 cifs_dbg(FYI, "cifs_reopen re 956 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); 1271 cifs_dbg(FYI, "oplock: %d\n", 957 cifs_dbg(FYI, "oplock: %d\n", oplock); 1272 goto reopen_error_exit; 958 goto reopen_error_exit; 1273 } 959 } 1274 960 1275 if (rdwr_for_fscache == 2) 961 if (rdwr_for_fscache == 2) 1276 cifs_invalidate_cache(inode, 962 cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 1277 963 1278 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 964 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1279 reopen_success: 965 reopen_success: 1280 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 966 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1281 cfile->invalidHandle = false; 967 cfile->invalidHandle = false; 1282 mutex_unlock(&cfile->fh_mutex); 968 mutex_unlock(&cfile->fh_mutex); 1283 cinode = CIFS_I(inode); 969 cinode = CIFS_I(inode); 1284 970 1285 if (can_flush) { 971 if (can_flush) { 1286 rc = filemap_write_and_wait(i 972 rc = filemap_write_and_wait(inode->i_mapping); 1287 if (!is_interrupt_error(rc)) 973 if (!is_interrupt_error(rc)) 1288 mapping_set_error(ino 974 mapping_set_error(inode->i_mapping, rc); 1289 975 1290 if (tcon->posix_extensions) { !! 976 if (tcon->posix_extensions) 1291 rc = smb311_posix_get !! 977 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid); 1292 !! 978 else if (tcon->unix_ext) 1293 } else if (tcon->unix_ext) { << 1294 rc = cifs_get_inode_i 979 rc = cifs_get_inode_info_unix(&inode, full_path, 1295 980 inode->i_sb, xid); 1296 } else { !! 981 else 1297 rc = cifs_get_inode_i 982 rc = cifs_get_inode_info(&inode, full_path, NULL, 1298 983 inode->i_sb, xid, NULL); 1299 } << 1300 } 984 } 1301 /* 985 /* 1302 * Else we are writing out data to se 986 * Else we are writing out data to server already and could deadlock if 1303 * we tried to flush data, and since 987 * we tried to flush data, and since we do not know if we have data that 1304 * would invalidate the current end o 988 * would invalidate the current end of file on the server we can not go 1305 * to the server to get the new inode 989 * to the server to get the new inode info. 1306 */ 990 */ 1307 991 1308 /* 992 /* 1309 * If the server returned a read oplo 993 * If the server returned a read oplock and we have mandatory brlocks, 1310 * set oplock level to None. 994 * set oplock level to None. 1311 */ 995 */ 1312 if (server->ops->is_read_op(oplock) & 996 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 1313 cifs_dbg(FYI, "Reset oplock v 997 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 1314 oplock = 0; 998 oplock = 0; 1315 } 999 } 1316 1000 1317 server->ops->set_fid(cfile, &cfile->f 1001 server->ops->set_fid(cfile, &cfile->fid, oplock); 1318 if (oparms.reconnect) 1002 if (oparms.reconnect) 1319 cifs_relock_file(cfile); 1003 cifs_relock_file(cfile); 1320 1004 1321 reopen_error_exit: 1005 reopen_error_exit: 1322 free_dentry_path(page); 1006 free_dentry_path(page); 1323 free_xid(xid); 1007 free_xid(xid); 1324 return rc; 1008 return rc; 1325 } 1009 } 1326 1010 1327 void smb2_deferred_work_close(struct work_str 1011 void smb2_deferred_work_close(struct work_struct *work) 1328 { 1012 { 1329 struct cifsFileInfo *cfile = containe 1013 struct cifsFileInfo *cfile = container_of(work, 1330 struct cifsFileInfo, 1014 struct cifsFileInfo, deferred.work); 1331 1015 1332 spin_lock(&CIFS_I(d_inode(cfile->dent 1016 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 1333 cifs_del_deferred_close(cfile); 1017 cifs_del_deferred_close(cfile); 1334 cfile->deferred_close_scheduled = fal 1018 cfile->deferred_close_scheduled = false; 1335 spin_unlock(&CIFS_I(d_inode(cfile->de 1019 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 1336 _cifsFileInfo_put(cfile, true, false) 1020 _cifsFileInfo_put(cfile, true, false); 1337 } 1021 } 1338 1022 1339 static bool << 1340 smb2_can_defer_close(struct inode *inode, str << 1341 { << 1342 struct cifs_sb_info *cifs_sb = CIFS_S << 1343 struct cifsInodeInfo *cinode = CIFS_I << 1344 << 1345 return (cifs_sb->ctx->closetimeo && c << 1346 (cinode->oplock == CI << 1347 cinode->oplock == CI << 1348 !test_bit(CIFS_INO_CL << 1349 << 1350 } << 1351 << 1352 int cifs_close(struct inode *inode, struct fi 1023 int cifs_close(struct inode *inode, struct file *file) 1353 { 1024 { 1354 struct cifsFileInfo *cfile; 1025 struct cifsFileInfo *cfile; 1355 struct cifsInodeInfo *cinode = CIFS_I 1026 struct cifsInodeInfo *cinode = CIFS_I(inode); 1356 struct cifs_sb_info *cifs_sb = CIFS_S 1027 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 1357 struct cifs_deferred_close *dclose; 1028 struct cifs_deferred_close *dclose; 1358 1029 1359 cifs_fscache_unuse_inode_cookie(inode 1030 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE); 1360 1031 1361 if (file->private_data != NULL) { 1032 if (file->private_data != NULL) { 1362 cfile = file->private_data; 1033 cfile = file->private_data; 1363 file->private_data = NULL; 1034 file->private_data = NULL; 1364 dclose = kmalloc(sizeof(struc 1035 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL); 1365 if ((cfile->status_file_delet !! 1036 if ((cifs_sb->ctx->closetimeo && cinode->oplock == CIFS_CACHE_RHW_FLG) 1366 (smb2_can_defer_close(ino !! 1037 && cinode->lease_granted && 1367 if (test_and_clear_bi !! 1038 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) && 1368 inode_set_mti !! 1039 dclose) { 1369 !! 1040 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) { >> 1041 inode->i_ctime = inode->i_mtime = current_time(inode); 1370 } 1042 } 1371 spin_lock(&cinode->de 1043 spin_lock(&cinode->deferred_lock); 1372 cifs_add_deferred_clo 1044 cifs_add_deferred_close(cfile, dclose); 1373 if (cfile->deferred_c 1045 if (cfile->deferred_close_scheduled && 1374 delayed_work_pend 1046 delayed_work_pending(&cfile->deferred)) { 1375 /* 1047 /* 1376 * If there i 1048 * If there is no pending work, mod_delayed_work queues new work. 1377 * So, Increa 1049 * So, Increase the ref count to avoid use-after-free. 1378 */ 1050 */ 1379 if (!mod_dela 1051 if (!mod_delayed_work(deferredclose_wq, 1380 1052 &cfile->deferred, cifs_sb->ctx->closetimeo)) 1381 cifsF 1053 cifsFileInfo_get(cfile); 1382 } else { 1054 } else { 1383 /* Deferred c 1055 /* Deferred close for files */ 1384 queue_delayed 1056 queue_delayed_work(deferredclose_wq, 1385 1057 &cfile->deferred, cifs_sb->ctx->closetimeo); 1386 cfile->deferr 1058 cfile->deferred_close_scheduled = true; 1387 spin_unlock(& 1059 spin_unlock(&cinode->deferred_lock); 1388 return 0; 1060 return 0; 1389 } 1061 } 1390 spin_unlock(&cinode-> 1062 spin_unlock(&cinode->deferred_lock); 1391 _cifsFileInfo_put(cfi 1063 _cifsFileInfo_put(cfile, true, false); 1392 } else { 1064 } else { 1393 _cifsFileInfo_put(cfi 1065 _cifsFileInfo_put(cfile, true, false); 1394 kfree(dclose); 1066 kfree(dclose); 1395 } 1067 } 1396 } 1068 } 1397 1069 1398 /* return code from the ->release op 1070 /* return code from the ->release op is always ignored */ 1399 return 0; 1071 return 0; 1400 } 1072 } 1401 1073 1402 void 1074 void 1403 cifs_reopen_persistent_handles(struct cifs_tc 1075 cifs_reopen_persistent_handles(struct cifs_tcon *tcon) 1404 { 1076 { 1405 struct cifsFileInfo *open_file, *tmp; 1077 struct cifsFileInfo *open_file, *tmp; 1406 LIST_HEAD(tmp_list); !! 1078 struct list_head tmp_list; 1407 1079 1408 if (!tcon->use_persistent || !tcon->n 1080 if (!tcon->use_persistent || !tcon->need_reopen_files) 1409 return; 1081 return; 1410 1082 1411 tcon->need_reopen_files = false; 1083 tcon->need_reopen_files = false; 1412 1084 1413 cifs_dbg(FYI, "Reopen persistent hand 1085 cifs_dbg(FYI, "Reopen persistent handles\n"); >> 1086 INIT_LIST_HEAD(&tmp_list); 1414 1087 1415 /* list all files open on tree connec 1088 /* list all files open on tree connection, reopen resilient handles */ 1416 spin_lock(&tcon->open_file_lock); 1089 spin_lock(&tcon->open_file_lock); 1417 list_for_each_entry(open_file, &tcon- 1090 list_for_each_entry(open_file, &tcon->openFileList, tlist) { 1418 if (!open_file->invalidHandle 1091 if (!open_file->invalidHandle) 1419 continue; 1092 continue; 1420 cifsFileInfo_get(open_file); 1093 cifsFileInfo_get(open_file); 1421 list_add_tail(&open_file->rli 1094 list_add_tail(&open_file->rlist, &tmp_list); 1422 } 1095 } 1423 spin_unlock(&tcon->open_file_lock); 1096 spin_unlock(&tcon->open_file_lock); 1424 1097 1425 list_for_each_entry_safe(open_file, t 1098 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) { 1426 if (cifs_reopen_file(open_fil 1099 if (cifs_reopen_file(open_file, false /* do not flush */)) 1427 tcon->need_reopen_fil 1100 tcon->need_reopen_files = true; 1428 list_del_init(&open_file->rli 1101 list_del_init(&open_file->rlist); 1429 cifsFileInfo_put(open_file); 1102 cifsFileInfo_put(open_file); 1430 } 1103 } 1431 } 1104 } 1432 1105 1433 int cifs_closedir(struct inode *inode, struct 1106 int cifs_closedir(struct inode *inode, struct file *file) 1434 { 1107 { 1435 int rc = 0; 1108 int rc = 0; 1436 unsigned int xid; 1109 unsigned int xid; 1437 struct cifsFileInfo *cfile = file->pr 1110 struct cifsFileInfo *cfile = file->private_data; 1438 struct cifs_tcon *tcon; 1111 struct cifs_tcon *tcon; 1439 struct TCP_Server_Info *server; 1112 struct TCP_Server_Info *server; 1440 char *buf; 1113 char *buf; 1441 1114 1442 cifs_dbg(FYI, "Closedir inode = 0x%p\ 1115 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); 1443 1116 1444 if (cfile == NULL) 1117 if (cfile == NULL) 1445 return rc; 1118 return rc; 1446 1119 1447 xid = get_xid(); 1120 xid = get_xid(); 1448 tcon = tlink_tcon(cfile->tlink); 1121 tcon = tlink_tcon(cfile->tlink); 1449 server = tcon->ses->server; 1122 server = tcon->ses->server; 1450 1123 1451 cifs_dbg(FYI, "Freeing private data i 1124 cifs_dbg(FYI, "Freeing private data in close dir\n"); 1452 spin_lock(&cfile->file_info_lock); 1125 spin_lock(&cfile->file_info_lock); 1453 if (server->ops->dir_needs_close(cfil 1126 if (server->ops->dir_needs_close(cfile)) { 1454 cfile->invalidHandle = true; 1127 cfile->invalidHandle = true; 1455 spin_unlock(&cfile->file_info 1128 spin_unlock(&cfile->file_info_lock); 1456 if (server->ops->close_dir) 1129 if (server->ops->close_dir) 1457 rc = server->ops->clo 1130 rc = server->ops->close_dir(xid, tcon, &cfile->fid); 1458 else 1131 else 1459 rc = -ENOSYS; 1132 rc = -ENOSYS; 1460 cifs_dbg(FYI, "Closing uncomp 1133 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); 1461 /* not much we can do if it f 1134 /* not much we can do if it fails anyway, ignore rc */ 1462 rc = 0; 1135 rc = 0; 1463 } else 1136 } else 1464 spin_unlock(&cfile->file_info 1137 spin_unlock(&cfile->file_info_lock); 1465 1138 1466 buf = cfile->srch_inf.ntwrk_buf_start 1139 buf = cfile->srch_inf.ntwrk_buf_start; 1467 if (buf) { 1140 if (buf) { 1468 cifs_dbg(FYI, "closedir free 1141 cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); 1469 cfile->srch_inf.ntwrk_buf_sta 1142 cfile->srch_inf.ntwrk_buf_start = NULL; 1470 if (cfile->srch_inf.smallBuf) 1143 if (cfile->srch_inf.smallBuf) 1471 cifs_small_buf_releas 1144 cifs_small_buf_release(buf); 1472 else 1145 else 1473 cifs_buf_release(buf) 1146 cifs_buf_release(buf); 1474 } 1147 } 1475 1148 1476 cifs_put_tlink(cfile->tlink); 1149 cifs_put_tlink(cfile->tlink); 1477 kfree(file->private_data); 1150 kfree(file->private_data); 1478 file->private_data = NULL; 1151 file->private_data = NULL; 1479 /* BB can we lock the filestruct whil 1152 /* BB can we lock the filestruct while this is going on? */ 1480 free_xid(xid); 1153 free_xid(xid); 1481 return rc; 1154 return rc; 1482 } 1155 } 1483 1156 1484 static struct cifsLockInfo * 1157 static struct cifsLockInfo * 1485 cifs_lock_init(__u64 offset, __u64 length, __ 1158 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags) 1486 { 1159 { 1487 struct cifsLockInfo *lock = 1160 struct cifsLockInfo *lock = 1488 kmalloc(sizeof(struct cifsLoc 1161 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); 1489 if (!lock) 1162 if (!lock) 1490 return lock; 1163 return lock; 1491 lock->offset = offset; 1164 lock->offset = offset; 1492 lock->length = length; 1165 lock->length = length; 1493 lock->type = type; 1166 lock->type = type; 1494 lock->pid = current->tgid; 1167 lock->pid = current->tgid; 1495 lock->flags = flags; 1168 lock->flags = flags; 1496 INIT_LIST_HEAD(&lock->blist); 1169 INIT_LIST_HEAD(&lock->blist); 1497 init_waitqueue_head(&lock->block_q); 1170 init_waitqueue_head(&lock->block_q); 1498 return lock; 1171 return lock; 1499 } 1172 } 1500 1173 1501 void 1174 void 1502 cifs_del_lock_waiters(struct cifsLockInfo *lo 1175 cifs_del_lock_waiters(struct cifsLockInfo *lock) 1503 { 1176 { 1504 struct cifsLockInfo *li, *tmp; 1177 struct cifsLockInfo *li, *tmp; 1505 list_for_each_entry_safe(li, tmp, &lo 1178 list_for_each_entry_safe(li, tmp, &lock->blist, blist) { 1506 list_del_init(&li->blist); 1179 list_del_init(&li->blist); 1507 wake_up(&li->block_q); 1180 wake_up(&li->block_q); 1508 } 1181 } 1509 } 1182 } 1510 1183 1511 #define CIFS_LOCK_OP 0 1184 #define CIFS_LOCK_OP 0 1512 #define CIFS_READ_OP 1 1185 #define CIFS_READ_OP 1 1513 #define CIFS_WRITE_OP 2 1186 #define CIFS_WRITE_OP 2 1514 1187 1515 /* @rw_check : 0 - no op, 1 - read, 2 - write 1188 /* @rw_check : 0 - no op, 1 - read, 2 - write */ 1516 static bool 1189 static bool 1517 cifs_find_fid_lock_conflict(struct cifs_fid_l 1190 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, 1518 __u64 length, __u 1191 __u64 length, __u8 type, __u16 flags, 1519 struct cifsFileIn 1192 struct cifsFileInfo *cfile, 1520 struct cifsLockIn 1193 struct cifsLockInfo **conf_lock, int rw_check) 1521 { 1194 { 1522 struct cifsLockInfo *li; 1195 struct cifsLockInfo *li; 1523 struct cifsFileInfo *cur_cfile = fdlo 1196 struct cifsFileInfo *cur_cfile = fdlocks->cfile; 1524 struct TCP_Server_Info *server = tlin 1197 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 1525 1198 1526 list_for_each_entry(li, &fdlocks->loc 1199 list_for_each_entry(li, &fdlocks->locks, llist) { 1527 if (offset + length <= li->of 1200 if (offset + length <= li->offset || 1528 offset >= li->offset + li 1201 offset >= li->offset + li->length) 1529 continue; 1202 continue; 1530 if (rw_check != CIFS_LOCK_OP 1203 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && 1531 server->ops->compare_fids 1204 server->ops->compare_fids(cfile, cur_cfile)) { 1532 /* shared lock preven 1205 /* shared lock prevents write op through the same fid */ 1533 if (!(li->type & serv 1206 if (!(li->type & server->vals->shared_lock_type) || 1534 rw_check != CIFS_ 1207 rw_check != CIFS_WRITE_OP) 1535 continue; 1208 continue; 1536 } 1209 } 1537 if ((type & server->vals->sha 1210 if ((type & server->vals->shared_lock_type) && 1538 ((server->ops->compare_fi 1211 ((server->ops->compare_fids(cfile, cur_cfile) && 1539 current->tgid == li->pid 1212 current->tgid == li->pid) || type == li->type)) 1540 continue; 1213 continue; 1541 if (rw_check == CIFS_LOCK_OP 1214 if (rw_check == CIFS_LOCK_OP && 1542 (flags & FL_OFDLCK) && (l 1215 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) && 1543 server->ops->compare_fids 1216 server->ops->compare_fids(cfile, cur_cfile)) 1544 continue; 1217 continue; 1545 if (conf_lock) 1218 if (conf_lock) 1546 *conf_lock = li; 1219 *conf_lock = li; 1547 return true; 1220 return true; 1548 } 1221 } 1549 return false; 1222 return false; 1550 } 1223 } 1551 1224 1552 bool 1225 bool 1553 cifs_find_lock_conflict(struct cifsFileInfo * 1226 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 1554 __u8 type, __u16 flag 1227 __u8 type, __u16 flags, 1555 struct cifsLockInfo * 1228 struct cifsLockInfo **conf_lock, int rw_check) 1556 { 1229 { 1557 bool rc = false; 1230 bool rc = false; 1558 struct cifs_fid_locks *cur; 1231 struct cifs_fid_locks *cur; 1559 struct cifsInodeInfo *cinode = CIFS_I 1232 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1560 1233 1561 list_for_each_entry(cur, &cinode->lli 1234 list_for_each_entry(cur, &cinode->llist, llist) { 1562 rc = cifs_find_fid_lock_confl 1235 rc = cifs_find_fid_lock_conflict(cur, offset, length, type, 1563 1236 flags, cfile, conf_lock, 1564 1237 rw_check); 1565 if (rc) 1238 if (rc) 1566 break; 1239 break; 1567 } 1240 } 1568 1241 1569 return rc; 1242 return rc; 1570 } 1243 } 1571 1244 1572 /* 1245 /* 1573 * Check if there is another lock that preven 1246 * Check if there is another lock that prevents us to set the lock (mandatory 1574 * style). If such a lock exists, update the 1247 * style). If such a lock exists, update the flock structure with its 1575 * properties. Otherwise, set the flock type 1248 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 1576 * or leave it the same if we can't. Returns 1249 * or leave it the same if we can't. Returns 0 if we don't need to request to 1577 * the server or 1 otherwise. 1250 * the server or 1 otherwise. 1578 */ 1251 */ 1579 static int 1252 static int 1580 cifs_lock_test(struct cifsFileInfo *cfile, __ 1253 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 1581 __u8 type, struct file_lock *f 1254 __u8 type, struct file_lock *flock) 1582 { 1255 { 1583 int rc = 0; 1256 int rc = 0; 1584 struct cifsLockInfo *conf_lock; 1257 struct cifsLockInfo *conf_lock; 1585 struct cifsInodeInfo *cinode = CIFS_I 1258 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1586 struct TCP_Server_Info *server = tlin 1259 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 1587 bool exist; 1260 bool exist; 1588 1261 1589 down_read(&cinode->lock_sem); 1262 down_read(&cinode->lock_sem); 1590 1263 1591 exist = cifs_find_lock_conflict(cfile 1264 exist = cifs_find_lock_conflict(cfile, offset, length, type, 1592 flock !! 1265 flock->fl_flags, &conf_lock, 1593 CIFS_ 1266 CIFS_LOCK_OP); 1594 if (exist) { 1267 if (exist) { 1595 flock->fl_start = conf_lock-> 1268 flock->fl_start = conf_lock->offset; 1596 flock->fl_end = conf_lock->of 1269 flock->fl_end = conf_lock->offset + conf_lock->length - 1; 1597 flock->c.flc_pid = conf_lock- !! 1270 flock->fl_pid = conf_lock->pid; 1598 if (conf_lock->type & server- 1271 if (conf_lock->type & server->vals->shared_lock_type) 1599 flock->c.flc_type = F !! 1272 flock->fl_type = F_RDLCK; 1600 else 1273 else 1601 flock->c.flc_type = F !! 1274 flock->fl_type = F_WRLCK; 1602 } else if (!cinode->can_cache_brlcks) 1275 } else if (!cinode->can_cache_brlcks) 1603 rc = 1; 1276 rc = 1; 1604 else 1277 else 1605 flock->c.flc_type = F_UNLCK; !! 1278 flock->fl_type = F_UNLCK; 1606 1279 1607 up_read(&cinode->lock_sem); 1280 up_read(&cinode->lock_sem); 1608 return rc; 1281 return rc; 1609 } 1282 } 1610 1283 1611 static void 1284 static void 1612 cifs_lock_add(struct cifsFileInfo *cfile, str 1285 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) 1613 { 1286 { 1614 struct cifsInodeInfo *cinode = CIFS_I 1287 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1615 cifs_down_write(&cinode->lock_sem); 1288 cifs_down_write(&cinode->lock_sem); 1616 list_add_tail(&lock->llist, &cfile->l 1289 list_add_tail(&lock->llist, &cfile->llist->locks); 1617 up_write(&cinode->lock_sem); 1290 up_write(&cinode->lock_sem); 1618 } 1291 } 1619 1292 1620 /* 1293 /* 1621 * Set the byte-range lock (mandatory style). 1294 * Set the byte-range lock (mandatory style). Returns: 1622 * 1) 0, if we set the lock and don't need to 1295 * 1) 0, if we set the lock and don't need to request to the server; 1623 * 2) 1, if no locks prevent us but we need t 1296 * 2) 1, if no locks prevent us but we need to request to the server; 1624 * 3) -EACCES, if there is a lock that preven 1297 * 3) -EACCES, if there is a lock that prevents us and wait is false. 1625 */ 1298 */ 1626 static int 1299 static int 1627 cifs_lock_add_if(struct cifsFileInfo *cfile, 1300 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, 1628 bool wait) 1301 bool wait) 1629 { 1302 { 1630 struct cifsLockInfo *conf_lock; 1303 struct cifsLockInfo *conf_lock; 1631 struct cifsInodeInfo *cinode = CIFS_I 1304 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1632 bool exist; 1305 bool exist; 1633 int rc = 0; 1306 int rc = 0; 1634 1307 1635 try_again: 1308 try_again: 1636 exist = false; 1309 exist = false; 1637 cifs_down_write(&cinode->lock_sem); 1310 cifs_down_write(&cinode->lock_sem); 1638 1311 1639 exist = cifs_find_lock_conflict(cfile 1312 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, 1640 lock- 1313 lock->type, lock->flags, &conf_lock, 1641 CIFS_ 1314 CIFS_LOCK_OP); 1642 if (!exist && cinode->can_cache_brlck 1315 if (!exist && cinode->can_cache_brlcks) { 1643 list_add_tail(&lock->llist, & 1316 list_add_tail(&lock->llist, &cfile->llist->locks); 1644 up_write(&cinode->lock_sem); 1317 up_write(&cinode->lock_sem); 1645 return rc; 1318 return rc; 1646 } 1319 } 1647 1320 1648 if (!exist) 1321 if (!exist) 1649 rc = 1; 1322 rc = 1; 1650 else if (!wait) 1323 else if (!wait) 1651 rc = -EACCES; 1324 rc = -EACCES; 1652 else { 1325 else { 1653 list_add_tail(&lock->blist, & 1326 list_add_tail(&lock->blist, &conf_lock->blist); 1654 up_write(&cinode->lock_sem); 1327 up_write(&cinode->lock_sem); 1655 rc = wait_event_interruptible 1328 rc = wait_event_interruptible(lock->block_q, 1656 (lock 1329 (lock->blist.prev == &lock->blist) && 1657 (lock 1330 (lock->blist.next == &lock->blist)); 1658 if (!rc) 1331 if (!rc) 1659 goto try_again; 1332 goto try_again; 1660 cifs_down_write(&cinode->lock 1333 cifs_down_write(&cinode->lock_sem); 1661 list_del_init(&lock->blist); 1334 list_del_init(&lock->blist); 1662 } 1335 } 1663 1336 1664 up_write(&cinode->lock_sem); 1337 up_write(&cinode->lock_sem); 1665 return rc; 1338 return rc; 1666 } 1339 } 1667 1340 1668 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1341 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1669 /* 1342 /* 1670 * Check if there is another lock that preven 1343 * Check if there is another lock that prevents us to set the lock (posix 1671 * style). If such a lock exists, update the 1344 * style). If such a lock exists, update the flock structure with its 1672 * properties. Otherwise, set the flock type 1345 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 1673 * or leave it the same if we can't. Returns 1346 * or leave it the same if we can't. Returns 0 if we don't need to request to 1674 * the server or 1 otherwise. 1347 * the server or 1 otherwise. 1675 */ 1348 */ 1676 static int 1349 static int 1677 cifs_posix_lock_test(struct file *file, struc 1350 cifs_posix_lock_test(struct file *file, struct file_lock *flock) 1678 { 1351 { 1679 int rc = 0; 1352 int rc = 0; 1680 struct cifsInodeInfo *cinode = CIFS_I 1353 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 1681 unsigned char saved_type = flock->c.f !! 1354 unsigned char saved_type = flock->fl_type; 1682 1355 1683 if ((flock->c.flc_flags & FL_POSIX) = !! 1356 if ((flock->fl_flags & FL_POSIX) == 0) 1684 return 1; 1357 return 1; 1685 1358 1686 down_read(&cinode->lock_sem); 1359 down_read(&cinode->lock_sem); 1687 posix_test_lock(file, flock); 1360 posix_test_lock(file, flock); 1688 1361 1689 if (lock_is_unlock(flock) && !cinode- !! 1362 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) { 1690 flock->c.flc_type = saved_typ !! 1363 flock->fl_type = saved_type; 1691 rc = 1; 1364 rc = 1; 1692 } 1365 } 1693 1366 1694 up_read(&cinode->lock_sem); 1367 up_read(&cinode->lock_sem); 1695 return rc; 1368 return rc; 1696 } 1369 } 1697 1370 1698 /* 1371 /* 1699 * Set the byte-range lock (posix style). Ret 1372 * Set the byte-range lock (posix style). Returns: 1700 * 1) <0, if the error occurs while setting t 1373 * 1) <0, if the error occurs while setting the lock; 1701 * 2) 0, if we set the lock and don't need to 1374 * 2) 0, if we set the lock and don't need to request to the server; 1702 * 3) FILE_LOCK_DEFERRED, if we will wait for 1375 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock; 1703 * 4) FILE_LOCK_DEFERRED + 1, if we need to r 1376 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server. 1704 */ 1377 */ 1705 static int 1378 static int 1706 cifs_posix_lock_set(struct file *file, struct 1379 cifs_posix_lock_set(struct file *file, struct file_lock *flock) 1707 { 1380 { 1708 struct cifsInodeInfo *cinode = CIFS_I 1381 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 1709 int rc = FILE_LOCK_DEFERRED + 1; 1382 int rc = FILE_LOCK_DEFERRED + 1; 1710 1383 1711 if ((flock->c.flc_flags & FL_POSIX) = !! 1384 if ((flock->fl_flags & FL_POSIX) == 0) 1712 return rc; 1385 return rc; 1713 1386 1714 cifs_down_write(&cinode->lock_sem); 1387 cifs_down_write(&cinode->lock_sem); 1715 if (!cinode->can_cache_brlcks) { 1388 if (!cinode->can_cache_brlcks) { 1716 up_write(&cinode->lock_sem); 1389 up_write(&cinode->lock_sem); 1717 return rc; 1390 return rc; 1718 } 1391 } 1719 1392 1720 rc = posix_lock_file(file, flock, NUL 1393 rc = posix_lock_file(file, flock, NULL); 1721 up_write(&cinode->lock_sem); 1394 up_write(&cinode->lock_sem); 1722 return rc; 1395 return rc; 1723 } 1396 } 1724 1397 1725 int 1398 int 1726 cifs_push_mandatory_locks(struct cifsFileInfo 1399 cifs_push_mandatory_locks(struct cifsFileInfo *cfile) 1727 { 1400 { 1728 unsigned int xid; 1401 unsigned int xid; 1729 int rc = 0, stored_rc; 1402 int rc = 0, stored_rc; 1730 struct cifsLockInfo *li, *tmp; 1403 struct cifsLockInfo *li, *tmp; 1731 struct cifs_tcon *tcon; 1404 struct cifs_tcon *tcon; 1732 unsigned int num, max_num, max_buf; 1405 unsigned int num, max_num, max_buf; 1733 LOCKING_ANDX_RANGE *buf, *cur; 1406 LOCKING_ANDX_RANGE *buf, *cur; 1734 static const int types[] = { 1407 static const int types[] = { 1735 LOCKING_ANDX_LARGE_FILES, 1408 LOCKING_ANDX_LARGE_FILES, 1736 LOCKING_ANDX_SHARED_LOCK | LO 1409 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 1737 }; 1410 }; 1738 int i; 1411 int i; 1739 1412 1740 xid = get_xid(); 1413 xid = get_xid(); 1741 tcon = tlink_tcon(cfile->tlink); 1414 tcon = tlink_tcon(cfile->tlink); 1742 1415 1743 /* 1416 /* 1744 * Accessing maxBuf is racy with cifs 1417 * Accessing maxBuf is racy with cifs_reconnect - need to store value 1745 * and check it before using. 1418 * and check it before using. 1746 */ 1419 */ 1747 max_buf = tcon->ses->server->maxBuf; 1420 max_buf = tcon->ses->server->maxBuf; 1748 if (max_buf < (sizeof(struct smb_hdr) 1421 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { 1749 free_xid(xid); 1422 free_xid(xid); 1750 return -EINVAL; 1423 return -EINVAL; 1751 } 1424 } 1752 1425 1753 BUILD_BUG_ON(sizeof(struct smb_hdr) + 1426 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 1754 PAGE_SIZE); 1427 PAGE_SIZE); 1755 max_buf = min_t(unsigned int, max_buf 1428 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 1756 PAGE_SIZE); 1429 PAGE_SIZE); 1757 max_num = (max_buf - sizeof(struct sm 1430 max_num = (max_buf - sizeof(struct smb_hdr)) / 1758 1431 sizeof(LOCKING_ANDX_RANGE); 1759 buf = kcalloc(max_num, sizeof(LOCKING 1432 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); 1760 if (!buf) { 1433 if (!buf) { 1761 free_xid(xid); 1434 free_xid(xid); 1762 return -ENOMEM; 1435 return -ENOMEM; 1763 } 1436 } 1764 1437 1765 for (i = 0; i < 2; i++) { 1438 for (i = 0; i < 2; i++) { 1766 cur = buf; 1439 cur = buf; 1767 num = 0; 1440 num = 0; 1768 list_for_each_entry_safe(li, 1441 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 1769 if (li->type != types 1442 if (li->type != types[i]) 1770 continue; 1443 continue; 1771 cur->Pid = cpu_to_le1 1444 cur->Pid = cpu_to_le16(li->pid); 1772 cur->LengthLow = cpu_ 1445 cur->LengthLow = cpu_to_le32((u32)li->length); 1773 cur->LengthHigh = cpu 1446 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 1774 cur->OffsetLow = cpu_ 1447 cur->OffsetLow = cpu_to_le32((u32)li->offset); 1775 cur->OffsetHigh = cpu 1448 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 1776 if (++num == max_num) 1449 if (++num == max_num) { 1777 stored_rc = c 1450 stored_rc = cifs_lockv(xid, tcon, 1778 1451 cfile->fid.netfid, 1779 1452 (__u8)li->type, 0, num, 1780 1453 buf); 1781 if (stored_rc 1454 if (stored_rc) 1782 rc = 1455 rc = stored_rc; 1783 cur = buf; 1456 cur = buf; 1784 num = 0; 1457 num = 0; 1785 } else 1458 } else 1786 cur++; 1459 cur++; 1787 } 1460 } 1788 1461 1789 if (num) { 1462 if (num) { 1790 stored_rc = cifs_lock 1463 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 1791 1464 (__u8)types[i], 0, num, buf); 1792 if (stored_rc) 1465 if (stored_rc) 1793 rc = stored_r 1466 rc = stored_rc; 1794 } 1467 } 1795 } 1468 } 1796 1469 1797 kfree(buf); 1470 kfree(buf); 1798 free_xid(xid); 1471 free_xid(xid); 1799 return rc; 1472 return rc; 1800 } 1473 } 1801 1474 1802 static __u32 1475 static __u32 1803 hash_lockowner(fl_owner_t owner) 1476 hash_lockowner(fl_owner_t owner) 1804 { 1477 { 1805 return cifs_lock_secret ^ hash32_ptr( 1478 return cifs_lock_secret ^ hash32_ptr((const void *)owner); 1806 } 1479 } 1807 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1480 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1808 1481 1809 struct lock_to_push { 1482 struct lock_to_push { 1810 struct list_head llist; 1483 struct list_head llist; 1811 __u64 offset; 1484 __u64 offset; 1812 __u64 length; 1485 __u64 length; 1813 __u32 pid; 1486 __u32 pid; 1814 __u16 netfid; 1487 __u16 netfid; 1815 __u8 type; 1488 __u8 type; 1816 }; 1489 }; 1817 1490 1818 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1491 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1819 static int 1492 static int 1820 cifs_push_posix_locks(struct cifsFileInfo *cf 1493 cifs_push_posix_locks(struct cifsFileInfo *cfile) 1821 { 1494 { 1822 struct inode *inode = d_inode(cfile-> 1495 struct inode *inode = d_inode(cfile->dentry); 1823 struct cifs_tcon *tcon = tlink_tcon(c 1496 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1824 struct file_lock *flock; 1497 struct file_lock *flock; 1825 struct file_lock_context *flctx = loc !! 1498 struct file_lock_context *flctx = inode->i_flctx; 1826 unsigned int count = 0, i; 1499 unsigned int count = 0, i; 1827 int rc = 0, xid, type; 1500 int rc = 0, xid, type; 1828 struct list_head locks_to_send, *el; 1501 struct list_head locks_to_send, *el; 1829 struct lock_to_push *lck, *tmp; 1502 struct lock_to_push *lck, *tmp; 1830 __u64 length; 1503 __u64 length; 1831 1504 1832 xid = get_xid(); 1505 xid = get_xid(); 1833 1506 1834 if (!flctx) 1507 if (!flctx) 1835 goto out; 1508 goto out; 1836 1509 1837 spin_lock(&flctx->flc_lock); 1510 spin_lock(&flctx->flc_lock); 1838 list_for_each(el, &flctx->flc_posix) 1511 list_for_each(el, &flctx->flc_posix) { 1839 count++; 1512 count++; 1840 } 1513 } 1841 spin_unlock(&flctx->flc_lock); 1514 spin_unlock(&flctx->flc_lock); 1842 1515 1843 INIT_LIST_HEAD(&locks_to_send); 1516 INIT_LIST_HEAD(&locks_to_send); 1844 1517 1845 /* 1518 /* 1846 * Allocating count locks is enough b 1519 * Allocating count locks is enough because no FL_POSIX locks can be 1847 * added to the list while we are hol 1520 * added to the list while we are holding cinode->lock_sem that 1848 * protects locking operations of thi 1521 * protects locking operations of this inode. 1849 */ 1522 */ 1850 for (i = 0; i < count; i++) { 1523 for (i = 0; i < count; i++) { 1851 lck = kmalloc(sizeof(struct l 1524 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); 1852 if (!lck) { 1525 if (!lck) { 1853 rc = -ENOMEM; 1526 rc = -ENOMEM; 1854 goto err_out; 1527 goto err_out; 1855 } 1528 } 1856 list_add_tail(&lck->llist, &l 1529 list_add_tail(&lck->llist, &locks_to_send); 1857 } 1530 } 1858 1531 1859 el = locks_to_send.next; 1532 el = locks_to_send.next; 1860 spin_lock(&flctx->flc_lock); 1533 spin_lock(&flctx->flc_lock); 1861 for_each_file_lock(flock, &flctx->flc !! 1534 list_for_each_entry(flock, &flctx->flc_posix, fl_list) { 1862 unsigned char ftype = flock-> << 1863 << 1864 if (el == &locks_to_send) { 1535 if (el == &locks_to_send) { 1865 /* 1536 /* 1866 * The list ended. We 1537 * The list ended. We don't have enough allocated 1867 * structures - somet 1538 * structures - something is really wrong. 1868 */ 1539 */ 1869 cifs_dbg(VFS, "Can't 1540 cifs_dbg(VFS, "Can't push all brlocks!\n"); 1870 break; 1541 break; 1871 } 1542 } 1872 length = cifs_flock_len(flock 1543 length = cifs_flock_len(flock); 1873 if (ftype == F_RDLCK || ftype !! 1544 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK) 1874 type = CIFS_RDLCK; 1545 type = CIFS_RDLCK; 1875 else 1546 else 1876 type = CIFS_WRLCK; 1547 type = CIFS_WRLCK; 1877 lck = list_entry(el, struct l 1548 lck = list_entry(el, struct lock_to_push, llist); 1878 lck->pid = hash_lockowner(flo !! 1549 lck->pid = hash_lockowner(flock->fl_owner); 1879 lck->netfid = cfile->fid.netf 1550 lck->netfid = cfile->fid.netfid; 1880 lck->length = length; 1551 lck->length = length; 1881 lck->type = type; 1552 lck->type = type; 1882 lck->offset = flock->fl_start 1553 lck->offset = flock->fl_start; 1883 } 1554 } 1884 spin_unlock(&flctx->flc_lock); 1555 spin_unlock(&flctx->flc_lock); 1885 1556 1886 list_for_each_entry_safe(lck, tmp, &l 1557 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 1887 int stored_rc; 1558 int stored_rc; 1888 1559 1889 stored_rc = CIFSSMBPosixLock( 1560 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, 1890 1561 lck->offset, lck->length, NULL, 1891 1562 lck->type, 0); 1892 if (stored_rc) 1563 if (stored_rc) 1893 rc = stored_rc; 1564 rc = stored_rc; 1894 list_del(&lck->llist); 1565 list_del(&lck->llist); 1895 kfree(lck); 1566 kfree(lck); 1896 } 1567 } 1897 1568 1898 out: 1569 out: 1899 free_xid(xid); 1570 free_xid(xid); 1900 return rc; 1571 return rc; 1901 err_out: 1572 err_out: 1902 list_for_each_entry_safe(lck, tmp, &l 1573 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 1903 list_del(&lck->llist); 1574 list_del(&lck->llist); 1904 kfree(lck); 1575 kfree(lck); 1905 } 1576 } 1906 goto out; 1577 goto out; 1907 } 1578 } 1908 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1579 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1909 1580 1910 static int 1581 static int 1911 cifs_push_locks(struct cifsFileInfo *cfile) 1582 cifs_push_locks(struct cifsFileInfo *cfile) 1912 { 1583 { 1913 struct cifsInodeInfo *cinode = CIFS_I 1584 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 1914 struct cifs_tcon *tcon = tlink_tcon(c 1585 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1915 int rc = 0; 1586 int rc = 0; 1916 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1587 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1917 struct cifs_sb_info *cifs_sb = CIFS_S 1588 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 1918 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1589 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1919 1590 1920 /* we are going to update can_cache_b 1591 /* we are going to update can_cache_brlcks here - need a write access */ 1921 cifs_down_write(&cinode->lock_sem); 1592 cifs_down_write(&cinode->lock_sem); 1922 if (!cinode->can_cache_brlcks) { 1593 if (!cinode->can_cache_brlcks) { 1923 up_write(&cinode->lock_sem); 1594 up_write(&cinode->lock_sem); 1924 return rc; 1595 return rc; 1925 } 1596 } 1926 1597 1927 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1598 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1928 if (cap_unix(tcon->ses) && 1599 if (cap_unix(tcon->ses) && 1929 (CIFS_UNIX_FCNTL_CAP & le64_to_cp 1600 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 1930 ((cifs_sb->mnt_cifs_flags & CIFS_ 1601 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 1931 rc = cifs_push_posix_locks(cf 1602 rc = cifs_push_posix_locks(cfile); 1932 else 1603 else 1933 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1604 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 1934 rc = tcon->ses->server->ops-> 1605 rc = tcon->ses->server->ops->push_mand_locks(cfile); 1935 1606 1936 cinode->can_cache_brlcks = false; 1607 cinode->can_cache_brlcks = false; 1937 up_write(&cinode->lock_sem); 1608 up_write(&cinode->lock_sem); 1938 return rc; 1609 return rc; 1939 } 1610 } 1940 1611 1941 static void 1612 static void 1942 cifs_read_flock(struct file_lock *flock, __u3 1613 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, 1943 bool *wait_flag, struct TCP_S 1614 bool *wait_flag, struct TCP_Server_Info *server) 1944 { 1615 { 1945 if (flock->c.flc_flags & FL_POSIX) !! 1616 if (flock->fl_flags & FL_POSIX) 1946 cifs_dbg(FYI, "Posix\n"); 1617 cifs_dbg(FYI, "Posix\n"); 1947 if (flock->c.flc_flags & FL_FLOCK) !! 1618 if (flock->fl_flags & FL_FLOCK) 1948 cifs_dbg(FYI, "Flock\n"); 1619 cifs_dbg(FYI, "Flock\n"); 1949 if (flock->c.flc_flags & FL_SLEEP) { !! 1620 if (flock->fl_flags & FL_SLEEP) { 1950 cifs_dbg(FYI, "Blocking lock\ 1621 cifs_dbg(FYI, "Blocking lock\n"); 1951 *wait_flag = true; 1622 *wait_flag = true; 1952 } 1623 } 1953 if (flock->c.flc_flags & FL_ACCESS) !! 1624 if (flock->fl_flags & FL_ACCESS) 1954 cifs_dbg(FYI, "Process suspen 1625 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); 1955 if (flock->c.flc_flags & FL_LEASE) !! 1626 if (flock->fl_flags & FL_LEASE) 1956 cifs_dbg(FYI, "Lease on file 1627 cifs_dbg(FYI, "Lease on file - not implemented yet\n"); 1957 if (flock->c.flc_flags & !! 1628 if (flock->fl_flags & 1958 (~(FL_POSIX | FL_FLOCK | FL_SLEEP 1629 (~(FL_POSIX | FL_FLOCK | FL_SLEEP | 1959 FL_ACCESS | FL_LEASE | FL_CLOS 1630 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK))) 1960 cifs_dbg(FYI, "Unknown lock f !! 1631 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags); 1961 flock->c.flc_flags); << 1962 1632 1963 *type = server->vals->large_lock_type 1633 *type = server->vals->large_lock_type; 1964 if (lock_is_write(flock)) { !! 1634 if (flock->fl_type == F_WRLCK) { 1965 cifs_dbg(FYI, "F_WRLCK\n"); 1635 cifs_dbg(FYI, "F_WRLCK\n"); 1966 *type |= server->vals->exclus 1636 *type |= server->vals->exclusive_lock_type; 1967 *lock = 1; 1637 *lock = 1; 1968 } else if (lock_is_unlock(flock)) { !! 1638 } else if (flock->fl_type == F_UNLCK) { 1969 cifs_dbg(FYI, "F_UNLCK\n"); 1639 cifs_dbg(FYI, "F_UNLCK\n"); 1970 *type |= server->vals->unlock 1640 *type |= server->vals->unlock_lock_type; 1971 *unlock = 1; 1641 *unlock = 1; 1972 /* Check if unlock includes m 1642 /* Check if unlock includes more than one lock range */ 1973 } else if (lock_is_read(flock)) { !! 1643 } else if (flock->fl_type == F_RDLCK) { 1974 cifs_dbg(FYI, "F_RDLCK\n"); 1644 cifs_dbg(FYI, "F_RDLCK\n"); 1975 *type |= server->vals->shared 1645 *type |= server->vals->shared_lock_type; 1976 *lock = 1; 1646 *lock = 1; 1977 } else if (flock->c.flc_type == F_EXL !! 1647 } else if (flock->fl_type == F_EXLCK) { 1978 cifs_dbg(FYI, "F_EXLCK\n"); 1648 cifs_dbg(FYI, "F_EXLCK\n"); 1979 *type |= server->vals->exclus 1649 *type |= server->vals->exclusive_lock_type; 1980 *lock = 1; 1650 *lock = 1; 1981 } else if (flock->c.flc_type == F_SHL !! 1651 } else if (flock->fl_type == F_SHLCK) { 1982 cifs_dbg(FYI, "F_SHLCK\n"); 1652 cifs_dbg(FYI, "F_SHLCK\n"); 1983 *type |= server->vals->shared 1653 *type |= server->vals->shared_lock_type; 1984 *lock = 1; 1654 *lock = 1; 1985 } else 1655 } else 1986 cifs_dbg(FYI, "Unknown type o 1656 cifs_dbg(FYI, "Unknown type of lock\n"); 1987 } 1657 } 1988 1658 1989 static int 1659 static int 1990 cifs_getlk(struct file *file, struct file_loc 1660 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, 1991 bool wait_flag, bool posix_lck, un 1661 bool wait_flag, bool posix_lck, unsigned int xid) 1992 { 1662 { 1993 int rc = 0; 1663 int rc = 0; 1994 __u64 length = cifs_flock_len(flock); 1664 __u64 length = cifs_flock_len(flock); 1995 struct cifsFileInfo *cfile = (struct 1665 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 1996 struct cifs_tcon *tcon = tlink_tcon(c 1666 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 1997 struct TCP_Server_Info *server = tcon 1667 struct TCP_Server_Info *server = tcon->ses->server; 1998 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1668 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1999 __u16 netfid = cfile->fid.netfid; 1669 __u16 netfid = cfile->fid.netfid; 2000 1670 2001 if (posix_lck) { 1671 if (posix_lck) { 2002 int posix_lock_type; 1672 int posix_lock_type; 2003 1673 2004 rc = cifs_posix_lock_test(fil 1674 rc = cifs_posix_lock_test(file, flock); 2005 if (!rc) 1675 if (!rc) 2006 return rc; 1676 return rc; 2007 1677 2008 if (type & server->vals->shar 1678 if (type & server->vals->shared_lock_type) 2009 posix_lock_type = CIF 1679 posix_lock_type = CIFS_RDLCK; 2010 else 1680 else 2011 posix_lock_type = CIF 1681 posix_lock_type = CIFS_WRLCK; 2012 rc = CIFSSMBPosixLock(xid, tc 1682 rc = CIFSSMBPosixLock(xid, tcon, netfid, 2013 hash_lo !! 1683 hash_lockowner(flock->fl_owner), 2014 flock-> 1684 flock->fl_start, length, flock, 2015 posix_l 1685 posix_lock_type, wait_flag); 2016 return rc; 1686 return rc; 2017 } 1687 } 2018 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1688 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2019 1689 2020 rc = cifs_lock_test(cfile, flock->fl_ 1690 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); 2021 if (!rc) 1691 if (!rc) 2022 return rc; 1692 return rc; 2023 1693 2024 /* BB we could chain these into one l 1694 /* BB we could chain these into one lock request BB */ 2025 rc = server->ops->mand_lock(xid, cfil 1695 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, 2026 1, 0, fal 1696 1, 0, false); 2027 if (rc == 0) { 1697 if (rc == 0) { 2028 rc = server->ops->mand_lock(x 1698 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2029 t 1699 type, 0, 1, false); 2030 flock->c.flc_type = F_UNLCK; !! 1700 flock->fl_type = F_UNLCK; 2031 if (rc != 0) 1701 if (rc != 0) 2032 cifs_dbg(VFS, "Error 1702 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 2033 rc); 1703 rc); 2034 return 0; 1704 return 0; 2035 } 1705 } 2036 1706 2037 if (type & server->vals->shared_lock_ 1707 if (type & server->vals->shared_lock_type) { 2038 flock->c.flc_type = F_WRLCK; !! 1708 flock->fl_type = F_WRLCK; 2039 return 0; 1709 return 0; 2040 } 1710 } 2041 1711 2042 type &= ~server->vals->exclusive_lock 1712 type &= ~server->vals->exclusive_lock_type; 2043 1713 2044 rc = server->ops->mand_lock(xid, cfil 1714 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2045 type | se 1715 type | server->vals->shared_lock_type, 2046 1, 0, fal 1716 1, 0, false); 2047 if (rc == 0) { 1717 if (rc == 0) { 2048 rc = server->ops->mand_lock(x 1718 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2049 type | server->vals-> 1719 type | server->vals->shared_lock_type, 0, 1, false); 2050 flock->c.flc_type = F_RDLCK; !! 1720 flock->fl_type = F_RDLCK; 2051 if (rc != 0) 1721 if (rc != 0) 2052 cifs_dbg(VFS, "Error 1722 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 2053 rc); 1723 rc); 2054 } else 1724 } else 2055 flock->c.flc_type = F_WRLCK; !! 1725 flock->fl_type = F_WRLCK; 2056 1726 2057 return 0; 1727 return 0; 2058 } 1728 } 2059 1729 2060 void 1730 void 2061 cifs_move_llist(struct list_head *source, str 1731 cifs_move_llist(struct list_head *source, struct list_head *dest) 2062 { 1732 { 2063 struct list_head *li, *tmp; 1733 struct list_head *li, *tmp; 2064 list_for_each_safe(li, tmp, source) 1734 list_for_each_safe(li, tmp, source) 2065 list_move(li, dest); 1735 list_move(li, dest); 2066 } 1736 } 2067 1737 2068 void 1738 void 2069 cifs_free_llist(struct list_head *llist) 1739 cifs_free_llist(struct list_head *llist) 2070 { 1740 { 2071 struct cifsLockInfo *li, *tmp; 1741 struct cifsLockInfo *li, *tmp; 2072 list_for_each_entry_safe(li, tmp, lli 1742 list_for_each_entry_safe(li, tmp, llist, llist) { 2073 cifs_del_lock_waiters(li); 1743 cifs_del_lock_waiters(li); 2074 list_del(&li->llist); 1744 list_del(&li->llist); 2075 kfree(li); 1745 kfree(li); 2076 } 1746 } 2077 } 1747 } 2078 1748 2079 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1749 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2080 int 1750 int 2081 cifs_unlock_range(struct cifsFileInfo *cfile, 1751 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, 2082 unsigned int xid) 1752 unsigned int xid) 2083 { 1753 { 2084 int rc = 0, stored_rc; 1754 int rc = 0, stored_rc; 2085 static const int types[] = { 1755 static const int types[] = { 2086 LOCKING_ANDX_LARGE_FILES, 1756 LOCKING_ANDX_LARGE_FILES, 2087 LOCKING_ANDX_SHARED_LOCK | LO 1757 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 2088 }; 1758 }; 2089 unsigned int i; 1759 unsigned int i; 2090 unsigned int max_num, num, max_buf; 1760 unsigned int max_num, num, max_buf; 2091 LOCKING_ANDX_RANGE *buf, *cur; 1761 LOCKING_ANDX_RANGE *buf, *cur; 2092 struct cifs_tcon *tcon = tlink_tcon(c 1762 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2093 struct cifsInodeInfo *cinode = CIFS_I 1763 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 2094 struct cifsLockInfo *li, *tmp; 1764 struct cifsLockInfo *li, *tmp; 2095 __u64 length = cifs_flock_len(flock); 1765 __u64 length = cifs_flock_len(flock); 2096 LIST_HEAD(tmp_llist); !! 1766 struct list_head tmp_llist; >> 1767 >> 1768 INIT_LIST_HEAD(&tmp_llist); 2097 1769 2098 /* 1770 /* 2099 * Accessing maxBuf is racy with cifs 1771 * Accessing maxBuf is racy with cifs_reconnect - need to store value 2100 * and check it before using. 1772 * and check it before using. 2101 */ 1773 */ 2102 max_buf = tcon->ses->server->maxBuf; 1774 max_buf = tcon->ses->server->maxBuf; 2103 if (max_buf < (sizeof(struct smb_hdr) 1775 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) 2104 return -EINVAL; 1776 return -EINVAL; 2105 1777 2106 BUILD_BUG_ON(sizeof(struct smb_hdr) + 1778 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 2107 PAGE_SIZE); 1779 PAGE_SIZE); 2108 max_buf = min_t(unsigned int, max_buf 1780 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 2109 PAGE_SIZE); 1781 PAGE_SIZE); 2110 max_num = (max_buf - sizeof(struct sm 1782 max_num = (max_buf - sizeof(struct smb_hdr)) / 2111 1783 sizeof(LOCKING_ANDX_RANGE); 2112 buf = kcalloc(max_num, sizeof(LOCKING 1784 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); 2113 if (!buf) 1785 if (!buf) 2114 return -ENOMEM; 1786 return -ENOMEM; 2115 1787 2116 cifs_down_write(&cinode->lock_sem); 1788 cifs_down_write(&cinode->lock_sem); 2117 for (i = 0; i < 2; i++) { 1789 for (i = 0; i < 2; i++) { 2118 cur = buf; 1790 cur = buf; 2119 num = 0; 1791 num = 0; 2120 list_for_each_entry_safe(li, 1792 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 2121 if (flock->fl_start > 1793 if (flock->fl_start > li->offset || 2122 (flock->fl_start 1794 (flock->fl_start + length) < 2123 (li->offset + li- 1795 (li->offset + li->length)) 2124 continue; 1796 continue; 2125 if (current->tgid != 1797 if (current->tgid != li->pid) 2126 continue; 1798 continue; 2127 if (types[i] != li->t 1799 if (types[i] != li->type) 2128 continue; 1800 continue; 2129 if (cinode->can_cache 1801 if (cinode->can_cache_brlcks) { 2130 /* 1802 /* 2131 * We can cac 1803 * We can cache brlock requests - simply remove 2132 * a lock fro 1804 * a lock from the file's list. 2133 */ 1805 */ 2134 list_del(&li- 1806 list_del(&li->llist); 2135 cifs_del_lock 1807 cifs_del_lock_waiters(li); 2136 kfree(li); 1808 kfree(li); 2137 continue; 1809 continue; 2138 } 1810 } 2139 cur->Pid = cpu_to_le1 1811 cur->Pid = cpu_to_le16(li->pid); 2140 cur->LengthLow = cpu_ 1812 cur->LengthLow = cpu_to_le32((u32)li->length); 2141 cur->LengthHigh = cpu 1813 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 2142 cur->OffsetLow = cpu_ 1814 cur->OffsetLow = cpu_to_le32((u32)li->offset); 2143 cur->OffsetHigh = cpu 1815 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 2144 /* 1816 /* 2145 * We need to save a 1817 * We need to save a lock here to let us add it again to 2146 * the file's list if 1818 * the file's list if the unlock range request fails on 2147 * the server. 1819 * the server. 2148 */ 1820 */ 2149 list_move(&li->llist, 1821 list_move(&li->llist, &tmp_llist); 2150 if (++num == max_num) 1822 if (++num == max_num) { 2151 stored_rc = c 1823 stored_rc = cifs_lockv(xid, tcon, 2152 1824 cfile->fid.netfid, 2153 1825 li->type, num, 0, buf); 2154 if (stored_rc 1826 if (stored_rc) { 2155 /* 1827 /* 2156 * We 1828 * We failed on the unlock range 2157 * re 1829 * request - add all locks from the tmp 2158 * li 1830 * list to the head of the file's list. 2159 */ 1831 */ 2160 cifs_ 1832 cifs_move_llist(&tmp_llist, 2161 1833 &cfile->llist->locks); 2162 rc = 1834 rc = stored_rc; 2163 } else 1835 } else 2164 /* 1836 /* 2165 * Th 1837 * The unlock range request succeed - 2166 * fr 1838 * free the tmp list. 2167 */ 1839 */ 2168 cifs_ 1840 cifs_free_llist(&tmp_llist); 2169 cur = buf; 1841 cur = buf; 2170 num = 0; 1842 num = 0; 2171 } else 1843 } else 2172 cur++; 1844 cur++; 2173 } 1845 } 2174 if (num) { 1846 if (num) { 2175 stored_rc = cifs_lock 1847 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 2176 1848 types[i], num, 0, buf); 2177 if (stored_rc) { 1849 if (stored_rc) { 2178 cifs_move_lli 1850 cifs_move_llist(&tmp_llist, 2179 1851 &cfile->llist->locks); 2180 rc = stored_r 1852 rc = stored_rc; 2181 } else 1853 } else 2182 cifs_free_lli 1854 cifs_free_llist(&tmp_llist); 2183 } 1855 } 2184 } 1856 } 2185 1857 2186 up_write(&cinode->lock_sem); 1858 up_write(&cinode->lock_sem); 2187 kfree(buf); 1859 kfree(buf); 2188 return rc; 1860 return rc; 2189 } 1861 } 2190 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1862 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2191 1863 2192 static int 1864 static int 2193 cifs_setlk(struct file *file, struct file_loc 1865 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, 2194 bool wait_flag, bool posix_lck, in 1866 bool wait_flag, bool posix_lck, int lock, int unlock, 2195 unsigned int xid) 1867 unsigned int xid) 2196 { 1868 { 2197 int rc = 0; 1869 int rc = 0; 2198 __u64 length = cifs_flock_len(flock); 1870 __u64 length = cifs_flock_len(flock); 2199 struct cifsFileInfo *cfile = (struct 1871 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2200 struct cifs_tcon *tcon = tlink_tcon(c 1872 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2201 struct TCP_Server_Info *server = tcon 1873 struct TCP_Server_Info *server = tcon->ses->server; 2202 struct inode *inode = d_inode(cfile-> 1874 struct inode *inode = d_inode(cfile->dentry); 2203 1875 2204 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 1876 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 2205 if (posix_lck) { 1877 if (posix_lck) { 2206 int posix_lock_type; 1878 int posix_lock_type; 2207 1879 2208 rc = cifs_posix_lock_set(file 1880 rc = cifs_posix_lock_set(file, flock); 2209 if (rc <= FILE_LOCK_DEFERRED) 1881 if (rc <= FILE_LOCK_DEFERRED) 2210 return rc; 1882 return rc; 2211 1883 2212 if (type & server->vals->shar 1884 if (type & server->vals->shared_lock_type) 2213 posix_lock_type = CIF 1885 posix_lock_type = CIFS_RDLCK; 2214 else 1886 else 2215 posix_lock_type = CIF 1887 posix_lock_type = CIFS_WRLCK; 2216 1888 2217 if (unlock == 1) 1889 if (unlock == 1) 2218 posix_lock_type = CIF 1890 posix_lock_type = CIFS_UNLCK; 2219 1891 2220 rc = CIFSSMBPosixLock(xid, tc 1892 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, 2221 hash_lo !! 1893 hash_lockowner(flock->fl_owner), 2222 flock-> 1894 flock->fl_start, length, 2223 NULL, p 1895 NULL, posix_lock_type, wait_flag); 2224 goto out; 1896 goto out; 2225 } 1897 } 2226 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY * 1898 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 2227 if (lock) { 1899 if (lock) { 2228 struct cifsLockInfo *lock; 1900 struct cifsLockInfo *lock; 2229 1901 2230 lock = cifs_lock_init(flock-> 1902 lock = cifs_lock_init(flock->fl_start, length, type, 2231 flock-> !! 1903 flock->fl_flags); 2232 if (!lock) 1904 if (!lock) 2233 return -ENOMEM; 1905 return -ENOMEM; 2234 1906 2235 rc = cifs_lock_add_if(cfile, 1907 rc = cifs_lock_add_if(cfile, lock, wait_flag); 2236 if (rc < 0) { 1908 if (rc < 0) { 2237 kfree(lock); 1909 kfree(lock); 2238 return rc; 1910 return rc; 2239 } 1911 } 2240 if (!rc) 1912 if (!rc) 2241 goto out; 1913 goto out; 2242 1914 2243 /* 1915 /* 2244 * Windows 7 server can delay 1916 * Windows 7 server can delay breaking lease from read to None 2245 * if we set a byte-range loc 1917 * if we set a byte-range lock on a file - break it explicitly 2246 * before sending the lock to 1918 * before sending the lock to the server to be sure the next 2247 * read won't conflict with n 1919 * read won't conflict with non-overlapted locks due to 2248 * pagereading. 1920 * pagereading. 2249 */ 1921 */ 2250 if (!CIFS_CACHE_WRITE(CIFS_I( 1922 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && 2251 CIFS_ 1923 CIFS_CACHE_READ(CIFS_I(inode))) { 2252 cifs_zap_mapping(inod 1924 cifs_zap_mapping(inode); 2253 cifs_dbg(FYI, "Set no 1925 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", 2254 inode); 1926 inode); 2255 CIFS_I(inode)->oplock 1927 CIFS_I(inode)->oplock = 0; 2256 } 1928 } 2257 1929 2258 rc = server->ops->mand_lock(x 1930 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 2259 t 1931 type, 1, 0, wait_flag); 2260 if (rc) { 1932 if (rc) { 2261 kfree(lock); 1933 kfree(lock); 2262 return rc; 1934 return rc; 2263 } 1935 } 2264 1936 2265 cifs_lock_add(cfile, lock); 1937 cifs_lock_add(cfile, lock); 2266 } else if (unlock) 1938 } else if (unlock) 2267 rc = server->ops->mand_unlock 1939 rc = server->ops->mand_unlock_range(cfile, flock, xid); 2268 1940 2269 out: 1941 out: 2270 if ((flock->c.flc_flags & FL_POSIX) | !! 1942 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) { 2271 /* 1943 /* 2272 * If this is a request to re 1944 * If this is a request to remove all locks because we 2273 * are closing the file, it d 1945 * are closing the file, it doesn't matter if the 2274 * unlocking failed as both c 1946 * unlocking failed as both cifs.ko and the SMB server 2275 * remove the lock on file cl 1947 * remove the lock on file close 2276 */ 1948 */ 2277 if (rc) { 1949 if (rc) { 2278 cifs_dbg(VFS, "%s fai 1950 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); 2279 if (!(flock->c.flc_fl !! 1951 if (!(flock->fl_flags & FL_CLOSE)) 2280 return rc; 1952 return rc; 2281 } 1953 } 2282 rc = locks_lock_file_wait(fil 1954 rc = locks_lock_file_wait(file, flock); 2283 } 1955 } 2284 return rc; 1956 return rc; 2285 } 1957 } 2286 1958 2287 int cifs_flock(struct file *file, int cmd, st 1959 int cifs_flock(struct file *file, int cmd, struct file_lock *fl) 2288 { 1960 { 2289 int rc, xid; 1961 int rc, xid; 2290 int lock = 0, unlock = 0; 1962 int lock = 0, unlock = 0; 2291 bool wait_flag = false; 1963 bool wait_flag = false; 2292 bool posix_lck = false; 1964 bool posix_lck = false; 2293 struct cifs_sb_info *cifs_sb; 1965 struct cifs_sb_info *cifs_sb; 2294 struct cifs_tcon *tcon; 1966 struct cifs_tcon *tcon; 2295 struct cifsFileInfo *cfile; 1967 struct cifsFileInfo *cfile; 2296 __u32 type; 1968 __u32 type; 2297 1969 2298 xid = get_xid(); 1970 xid = get_xid(); 2299 1971 2300 if (!(fl->c.flc_flags & FL_FLOCK)) { !! 1972 if (!(fl->fl_flags & FL_FLOCK)) { 2301 rc = -ENOLCK; 1973 rc = -ENOLCK; 2302 free_xid(xid); 1974 free_xid(xid); 2303 return rc; 1975 return rc; 2304 } 1976 } 2305 1977 2306 cfile = (struct cifsFileInfo *)file-> 1978 cfile = (struct cifsFileInfo *)file->private_data; 2307 tcon = tlink_tcon(cfile->tlink); 1979 tcon = tlink_tcon(cfile->tlink); 2308 1980 2309 cifs_read_flock(fl, &type, &lock, &un 1981 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag, 2310 tcon->ses->server); 1982 tcon->ses->server); 2311 cifs_sb = CIFS_FILE_SB(file); 1983 cifs_sb = CIFS_FILE_SB(file); 2312 1984 2313 if (cap_unix(tcon->ses) && 1985 if (cap_unix(tcon->ses) && 2314 (CIFS_UNIX_FCNTL_CAP & le64_to_cp 1986 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2315 ((cifs_sb->mnt_cifs_flags & CIFS_ 1987 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2316 posix_lck = true; 1988 posix_lck = true; 2317 1989 2318 if (!lock && !unlock) { 1990 if (!lock && !unlock) { 2319 /* 1991 /* 2320 * if no lock or unlock then 1992 * if no lock or unlock then nothing to do since we do not 2321 * know what it is 1993 * know what it is 2322 */ 1994 */ 2323 rc = -EOPNOTSUPP; 1995 rc = -EOPNOTSUPP; 2324 free_xid(xid); 1996 free_xid(xid); 2325 return rc; 1997 return rc; 2326 } 1998 } 2327 1999 2328 rc = cifs_setlk(file, fl, type, wait_ 2000 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock, 2329 xid); 2001 xid); 2330 free_xid(xid); 2002 free_xid(xid); 2331 return rc; 2003 return rc; 2332 2004 2333 2005 2334 } 2006 } 2335 2007 2336 int cifs_lock(struct file *file, int cmd, str 2008 int cifs_lock(struct file *file, int cmd, struct file_lock *flock) 2337 { 2009 { 2338 int rc, xid; 2010 int rc, xid; 2339 int lock = 0, unlock = 0; 2011 int lock = 0, unlock = 0; 2340 bool wait_flag = false; 2012 bool wait_flag = false; 2341 bool posix_lck = false; 2013 bool posix_lck = false; 2342 struct cifs_sb_info *cifs_sb; 2014 struct cifs_sb_info *cifs_sb; 2343 struct cifs_tcon *tcon; 2015 struct cifs_tcon *tcon; 2344 struct cifsFileInfo *cfile; 2016 struct cifsFileInfo *cfile; 2345 __u32 type; 2017 __u32 type; 2346 2018 2347 rc = -EACCES; 2019 rc = -EACCES; 2348 xid = get_xid(); 2020 xid = get_xid(); 2349 2021 2350 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type 2022 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd, 2351 flock->c.flc_flags, flock->c !! 2023 flock->fl_flags, flock->fl_type, (long long)flock->fl_start, 2352 (long long)flock->fl_start, << 2353 (long long)flock->fl_end); 2024 (long long)flock->fl_end); 2354 2025 2355 cfile = (struct cifsFileInfo *)file-> 2026 cfile = (struct cifsFileInfo *)file->private_data; 2356 tcon = tlink_tcon(cfile->tlink); 2027 tcon = tlink_tcon(cfile->tlink); 2357 2028 2358 cifs_read_flock(flock, &type, &lock, 2029 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, 2359 tcon->ses->server); 2030 tcon->ses->server); 2360 cifs_sb = CIFS_FILE_SB(file); 2031 cifs_sb = CIFS_FILE_SB(file); 2361 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS 2032 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags); 2362 2033 2363 if (cap_unix(tcon->ses) && 2034 if (cap_unix(tcon->ses) && 2364 (CIFS_UNIX_FCNTL_CAP & le64_to_cp 2035 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2365 ((cifs_sb->mnt_cifs_flags & CIFS_ 2036 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2366 posix_lck = true; 2037 posix_lck = true; 2367 /* 2038 /* 2368 * BB add code here to normalize offs 2039 * BB add code here to normalize offset and length to account for 2369 * negative length which we can not a 2040 * negative length which we can not accept over the wire. 2370 */ 2041 */ 2371 if (IS_GETLK(cmd)) { 2042 if (IS_GETLK(cmd)) { 2372 rc = cifs_getlk(file, flock, 2043 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); 2373 free_xid(xid); 2044 free_xid(xid); 2374 return rc; 2045 return rc; 2375 } 2046 } 2376 2047 2377 if (!lock && !unlock) { 2048 if (!lock && !unlock) { 2378 /* 2049 /* 2379 * if no lock or unlock then 2050 * if no lock or unlock then nothing to do since we do not 2380 * know what it is 2051 * know what it is 2381 */ 2052 */ 2382 free_xid(xid); 2053 free_xid(xid); 2383 return -EOPNOTSUPP; 2054 return -EOPNOTSUPP; 2384 } 2055 } 2385 2056 2386 rc = cifs_setlk(file, flock, type, wa 2057 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, 2387 xid); 2058 xid); 2388 free_xid(xid); 2059 free_xid(xid); 2389 return rc; 2060 return rc; 2390 } 2061 } 2391 2062 2392 void cifs_write_subrequest_terminated(struct !! 2063 /* 2393 bool wa !! 2064 * update the file size (if needed) after a write. Should be called with >> 2065 * the inode->i_lock held >> 2066 */ >> 2067 void >> 2068 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, >> 2069 unsigned int bytes_written) 2394 { 2070 { 2395 struct netfs_io_request *wreq = wdata !! 2071 loff_t end_of_write = offset + bytes_written; 2396 struct netfs_inode *ictx = netfs_inod << 2397 loff_t wrend; << 2398 2072 2399 if (result > 0) { !! 2073 if (end_of_write > cifsi->server_eof) 2400 wrend = wdata->subreq.start + !! 2074 cifsi->server_eof = end_of_write; >> 2075 } 2401 2076 2402 if (wrend > ictx->zero_point !! 2077 static ssize_t 2403 (wdata->rreq->origin == N !! 2078 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data, 2404 wdata->rreq->origin == N !! 2079 size_t write_size, loff_t *offset) 2405 ictx->zero_point = wr !! 2080 { 2406 if (wrend > ictx->remote_i_si !! 2081 int rc = 0; 2407 netfs_resize_file(ict !! 2082 unsigned int bytes_written = 0; >> 2083 unsigned int total_written; >> 2084 struct cifs_tcon *tcon; >> 2085 struct TCP_Server_Info *server; >> 2086 unsigned int xid; >> 2087 struct dentry *dentry = open_file->dentry; >> 2088 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry)); >> 2089 struct cifs_io_parms io_parms = {0}; >> 2090 >> 2091 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n", >> 2092 write_size, *offset, dentry); >> 2093 >> 2094 tcon = tlink_tcon(open_file->tlink); >> 2095 server = tcon->ses->server; >> 2096 >> 2097 if (!server->ops->sync_write) >> 2098 return -ENOSYS; >> 2099 >> 2100 xid = get_xid(); >> 2101 >> 2102 for (total_written = 0; write_size > total_written; >> 2103 total_written += bytes_written) { >> 2104 rc = -EAGAIN; >> 2105 while (rc == -EAGAIN) { >> 2106 struct kvec iov[2]; >> 2107 unsigned int len; >> 2108 >> 2109 if (open_file->invalidHandle) { >> 2110 /* we could deadlock if we called >> 2111 filemap_fdatawait from here so tell >> 2112 reopen_file not to flush data to >> 2113 server now */ >> 2114 rc = cifs_reopen_file(open_file, false); >> 2115 if (rc != 0) >> 2116 break; >> 2117 } >> 2118 >> 2119 len = min(server->ops->wp_retry_size(d_inode(dentry)), >> 2120 (unsigned int)write_size - total_written); >> 2121 /* iov[0] is reserved for smb header */ >> 2122 iov[1].iov_base = (char *)write_data + total_written; >> 2123 iov[1].iov_len = len; >> 2124 io_parms.pid = pid; >> 2125 io_parms.tcon = tcon; >> 2126 io_parms.offset = *offset; >> 2127 io_parms.length = len; >> 2128 rc = server->ops->sync_write(xid, &open_file->fid, >> 2129 &io_parms, &bytes_written, iov, 1); >> 2130 } >> 2131 if (rc || (bytes_written == 0)) { >> 2132 if (total_written) >> 2133 break; >> 2134 else { >> 2135 free_xid(xid); >> 2136 return rc; >> 2137 } >> 2138 } else { >> 2139 spin_lock(&d_inode(dentry)->i_lock); >> 2140 cifs_update_eof(cifsi, *offset, bytes_written); >> 2141 spin_unlock(&d_inode(dentry)->i_lock); >> 2142 *offset += bytes_written; >> 2143 } 2408 } 2144 } 2409 2145 2410 netfs_write_subrequest_terminated(&wd !! 2146 cifs_stats_bytes_written(tcon, total_written); >> 2147 >> 2148 if (total_written > 0) { >> 2149 spin_lock(&d_inode(dentry)->i_lock); >> 2150 if (*offset > d_inode(dentry)->i_size) { >> 2151 i_size_write(d_inode(dentry), *offset); >> 2152 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9; >> 2153 } >> 2154 spin_unlock(&d_inode(dentry)->i_lock); >> 2155 } >> 2156 mark_inode_dirty_sync(d_inode(dentry)); >> 2157 free_xid(xid); >> 2158 return total_written; 2411 } 2159 } 2412 2160 2413 struct cifsFileInfo *find_readable_file(struc 2161 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, 2414 bool 2162 bool fsuid_only) 2415 { 2163 { 2416 struct cifsFileInfo *open_file = NULL 2164 struct cifsFileInfo *open_file = NULL; 2417 struct cifs_sb_info *cifs_sb = CIFS_S 2165 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb); 2418 2166 2419 /* only filter by fsuid on multiuser 2167 /* only filter by fsuid on multiuser mounts */ 2420 if (!(cifs_sb->mnt_cifs_flags & CIFS_ 2168 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 2421 fsuid_only = false; 2169 fsuid_only = false; 2422 2170 2423 spin_lock(&cifs_inode->open_file_lock 2171 spin_lock(&cifs_inode->open_file_lock); 2424 /* we could simply get the first_list 2172 /* we could simply get the first_list_entry since write-only entries 2425 are always at the end of the list 2173 are always at the end of the list but since the first entry might 2426 have a close pending, we go throug 2174 have a close pending, we go through the whole list */ 2427 list_for_each_entry(open_file, &cifs_ 2175 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 2428 if (fsuid_only && !uid_eq(ope 2176 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 2429 continue; 2177 continue; 2430 if (OPEN_FMODE(open_file->f_f 2178 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { 2431 if ((!open_file->inva 2179 if ((!open_file->invalidHandle)) { 2432 /* found a go 2180 /* found a good file */ 2433 /* lock it so 2181 /* lock it so it will not be closed on us */ 2434 cifsFileInfo_ 2182 cifsFileInfo_get(open_file); 2435 spin_unlock(& 2183 spin_unlock(&cifs_inode->open_file_lock); 2436 return open_f 2184 return open_file; 2437 } /* else might as we 2185 } /* else might as well continue, and look for 2438 another, or simp 2186 another, or simply have the caller reopen it 2439 again rather tha 2187 again rather than trying to fix this handle */ 2440 } else /* write only file */ 2188 } else /* write only file */ 2441 break; /* write only 2189 break; /* write only files are last so must be done */ 2442 } 2190 } 2443 spin_unlock(&cifs_inode->open_file_lo 2191 spin_unlock(&cifs_inode->open_file_lock); 2444 return NULL; 2192 return NULL; 2445 } 2193 } 2446 2194 2447 /* Return -EBADF if no handle is found and ge 2195 /* Return -EBADF if no handle is found and general rc otherwise */ 2448 int 2196 int 2449 cifs_get_writable_file(struct cifsInodeInfo * 2197 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags, 2450 struct cifsFileInfo ** 2198 struct cifsFileInfo **ret_file) 2451 { 2199 { 2452 struct cifsFileInfo *open_file, *inv_ 2200 struct cifsFileInfo *open_file, *inv_file = NULL; 2453 struct cifs_sb_info *cifs_sb; 2201 struct cifs_sb_info *cifs_sb; 2454 bool any_available = false; 2202 bool any_available = false; 2455 int rc = -EBADF; 2203 int rc = -EBADF; 2456 unsigned int refind = 0; 2204 unsigned int refind = 0; 2457 bool fsuid_only = flags & FIND_WR_FSU 2205 bool fsuid_only = flags & FIND_WR_FSUID_ONLY; 2458 bool with_delete = flags & FIND_WR_WI 2206 bool with_delete = flags & FIND_WR_WITH_DELETE; 2459 *ret_file = NULL; 2207 *ret_file = NULL; 2460 2208 2461 /* 2209 /* 2462 * Having a null inode here (because 2210 * Having a null inode here (because mapping->host was set to zero by 2463 * the VFS or MM) should not happen b 2211 * the VFS or MM) should not happen but we had reports of on oops (due 2464 * to it being zero) during stress te 2212 * to it being zero) during stress testcases so we need to check for it 2465 */ 2213 */ 2466 2214 2467 if (cifs_inode == NULL) { 2215 if (cifs_inode == NULL) { 2468 cifs_dbg(VFS, "Null inode pas 2216 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); 2469 dump_stack(); 2217 dump_stack(); 2470 return rc; 2218 return rc; 2471 } 2219 } 2472 2220 2473 cifs_sb = CIFS_SB(cifs_inode->netfs.i 2221 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb); 2474 2222 2475 /* only filter by fsuid on multiuser 2223 /* only filter by fsuid on multiuser mounts */ 2476 if (!(cifs_sb->mnt_cifs_flags & CIFS_ 2224 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 2477 fsuid_only = false; 2225 fsuid_only = false; 2478 2226 2479 spin_lock(&cifs_inode->open_file_lock 2227 spin_lock(&cifs_inode->open_file_lock); 2480 refind_writable: 2228 refind_writable: 2481 if (refind > MAX_REOPEN_ATT) { 2229 if (refind > MAX_REOPEN_ATT) { 2482 spin_unlock(&cifs_inode->open 2230 spin_unlock(&cifs_inode->open_file_lock); 2483 return rc; 2231 return rc; 2484 } 2232 } 2485 list_for_each_entry(open_file, &cifs_ 2233 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 2486 if (!any_available && open_fi 2234 if (!any_available && open_file->pid != current->tgid) 2487 continue; 2235 continue; 2488 if (fsuid_only && !uid_eq(ope 2236 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 2489 continue; 2237 continue; 2490 if (with_delete && !(open_fil 2238 if (with_delete && !(open_file->fid.access & DELETE)) 2491 continue; 2239 continue; 2492 if (OPEN_FMODE(open_file->f_f 2240 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 2493 if (!open_file->inval 2241 if (!open_file->invalidHandle) { 2494 /* found a go 2242 /* found a good writable file */ 2495 cifsFileInfo_ 2243 cifsFileInfo_get(open_file); 2496 spin_unlock(& 2244 spin_unlock(&cifs_inode->open_file_lock); 2497 *ret_file = o 2245 *ret_file = open_file; 2498 return 0; 2246 return 0; 2499 } else { 2247 } else { 2500 if (!inv_file 2248 if (!inv_file) 2501 inv_f 2249 inv_file = open_file; 2502 } 2250 } 2503 } 2251 } 2504 } 2252 } 2505 /* couldn't find usable FH with same !! 2253 /* couldn't find useable FH with same pid, try any available */ 2506 if (!any_available) { 2254 if (!any_available) { 2507 any_available = true; 2255 any_available = true; 2508 goto refind_writable; 2256 goto refind_writable; 2509 } 2257 } 2510 2258 2511 if (inv_file) { 2259 if (inv_file) { 2512 any_available = false; 2260 any_available = false; 2513 cifsFileInfo_get(inv_file); 2261 cifsFileInfo_get(inv_file); 2514 } 2262 } 2515 2263 2516 spin_unlock(&cifs_inode->open_file_lo 2264 spin_unlock(&cifs_inode->open_file_lock); 2517 2265 2518 if (inv_file) { 2266 if (inv_file) { 2519 rc = cifs_reopen_file(inv_fil 2267 rc = cifs_reopen_file(inv_file, false); 2520 if (!rc) { 2268 if (!rc) { 2521 *ret_file = inv_file; 2269 *ret_file = inv_file; 2522 return 0; 2270 return 0; 2523 } 2271 } 2524 2272 2525 spin_lock(&cifs_inode->open_f 2273 spin_lock(&cifs_inode->open_file_lock); 2526 list_move_tail(&inv_file->fli 2274 list_move_tail(&inv_file->flist, &cifs_inode->openFileList); 2527 spin_unlock(&cifs_inode->open 2275 spin_unlock(&cifs_inode->open_file_lock); 2528 cifsFileInfo_put(inv_file); 2276 cifsFileInfo_put(inv_file); 2529 ++refind; 2277 ++refind; 2530 inv_file = NULL; 2278 inv_file = NULL; 2531 spin_lock(&cifs_inode->open_f 2279 spin_lock(&cifs_inode->open_file_lock); 2532 goto refind_writable; 2280 goto refind_writable; 2533 } 2281 } 2534 2282 2535 return rc; 2283 return rc; 2536 } 2284 } 2537 2285 2538 struct cifsFileInfo * 2286 struct cifsFileInfo * 2539 find_writable_file(struct cifsInodeInfo *cifs 2287 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags) 2540 { 2288 { 2541 struct cifsFileInfo *cfile; 2289 struct cifsFileInfo *cfile; 2542 int rc; 2290 int rc; 2543 2291 2544 rc = cifs_get_writable_file(cifs_inod 2292 rc = cifs_get_writable_file(cifs_inode, flags, &cfile); 2545 if (rc) 2293 if (rc) 2546 cifs_dbg(FYI, "Couldn't find 2294 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc); 2547 2295 2548 return cfile; 2296 return cfile; 2549 } 2297 } 2550 2298 2551 int 2299 int 2552 cifs_get_writable_path(struct cifs_tcon *tcon 2300 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name, 2553 int flags, 2301 int flags, 2554 struct cifsFileInfo ** 2302 struct cifsFileInfo **ret_file) 2555 { 2303 { 2556 struct cifsFileInfo *cfile; 2304 struct cifsFileInfo *cfile; 2557 void *page = alloc_dentry_path(); 2305 void *page = alloc_dentry_path(); 2558 2306 2559 *ret_file = NULL; 2307 *ret_file = NULL; 2560 2308 2561 spin_lock(&tcon->open_file_lock); 2309 spin_lock(&tcon->open_file_lock); 2562 list_for_each_entry(cfile, &tcon->ope 2310 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 2563 struct cifsInodeInfo *cinode; 2311 struct cifsInodeInfo *cinode; 2564 const char *full_path = build 2312 const char *full_path = build_path_from_dentry(cfile->dentry, page); 2565 if (IS_ERR(full_path)) { 2313 if (IS_ERR(full_path)) { 2566 spin_unlock(&tcon->op 2314 spin_unlock(&tcon->open_file_lock); 2567 free_dentry_path(page 2315 free_dentry_path(page); 2568 return PTR_ERR(full_p 2316 return PTR_ERR(full_path); 2569 } 2317 } 2570 if (strcmp(full_path, name)) 2318 if (strcmp(full_path, name)) 2571 continue; 2319 continue; 2572 2320 2573 cinode = CIFS_I(d_inode(cfile 2321 cinode = CIFS_I(d_inode(cfile->dentry)); 2574 spin_unlock(&tcon->open_file_ 2322 spin_unlock(&tcon->open_file_lock); 2575 free_dentry_path(page); 2323 free_dentry_path(page); 2576 return cifs_get_writable_file 2324 return cifs_get_writable_file(cinode, flags, ret_file); 2577 } 2325 } 2578 2326 2579 spin_unlock(&tcon->open_file_lock); 2327 spin_unlock(&tcon->open_file_lock); 2580 free_dentry_path(page); 2328 free_dentry_path(page); 2581 return -ENOENT; 2329 return -ENOENT; 2582 } 2330 } 2583 2331 2584 int 2332 int 2585 cifs_get_readable_path(struct cifs_tcon *tcon 2333 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name, 2586 struct cifsFileInfo ** 2334 struct cifsFileInfo **ret_file) 2587 { 2335 { 2588 struct cifsFileInfo *cfile; 2336 struct cifsFileInfo *cfile; 2589 void *page = alloc_dentry_path(); 2337 void *page = alloc_dentry_path(); 2590 2338 2591 *ret_file = NULL; 2339 *ret_file = NULL; 2592 2340 2593 spin_lock(&tcon->open_file_lock); 2341 spin_lock(&tcon->open_file_lock); 2594 list_for_each_entry(cfile, &tcon->ope 2342 list_for_each_entry(cfile, &tcon->openFileList, tlist) { 2595 struct cifsInodeInfo *cinode; 2343 struct cifsInodeInfo *cinode; 2596 const char *full_path = build 2344 const char *full_path = build_path_from_dentry(cfile->dentry, page); 2597 if (IS_ERR(full_path)) { 2345 if (IS_ERR(full_path)) { 2598 spin_unlock(&tcon->op 2346 spin_unlock(&tcon->open_file_lock); 2599 free_dentry_path(page 2347 free_dentry_path(page); 2600 return PTR_ERR(full_p 2348 return PTR_ERR(full_path); 2601 } 2349 } 2602 if (strcmp(full_path, name)) 2350 if (strcmp(full_path, name)) 2603 continue; 2351 continue; 2604 2352 2605 cinode = CIFS_I(d_inode(cfile 2353 cinode = CIFS_I(d_inode(cfile->dentry)); 2606 spin_unlock(&tcon->open_file_ 2354 spin_unlock(&tcon->open_file_lock); 2607 free_dentry_path(page); 2355 free_dentry_path(page); 2608 *ret_file = find_readable_fil 2356 *ret_file = find_readable_file(cinode, 0); 2609 return *ret_file ? 0 : -ENOEN 2357 return *ret_file ? 0 : -ENOENT; 2610 } 2358 } 2611 2359 2612 spin_unlock(&tcon->open_file_lock); 2360 spin_unlock(&tcon->open_file_lock); 2613 free_dentry_path(page); 2361 free_dentry_path(page); 2614 return -ENOENT; 2362 return -ENOENT; 2615 } 2363 } 2616 2364 >> 2365 void >> 2366 cifs_writedata_release(struct kref *refcount) >> 2367 { >> 2368 struct cifs_writedata *wdata = container_of(refcount, >> 2369 struct cifs_writedata, refcount); >> 2370 #ifdef CONFIG_CIFS_SMB_DIRECT >> 2371 if (wdata->mr) { >> 2372 smbd_deregister_mr(wdata->mr); >> 2373 wdata->mr = NULL; >> 2374 } >> 2375 #endif >> 2376 >> 2377 if (wdata->cfile) >> 2378 cifsFileInfo_put(wdata->cfile); >> 2379 >> 2380 kvfree(wdata->pages); >> 2381 kfree(wdata); >> 2382 } >> 2383 2617 /* 2384 /* 2618 * Flush data on a strict file. !! 2385 * Write failed with a retryable error. Resend the write request. It's also >> 2386 * possible that the page was redirtied so re-clean the page. 2619 */ 2387 */ >> 2388 static void >> 2389 cifs_writev_requeue(struct cifs_writedata *wdata) >> 2390 { >> 2391 int i, rc = 0; >> 2392 struct inode *inode = d_inode(wdata->cfile->dentry); >> 2393 struct TCP_Server_Info *server; >> 2394 unsigned int rest_len; >> 2395 >> 2396 server = tlink_tcon(wdata->cfile->tlink)->ses->server; >> 2397 i = 0; >> 2398 rest_len = wdata->bytes; >> 2399 do { >> 2400 struct cifs_writedata *wdata2; >> 2401 unsigned int j, nr_pages, wsize, tailsz, cur_len; >> 2402 >> 2403 wsize = server->ops->wp_retry_size(inode); >> 2404 if (wsize < rest_len) { >> 2405 nr_pages = wsize / PAGE_SIZE; >> 2406 if (!nr_pages) { >> 2407 rc = -EOPNOTSUPP; >> 2408 break; >> 2409 } >> 2410 cur_len = nr_pages * PAGE_SIZE; >> 2411 tailsz = PAGE_SIZE; >> 2412 } else { >> 2413 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE); >> 2414 cur_len = rest_len; >> 2415 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE; >> 2416 } >> 2417 >> 2418 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete); >> 2419 if (!wdata2) { >> 2420 rc = -ENOMEM; >> 2421 break; >> 2422 } >> 2423 >> 2424 for (j = 0; j < nr_pages; j++) { >> 2425 wdata2->pages[j] = wdata->pages[i + j]; >> 2426 lock_page(wdata2->pages[j]); >> 2427 clear_page_dirty_for_io(wdata2->pages[j]); >> 2428 } >> 2429 >> 2430 wdata2->sync_mode = wdata->sync_mode; >> 2431 wdata2->nr_pages = nr_pages; >> 2432 wdata2->offset = page_offset(wdata2->pages[0]); >> 2433 wdata2->pagesz = PAGE_SIZE; >> 2434 wdata2->tailsz = tailsz; >> 2435 wdata2->bytes = cur_len; >> 2436 >> 2437 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, >> 2438 &wdata2->cfile); >> 2439 if (!wdata2->cfile) { >> 2440 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n", >> 2441 rc); >> 2442 if (!is_retryable_error(rc)) >> 2443 rc = -EBADF; >> 2444 } else { >> 2445 wdata2->pid = wdata2->cfile->pid; >> 2446 rc = server->ops->async_writev(wdata2, >> 2447 cifs_writedata_release); >> 2448 } >> 2449 >> 2450 for (j = 0; j < nr_pages; j++) { >> 2451 unlock_page(wdata2->pages[j]); >> 2452 if (rc != 0 && !is_retryable_error(rc)) { >> 2453 SetPageError(wdata2->pages[j]); >> 2454 end_page_writeback(wdata2->pages[j]); >> 2455 put_page(wdata2->pages[j]); >> 2456 } >> 2457 } >> 2458 >> 2459 kref_put(&wdata2->refcount, cifs_writedata_release); >> 2460 if (rc) { >> 2461 if (is_retryable_error(rc)) >> 2462 continue; >> 2463 i += nr_pages; >> 2464 break; >> 2465 } >> 2466 >> 2467 rest_len -= cur_len; >> 2468 i += nr_pages; >> 2469 } while (i < wdata->nr_pages); >> 2470 >> 2471 /* cleanup remaining pages from the original wdata */ >> 2472 for (; i < wdata->nr_pages; i++) { >> 2473 SetPageError(wdata->pages[i]); >> 2474 end_page_writeback(wdata->pages[i]); >> 2475 put_page(wdata->pages[i]); >> 2476 } >> 2477 >> 2478 if (rc != 0 && !is_retryable_error(rc)) >> 2479 mapping_set_error(inode->i_mapping, rc); >> 2480 kref_put(&wdata->refcount, cifs_writedata_release); >> 2481 } >> 2482 >> 2483 void >> 2484 cifs_writev_complete(struct work_struct *work) >> 2485 { >> 2486 struct cifs_writedata *wdata = container_of(work, >> 2487 struct cifs_writedata, work); >> 2488 struct inode *inode = d_inode(wdata->cfile->dentry); >> 2489 int i = 0; >> 2490 >> 2491 if (wdata->result == 0) { >> 2492 spin_lock(&inode->i_lock); >> 2493 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes); >> 2494 spin_unlock(&inode->i_lock); >> 2495 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink), >> 2496 wdata->bytes); >> 2497 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN) >> 2498 return cifs_writev_requeue(wdata); >> 2499 >> 2500 for (i = 0; i < wdata->nr_pages; i++) { >> 2501 struct page *page = wdata->pages[i]; >> 2502 >> 2503 if (wdata->result == -EAGAIN) >> 2504 __set_page_dirty_nobuffers(page); >> 2505 else if (wdata->result < 0) >> 2506 SetPageError(page); >> 2507 end_page_writeback(page); >> 2508 cifs_readpage_to_fscache(inode, page); >> 2509 put_page(page); >> 2510 } >> 2511 if (wdata->result != -EAGAIN) >> 2512 mapping_set_error(inode->i_mapping, wdata->result); >> 2513 kref_put(&wdata->refcount, cifs_writedata_release); >> 2514 } >> 2515 >> 2516 struct cifs_writedata * >> 2517 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete) >> 2518 { >> 2519 struct cifs_writedata *writedata = NULL; >> 2520 struct page **pages = >> 2521 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); >> 2522 if (pages) { >> 2523 writedata = cifs_writedata_direct_alloc(pages, complete); >> 2524 if (!writedata) >> 2525 kvfree(pages); >> 2526 } >> 2527 >> 2528 return writedata; >> 2529 } >> 2530 >> 2531 struct cifs_writedata * >> 2532 cifs_writedata_direct_alloc(struct page **pages, work_func_t complete) >> 2533 { >> 2534 struct cifs_writedata *wdata; >> 2535 >> 2536 wdata = kzalloc(sizeof(*wdata), GFP_NOFS); >> 2537 if (wdata != NULL) { >> 2538 wdata->pages = pages; >> 2539 kref_init(&wdata->refcount); >> 2540 INIT_LIST_HEAD(&wdata->list); >> 2541 init_completion(&wdata->done); >> 2542 INIT_WORK(&wdata->work, complete); >> 2543 } >> 2544 return wdata; >> 2545 } >> 2546 >> 2547 >> 2548 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) >> 2549 { >> 2550 struct address_space *mapping = page->mapping; >> 2551 loff_t offset = (loff_t)page->index << PAGE_SHIFT; >> 2552 char *write_data; >> 2553 int rc = -EFAULT; >> 2554 int bytes_written = 0; >> 2555 struct inode *inode; >> 2556 struct cifsFileInfo *open_file; >> 2557 >> 2558 if (!mapping || !mapping->host) >> 2559 return -EFAULT; >> 2560 >> 2561 inode = page->mapping->host; >> 2562 >> 2563 offset += (loff_t)from; >> 2564 write_data = kmap(page); >> 2565 write_data += from; >> 2566 >> 2567 if ((to > PAGE_SIZE) || (from > to)) { >> 2568 kunmap(page); >> 2569 return -EIO; >> 2570 } >> 2571 >> 2572 /* racing with truncate? */ >> 2573 if (offset > mapping->host->i_size) { >> 2574 kunmap(page); >> 2575 return 0; /* don't care */ >> 2576 } >> 2577 >> 2578 /* check to make sure that we are not extending the file */ >> 2579 if (mapping->host->i_size - offset < (loff_t)to) >> 2580 to = (unsigned)(mapping->host->i_size - offset); >> 2581 >> 2582 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY, >> 2583 &open_file); >> 2584 if (!rc) { >> 2585 bytes_written = cifs_write(open_file, open_file->pid, >> 2586 write_data, to - from, &offset); >> 2587 cifsFileInfo_put(open_file); >> 2588 /* Does mm or vfs already set times? */ >> 2589 inode->i_atime = inode->i_mtime = current_time(inode); >> 2590 if ((bytes_written > 0) && (offset)) >> 2591 rc = 0; >> 2592 else if (bytes_written < 0) >> 2593 rc = bytes_written; >> 2594 else >> 2595 rc = -EFAULT; >> 2596 } else { >> 2597 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc); >> 2598 if (!is_retryable_error(rc)) >> 2599 rc = -EIO; >> 2600 } >> 2601 >> 2602 kunmap(page); >> 2603 return rc; >> 2604 } >> 2605 >> 2606 static struct cifs_writedata * >> 2607 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping, >> 2608 pgoff_t end, pgoff_t *index, >> 2609 unsigned int *found_pages) >> 2610 { >> 2611 struct cifs_writedata *wdata; >> 2612 >> 2613 wdata = cifs_writedata_alloc((unsigned int)tofind, >> 2614 cifs_writev_complete); >> 2615 if (!wdata) >> 2616 return NULL; >> 2617 >> 2618 *found_pages = find_get_pages_range_tag(mapping, index, end, >> 2619 PAGECACHE_TAG_DIRTY, tofind, wdata->pages); >> 2620 return wdata; >> 2621 } >> 2622 >> 2623 static unsigned int >> 2624 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages, >> 2625 struct address_space *mapping, >> 2626 struct writeback_control *wbc, >> 2627 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done) >> 2628 { >> 2629 unsigned int nr_pages = 0, i; >> 2630 struct page *page; >> 2631 >> 2632 for (i = 0; i < found_pages; i++) { >> 2633 page = wdata->pages[i]; >> 2634 /* >> 2635 * At this point we hold neither the i_pages lock nor the >> 2636 * page lock: the page may be truncated or invalidated >> 2637 * (changing page->mapping to NULL), or even swizzled >> 2638 * back from swapper_space to tmpfs file mapping >> 2639 */ >> 2640 >> 2641 if (nr_pages == 0) >> 2642 lock_page(page); >> 2643 else if (!trylock_page(page)) >> 2644 break; >> 2645 >> 2646 if (unlikely(page->mapping != mapping)) { >> 2647 unlock_page(page); >> 2648 break; >> 2649 } >> 2650 >> 2651 if (!wbc->range_cyclic && page->index > end) { >> 2652 *done = true; >> 2653 unlock_page(page); >> 2654 break; >> 2655 } >> 2656 >> 2657 if (*next && (page->index != *next)) { >> 2658 /* Not next consecutive page */ >> 2659 unlock_page(page); >> 2660 break; >> 2661 } >> 2662 >> 2663 if (wbc->sync_mode != WB_SYNC_NONE) >> 2664 wait_on_page_writeback(page); >> 2665 >> 2666 if (PageWriteback(page) || >> 2667 !clear_page_dirty_for_io(page)) { >> 2668 unlock_page(page); >> 2669 break; >> 2670 } >> 2671 >> 2672 /* >> 2673 * This actually clears the dirty bit in the radix tree. >> 2674 * See cifs_writepage() for more commentary. >> 2675 */ >> 2676 set_page_writeback(page); >> 2677 if (page_offset(page) >= i_size_read(mapping->host)) { >> 2678 *done = true; >> 2679 unlock_page(page); >> 2680 end_page_writeback(page); >> 2681 break; >> 2682 } >> 2683 >> 2684 wdata->pages[i] = page; >> 2685 *next = page->index + 1; >> 2686 ++nr_pages; >> 2687 } >> 2688 >> 2689 /* reset index to refind any pages skipped */ >> 2690 if (nr_pages == 0) >> 2691 *index = wdata->pages[0]->index + 1; >> 2692 >> 2693 /* put any pages we aren't going to use */ >> 2694 for (i = nr_pages; i < found_pages; i++) { >> 2695 put_page(wdata->pages[i]); >> 2696 wdata->pages[i] = NULL; >> 2697 } >> 2698 >> 2699 return nr_pages; >> 2700 } >> 2701 >> 2702 static int >> 2703 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, >> 2704 struct address_space *mapping, struct writeback_control *wbc) >> 2705 { >> 2706 int rc; >> 2707 >> 2708 wdata->sync_mode = wbc->sync_mode; >> 2709 wdata->nr_pages = nr_pages; >> 2710 wdata->offset = page_offset(wdata->pages[0]); >> 2711 wdata->pagesz = PAGE_SIZE; >> 2712 wdata->tailsz = min(i_size_read(mapping->host) - >> 2713 page_offset(wdata->pages[nr_pages - 1]), >> 2714 (loff_t)PAGE_SIZE); >> 2715 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz; >> 2716 wdata->pid = wdata->cfile->pid; >> 2717 >> 2718 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes); >> 2719 if (rc) >> 2720 return rc; >> 2721 >> 2722 if (wdata->cfile->invalidHandle) >> 2723 rc = -EAGAIN; >> 2724 else >> 2725 rc = wdata->server->ops->async_writev(wdata, >> 2726 cifs_writedata_release); >> 2727 >> 2728 return rc; >> 2729 } >> 2730 >> 2731 static int cifs_writepages(struct address_space *mapping, >> 2732 struct writeback_control *wbc) >> 2733 { >> 2734 struct inode *inode = mapping->host; >> 2735 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); >> 2736 struct TCP_Server_Info *server; >> 2737 bool done = false, scanned = false, range_whole = false; >> 2738 pgoff_t end, index; >> 2739 struct cifs_writedata *wdata; >> 2740 struct cifsFileInfo *cfile = NULL; >> 2741 int rc = 0; >> 2742 int saved_rc = 0; >> 2743 unsigned int xid; >> 2744 >> 2745 /* >> 2746 * If wsize is smaller than the page cache size, default to writing >> 2747 * one page at a time via cifs_writepage >> 2748 */ >> 2749 if (cifs_sb->ctx->wsize < PAGE_SIZE) >> 2750 return generic_writepages(mapping, wbc); >> 2751 >> 2752 xid = get_xid(); >> 2753 if (wbc->range_cyclic) { >> 2754 index = mapping->writeback_index; /* Start from prev offset */ >> 2755 end = -1; >> 2756 } else { >> 2757 index = wbc->range_start >> PAGE_SHIFT; >> 2758 end = wbc->range_end >> PAGE_SHIFT; >> 2759 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) >> 2760 range_whole = true; >> 2761 scanned = true; >> 2762 } >> 2763 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses); >> 2764 >> 2765 retry: >> 2766 while (!done && index <= end) { >> 2767 unsigned int i, nr_pages, found_pages, wsize; >> 2768 pgoff_t next = 0, tofind, saved_index = index; >> 2769 struct cifs_credits credits_on_stack; >> 2770 struct cifs_credits *credits = &credits_on_stack; >> 2771 int get_file_rc = 0; >> 2772 >> 2773 if (cfile) >> 2774 cifsFileInfo_put(cfile); >> 2775 >> 2776 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile); >> 2777 >> 2778 /* in case of an error store it to return later */ >> 2779 if (rc) >> 2780 get_file_rc = rc; >> 2781 >> 2782 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize, >> 2783 &wsize, credits); >> 2784 if (rc != 0) { >> 2785 done = true; >> 2786 break; >> 2787 } >> 2788 >> 2789 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1; >> 2790 >> 2791 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index, >> 2792 &found_pages); >> 2793 if (!wdata) { >> 2794 rc = -ENOMEM; >> 2795 done = true; >> 2796 add_credits_and_wake_if(server, credits, 0); >> 2797 break; >> 2798 } >> 2799 >> 2800 if (found_pages == 0) { >> 2801 kref_put(&wdata->refcount, cifs_writedata_release); >> 2802 add_credits_and_wake_if(server, credits, 0); >> 2803 break; >> 2804 } >> 2805 >> 2806 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc, >> 2807 end, &index, &next, &done); >> 2808 >> 2809 /* nothing to write? */ >> 2810 if (nr_pages == 0) { >> 2811 kref_put(&wdata->refcount, cifs_writedata_release); >> 2812 add_credits_and_wake_if(server, credits, 0); >> 2813 continue; >> 2814 } >> 2815 >> 2816 wdata->credits = credits_on_stack; >> 2817 wdata->cfile = cfile; >> 2818 wdata->server = server; >> 2819 cfile = NULL; >> 2820 >> 2821 if (!wdata->cfile) { >> 2822 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", >> 2823 get_file_rc); >> 2824 if (is_retryable_error(get_file_rc)) >> 2825 rc = get_file_rc; >> 2826 else >> 2827 rc = -EBADF; >> 2828 } else >> 2829 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc); >> 2830 >> 2831 for (i = 0; i < nr_pages; ++i) >> 2832 unlock_page(wdata->pages[i]); >> 2833 >> 2834 /* send failure -- clean up the mess */ >> 2835 if (rc != 0) { >> 2836 add_credits_and_wake_if(server, &wdata->credits, 0); >> 2837 for (i = 0; i < nr_pages; ++i) { >> 2838 if (is_retryable_error(rc)) >> 2839 redirty_page_for_writepage(wbc, >> 2840 wdata->pages[i]); >> 2841 else >> 2842 SetPageError(wdata->pages[i]); >> 2843 end_page_writeback(wdata->pages[i]); >> 2844 put_page(wdata->pages[i]); >> 2845 } >> 2846 if (!is_retryable_error(rc)) >> 2847 mapping_set_error(mapping, rc); >> 2848 } >> 2849 kref_put(&wdata->refcount, cifs_writedata_release); >> 2850 >> 2851 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) { >> 2852 index = saved_index; >> 2853 continue; >> 2854 } >> 2855 >> 2856 /* Return immediately if we received a signal during writing */ >> 2857 if (is_interrupt_error(rc)) { >> 2858 done = true; >> 2859 break; >> 2860 } >> 2861 >> 2862 if (rc != 0 && saved_rc == 0) >> 2863 saved_rc = rc; >> 2864 >> 2865 wbc->nr_to_write -= nr_pages; >> 2866 if (wbc->nr_to_write <= 0) >> 2867 done = true; >> 2868 >> 2869 index = next; >> 2870 } >> 2871 >> 2872 if (!scanned && !done) { >> 2873 /* >> 2874 * We hit the last page and there is more work to be done: wrap >> 2875 * back to the start of the file >> 2876 */ >> 2877 scanned = true; >> 2878 index = 0; >> 2879 goto retry; >> 2880 } >> 2881 >> 2882 if (saved_rc != 0) >> 2883 rc = saved_rc; >> 2884 >> 2885 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) >> 2886 mapping->writeback_index = index; >> 2887 >> 2888 if (cfile) >> 2889 cifsFileInfo_put(cfile); >> 2890 free_xid(xid); >> 2891 /* Indication to update ctime and mtime as close is deferred */ >> 2892 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); >> 2893 return rc; >> 2894 } >> 2895 >> 2896 static int >> 2897 cifs_writepage_locked(struct page *page, struct writeback_control *wbc) >> 2898 { >> 2899 int rc; >> 2900 unsigned int xid; >> 2901 >> 2902 xid = get_xid(); >> 2903 /* BB add check for wbc flags */ >> 2904 get_page(page); >> 2905 if (!PageUptodate(page)) >> 2906 cifs_dbg(FYI, "ppw - page not up to date\n"); >> 2907 >> 2908 /* >> 2909 * Set the "writeback" flag, and clear "dirty" in the radix tree. >> 2910 * >> 2911 * A writepage() implementation always needs to do either this, >> 2912 * or re-dirty the page with "redirty_page_for_writepage()" in >> 2913 * the case of a failure. >> 2914 * >> 2915 * Just unlocking the page will cause the radix tree tag-bits >> 2916 * to fail to update with the state of the page correctly. >> 2917 */ >> 2918 set_page_writeback(page); >> 2919 retry_write: >> 2920 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE); >> 2921 if (is_retryable_error(rc)) { >> 2922 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) >> 2923 goto retry_write; >> 2924 redirty_page_for_writepage(wbc, page); >> 2925 } else if (rc != 0) { >> 2926 SetPageError(page); >> 2927 mapping_set_error(page->mapping, rc); >> 2928 } else { >> 2929 SetPageUptodate(page); >> 2930 } >> 2931 end_page_writeback(page); >> 2932 put_page(page); >> 2933 free_xid(xid); >> 2934 return rc; >> 2935 } >> 2936 >> 2937 static int cifs_writepage(struct page *page, struct writeback_control *wbc) >> 2938 { >> 2939 int rc = cifs_writepage_locked(page, wbc); >> 2940 unlock_page(page); >> 2941 return rc; >> 2942 } >> 2943 >> 2944 static int cifs_write_end(struct file *file, struct address_space *mapping, >> 2945 loff_t pos, unsigned len, unsigned copied, >> 2946 struct page *page, void *fsdata) >> 2947 { >> 2948 int rc; >> 2949 struct inode *inode = mapping->host; >> 2950 struct cifsFileInfo *cfile = file->private_data; >> 2951 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); >> 2952 __u32 pid; >> 2953 >> 2954 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) >> 2955 pid = cfile->pid; >> 2956 else >> 2957 pid = current->tgid; >> 2958 >> 2959 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n", >> 2960 page, pos, copied); >> 2961 >> 2962 if (PageChecked(page)) { >> 2963 if (copied == len) >> 2964 SetPageUptodate(page); >> 2965 ClearPageChecked(page); >> 2966 } else if (!PageUptodate(page) && copied == PAGE_SIZE) >> 2967 SetPageUptodate(page); >> 2968 >> 2969 if (!PageUptodate(page)) { >> 2970 char *page_data; >> 2971 unsigned offset = pos & (PAGE_SIZE - 1); >> 2972 unsigned int xid; >> 2973 >> 2974 xid = get_xid(); >> 2975 /* this is probably better than directly calling >> 2976 partialpage_write since in this function the file handle is >> 2977 known which we might as well leverage */ >> 2978 /* BB check if anything else missing out of ppw >> 2979 such as updating last write time */ >> 2980 page_data = kmap(page); >> 2981 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); >> 2982 /* if (rc < 0) should we set writebehind rc? */ >> 2983 kunmap(page); >> 2984 >> 2985 free_xid(xid); >> 2986 } else { >> 2987 rc = copied; >> 2988 pos += copied; >> 2989 set_page_dirty(page); >> 2990 } >> 2991 >> 2992 if (rc > 0) { >> 2993 spin_lock(&inode->i_lock); >> 2994 if (pos > inode->i_size) { >> 2995 i_size_write(inode, pos); >> 2996 inode->i_blocks = (512 - 1 + pos) >> 9; >> 2997 } >> 2998 spin_unlock(&inode->i_lock); >> 2999 } >> 3000 >> 3001 unlock_page(page); >> 3002 put_page(page); >> 3003 /* Indication to update ctime and mtime as close is deferred */ >> 3004 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); >> 3005 >> 3006 return rc; >> 3007 } >> 3008 2620 int cifs_strict_fsync(struct file *file, loff 3009 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, 2621 int datasync) 3010 int datasync) 2622 { 3011 { 2623 unsigned int xid; 3012 unsigned int xid; 2624 int rc = 0; 3013 int rc = 0; 2625 struct cifs_tcon *tcon; 3014 struct cifs_tcon *tcon; 2626 struct TCP_Server_Info *server; 3015 struct TCP_Server_Info *server; 2627 struct cifsFileInfo *smbfile = file-> 3016 struct cifsFileInfo *smbfile = file->private_data; 2628 struct inode *inode = file_inode(file 3017 struct inode *inode = file_inode(file); 2629 struct cifs_sb_info *cifs_sb = CIFS_S 3018 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 2630 3019 2631 rc = file_write_and_wait_range(file, 3020 rc = file_write_and_wait_range(file, start, end); 2632 if (rc) { 3021 if (rc) { 2633 trace_cifs_fsync_err(inode->i 3022 trace_cifs_fsync_err(inode->i_ino, rc); 2634 return rc; 3023 return rc; 2635 } 3024 } 2636 3025 2637 xid = get_xid(); 3026 xid = get_xid(); 2638 3027 2639 cifs_dbg(FYI, "Sync file - name: %pD 3028 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", 2640 file, datasync); 3029 file, datasync); 2641 3030 2642 if (!CIFS_CACHE_READ(CIFS_I(inode))) 3031 if (!CIFS_CACHE_READ(CIFS_I(inode))) { 2643 rc = cifs_zap_mapping(inode); 3032 rc = cifs_zap_mapping(inode); 2644 if (rc) { 3033 if (rc) { 2645 cifs_dbg(FYI, "rc: %d 3034 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc); 2646 rc = 0; /* don't care 3035 rc = 0; /* don't care about it in fsync */ 2647 } 3036 } 2648 } 3037 } 2649 3038 2650 tcon = tlink_tcon(smbfile->tlink); 3039 tcon = tlink_tcon(smbfile->tlink); 2651 if (!(cifs_sb->mnt_cifs_flags & CIFS_ 3040 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { 2652 server = tcon->ses->server; 3041 server = tcon->ses->server; 2653 if (server->ops->flush == NUL 3042 if (server->ops->flush == NULL) { 2654 rc = -ENOSYS; 3043 rc = -ENOSYS; 2655 goto strict_fsync_exi 3044 goto strict_fsync_exit; 2656 } 3045 } 2657 3046 2658 if ((OPEN_FMODE(smbfile->f_fl 3047 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { 2659 smbfile = find_writab 3048 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); 2660 if (smbfile) { 3049 if (smbfile) { 2661 rc = server-> 3050 rc = server->ops->flush(xid, tcon, &smbfile->fid); 2662 cifsFileInfo_ 3051 cifsFileInfo_put(smbfile); 2663 } else 3052 } else 2664 cifs_dbg(FYI, 3053 cifs_dbg(FYI, "ignore fsync for file not open for write\n"); 2665 } else 3054 } else 2666 rc = server->ops->flu 3055 rc = server->ops->flush(xid, tcon, &smbfile->fid); 2667 } 3056 } 2668 3057 2669 strict_fsync_exit: 3058 strict_fsync_exit: 2670 free_xid(xid); 3059 free_xid(xid); 2671 return rc; 3060 return rc; 2672 } 3061 } 2673 3062 2674 /* << 2675 * Flush data on a non-strict data. << 2676 */ << 2677 int cifs_fsync(struct file *file, loff_t star 3063 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 2678 { 3064 { 2679 unsigned int xid; 3065 unsigned int xid; 2680 int rc = 0; 3066 int rc = 0; 2681 struct cifs_tcon *tcon; 3067 struct cifs_tcon *tcon; 2682 struct TCP_Server_Info *server; 3068 struct TCP_Server_Info *server; 2683 struct cifsFileInfo *smbfile = file-> 3069 struct cifsFileInfo *smbfile = file->private_data; 2684 struct inode *inode = file_inode(file 3070 struct inode *inode = file_inode(file); 2685 struct cifs_sb_info *cifs_sb = CIFS_F 3071 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); 2686 3072 2687 rc = file_write_and_wait_range(file, 3073 rc = file_write_and_wait_range(file, start, end); 2688 if (rc) { 3074 if (rc) { 2689 trace_cifs_fsync_err(file_ino 3075 trace_cifs_fsync_err(file_inode(file)->i_ino, rc); 2690 return rc; 3076 return rc; 2691 } 3077 } 2692 3078 2693 xid = get_xid(); 3079 xid = get_xid(); 2694 3080 2695 cifs_dbg(FYI, "Sync file - name: %pD 3081 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", 2696 file, datasync); 3082 file, datasync); 2697 3083 2698 tcon = tlink_tcon(smbfile->tlink); 3084 tcon = tlink_tcon(smbfile->tlink); 2699 if (!(cifs_sb->mnt_cifs_flags & CIFS_ 3085 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { 2700 server = tcon->ses->server; 3086 server = tcon->ses->server; 2701 if (server->ops->flush == NUL 3087 if (server->ops->flush == NULL) { 2702 rc = -ENOSYS; 3088 rc = -ENOSYS; 2703 goto fsync_exit; 3089 goto fsync_exit; 2704 } 3090 } 2705 3091 2706 if ((OPEN_FMODE(smbfile->f_fl 3092 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { 2707 smbfile = find_writab 3093 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); 2708 if (smbfile) { 3094 if (smbfile) { 2709 rc = server-> 3095 rc = server->ops->flush(xid, tcon, &smbfile->fid); 2710 cifsFileInfo_ 3096 cifsFileInfo_put(smbfile); 2711 } else 3097 } else 2712 cifs_dbg(FYI, 3098 cifs_dbg(FYI, "ignore fsync for file not open for write\n"); 2713 } else 3099 } else 2714 rc = server->ops->flu 3100 rc = server->ops->flush(xid, tcon, &smbfile->fid); 2715 } 3101 } 2716 3102 2717 fsync_exit: 3103 fsync_exit: 2718 free_xid(xid); 3104 free_xid(xid); 2719 return rc; 3105 return rc; 2720 } 3106 } 2721 3107 2722 /* 3108 /* 2723 * As file closes, flush all cached write dat 3109 * As file closes, flush all cached write data for this inode checking 2724 * for write behind errors. 3110 * for write behind errors. 2725 */ 3111 */ 2726 int cifs_flush(struct file *file, fl_owner_t 3112 int cifs_flush(struct file *file, fl_owner_t id) 2727 { 3113 { 2728 struct inode *inode = file_inode(file 3114 struct inode *inode = file_inode(file); 2729 int rc = 0; 3115 int rc = 0; 2730 3116 2731 if (file->f_mode & FMODE_WRITE) 3117 if (file->f_mode & FMODE_WRITE) 2732 rc = filemap_write_and_wait(i 3118 rc = filemap_write_and_wait(inode->i_mapping); 2733 3119 2734 cifs_dbg(FYI, "Flush inode %p file %p 3120 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); 2735 if (rc) { 3121 if (rc) { 2736 /* get more nuanced writeback 3122 /* get more nuanced writeback errors */ 2737 rc = filemap_check_wb_err(fil 3123 rc = filemap_check_wb_err(file->f_mapping, 0); 2738 trace_cifs_flush_err(inode->i 3124 trace_cifs_flush_err(inode->i_ino, rc); 2739 } 3125 } 2740 return rc; 3126 return rc; 2741 } 3127 } 2742 3128 >> 3129 static int >> 3130 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) >> 3131 { >> 3132 int rc = 0; >> 3133 unsigned long i; >> 3134 >> 3135 for (i = 0; i < num_pages; i++) { >> 3136 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); >> 3137 if (!pages[i]) { >> 3138 /* >> 3139 * save number of pages we have already allocated and >> 3140 * return with ENOMEM error >> 3141 */ >> 3142 num_pages = i; >> 3143 rc = -ENOMEM; >> 3144 break; >> 3145 } >> 3146 } >> 3147 >> 3148 if (rc) { >> 3149 for (i = 0; i < num_pages; i++) >> 3150 put_page(pages[i]); >> 3151 } >> 3152 return rc; >> 3153 } >> 3154 >> 3155 static inline >> 3156 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) >> 3157 { >> 3158 size_t num_pages; >> 3159 size_t clen; >> 3160 >> 3161 clen = min_t(const size_t, len, wsize); >> 3162 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE); >> 3163 >> 3164 if (cur_len) >> 3165 *cur_len = clen; >> 3166 >> 3167 return num_pages; >> 3168 } >> 3169 >> 3170 static void >> 3171 cifs_uncached_writedata_release(struct kref *refcount) >> 3172 { >> 3173 int i; >> 3174 struct cifs_writedata *wdata = container_of(refcount, >> 3175 struct cifs_writedata, refcount); >> 3176 >> 3177 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release); >> 3178 for (i = 0; i < wdata->nr_pages; i++) >> 3179 put_page(wdata->pages[i]); >> 3180 cifs_writedata_release(refcount); >> 3181 } >> 3182 >> 3183 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx); >> 3184 >> 3185 static void >> 3186 cifs_uncached_writev_complete(struct work_struct *work) >> 3187 { >> 3188 struct cifs_writedata *wdata = container_of(work, >> 3189 struct cifs_writedata, work); >> 3190 struct inode *inode = d_inode(wdata->cfile->dentry); >> 3191 struct cifsInodeInfo *cifsi = CIFS_I(inode); >> 3192 >> 3193 spin_lock(&inode->i_lock); >> 3194 cifs_update_eof(cifsi, wdata->offset, wdata->bytes); >> 3195 if (cifsi->server_eof > inode->i_size) >> 3196 i_size_write(inode, cifsi->server_eof); >> 3197 spin_unlock(&inode->i_lock); >> 3198 >> 3199 complete(&wdata->done); >> 3200 collect_uncached_write_data(wdata->ctx); >> 3201 /* the below call can possibly free the last ref to aio ctx */ >> 3202 kref_put(&wdata->refcount, cifs_uncached_writedata_release); >> 3203 } >> 3204 >> 3205 static int >> 3206 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, >> 3207 size_t *len, unsigned long *num_pages) >> 3208 { >> 3209 size_t save_len, copied, bytes, cur_len = *len; >> 3210 unsigned long i, nr_pages = *num_pages; >> 3211 >> 3212 save_len = cur_len; >> 3213 for (i = 0; i < nr_pages; i++) { >> 3214 bytes = min_t(const size_t, cur_len, PAGE_SIZE); >> 3215 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from); >> 3216 cur_len -= copied; >> 3217 /* >> 3218 * If we didn't copy as much as we expected, then that >> 3219 * may mean we trod into an unmapped area. Stop copying >> 3220 * at that point. On the next pass through the big >> 3221 * loop, we'll likely end up getting a zero-length >> 3222 * write and bailing out of it. >> 3223 */ >> 3224 if (copied < bytes) >> 3225 break; >> 3226 } >> 3227 cur_len = save_len - cur_len; >> 3228 *len = cur_len; >> 3229 >> 3230 /* >> 3231 * If we have no data to send, then that probably means that >> 3232 * the copy above failed altogether. That's most likely because >> 3233 * the address in the iovec was bogus. Return -EFAULT and let >> 3234 * the caller free anything we allocated and bail out. >> 3235 */ >> 3236 if (!cur_len) >> 3237 return -EFAULT; >> 3238 >> 3239 /* >> 3240 * i + 1 now represents the number of pages we actually used in >> 3241 * the copy phase above. >> 3242 */ >> 3243 *num_pages = i + 1; >> 3244 return 0; >> 3245 } >> 3246 >> 3247 static int >> 3248 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, >> 3249 struct cifs_aio_ctx *ctx) >> 3250 { >> 3251 unsigned int wsize; >> 3252 struct cifs_credits credits; >> 3253 int rc; >> 3254 struct TCP_Server_Info *server = wdata->server; >> 3255 >> 3256 do { >> 3257 if (wdata->cfile->invalidHandle) { >> 3258 rc = cifs_reopen_file(wdata->cfile, false); >> 3259 if (rc == -EAGAIN) >> 3260 continue; >> 3261 else if (rc) >> 3262 break; >> 3263 } >> 3264 >> 3265 >> 3266 /* >> 3267 * Wait for credits to resend this wdata. >> 3268 * Note: we are attempting to resend the whole wdata not in >> 3269 * segments >> 3270 */ >> 3271 do { >> 3272 rc = server->ops->wait_mtu_credits(server, wdata->bytes, >> 3273 &wsize, &credits); >> 3274 if (rc) >> 3275 goto fail; >> 3276 >> 3277 if (wsize < wdata->bytes) { >> 3278 add_credits_and_wake_if(server, &credits, 0); >> 3279 msleep(1000); >> 3280 } >> 3281 } while (wsize < wdata->bytes); >> 3282 wdata->credits = credits; >> 3283 >> 3284 rc = adjust_credits(server, &wdata->credits, wdata->bytes); >> 3285 >> 3286 if (!rc) { >> 3287 if (wdata->cfile->invalidHandle) >> 3288 rc = -EAGAIN; >> 3289 else { >> 3290 #ifdef CONFIG_CIFS_SMB_DIRECT >> 3291 if (wdata->mr) { >> 3292 wdata->mr->need_invalidate = true; >> 3293 smbd_deregister_mr(wdata->mr); >> 3294 wdata->mr = NULL; >> 3295 } >> 3296 #endif >> 3297 rc = server->ops->async_writev(wdata, >> 3298 cifs_uncached_writedata_release); >> 3299 } >> 3300 } >> 3301 >> 3302 /* If the write was successfully sent, we are done */ >> 3303 if (!rc) { >> 3304 list_add_tail(&wdata->list, wdata_list); >> 3305 return 0; >> 3306 } >> 3307 >> 3308 /* Roll back credits and retry if needed */ >> 3309 add_credits_and_wake_if(server, &wdata->credits, 0); >> 3310 } while (rc == -EAGAIN); >> 3311 >> 3312 fail: >> 3313 kref_put(&wdata->refcount, cifs_uncached_writedata_release); >> 3314 return rc; >> 3315 } >> 3316 >> 3317 static int >> 3318 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, >> 3319 struct cifsFileInfo *open_file, >> 3320 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list, >> 3321 struct cifs_aio_ctx *ctx) >> 3322 { >> 3323 int rc = 0; >> 3324 size_t cur_len; >> 3325 unsigned long nr_pages, num_pages, i; >> 3326 struct cifs_writedata *wdata; >> 3327 struct iov_iter saved_from = *from; >> 3328 loff_t saved_offset = offset; >> 3329 pid_t pid; >> 3330 struct TCP_Server_Info *server; >> 3331 struct page **pagevec; >> 3332 size_t start; >> 3333 unsigned int xid; >> 3334 >> 3335 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) >> 3336 pid = open_file->pid; >> 3337 else >> 3338 pid = current->tgid; >> 3339 >> 3340 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); >> 3341 xid = get_xid(); >> 3342 >> 3343 do { >> 3344 unsigned int wsize; >> 3345 struct cifs_credits credits_on_stack; >> 3346 struct cifs_credits *credits = &credits_on_stack; >> 3347 >> 3348 if (open_file->invalidHandle) { >> 3349 rc = cifs_reopen_file(open_file, false); >> 3350 if (rc == -EAGAIN) >> 3351 continue; >> 3352 else if (rc) >> 3353 break; >> 3354 } >> 3355 >> 3356 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize, >> 3357 &wsize, credits); >> 3358 if (rc) >> 3359 break; >> 3360 >> 3361 cur_len = min_t(const size_t, len, wsize); >> 3362 >> 3363 if (ctx->direct_io) { >> 3364 ssize_t result; >> 3365 >> 3366 result = iov_iter_get_pages_alloc2( >> 3367 from, &pagevec, cur_len, &start); >> 3368 if (result < 0) { >> 3369 cifs_dbg(VFS, >> 3370 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n", >> 3371 result, iov_iter_type(from), >> 3372 from->iov_offset, from->count); >> 3373 dump_stack(); >> 3374 >> 3375 rc = result; >> 3376 add_credits_and_wake_if(server, credits, 0); >> 3377 break; >> 3378 } >> 3379 cur_len = (size_t)result; >> 3380 >> 3381 nr_pages = >> 3382 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE; >> 3383 >> 3384 wdata = cifs_writedata_direct_alloc(pagevec, >> 3385 cifs_uncached_writev_complete); >> 3386 if (!wdata) { >> 3387 rc = -ENOMEM; >> 3388 for (i = 0; i < nr_pages; i++) >> 3389 put_page(pagevec[i]); >> 3390 kvfree(pagevec); >> 3391 add_credits_and_wake_if(server, credits, 0); >> 3392 break; >> 3393 } >> 3394 >> 3395 >> 3396 wdata->page_offset = start; >> 3397 wdata->tailsz = >> 3398 nr_pages > 1 ? >> 3399 cur_len - (PAGE_SIZE - start) - >> 3400 (nr_pages - 2) * PAGE_SIZE : >> 3401 cur_len; >> 3402 } else { >> 3403 nr_pages = get_numpages(wsize, len, &cur_len); >> 3404 wdata = cifs_writedata_alloc(nr_pages, >> 3405 cifs_uncached_writev_complete); >> 3406 if (!wdata) { >> 3407 rc = -ENOMEM; >> 3408 add_credits_and_wake_if(server, credits, 0); >> 3409 break; >> 3410 } >> 3411 >> 3412 rc = cifs_write_allocate_pages(wdata->pages, nr_pages); >> 3413 if (rc) { >> 3414 kvfree(wdata->pages); >> 3415 kfree(wdata); >> 3416 add_credits_and_wake_if(server, credits, 0); >> 3417 break; >> 3418 } >> 3419 >> 3420 num_pages = nr_pages; >> 3421 rc = wdata_fill_from_iovec( >> 3422 wdata, from, &cur_len, &num_pages); >> 3423 if (rc) { >> 3424 for (i = 0; i < nr_pages; i++) >> 3425 put_page(wdata->pages[i]); >> 3426 kvfree(wdata->pages); >> 3427 kfree(wdata); >> 3428 add_credits_and_wake_if(server, credits, 0); >> 3429 break; >> 3430 } >> 3431 >> 3432 /* >> 3433 * Bring nr_pages down to the number of pages we >> 3434 * actually used, and free any pages that we didn't use. >> 3435 */ >> 3436 for ( ; nr_pages > num_pages; nr_pages--) >> 3437 put_page(wdata->pages[nr_pages - 1]); >> 3438 >> 3439 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE); >> 3440 } >> 3441 >> 3442 wdata->sync_mode = WB_SYNC_ALL; >> 3443 wdata->nr_pages = nr_pages; >> 3444 wdata->offset = (__u64)offset; >> 3445 wdata->cfile = cifsFileInfo_get(open_file); >> 3446 wdata->server = server; >> 3447 wdata->pid = pid; >> 3448 wdata->bytes = cur_len; >> 3449 wdata->pagesz = PAGE_SIZE; >> 3450 wdata->credits = credits_on_stack; >> 3451 wdata->ctx = ctx; >> 3452 kref_get(&ctx->refcount); >> 3453 >> 3454 rc = adjust_credits(server, &wdata->credits, wdata->bytes); >> 3455 >> 3456 if (!rc) { >> 3457 if (wdata->cfile->invalidHandle) >> 3458 rc = -EAGAIN; >> 3459 else >> 3460 rc = server->ops->async_writev(wdata, >> 3461 cifs_uncached_writedata_release); >> 3462 } >> 3463 >> 3464 if (rc) { >> 3465 add_credits_and_wake_if(server, &wdata->credits, 0); >> 3466 kref_put(&wdata->refcount, >> 3467 cifs_uncached_writedata_release); >> 3468 if (rc == -EAGAIN) { >> 3469 *from = saved_from; >> 3470 iov_iter_advance(from, offset - saved_offset); >> 3471 continue; >> 3472 } >> 3473 break; >> 3474 } >> 3475 >> 3476 list_add_tail(&wdata->list, wdata_list); >> 3477 offset += cur_len; >> 3478 len -= cur_len; >> 3479 } while (len > 0); >> 3480 >> 3481 free_xid(xid); >> 3482 return rc; >> 3483 } >> 3484 >> 3485 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) >> 3486 { >> 3487 struct cifs_writedata *wdata, *tmp; >> 3488 struct cifs_tcon *tcon; >> 3489 struct cifs_sb_info *cifs_sb; >> 3490 struct dentry *dentry = ctx->cfile->dentry; >> 3491 ssize_t rc; >> 3492 >> 3493 tcon = tlink_tcon(ctx->cfile->tlink); >> 3494 cifs_sb = CIFS_SB(dentry->d_sb); >> 3495 >> 3496 mutex_lock(&ctx->aio_mutex); >> 3497 >> 3498 if (list_empty(&ctx->list)) { >> 3499 mutex_unlock(&ctx->aio_mutex); >> 3500 return; >> 3501 } >> 3502 >> 3503 rc = ctx->rc; >> 3504 /* >> 3505 * Wait for and collect replies for any successful sends in order of >> 3506 * increasing offset. Once an error is hit, then return without waiting >> 3507 * for any more replies. >> 3508 */ >> 3509 restart_loop: >> 3510 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) { >> 3511 if (!rc) { >> 3512 if (!try_wait_for_completion(&wdata->done)) { >> 3513 mutex_unlock(&ctx->aio_mutex); >> 3514 return; >> 3515 } >> 3516 >> 3517 if (wdata->result) >> 3518 rc = wdata->result; >> 3519 else >> 3520 ctx->total_len += wdata->bytes; >> 3521 >> 3522 /* resend call if it's a retryable error */ >> 3523 if (rc == -EAGAIN) { >> 3524 struct list_head tmp_list; >> 3525 struct iov_iter tmp_from = ctx->iter; >> 3526 >> 3527 INIT_LIST_HEAD(&tmp_list); >> 3528 list_del_init(&wdata->list); >> 3529 >> 3530 if (ctx->direct_io) >> 3531 rc = cifs_resend_wdata( >> 3532 wdata, &tmp_list, ctx); >> 3533 else { >> 3534 iov_iter_advance(&tmp_from, >> 3535 wdata->offset - ctx->pos); >> 3536 >> 3537 rc = cifs_write_from_iter(wdata->offset, >> 3538 wdata->bytes, &tmp_from, >> 3539 ctx->cfile, cifs_sb, &tmp_list, >> 3540 ctx); >> 3541 >> 3542 kref_put(&wdata->refcount, >> 3543 cifs_uncached_writedata_release); >> 3544 } >> 3545 >> 3546 list_splice(&tmp_list, &ctx->list); >> 3547 goto restart_loop; >> 3548 } >> 3549 } >> 3550 list_del_init(&wdata->list); >> 3551 kref_put(&wdata->refcount, cifs_uncached_writedata_release); >> 3552 } >> 3553 >> 3554 cifs_stats_bytes_written(tcon, ctx->total_len); >> 3555 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); >> 3556 >> 3557 ctx->rc = (rc == 0) ? ctx->total_len : rc; >> 3558 >> 3559 mutex_unlock(&ctx->aio_mutex); >> 3560 >> 3561 if (ctx->iocb && ctx->iocb->ki_complete) >> 3562 ctx->iocb->ki_complete(ctx->iocb, ctx->rc); >> 3563 else >> 3564 complete(&ctx->done); >> 3565 } >> 3566 >> 3567 static ssize_t __cifs_writev( >> 3568 struct kiocb *iocb, struct iov_iter *from, bool direct) >> 3569 { >> 3570 struct file *file = iocb->ki_filp; >> 3571 ssize_t total_written = 0; >> 3572 struct cifsFileInfo *cfile; >> 3573 struct cifs_tcon *tcon; >> 3574 struct cifs_sb_info *cifs_sb; >> 3575 struct cifs_aio_ctx *ctx; >> 3576 struct iov_iter saved_from = *from; >> 3577 size_t len = iov_iter_count(from); >> 3578 int rc; >> 3579 >> 3580 /* >> 3581 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC. >> 3582 * In this case, fall back to non-direct write function. >> 3583 * this could be improved by getting pages directly in ITER_KVEC >> 3584 */ >> 3585 if (direct && iov_iter_is_kvec(from)) { >> 3586 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n"); >> 3587 direct = false; >> 3588 } >> 3589 >> 3590 rc = generic_write_checks(iocb, from); >> 3591 if (rc <= 0) >> 3592 return rc; >> 3593 >> 3594 cifs_sb = CIFS_FILE_SB(file); >> 3595 cfile = file->private_data; >> 3596 tcon = tlink_tcon(cfile->tlink); >> 3597 >> 3598 if (!tcon->ses->server->ops->async_writev) >> 3599 return -ENOSYS; >> 3600 >> 3601 ctx = cifs_aio_ctx_alloc(); >> 3602 if (!ctx) >> 3603 return -ENOMEM; >> 3604 >> 3605 ctx->cfile = cifsFileInfo_get(cfile); >> 3606 >> 3607 if (!is_sync_kiocb(iocb)) >> 3608 ctx->iocb = iocb; >> 3609 >> 3610 ctx->pos = iocb->ki_pos; >> 3611 >> 3612 if (direct) { >> 3613 ctx->direct_io = true; >> 3614 ctx->iter = *from; >> 3615 ctx->len = len; >> 3616 } else { >> 3617 rc = setup_aio_ctx_iter(ctx, from, ITER_SOURCE); >> 3618 if (rc) { >> 3619 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 3620 return rc; >> 3621 } >> 3622 } >> 3623 >> 3624 /* grab a lock here due to read response handlers can access ctx */ >> 3625 mutex_lock(&ctx->aio_mutex); >> 3626 >> 3627 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from, >> 3628 cfile, cifs_sb, &ctx->list, ctx); >> 3629 >> 3630 /* >> 3631 * If at least one write was successfully sent, then discard any rc >> 3632 * value from the later writes. If the other write succeeds, then >> 3633 * we'll end up returning whatever was written. If it fails, then >> 3634 * we'll get a new rc value from that. >> 3635 */ >> 3636 if (!list_empty(&ctx->list)) >> 3637 rc = 0; >> 3638 >> 3639 mutex_unlock(&ctx->aio_mutex); >> 3640 >> 3641 if (rc) { >> 3642 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 3643 return rc; >> 3644 } >> 3645 >> 3646 if (!is_sync_kiocb(iocb)) { >> 3647 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 3648 return -EIOCBQUEUED; >> 3649 } >> 3650 >> 3651 rc = wait_for_completion_killable(&ctx->done); >> 3652 if (rc) { >> 3653 mutex_lock(&ctx->aio_mutex); >> 3654 ctx->rc = rc = -EINTR; >> 3655 total_written = ctx->total_len; >> 3656 mutex_unlock(&ctx->aio_mutex); >> 3657 } else { >> 3658 rc = ctx->rc; >> 3659 total_written = ctx->total_len; >> 3660 } >> 3661 >> 3662 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 3663 >> 3664 if (unlikely(!total_written)) >> 3665 return rc; >> 3666 >> 3667 iocb->ki_pos += total_written; >> 3668 return total_written; >> 3669 } >> 3670 >> 3671 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from) >> 3672 { >> 3673 struct file *file = iocb->ki_filp; >> 3674 >> 3675 cifs_revalidate_mapping(file->f_inode); >> 3676 return __cifs_writev(iocb, from, true); >> 3677 } >> 3678 >> 3679 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) >> 3680 { >> 3681 return __cifs_writev(iocb, from, false); >> 3682 } >> 3683 2743 static ssize_t 3684 static ssize_t 2744 cifs_writev(struct kiocb *iocb, struct iov_it 3685 cifs_writev(struct kiocb *iocb, struct iov_iter *from) 2745 { 3686 { 2746 struct file *file = iocb->ki_filp; 3687 struct file *file = iocb->ki_filp; 2747 struct cifsFileInfo *cfile = (struct 3688 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 2748 struct inode *inode = file->f_mapping 3689 struct inode *inode = file->f_mapping->host; 2749 struct cifsInodeInfo *cinode = CIFS_I 3690 struct cifsInodeInfo *cinode = CIFS_I(inode); 2750 struct TCP_Server_Info *server = tlin 3691 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 2751 struct cifs_sb_info *cifs_sb = CIFS_S << 2752 ssize_t rc; 3692 ssize_t rc; 2753 3693 2754 rc = netfs_start_io_write(inode); !! 3694 inode_lock(inode); 2755 if (rc < 0) << 2756 return rc; << 2757 << 2758 /* 3695 /* 2759 * We need to hold the sem to be sure 3696 * We need to hold the sem to be sure nobody modifies lock list 2760 * with a brlock that prevents writin 3697 * with a brlock that prevents writing. 2761 */ 3698 */ 2762 down_read(&cinode->lock_sem); 3699 down_read(&cinode->lock_sem); 2763 3700 2764 rc = generic_write_checks(iocb, from) 3701 rc = generic_write_checks(iocb, from); 2765 if (rc <= 0) 3702 if (rc <= 0) 2766 goto out; 3703 goto out; 2767 3704 2768 if ((cifs_sb->mnt_cifs_flags & CIFS_M !! 3705 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), 2769 (cifs_find_lock_conflict(cfile, i << 2770 server-> 3706 server->vals->exclusive_lock_type, 0, 2771 NULL, CI !! 3707 NULL, CIFS_WRITE_OP)) >> 3708 rc = __generic_file_write_iter(iocb, from); >> 3709 else 2772 rc = -EACCES; 3710 rc = -EACCES; 2773 goto out; << 2774 } << 2775 << 2776 rc = netfs_buffered_write_iter_locked << 2777 << 2778 out: 3711 out: 2779 up_read(&cinode->lock_sem); 3712 up_read(&cinode->lock_sem); 2780 netfs_end_io_write(inode); !! 3713 inode_unlock(inode); >> 3714 2781 if (rc > 0) 3715 if (rc > 0) 2782 rc = generic_write_sync(iocb, 3716 rc = generic_write_sync(iocb, rc); 2783 return rc; 3717 return rc; 2784 } 3718 } 2785 3719 2786 ssize_t 3720 ssize_t 2787 cifs_strict_writev(struct kiocb *iocb, struct 3721 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) 2788 { 3722 { 2789 struct inode *inode = file_inode(iocb 3723 struct inode *inode = file_inode(iocb->ki_filp); 2790 struct cifsInodeInfo *cinode = CIFS_I 3724 struct cifsInodeInfo *cinode = CIFS_I(inode); 2791 struct cifs_sb_info *cifs_sb = CIFS_S 3725 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 2792 struct cifsFileInfo *cfile = (struct 3726 struct cifsFileInfo *cfile = (struct cifsFileInfo *) 2793 3727 iocb->ki_filp->private_data; 2794 struct cifs_tcon *tcon = tlink_tcon(c 3728 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2795 ssize_t written; 3729 ssize_t written; 2796 3730 2797 written = cifs_get_writer(cinode); 3731 written = cifs_get_writer(cinode); 2798 if (written) 3732 if (written) 2799 return written; 3733 return written; 2800 3734 2801 if (CIFS_CACHE_WRITE(cinode)) { 3735 if (CIFS_CACHE_WRITE(cinode)) { 2802 if (cap_unix(tcon->ses) && 3736 if (cap_unix(tcon->ses) && 2803 (CIFS_UNIX_FCNTL_CAP & le !! 3737 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) 2804 ((cifs_sb->mnt_cifs_flags !! 3738 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) { 2805 written = netfs_file_ !! 3739 written = generic_file_write_iter(iocb, from); 2806 goto out; 3740 goto out; 2807 } 3741 } 2808 written = cifs_writev(iocb, f 3742 written = cifs_writev(iocb, from); 2809 goto out; 3743 goto out; 2810 } 3744 } 2811 /* 3745 /* 2812 * For non-oplocked files in strict c 3746 * For non-oplocked files in strict cache mode we need to write the data 2813 * to the server exactly from the pos 3747 * to the server exactly from the pos to pos+len-1 rather than flush all 2814 * affected pages because it may caus 3748 * affected pages because it may cause a error with mandatory locks on 2815 * these pages but not on the region 3749 * these pages but not on the region from pos to ppos+len-1. 2816 */ 3750 */ 2817 written = netfs_file_write_iter(iocb, !! 3751 written = cifs_user_writev(iocb, from); 2818 if (CIFS_CACHE_READ(cinode)) { 3752 if (CIFS_CACHE_READ(cinode)) { 2819 /* 3753 /* 2820 * We have read level caching 3754 * We have read level caching and we have just sent a write 2821 * request to the server thus 3755 * request to the server thus making data in the cache stale. 2822 * Zap the cache and set oplo 3756 * Zap the cache and set oplock/lease level to NONE to avoid 2823 * reading stale data from th 3757 * reading stale data from the cache. All subsequent read 2824 * operations will read new d 3758 * operations will read new data from the server. 2825 */ 3759 */ 2826 cifs_zap_mapping(inode); 3760 cifs_zap_mapping(inode); 2827 cifs_dbg(FYI, "Set Oplock/Lea 3761 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", 2828 inode); 3762 inode); 2829 cinode->oplock = 0; 3763 cinode->oplock = 0; 2830 } 3764 } 2831 out: 3765 out: 2832 cifs_put_writer(cinode); 3766 cifs_put_writer(cinode); 2833 return written; 3767 return written; 2834 } 3768 } 2835 3769 2836 ssize_t cifs_loose_read_iter(struct kiocb *io !! 3770 static struct cifs_readdata * >> 3771 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete) 2837 { 3772 { 2838 ssize_t rc; !! 3773 struct cifs_readdata *rdata; 2839 struct inode *inode = file_inode(iocb << 2840 3774 2841 if (iocb->ki_flags & IOCB_DIRECT) !! 3775 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL); 2842 return netfs_unbuffered_read_ !! 3776 if (rdata != NULL) { >> 3777 rdata->pages = pages; >> 3778 kref_init(&rdata->refcount); >> 3779 INIT_LIST_HEAD(&rdata->list); >> 3780 init_completion(&rdata->done); >> 3781 INIT_WORK(&rdata->work, complete); >> 3782 } 2843 3783 2844 rc = cifs_revalidate_mapping(inode); !! 3784 return rdata; 2845 if (rc) !! 3785 } 2846 return rc; !! 3786 >> 3787 static struct cifs_readdata * >> 3788 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete) >> 3789 { >> 3790 struct page **pages = >> 3791 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); >> 3792 struct cifs_readdata *ret = NULL; 2847 3793 2848 return netfs_file_read_iter(iocb, ite !! 3794 if (pages) { >> 3795 ret = cifs_readdata_direct_alloc(pages, complete); >> 3796 if (!ret) >> 3797 kfree(pages); >> 3798 } >> 3799 >> 3800 return ret; 2849 } 3801 } 2850 3802 2851 ssize_t cifs_file_write_iter(struct kiocb *io !! 3803 void >> 3804 cifs_readdata_release(struct kref *refcount) 2852 { 3805 { 2853 struct inode *inode = file_inode(iocb !! 3806 struct cifs_readdata *rdata = container_of(refcount, 2854 struct cifsInodeInfo *cinode = CIFS_I !! 3807 struct cifs_readdata, refcount); 2855 ssize_t written; !! 3808 #ifdef CONFIG_CIFS_SMB_DIRECT 2856 int rc; !! 3809 if (rdata->mr) { >> 3810 smbd_deregister_mr(rdata->mr); >> 3811 rdata->mr = NULL; >> 3812 } >> 3813 #endif >> 3814 if (rdata->cfile) >> 3815 cifsFileInfo_put(rdata->cfile); 2857 3816 2858 if (iocb->ki_filp->f_flags & O_DIRECT !! 3817 kvfree(rdata->pages); 2859 written = netfs_unbuffered_wr !! 3818 kfree(rdata); 2860 if (written > 0 && CIFS_CACHE !! 3819 } 2861 cifs_zap_mapping(inod !! 3820 2862 cifs_dbg(FYI, !! 3821 static int 2863 "Set no oplo !! 3822 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages) 2864 inode); !! 3823 { 2865 cinode->oplock = 0; !! 3824 int rc = 0; >> 3825 struct page *page; >> 3826 unsigned int i; >> 3827 >> 3828 for (i = 0; i < nr_pages; i++) { >> 3829 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); >> 3830 if (!page) { >> 3831 rc = -ENOMEM; >> 3832 break; 2866 } 3833 } 2867 return written; !! 3834 rdata->pages[i] = page; 2868 } 3835 } 2869 3836 2870 written = cifs_get_writer(cinode); !! 3837 if (rc) { 2871 if (written) !! 3838 unsigned int nr_page_failed = i; 2872 return written; << 2873 3839 2874 written = netfs_file_write_iter(iocb, !! 3840 for (i = 0; i < nr_page_failed; i++) { >> 3841 put_page(rdata->pages[i]); >> 3842 rdata->pages[i] = NULL; >> 3843 } >> 3844 } >> 3845 return rc; >> 3846 } 2875 3847 2876 if (!CIFS_CACHE_WRITE(CIFS_I(inode))) !! 3848 static void 2877 rc = filemap_fdatawrite(inode !! 3849 cifs_uncached_readdata_release(struct kref *refcount) >> 3850 { >> 3851 struct cifs_readdata *rdata = container_of(refcount, >> 3852 struct cifs_readdata, refcount); >> 3853 unsigned int i; >> 3854 >> 3855 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); >> 3856 for (i = 0; i < rdata->nr_pages; i++) { >> 3857 put_page(rdata->pages[i]); >> 3858 } >> 3859 cifs_readdata_release(refcount); >> 3860 } >> 3861 >> 3862 /** >> 3863 * cifs_readdata_to_iov - copy data from pages in response to an iovec >> 3864 * @rdata: the readdata response with list of pages holding data >> 3865 * @iter: destination for our data >> 3866 * >> 3867 * This function copies data from a list of pages in a readdata response into >> 3868 * an array of iovecs. It will first calculate where the data should go >> 3869 * based on the info in the readdata and then copy the data into that spot. >> 3870 */ >> 3871 static int >> 3872 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter) >> 3873 { >> 3874 size_t remaining = rdata->got_bytes; >> 3875 unsigned int i; >> 3876 >> 3877 for (i = 0; i < rdata->nr_pages; i++) { >> 3878 struct page *page = rdata->pages[i]; >> 3879 size_t copy = min_t(size_t, remaining, PAGE_SIZE); >> 3880 size_t written; >> 3881 >> 3882 if (unlikely(iov_iter_is_pipe(iter))) { >> 3883 void *addr = kmap_atomic(page); >> 3884 >> 3885 written = copy_to_iter(addr, copy, iter); >> 3886 kunmap_atomic(addr); >> 3887 } else >> 3888 written = copy_page_to_iter(page, 0, copy, iter); >> 3889 remaining -= written; >> 3890 if (written < copy && iov_iter_count(iter) > 0) >> 3891 break; >> 3892 } >> 3893 return remaining ? -EFAULT : 0; >> 3894 } >> 3895 >> 3896 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx); >> 3897 >> 3898 static void >> 3899 cifs_uncached_readv_complete(struct work_struct *work) >> 3900 { >> 3901 struct cifs_readdata *rdata = container_of(work, >> 3902 struct cifs_readdata, work); >> 3903 >> 3904 complete(&rdata->done); >> 3905 collect_uncached_read_data(rdata->ctx); >> 3906 /* the below call can possibly free the last ref to aio ctx */ >> 3907 kref_put(&rdata->refcount, cifs_uncached_readdata_release); >> 3908 } >> 3909 >> 3910 static int >> 3911 uncached_fill_pages(struct TCP_Server_Info *server, >> 3912 struct cifs_readdata *rdata, struct iov_iter *iter, >> 3913 unsigned int len) >> 3914 { >> 3915 int result = 0; >> 3916 unsigned int i; >> 3917 unsigned int nr_pages = rdata->nr_pages; >> 3918 unsigned int page_offset = rdata->page_offset; >> 3919 >> 3920 rdata->got_bytes = 0; >> 3921 rdata->tailsz = PAGE_SIZE; >> 3922 for (i = 0; i < nr_pages; i++) { >> 3923 struct page *page = rdata->pages[i]; >> 3924 size_t n; >> 3925 unsigned int segment_size = rdata->pagesz; >> 3926 >> 3927 if (i == 0) >> 3928 segment_size -= page_offset; >> 3929 else >> 3930 page_offset = 0; >> 3931 >> 3932 >> 3933 if (len <= 0) { >> 3934 /* no need to hold page hostage */ >> 3935 rdata->pages[i] = NULL; >> 3936 rdata->nr_pages--; >> 3937 put_page(page); >> 3938 continue; >> 3939 } >> 3940 >> 3941 n = len; >> 3942 if (len >= segment_size) >> 3943 /* enough data to fill the page */ >> 3944 n = segment_size; >> 3945 else >> 3946 rdata->tailsz = len; >> 3947 len -= n; >> 3948 >> 3949 if (iter) >> 3950 result = copy_page_from_iter( >> 3951 page, page_offset, n, iter); >> 3952 #ifdef CONFIG_CIFS_SMB_DIRECT >> 3953 else if (rdata->mr) >> 3954 result = n; >> 3955 #endif >> 3956 else >> 3957 result = cifs_read_page_from_socket( >> 3958 server, page, page_offset, n); >> 3959 if (result < 0) >> 3960 break; >> 3961 >> 3962 rdata->got_bytes += result; >> 3963 } >> 3964 >> 3965 return result != -ECONNABORTED && rdata->got_bytes > 0 ? >> 3966 rdata->got_bytes : result; >> 3967 } >> 3968 >> 3969 static int >> 3970 cifs_uncached_read_into_pages(struct TCP_Server_Info *server, >> 3971 struct cifs_readdata *rdata, unsigned int len) >> 3972 { >> 3973 return uncached_fill_pages(server, rdata, NULL, len); >> 3974 } >> 3975 >> 3976 static int >> 3977 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server, >> 3978 struct cifs_readdata *rdata, >> 3979 struct iov_iter *iter) >> 3980 { >> 3981 return uncached_fill_pages(server, rdata, iter, iter->count); >> 3982 } >> 3983 >> 3984 static int cifs_resend_rdata(struct cifs_readdata *rdata, >> 3985 struct list_head *rdata_list, >> 3986 struct cifs_aio_ctx *ctx) >> 3987 { >> 3988 unsigned int rsize; >> 3989 struct cifs_credits credits; >> 3990 int rc; >> 3991 struct TCP_Server_Info *server; >> 3992 >> 3993 /* XXX: should we pick a new channel here? */ >> 3994 server = rdata->server; >> 3995 >> 3996 do { >> 3997 if (rdata->cfile->invalidHandle) { >> 3998 rc = cifs_reopen_file(rdata->cfile, true); >> 3999 if (rc == -EAGAIN) >> 4000 continue; >> 4001 else if (rc) >> 4002 break; >> 4003 } >> 4004 >> 4005 /* >> 4006 * Wait for credits to resend this rdata. >> 4007 * Note: we are attempting to resend the whole rdata not in >> 4008 * segments >> 4009 */ >> 4010 do { >> 4011 rc = server->ops->wait_mtu_credits(server, rdata->bytes, >> 4012 &rsize, &credits); >> 4013 >> 4014 if (rc) >> 4015 goto fail; >> 4016 >> 4017 if (rsize < rdata->bytes) { >> 4018 add_credits_and_wake_if(server, &credits, 0); >> 4019 msleep(1000); >> 4020 } >> 4021 } while (rsize < rdata->bytes); >> 4022 rdata->credits = credits; >> 4023 >> 4024 rc = adjust_credits(server, &rdata->credits, rdata->bytes); >> 4025 if (!rc) { >> 4026 if (rdata->cfile->invalidHandle) >> 4027 rc = -EAGAIN; >> 4028 else { >> 4029 #ifdef CONFIG_CIFS_SMB_DIRECT >> 4030 if (rdata->mr) { >> 4031 rdata->mr->need_invalidate = true; >> 4032 smbd_deregister_mr(rdata->mr); >> 4033 rdata->mr = NULL; >> 4034 } >> 4035 #endif >> 4036 rc = server->ops->async_readv(rdata); >> 4037 } >> 4038 } >> 4039 >> 4040 /* If the read was successfully sent, we are done */ >> 4041 if (!rc) { >> 4042 /* Add to aio pending list */ >> 4043 list_add_tail(&rdata->list, rdata_list); >> 4044 return 0; >> 4045 } >> 4046 >> 4047 /* Roll back credits and retry if needed */ >> 4048 add_credits_and_wake_if(server, &rdata->credits, 0); >> 4049 } while (rc == -EAGAIN); >> 4050 >> 4051 fail: >> 4052 kref_put(&rdata->refcount, cifs_uncached_readdata_release); >> 4053 return rc; >> 4054 } >> 4055 >> 4056 static int >> 4057 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file, >> 4058 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list, >> 4059 struct cifs_aio_ctx *ctx) >> 4060 { >> 4061 struct cifs_readdata *rdata; >> 4062 unsigned int npages, rsize; >> 4063 struct cifs_credits credits_on_stack; >> 4064 struct cifs_credits *credits = &credits_on_stack; >> 4065 size_t cur_len; >> 4066 int rc; >> 4067 pid_t pid; >> 4068 struct TCP_Server_Info *server; >> 4069 struct page **pagevec; >> 4070 size_t start; >> 4071 struct iov_iter direct_iov = ctx->iter; >> 4072 >> 4073 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); >> 4074 >> 4075 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) >> 4076 pid = open_file->pid; >> 4077 else >> 4078 pid = current->tgid; >> 4079 >> 4080 if (ctx->direct_io) >> 4081 iov_iter_advance(&direct_iov, offset - ctx->pos); >> 4082 >> 4083 do { >> 4084 if (open_file->invalidHandle) { >> 4085 rc = cifs_reopen_file(open_file, true); >> 4086 if (rc == -EAGAIN) >> 4087 continue; >> 4088 else if (rc) >> 4089 break; >> 4090 } >> 4091 >> 4092 if (cifs_sb->ctx->rsize == 0) >> 4093 cifs_sb->ctx->rsize = >> 4094 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink), >> 4095 cifs_sb->ctx); >> 4096 >> 4097 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize, >> 4098 &rsize, credits); 2878 if (rc) 4099 if (rc) 2879 cifs_dbg(FYI, "cifs_f !! 4100 break; 2880 rc, inode); !! 4101 >> 4102 cur_len = min_t(const size_t, len, rsize); >> 4103 >> 4104 if (ctx->direct_io) { >> 4105 ssize_t result; >> 4106 >> 4107 result = iov_iter_get_pages_alloc2( >> 4108 &direct_iov, &pagevec, >> 4109 cur_len, &start); >> 4110 if (result < 0) { >> 4111 cifs_dbg(VFS, >> 4112 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n", >> 4113 result, iov_iter_type(&direct_iov), >> 4114 direct_iov.iov_offset, >> 4115 direct_iov.count); >> 4116 dump_stack(); >> 4117 >> 4118 rc = result; >> 4119 add_credits_and_wake_if(server, credits, 0); >> 4120 break; >> 4121 } >> 4122 cur_len = (size_t)result; >> 4123 >> 4124 rdata = cifs_readdata_direct_alloc( >> 4125 pagevec, cifs_uncached_readv_complete); >> 4126 if (!rdata) { >> 4127 add_credits_and_wake_if(server, credits, 0); >> 4128 rc = -ENOMEM; >> 4129 break; >> 4130 } >> 4131 >> 4132 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE; >> 4133 rdata->page_offset = start; >> 4134 rdata->tailsz = npages > 1 ? >> 4135 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE : >> 4136 cur_len; >> 4137 >> 4138 } else { >> 4139 >> 4140 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE); >> 4141 /* allocate a readdata struct */ >> 4142 rdata = cifs_readdata_alloc(npages, >> 4143 cifs_uncached_readv_complete); >> 4144 if (!rdata) { >> 4145 add_credits_and_wake_if(server, credits, 0); >> 4146 rc = -ENOMEM; >> 4147 break; >> 4148 } >> 4149 >> 4150 rc = cifs_read_allocate_pages(rdata, npages); >> 4151 if (rc) { >> 4152 kvfree(rdata->pages); >> 4153 kfree(rdata); >> 4154 add_credits_and_wake_if(server, credits, 0); >> 4155 break; >> 4156 } >> 4157 >> 4158 rdata->tailsz = PAGE_SIZE; >> 4159 } >> 4160 >> 4161 rdata->server = server; >> 4162 rdata->cfile = cifsFileInfo_get(open_file); >> 4163 rdata->nr_pages = npages; >> 4164 rdata->offset = offset; >> 4165 rdata->bytes = cur_len; >> 4166 rdata->pid = pid; >> 4167 rdata->pagesz = PAGE_SIZE; >> 4168 rdata->read_into_pages = cifs_uncached_read_into_pages; >> 4169 rdata->copy_into_pages = cifs_uncached_copy_into_pages; >> 4170 rdata->credits = credits_on_stack; >> 4171 rdata->ctx = ctx; >> 4172 kref_get(&ctx->refcount); >> 4173 >> 4174 rc = adjust_credits(server, &rdata->credits, rdata->bytes); >> 4175 >> 4176 if (!rc) { >> 4177 if (rdata->cfile->invalidHandle) >> 4178 rc = -EAGAIN; >> 4179 else >> 4180 rc = server->ops->async_readv(rdata); >> 4181 } >> 4182 >> 4183 if (rc) { >> 4184 add_credits_and_wake_if(server, &rdata->credits, 0); >> 4185 kref_put(&rdata->refcount, >> 4186 cifs_uncached_readdata_release); >> 4187 if (rc == -EAGAIN) { >> 4188 iov_iter_revert(&direct_iov, cur_len); >> 4189 continue; >> 4190 } >> 4191 break; >> 4192 } >> 4193 >> 4194 list_add_tail(&rdata->list, rdata_list); >> 4195 offset += cur_len; >> 4196 len -= cur_len; >> 4197 } while (len > 0); >> 4198 >> 4199 return rc; >> 4200 } >> 4201 >> 4202 static void >> 4203 collect_uncached_read_data(struct cifs_aio_ctx *ctx) >> 4204 { >> 4205 struct cifs_readdata *rdata, *tmp; >> 4206 struct iov_iter *to = &ctx->iter; >> 4207 struct cifs_sb_info *cifs_sb; >> 4208 int rc; >> 4209 >> 4210 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb); >> 4211 >> 4212 mutex_lock(&ctx->aio_mutex); >> 4213 >> 4214 if (list_empty(&ctx->list)) { >> 4215 mutex_unlock(&ctx->aio_mutex); >> 4216 return; 2881 } 4217 } 2882 4218 2883 cifs_put_writer(cinode); !! 4219 rc = ctx->rc; 2884 return written; !! 4220 /* the loop below should proceed in the order of increasing offsets */ >> 4221 again: >> 4222 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) { >> 4223 if (!rc) { >> 4224 if (!try_wait_for_completion(&rdata->done)) { >> 4225 mutex_unlock(&ctx->aio_mutex); >> 4226 return; >> 4227 } >> 4228 >> 4229 if (rdata->result == -EAGAIN) { >> 4230 /* resend call if it's a retryable error */ >> 4231 struct list_head tmp_list; >> 4232 unsigned int got_bytes = rdata->got_bytes; >> 4233 >> 4234 list_del_init(&rdata->list); >> 4235 INIT_LIST_HEAD(&tmp_list); >> 4236 >> 4237 /* >> 4238 * Got a part of data and then reconnect has >> 4239 * happened -- fill the buffer and continue >> 4240 * reading. >> 4241 */ >> 4242 if (got_bytes && got_bytes < rdata->bytes) { >> 4243 rc = 0; >> 4244 if (!ctx->direct_io) >> 4245 rc = cifs_readdata_to_iov(rdata, to); >> 4246 if (rc) { >> 4247 kref_put(&rdata->refcount, >> 4248 cifs_uncached_readdata_release); >> 4249 continue; >> 4250 } >> 4251 } >> 4252 >> 4253 if (ctx->direct_io) { >> 4254 /* >> 4255 * Re-use rdata as this is a >> 4256 * direct I/O >> 4257 */ >> 4258 rc = cifs_resend_rdata( >> 4259 rdata, >> 4260 &tmp_list, ctx); >> 4261 } else { >> 4262 rc = cifs_send_async_read( >> 4263 rdata->offset + got_bytes, >> 4264 rdata->bytes - got_bytes, >> 4265 rdata->cfile, cifs_sb, >> 4266 &tmp_list, ctx); >> 4267 >> 4268 kref_put(&rdata->refcount, >> 4269 cifs_uncached_readdata_release); >> 4270 } >> 4271 >> 4272 list_splice(&tmp_list, &ctx->list); >> 4273 >> 4274 goto again; >> 4275 } else if (rdata->result) >> 4276 rc = rdata->result; >> 4277 else if (!ctx->direct_io) >> 4278 rc = cifs_readdata_to_iov(rdata, to); >> 4279 >> 4280 /* if there was a short read -- discard anything left */ >> 4281 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes) >> 4282 rc = -ENODATA; >> 4283 >> 4284 ctx->total_len += rdata->got_bytes; >> 4285 } >> 4286 list_del_init(&rdata->list); >> 4287 kref_put(&rdata->refcount, cifs_uncached_readdata_release); >> 4288 } >> 4289 >> 4290 if (!ctx->direct_io) >> 4291 ctx->total_len = ctx->len - iov_iter_count(to); >> 4292 >> 4293 /* mask nodata case */ >> 4294 if (rc == -ENODATA) >> 4295 rc = 0; >> 4296 >> 4297 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc; >> 4298 >> 4299 mutex_unlock(&ctx->aio_mutex); >> 4300 >> 4301 if (ctx->iocb && ctx->iocb->ki_complete) >> 4302 ctx->iocb->ki_complete(ctx->iocb, ctx->rc); >> 4303 else >> 4304 complete(&ctx->done); >> 4305 } >> 4306 >> 4307 static ssize_t __cifs_readv( >> 4308 struct kiocb *iocb, struct iov_iter *to, bool direct) >> 4309 { >> 4310 size_t len; >> 4311 struct file *file = iocb->ki_filp; >> 4312 struct cifs_sb_info *cifs_sb; >> 4313 struct cifsFileInfo *cfile; >> 4314 struct cifs_tcon *tcon; >> 4315 ssize_t rc, total_read = 0; >> 4316 loff_t offset = iocb->ki_pos; >> 4317 struct cifs_aio_ctx *ctx; >> 4318 >> 4319 /* >> 4320 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC, >> 4321 * fall back to data copy read path >> 4322 * this could be improved by getting pages directly in ITER_KVEC >> 4323 */ >> 4324 if (direct && iov_iter_is_kvec(to)) { >> 4325 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n"); >> 4326 direct = false; >> 4327 } >> 4328 >> 4329 len = iov_iter_count(to); >> 4330 if (!len) >> 4331 return 0; >> 4332 >> 4333 cifs_sb = CIFS_FILE_SB(file); >> 4334 cfile = file->private_data; >> 4335 tcon = tlink_tcon(cfile->tlink); >> 4336 >> 4337 if (!tcon->ses->server->ops->async_readv) >> 4338 return -ENOSYS; >> 4339 >> 4340 if ((file->f_flags & O_ACCMODE) == O_WRONLY) >> 4341 cifs_dbg(FYI, "attempting read on write only file instance\n"); >> 4342 >> 4343 ctx = cifs_aio_ctx_alloc(); >> 4344 if (!ctx) >> 4345 return -ENOMEM; >> 4346 >> 4347 ctx->cfile = cifsFileInfo_get(cfile); >> 4348 >> 4349 if (!is_sync_kiocb(iocb)) >> 4350 ctx->iocb = iocb; >> 4351 >> 4352 if (user_backed_iter(to)) >> 4353 ctx->should_dirty = true; >> 4354 >> 4355 if (direct) { >> 4356 ctx->pos = offset; >> 4357 ctx->direct_io = true; >> 4358 ctx->iter = *to; >> 4359 ctx->len = len; >> 4360 } else { >> 4361 rc = setup_aio_ctx_iter(ctx, to, ITER_DEST); >> 4362 if (rc) { >> 4363 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 4364 return rc; >> 4365 } >> 4366 len = ctx->len; >> 4367 } >> 4368 >> 4369 if (direct) { >> 4370 rc = filemap_write_and_wait_range(file->f_inode->i_mapping, >> 4371 offset, offset + len - 1); >> 4372 if (rc) { >> 4373 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 4374 return -EAGAIN; >> 4375 } >> 4376 } >> 4377 >> 4378 /* grab a lock here due to read response handlers can access ctx */ >> 4379 mutex_lock(&ctx->aio_mutex); >> 4380 >> 4381 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx); >> 4382 >> 4383 /* if at least one read request send succeeded, then reset rc */ >> 4384 if (!list_empty(&ctx->list)) >> 4385 rc = 0; >> 4386 >> 4387 mutex_unlock(&ctx->aio_mutex); >> 4388 >> 4389 if (rc) { >> 4390 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 4391 return rc; >> 4392 } >> 4393 >> 4394 if (!is_sync_kiocb(iocb)) { >> 4395 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 4396 return -EIOCBQUEUED; >> 4397 } >> 4398 >> 4399 rc = wait_for_completion_killable(&ctx->done); >> 4400 if (rc) { >> 4401 mutex_lock(&ctx->aio_mutex); >> 4402 ctx->rc = rc = -EINTR; >> 4403 total_read = ctx->total_len; >> 4404 mutex_unlock(&ctx->aio_mutex); >> 4405 } else { >> 4406 rc = ctx->rc; >> 4407 total_read = ctx->total_len; >> 4408 } >> 4409 >> 4410 kref_put(&ctx->refcount, cifs_aio_ctx_release); >> 4411 >> 4412 if (total_read) { >> 4413 iocb->ki_pos += total_read; >> 4414 return total_read; >> 4415 } >> 4416 return rc; >> 4417 } >> 4418 >> 4419 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to) >> 4420 { >> 4421 return __cifs_readv(iocb, to, true); >> 4422 } >> 4423 >> 4424 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to) >> 4425 { >> 4426 return __cifs_readv(iocb, to, false); 2885 } 4427 } 2886 4428 2887 ssize_t 4429 ssize_t 2888 cifs_strict_readv(struct kiocb *iocb, struct 4430 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) 2889 { 4431 { 2890 struct inode *inode = file_inode(iocb 4432 struct inode *inode = file_inode(iocb->ki_filp); 2891 struct cifsInodeInfo *cinode = CIFS_I 4433 struct cifsInodeInfo *cinode = CIFS_I(inode); 2892 struct cifs_sb_info *cifs_sb = CIFS_S 4434 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 2893 struct cifsFileInfo *cfile = (struct 4435 struct cifsFileInfo *cfile = (struct cifsFileInfo *) 2894 4436 iocb->ki_filp->private_data; 2895 struct cifs_tcon *tcon = tlink_tcon(c 4437 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 2896 int rc = -EACCES; 4438 int rc = -EACCES; 2897 4439 2898 /* 4440 /* 2899 * In strict cache mode we need to re 4441 * In strict cache mode we need to read from the server all the time 2900 * if we don't have level II oplock b 4442 * if we don't have level II oplock because the server can delay mtime 2901 * change - so we can't make a decisi 4443 * change - so we can't make a decision about inode invalidating. 2902 * And we can also fail with pageread 4444 * And we can also fail with pagereading if there are mandatory locks 2903 * on pages affected by this read but 4445 * on pages affected by this read but not on the region from pos to 2904 * pos+len-1. 4446 * pos+len-1. 2905 */ 4447 */ 2906 if (!CIFS_CACHE_READ(cinode)) 4448 if (!CIFS_CACHE_READ(cinode)) 2907 return netfs_unbuffered_read_ !! 4449 return cifs_user_readv(iocb, to); 2908 4450 2909 if ((cifs_sb->mnt_cifs_flags & CIFS_M !! 4451 if (cap_unix(tcon->ses) && 2910 if (iocb->ki_flags & IOCB_DIR !! 4452 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 2911 return netfs_unbuffer !! 4453 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 2912 return netfs_buffered_read_it !! 4454 return generic_file_read_iter(iocb, to); 2913 } << 2914 4455 2915 /* 4456 /* 2916 * We need to hold the sem to be sure 4457 * We need to hold the sem to be sure nobody modifies lock list 2917 * with a brlock that prevents readin 4458 * with a brlock that prevents reading. 2918 */ 4459 */ 2919 if (iocb->ki_flags & IOCB_DIRECT) { !! 4460 down_read(&cinode->lock_sem); 2920 rc = netfs_start_io_direct(in !! 4461 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to), 2921 if (rc < 0) !! 4462 tcon->ses->server->vals->shared_lock_type, 2922 goto out; !! 4463 0, NULL, CIFS_READ_OP)) 2923 rc = -EACCES; !! 4464 rc = generic_file_read_iter(iocb, to); 2924 down_read(&cinode->lock_sem); !! 4465 up_read(&cinode->lock_sem); 2925 if (!cifs_find_lock_conflict( << 2926 cfile, iocb->ki_p << 2927 tcon->ses->server << 2928 0, NULL, CIFS_REA << 2929 rc = netfs_unbuffered << 2930 up_read(&cinode->lock_sem); << 2931 netfs_end_io_direct(inode); << 2932 } else { << 2933 rc = netfs_start_io_read(inod << 2934 if (rc < 0) << 2935 goto out; << 2936 rc = -EACCES; << 2937 down_read(&cinode->lock_sem); << 2938 if (!cifs_find_lock_conflict( << 2939 cfile, iocb->ki_p << 2940 tcon->ses->server << 2941 0, NULL, CIFS_REA << 2942 rc = filemap_read(ioc << 2943 up_read(&cinode->lock_sem); << 2944 netfs_end_io_read(inode); << 2945 } << 2946 out: << 2947 return rc; 4466 return rc; 2948 } 4467 } 2949 4468 2950 static vm_fault_t cifs_page_mkwrite(struct vm !! 4469 static ssize_t >> 4470 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset) 2951 { 4471 { 2952 return netfs_page_mkwrite(vmf, NULL); !! 4472 int rc = -EACCES; >> 4473 unsigned int bytes_read = 0; >> 4474 unsigned int total_read; >> 4475 unsigned int current_read_size; >> 4476 unsigned int rsize; >> 4477 struct cifs_sb_info *cifs_sb; >> 4478 struct cifs_tcon *tcon; >> 4479 struct TCP_Server_Info *server; >> 4480 unsigned int xid; >> 4481 char *cur_offset; >> 4482 struct cifsFileInfo *open_file; >> 4483 struct cifs_io_parms io_parms = {0}; >> 4484 int buf_type = CIFS_NO_BUFFER; >> 4485 __u32 pid; >> 4486 >> 4487 xid = get_xid(); >> 4488 cifs_sb = CIFS_FILE_SB(file); >> 4489 >> 4490 /* FIXME: set up handlers for larger reads and/or convert to async */ >> 4491 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize); >> 4492 >> 4493 if (file->private_data == NULL) { >> 4494 rc = -EBADF; >> 4495 free_xid(xid); >> 4496 return rc; >> 4497 } >> 4498 open_file = file->private_data; >> 4499 tcon = tlink_tcon(open_file->tlink); >> 4500 server = cifs_pick_channel(tcon->ses); >> 4501 >> 4502 if (!server->ops->sync_read) { >> 4503 free_xid(xid); >> 4504 return -ENOSYS; >> 4505 } >> 4506 >> 4507 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) >> 4508 pid = open_file->pid; >> 4509 else >> 4510 pid = current->tgid; >> 4511 >> 4512 if ((file->f_flags & O_ACCMODE) == O_WRONLY) >> 4513 cifs_dbg(FYI, "attempting read on write only file instance\n"); >> 4514 >> 4515 for (total_read = 0, cur_offset = read_data; read_size > total_read; >> 4516 total_read += bytes_read, cur_offset += bytes_read) { >> 4517 do { >> 4518 current_read_size = min_t(uint, read_size - total_read, >> 4519 rsize); >> 4520 /* >> 4521 * For windows me and 9x we do not want to request more >> 4522 * than it negotiated since it will refuse the read >> 4523 * then. >> 4524 */ >> 4525 if (!(tcon->ses->capabilities & >> 4526 tcon->ses->server->vals->cap_large_files)) { >> 4527 current_read_size = min_t(uint, >> 4528 current_read_size, CIFSMaxBufSize); >> 4529 } >> 4530 if (open_file->invalidHandle) { >> 4531 rc = cifs_reopen_file(open_file, true); >> 4532 if (rc != 0) >> 4533 break; >> 4534 } >> 4535 io_parms.pid = pid; >> 4536 io_parms.tcon = tcon; >> 4537 io_parms.offset = *offset; >> 4538 io_parms.length = current_read_size; >> 4539 io_parms.server = server; >> 4540 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms, >> 4541 &bytes_read, &cur_offset, >> 4542 &buf_type); >> 4543 } while (rc == -EAGAIN); >> 4544 >> 4545 if (rc || (bytes_read == 0)) { >> 4546 if (total_read) { >> 4547 break; >> 4548 } else { >> 4549 free_xid(xid); >> 4550 return rc; >> 4551 } >> 4552 } else { >> 4553 cifs_stats_bytes_read(tcon, total_read); >> 4554 *offset += bytes_read; >> 4555 } >> 4556 } >> 4557 free_xid(xid); >> 4558 return total_read; >> 4559 } >> 4560 >> 4561 /* >> 4562 * If the page is mmap'ed into a process' page tables, then we need to make >> 4563 * sure that it doesn't change while being written back. >> 4564 */ >> 4565 static vm_fault_t >> 4566 cifs_page_mkwrite(struct vm_fault *vmf) >> 4567 { >> 4568 struct page *page = vmf->page; >> 4569 >> 4570 /* Wait for the page to be written to the cache before we allow it to >> 4571 * be modified. We then assume the entire page will need writing back. >> 4572 */ >> 4573 #ifdef CONFIG_CIFS_FSCACHE >> 4574 if (PageFsCache(page) && >> 4575 wait_on_page_fscache_killable(page) < 0) >> 4576 return VM_FAULT_RETRY; >> 4577 #endif >> 4578 >> 4579 wait_on_page_writeback(page); >> 4580 >> 4581 if (lock_page_killable(page) < 0) >> 4582 return VM_FAULT_RETRY; >> 4583 return VM_FAULT_LOCKED; 2953 } 4584 } 2954 4585 2955 static const struct vm_operations_struct cifs 4586 static const struct vm_operations_struct cifs_file_vm_ops = { 2956 .fault = filemap_fault, 4587 .fault = filemap_fault, 2957 .map_pages = filemap_map_pages, 4588 .map_pages = filemap_map_pages, 2958 .page_mkwrite = cifs_page_mkwrite, 4589 .page_mkwrite = cifs_page_mkwrite, 2959 }; 4590 }; 2960 4591 2961 int cifs_file_strict_mmap(struct file *file, 4592 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) 2962 { 4593 { 2963 int xid, rc = 0; 4594 int xid, rc = 0; 2964 struct inode *inode = file_inode(file 4595 struct inode *inode = file_inode(file); 2965 4596 2966 xid = get_xid(); 4597 xid = get_xid(); 2967 4598 2968 if (!CIFS_CACHE_READ(CIFS_I(inode))) 4599 if (!CIFS_CACHE_READ(CIFS_I(inode))) 2969 rc = cifs_zap_mapping(inode); 4600 rc = cifs_zap_mapping(inode); 2970 if (!rc) 4601 if (!rc) 2971 rc = generic_file_mmap(file, 4602 rc = generic_file_mmap(file, vma); 2972 if (!rc) 4603 if (!rc) 2973 vma->vm_ops = &cifs_file_vm_o 4604 vma->vm_ops = &cifs_file_vm_ops; 2974 4605 2975 free_xid(xid); 4606 free_xid(xid); 2976 return rc; 4607 return rc; 2977 } 4608 } 2978 4609 2979 int cifs_file_mmap(struct file *file, struct 4610 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) 2980 { 4611 { 2981 int rc, xid; 4612 int rc, xid; 2982 4613 2983 xid = get_xid(); 4614 xid = get_xid(); 2984 4615 2985 rc = cifs_revalidate_file(file); 4616 rc = cifs_revalidate_file(file); 2986 if (rc) 4617 if (rc) 2987 cifs_dbg(FYI, "Validation pri 4618 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", 2988 rc); 4619 rc); 2989 if (!rc) 4620 if (!rc) 2990 rc = generic_file_mmap(file, 4621 rc = generic_file_mmap(file, vma); 2991 if (!rc) 4622 if (!rc) 2992 vma->vm_ops = &cifs_file_vm_o 4623 vma->vm_ops = &cifs_file_vm_ops; 2993 4624 2994 free_xid(xid); 4625 free_xid(xid); 2995 return rc; 4626 return rc; 2996 } 4627 } 2997 4628 >> 4629 static void >> 4630 cifs_readv_complete(struct work_struct *work) >> 4631 { >> 4632 unsigned int i, got_bytes; >> 4633 struct cifs_readdata *rdata = container_of(work, >> 4634 struct cifs_readdata, work); >> 4635 >> 4636 got_bytes = rdata->got_bytes; >> 4637 for (i = 0; i < rdata->nr_pages; i++) { >> 4638 struct page *page = rdata->pages[i]; >> 4639 >> 4640 if (rdata->result == 0 || >> 4641 (rdata->result == -EAGAIN && got_bytes)) { >> 4642 flush_dcache_page(page); >> 4643 SetPageUptodate(page); >> 4644 } else >> 4645 SetPageError(page); >> 4646 >> 4647 if (rdata->result == 0 || >> 4648 (rdata->result == -EAGAIN && got_bytes)) >> 4649 cifs_readpage_to_fscache(rdata->mapping->host, page); >> 4650 >> 4651 unlock_page(page); >> 4652 >> 4653 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes); >> 4654 >> 4655 put_page(page); >> 4656 rdata->pages[i] = NULL; >> 4657 } >> 4658 kref_put(&rdata->refcount, cifs_readdata_release); >> 4659 } >> 4660 >> 4661 static int >> 4662 readpages_fill_pages(struct TCP_Server_Info *server, >> 4663 struct cifs_readdata *rdata, struct iov_iter *iter, >> 4664 unsigned int len) >> 4665 { >> 4666 int result = 0; >> 4667 unsigned int i; >> 4668 u64 eof; >> 4669 pgoff_t eof_index; >> 4670 unsigned int nr_pages = rdata->nr_pages; >> 4671 unsigned int page_offset = rdata->page_offset; >> 4672 >> 4673 /* determine the eof that the server (probably) has */ >> 4674 eof = CIFS_I(rdata->mapping->host)->server_eof; >> 4675 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0; >> 4676 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index); >> 4677 >> 4678 rdata->got_bytes = 0; >> 4679 rdata->tailsz = PAGE_SIZE; >> 4680 for (i = 0; i < nr_pages; i++) { >> 4681 struct page *page = rdata->pages[i]; >> 4682 unsigned int to_read = rdata->pagesz; >> 4683 size_t n; >> 4684 >> 4685 if (i == 0) >> 4686 to_read -= page_offset; >> 4687 else >> 4688 page_offset = 0; >> 4689 >> 4690 n = to_read; >> 4691 >> 4692 if (len >= to_read) { >> 4693 len -= to_read; >> 4694 } else if (len > 0) { >> 4695 /* enough for partial page, fill and zero the rest */ >> 4696 zero_user(page, len + page_offset, to_read - len); >> 4697 n = rdata->tailsz = len; >> 4698 len = 0; >> 4699 } else if (page->index > eof_index) { >> 4700 /* >> 4701 * The VFS will not try to do readahead past the >> 4702 * i_size, but it's possible that we have outstanding >> 4703 * writes with gaps in the middle and the i_size hasn't >> 4704 * caught up yet. Populate those with zeroed out pages >> 4705 * to prevent the VFS from repeatedly attempting to >> 4706 * fill them until the writes are flushed. >> 4707 */ >> 4708 zero_user(page, 0, PAGE_SIZE); >> 4709 flush_dcache_page(page); >> 4710 SetPageUptodate(page); >> 4711 unlock_page(page); >> 4712 put_page(page); >> 4713 rdata->pages[i] = NULL; >> 4714 rdata->nr_pages--; >> 4715 continue; >> 4716 } else { >> 4717 /* no need to hold page hostage */ >> 4718 unlock_page(page); >> 4719 put_page(page); >> 4720 rdata->pages[i] = NULL; >> 4721 rdata->nr_pages--; >> 4722 continue; >> 4723 } >> 4724 >> 4725 if (iter) >> 4726 result = copy_page_from_iter( >> 4727 page, page_offset, n, iter); >> 4728 #ifdef CONFIG_CIFS_SMB_DIRECT >> 4729 else if (rdata->mr) >> 4730 result = n; >> 4731 #endif >> 4732 else >> 4733 result = cifs_read_page_from_socket( >> 4734 server, page, page_offset, n); >> 4735 if (result < 0) >> 4736 break; >> 4737 >> 4738 rdata->got_bytes += result; >> 4739 } >> 4740 >> 4741 return result != -ECONNABORTED && rdata->got_bytes > 0 ? >> 4742 rdata->got_bytes : result; >> 4743 } >> 4744 >> 4745 static int >> 4746 cifs_readpages_read_into_pages(struct TCP_Server_Info *server, >> 4747 struct cifs_readdata *rdata, unsigned int len) >> 4748 { >> 4749 return readpages_fill_pages(server, rdata, NULL, len); >> 4750 } >> 4751 >> 4752 static int >> 4753 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server, >> 4754 struct cifs_readdata *rdata, >> 4755 struct iov_iter *iter) >> 4756 { >> 4757 return readpages_fill_pages(server, rdata, iter, iter->count); >> 4758 } >> 4759 >> 4760 static void cifs_readahead(struct readahead_control *ractl) >> 4761 { >> 4762 int rc; >> 4763 struct cifsFileInfo *open_file = ractl->file->private_data; >> 4764 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file); >> 4765 struct TCP_Server_Info *server; >> 4766 pid_t pid; >> 4767 unsigned int xid, nr_pages, last_batch_size = 0, cache_nr_pages = 0; >> 4768 pgoff_t next_cached = ULONG_MAX; >> 4769 bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) && >> 4770 cifs_inode_cookie(ractl->mapping->host)->cache_priv; >> 4771 bool check_cache = caching; >> 4772 >> 4773 xid = get_xid(); >> 4774 >> 4775 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) >> 4776 pid = open_file->pid; >> 4777 else >> 4778 pid = current->tgid; >> 4779 >> 4780 rc = 0; >> 4781 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); >> 4782 >> 4783 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n", >> 4784 __func__, ractl->file, ractl->mapping, readahead_count(ractl)); >> 4785 >> 4786 /* >> 4787 * Chop the readahead request up into rsize-sized read requests. >> 4788 */ >> 4789 while ((nr_pages = readahead_count(ractl) - last_batch_size)) { >> 4790 unsigned int i, got, rsize; >> 4791 struct page *page; >> 4792 struct cifs_readdata *rdata; >> 4793 struct cifs_credits credits_on_stack; >> 4794 struct cifs_credits *credits = &credits_on_stack; >> 4795 pgoff_t index = readahead_index(ractl) + last_batch_size; >> 4796 >> 4797 /* >> 4798 * Find out if we have anything cached in the range of >> 4799 * interest, and if so, where the next chunk of cached data is. >> 4800 */ >> 4801 if (caching) { >> 4802 if (check_cache) { >> 4803 rc = cifs_fscache_query_occupancy( >> 4804 ractl->mapping->host, index, nr_pages, >> 4805 &next_cached, &cache_nr_pages); >> 4806 if (rc < 0) >> 4807 caching = false; >> 4808 check_cache = false; >> 4809 } >> 4810 >> 4811 if (index == next_cached) { >> 4812 /* >> 4813 * TODO: Send a whole batch of pages to be read >> 4814 * by the cache. >> 4815 */ >> 4816 struct folio *folio = readahead_folio(ractl); >> 4817 >> 4818 last_batch_size = folio_nr_pages(folio); >> 4819 if (cifs_readpage_from_fscache(ractl->mapping->host, >> 4820 &folio->page) < 0) { >> 4821 /* >> 4822 * TODO: Deal with cache read failure >> 4823 * here, but for the moment, delegate >> 4824 * that to readpage. >> 4825 */ >> 4826 caching = false; >> 4827 } >> 4828 folio_unlock(folio); >> 4829 next_cached++; >> 4830 cache_nr_pages--; >> 4831 if (cache_nr_pages == 0) >> 4832 check_cache = true; >> 4833 continue; >> 4834 } >> 4835 } >> 4836 >> 4837 if (open_file->invalidHandle) { >> 4838 rc = cifs_reopen_file(open_file, true); >> 4839 if (rc) { >> 4840 if (rc == -EAGAIN) >> 4841 continue; >> 4842 break; >> 4843 } >> 4844 } >> 4845 >> 4846 if (cifs_sb->ctx->rsize == 0) >> 4847 cifs_sb->ctx->rsize = >> 4848 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink), >> 4849 cifs_sb->ctx); >> 4850 >> 4851 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize, >> 4852 &rsize, credits); >> 4853 if (rc) >> 4854 break; >> 4855 nr_pages = min_t(size_t, rsize / PAGE_SIZE, readahead_count(ractl)); >> 4856 nr_pages = min_t(size_t, nr_pages, next_cached - index); >> 4857 >> 4858 /* >> 4859 * Give up immediately if rsize is too small to read an entire >> 4860 * page. The VFS will fall back to readpage. We should never >> 4861 * reach this point however since we set ra_pages to 0 when the >> 4862 * rsize is smaller than a cache page. >> 4863 */ >> 4864 if (unlikely(!nr_pages)) { >> 4865 add_credits_and_wake_if(server, credits, 0); >> 4866 break; >> 4867 } >> 4868 >> 4869 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete); >> 4870 if (!rdata) { >> 4871 /* best to give up if we're out of mem */ >> 4872 add_credits_and_wake_if(server, credits, 0); >> 4873 break; >> 4874 } >> 4875 >> 4876 got = __readahead_batch(ractl, rdata->pages, nr_pages); >> 4877 if (got != nr_pages) { >> 4878 pr_warn("__readahead_batch() returned %u/%u\n", >> 4879 got, nr_pages); >> 4880 nr_pages = got; >> 4881 } >> 4882 >> 4883 rdata->nr_pages = nr_pages; >> 4884 rdata->bytes = readahead_batch_length(ractl); >> 4885 rdata->cfile = cifsFileInfo_get(open_file); >> 4886 rdata->server = server; >> 4887 rdata->mapping = ractl->mapping; >> 4888 rdata->offset = readahead_pos(ractl); >> 4889 rdata->pid = pid; >> 4890 rdata->pagesz = PAGE_SIZE; >> 4891 rdata->tailsz = PAGE_SIZE; >> 4892 rdata->read_into_pages = cifs_readpages_read_into_pages; >> 4893 rdata->copy_into_pages = cifs_readpages_copy_into_pages; >> 4894 rdata->credits = credits_on_stack; >> 4895 >> 4896 rc = adjust_credits(server, &rdata->credits, rdata->bytes); >> 4897 if (!rc) { >> 4898 if (rdata->cfile->invalidHandle) >> 4899 rc = -EAGAIN; >> 4900 else >> 4901 rc = server->ops->async_readv(rdata); >> 4902 } >> 4903 >> 4904 if (rc) { >> 4905 add_credits_and_wake_if(server, &rdata->credits, 0); >> 4906 for (i = 0; i < rdata->nr_pages; i++) { >> 4907 page = rdata->pages[i]; >> 4908 unlock_page(page); >> 4909 put_page(page); >> 4910 } >> 4911 /* Fallback to the readpage in error/reconnect cases */ >> 4912 kref_put(&rdata->refcount, cifs_readdata_release); >> 4913 break; >> 4914 } >> 4915 >> 4916 kref_put(&rdata->refcount, cifs_readdata_release); >> 4917 last_batch_size = nr_pages; >> 4918 } >> 4919 >> 4920 free_xid(xid); >> 4921 } >> 4922 >> 4923 /* >> 4924 * cifs_readpage_worker must be called with the page pinned >> 4925 */ >> 4926 static int cifs_readpage_worker(struct file *file, struct page *page, >> 4927 loff_t *poffset) >> 4928 { >> 4929 char *read_data; >> 4930 int rc; >> 4931 >> 4932 /* Is the page cached? */ >> 4933 rc = cifs_readpage_from_fscache(file_inode(file), page); >> 4934 if (rc == 0) >> 4935 goto read_complete; >> 4936 >> 4937 read_data = kmap(page); >> 4938 /* for reads over a certain size could initiate async read ahead */ >> 4939 >> 4940 rc = cifs_read(file, read_data, PAGE_SIZE, poffset); >> 4941 >> 4942 if (rc < 0) >> 4943 goto io_error; >> 4944 else >> 4945 cifs_dbg(FYI, "Bytes read %d\n", rc); >> 4946 >> 4947 /* we do not want atime to be less than mtime, it broke some apps */ >> 4948 file_inode(file)->i_atime = current_time(file_inode(file)); >> 4949 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime))) >> 4950 file_inode(file)->i_atime = file_inode(file)->i_mtime; >> 4951 else >> 4952 file_inode(file)->i_atime = current_time(file_inode(file)); >> 4953 >> 4954 if (PAGE_SIZE > rc) >> 4955 memset(read_data + rc, 0, PAGE_SIZE - rc); >> 4956 >> 4957 flush_dcache_page(page); >> 4958 SetPageUptodate(page); >> 4959 >> 4960 /* send this page to the cache */ >> 4961 cifs_readpage_to_fscache(file_inode(file), page); >> 4962 >> 4963 rc = 0; >> 4964 >> 4965 io_error: >> 4966 kunmap(page); >> 4967 >> 4968 read_complete: >> 4969 unlock_page(page); >> 4970 return rc; >> 4971 } >> 4972 >> 4973 static int cifs_read_folio(struct file *file, struct folio *folio) >> 4974 { >> 4975 struct page *page = &folio->page; >> 4976 loff_t offset = page_file_offset(page); >> 4977 int rc = -EACCES; >> 4978 unsigned int xid; >> 4979 >> 4980 xid = get_xid(); >> 4981 >> 4982 if (file->private_data == NULL) { >> 4983 rc = -EBADF; >> 4984 free_xid(xid); >> 4985 return rc; >> 4986 } >> 4987 >> 4988 cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n", >> 4989 page, (int)offset, (int)offset); >> 4990 >> 4991 rc = cifs_readpage_worker(file, page, &offset); >> 4992 >> 4993 free_xid(xid); >> 4994 return rc; >> 4995 } >> 4996 2998 static int is_inode_writable(struct cifsInode 4997 static int is_inode_writable(struct cifsInodeInfo *cifs_inode) 2999 { 4998 { 3000 struct cifsFileInfo *open_file; 4999 struct cifsFileInfo *open_file; 3001 5000 3002 spin_lock(&cifs_inode->open_file_lock 5001 spin_lock(&cifs_inode->open_file_lock); 3003 list_for_each_entry(open_file, &cifs_ 5002 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 3004 if (OPEN_FMODE(open_file->f_f 5003 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 3005 spin_unlock(&cifs_ino 5004 spin_unlock(&cifs_inode->open_file_lock); 3006 return 1; 5005 return 1; 3007 } 5006 } 3008 } 5007 } 3009 spin_unlock(&cifs_inode->open_file_lo 5008 spin_unlock(&cifs_inode->open_file_lock); 3010 return 0; 5009 return 0; 3011 } 5010 } 3012 5011 3013 /* We do not want to update the file size fro 5012 /* We do not want to update the file size from server for inodes 3014 open for write - to avoid races with write 5013 open for write - to avoid races with writepage extending 3015 the file - in the future we could consider 5014 the file - in the future we could consider allowing 3016 refreshing the inode only on increases in 5015 refreshing the inode only on increases in the file size 3017 but this is tricky to do without racing wi 5016 but this is tricky to do without racing with writebehind 3018 page caching in the current Linux kernel d 5017 page caching in the current Linux kernel design */ 3019 bool is_size_safe_to_change(struct cifsInodeI !! 5018 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file) 3020 bool from_readdir << 3021 { 5019 { 3022 if (!cifsInode) 5020 if (!cifsInode) 3023 return true; 5021 return true; 3024 5022 3025 if (is_inode_writable(cifsInode) || !! 5023 if (is_inode_writable(cifsInode)) { 3026 ((cifsInode->oplock & CIFS_CA << 3027 /* This inode is open for wri 5024 /* This inode is open for write at least once */ 3028 struct cifs_sb_info *cifs_sb; 5025 struct cifs_sb_info *cifs_sb; 3029 5026 3030 cifs_sb = CIFS_SB(cifsInode-> 5027 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb); 3031 if (cifs_sb->mnt_cifs_flags & 5028 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 3032 /* since no page cach 5029 /* since no page cache to corrupt on directio 3033 we can change size sa 5030 we can change size safely */ 3034 return true; 5031 return true; 3035 } 5032 } 3036 5033 3037 if (i_size_read(&cifsInode->n 5034 if (i_size_read(&cifsInode->netfs.inode) < end_of_file) 3038 return true; 5035 return true; 3039 5036 3040 return false; 5037 return false; 3041 } else 5038 } else 3042 return true; 5039 return true; 3043 } 5040 } 3044 5041 >> 5042 static int cifs_write_begin(struct file *file, struct address_space *mapping, >> 5043 loff_t pos, unsigned len, >> 5044 struct page **pagep, void **fsdata) >> 5045 { >> 5046 int oncethru = 0; >> 5047 pgoff_t index = pos >> PAGE_SHIFT; >> 5048 loff_t offset = pos & (PAGE_SIZE - 1); >> 5049 loff_t page_start = pos & PAGE_MASK; >> 5050 loff_t i_size; >> 5051 struct page *page; >> 5052 int rc = 0; >> 5053 >> 5054 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len); >> 5055 >> 5056 start: >> 5057 page = grab_cache_page_write_begin(mapping, index); >> 5058 if (!page) { >> 5059 rc = -ENOMEM; >> 5060 goto out; >> 5061 } >> 5062 >> 5063 if (PageUptodate(page)) >> 5064 goto out; >> 5065 >> 5066 /* >> 5067 * If we write a full page it will be up to date, no need to read from >> 5068 * the server. If the write is short, we'll end up doing a sync write >> 5069 * instead. >> 5070 */ >> 5071 if (len == PAGE_SIZE) >> 5072 goto out; >> 5073 >> 5074 /* >> 5075 * optimize away the read when we have an oplock, and we're not >> 5076 * expecting to use any of the data we'd be reading in. That >> 5077 * is, when the page lies beyond the EOF, or straddles the EOF >> 5078 * and the write will cover all of the existing data. >> 5079 */ >> 5080 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) { >> 5081 i_size = i_size_read(mapping->host); >> 5082 if (page_start >= i_size || >> 5083 (offset == 0 && (pos + len) >= i_size)) { >> 5084 zero_user_segments(page, 0, offset, >> 5085 offset + len, >> 5086 PAGE_SIZE); >> 5087 /* >> 5088 * PageChecked means that the parts of the page >> 5089 * to which we're not writing are considered up >> 5090 * to date. Once the data is copied to the >> 5091 * page, it can be set uptodate. >> 5092 */ >> 5093 SetPageChecked(page); >> 5094 goto out; >> 5095 } >> 5096 } >> 5097 >> 5098 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) { >> 5099 /* >> 5100 * might as well read a page, it is fast enough. If we get >> 5101 * an error, we don't need to return it. cifs_write_end will >> 5102 * do a sync write instead since PG_uptodate isn't set. >> 5103 */ >> 5104 cifs_readpage_worker(file, page, &page_start); >> 5105 put_page(page); >> 5106 oncethru = 1; >> 5107 goto start; >> 5108 } else { >> 5109 /* we could try using another file handle if there is one - >> 5110 but how would we lock it to prevent close of that handle >> 5111 racing with this read? In any case >> 5112 this will be written out by write_end so is fine */ >> 5113 } >> 5114 out: >> 5115 *pagep = page; >> 5116 return rc; >> 5117 } >> 5118 >> 5119 static bool cifs_release_folio(struct folio *folio, gfp_t gfp) >> 5120 { >> 5121 if (folio_test_private(folio)) >> 5122 return 0; >> 5123 if (folio_test_fscache(folio)) { >> 5124 if (current_is_kswapd() || !(gfp & __GFP_FS)) >> 5125 return false; >> 5126 folio_wait_fscache(folio); >> 5127 } >> 5128 fscache_note_page_release(cifs_inode_cookie(folio->mapping->host)); >> 5129 return true; >> 5130 } >> 5131 >> 5132 static void cifs_invalidate_folio(struct folio *folio, size_t offset, >> 5133 size_t length) >> 5134 { >> 5135 folio_wait_fscache(folio); >> 5136 } >> 5137 >> 5138 static int cifs_launder_folio(struct folio *folio) >> 5139 { >> 5140 int rc = 0; >> 5141 loff_t range_start = folio_pos(folio); >> 5142 loff_t range_end = range_start + folio_size(folio); >> 5143 struct writeback_control wbc = { >> 5144 .sync_mode = WB_SYNC_ALL, >> 5145 .nr_to_write = 0, >> 5146 .range_start = range_start, >> 5147 .range_end = range_end, >> 5148 }; >> 5149 >> 5150 cifs_dbg(FYI, "Launder page: %lu\n", folio->index); >> 5151 >> 5152 if (folio_clear_dirty_for_io(folio)) >> 5153 rc = cifs_writepage_locked(&folio->page, &wbc); >> 5154 >> 5155 folio_wait_fscache(folio); >> 5156 return rc; >> 5157 } >> 5158 3045 void cifs_oplock_break(struct work_struct *wo 5159 void cifs_oplock_break(struct work_struct *work) 3046 { 5160 { 3047 struct cifsFileInfo *cfile = containe 5161 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, 3048 5162 oplock_break); 3049 struct inode *inode = d_inode(cfile-> 5163 struct inode *inode = d_inode(cfile->dentry); 3050 struct cifs_sb_info *cifs_sb = CIFS_S 5164 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 3051 struct cifsInodeInfo *cinode = CIFS_I 5165 struct cifsInodeInfo *cinode = CIFS_I(inode); 3052 struct cifs_tcon *tcon; 5166 struct cifs_tcon *tcon; 3053 struct TCP_Server_Info *server; 5167 struct TCP_Server_Info *server; 3054 struct tcon_link *tlink; 5168 struct tcon_link *tlink; 3055 int rc = 0; 5169 int rc = 0; 3056 bool purge_cache = false, oplock_brea 5170 bool purge_cache = false, oplock_break_cancelled; 3057 __u64 persistent_fid, volatile_fid; 5171 __u64 persistent_fid, volatile_fid; 3058 __u16 net_fid; 5172 __u16 net_fid; 3059 5173 3060 wait_on_bit(&cinode->flags, CIFS_INOD 5174 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, 3061 TASK_UNINTERRUPTIBLE) 5175 TASK_UNINTERRUPTIBLE); 3062 5176 3063 tlink = cifs_sb_tlink(cifs_sb); 5177 tlink = cifs_sb_tlink(cifs_sb); 3064 if (IS_ERR(tlink)) 5178 if (IS_ERR(tlink)) 3065 goto out; 5179 goto out; 3066 tcon = tlink_tcon(tlink); 5180 tcon = tlink_tcon(tlink); 3067 server = tcon->ses->server; 5181 server = tcon->ses->server; 3068 5182 3069 server->ops->downgrade_oplock(server, 5183 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level, 3070 cfile-> 5184 cfile->oplock_epoch, &purge_cache); 3071 5185 3072 if (!CIFS_CACHE_WRITE(cinode) && CIFS 5186 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) && 3073 5187 cifs_has_mand_locks(cinode)) { 3074 cifs_dbg(FYI, "Reset oplock t 5188 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", 3075 inode); 5189 inode); 3076 cinode->oplock = 0; 5190 cinode->oplock = 0; 3077 } 5191 } 3078 5192 3079 if (inode && S_ISREG(inode->i_mode)) 5193 if (inode && S_ISREG(inode->i_mode)) { 3080 if (CIFS_CACHE_READ(cinode)) 5194 if (CIFS_CACHE_READ(cinode)) 3081 break_lease(inode, O_ 5195 break_lease(inode, O_RDONLY); 3082 else 5196 else 3083 break_lease(inode, O_ 5197 break_lease(inode, O_WRONLY); 3084 rc = filemap_fdatawrite(inode 5198 rc = filemap_fdatawrite(inode->i_mapping); 3085 if (!CIFS_CACHE_READ(cinode) 5199 if (!CIFS_CACHE_READ(cinode) || purge_cache) { 3086 rc = filemap_fdatawai 5200 rc = filemap_fdatawait(inode->i_mapping); 3087 mapping_set_error(ino 5201 mapping_set_error(inode->i_mapping, rc); 3088 cifs_zap_mapping(inod 5202 cifs_zap_mapping(inode); 3089 } 5203 } 3090 cifs_dbg(FYI, "Oplock flush i 5204 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); 3091 if (CIFS_CACHE_WRITE(cinode)) 5205 if (CIFS_CACHE_WRITE(cinode)) 3092 goto oplock_break_ack 5206 goto oplock_break_ack; 3093 } 5207 } 3094 5208 3095 rc = cifs_push_locks(cfile); 5209 rc = cifs_push_locks(cfile); 3096 if (rc) 5210 if (rc) 3097 cifs_dbg(VFS, "Push locks rc 5211 cifs_dbg(VFS, "Push locks rc = %d\n", rc); 3098 5212 3099 oplock_break_ack: 5213 oplock_break_ack: 3100 /* 5214 /* 3101 * When oplock break is received and 5215 * When oplock break is received and there are no active 3102 * file handles but cached, then sche 5216 * file handles but cached, then schedule deferred close immediately. 3103 * So, new open will not use cached h 5217 * So, new open will not use cached handle. 3104 */ 5218 */ 3105 5219 3106 if (!CIFS_CACHE_HANDLE(cinode) && !li 5220 if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes)) 3107 cifs_close_deferred_file(cino 5221 cifs_close_deferred_file(cinode); 3108 5222 3109 persistent_fid = cfile->fid.persisten 5223 persistent_fid = cfile->fid.persistent_fid; 3110 volatile_fid = cfile->fid.volatile_fi 5224 volatile_fid = cfile->fid.volatile_fid; 3111 net_fid = cfile->fid.netfid; 5225 net_fid = cfile->fid.netfid; 3112 oplock_break_cancelled = cfile->oploc 5226 oplock_break_cancelled = cfile->oplock_break_cancelled; 3113 5227 3114 _cifsFileInfo_put(cfile, false /* do 5228 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false); 3115 /* 5229 /* 3116 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 5230 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require 3117 * an acknowledgment to be sent when 5231 * an acknowledgment to be sent when the file has already been closed. 3118 */ 5232 */ 3119 spin_lock(&cinode->open_file_lock); 5233 spin_lock(&cinode->open_file_lock); 3120 /* check list empty since can race wi 5234 /* check list empty since can race with kill_sb calling tree disconnect */ 3121 if (!oplock_break_cancelled && !list_ 5235 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) { 3122 spin_unlock(&cinode->open_fil 5236 spin_unlock(&cinode->open_file_lock); 3123 rc = server->ops->oplock_resp 5237 rc = server->ops->oplock_response(tcon, persistent_fid, 3124 5238 volatile_fid, net_fid, cinode); 3125 cifs_dbg(FYI, "Oplock release 5239 cifs_dbg(FYI, "Oplock release rc = %d\n", rc); 3126 } else 5240 } else 3127 spin_unlock(&cinode->open_fil 5241 spin_unlock(&cinode->open_file_lock); 3128 5242 3129 cifs_put_tlink(tlink); 5243 cifs_put_tlink(tlink); 3130 out: 5244 out: 3131 cifs_done_oplock_break(cinode); 5245 cifs_done_oplock_break(cinode); 3132 } 5246 } 3133 5247 >> 5248 /* >> 5249 * The presence of cifs_direct_io() in the address space ops vector >> 5250 * allowes open() O_DIRECT flags which would have failed otherwise. >> 5251 * >> 5252 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests >> 5253 * so this method should never be called. >> 5254 * >> 5255 * Direct IO is not yet supported in the cached mode. >> 5256 */ >> 5257 static ssize_t >> 5258 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter) >> 5259 { >> 5260 /* >> 5261 * FIXME >> 5262 * Eventually need to support direct IO for non forcedirectio mounts >> 5263 */ >> 5264 return -EINVAL; >> 5265 } >> 5266 3134 static int cifs_swap_activate(struct swap_inf 5267 static int cifs_swap_activate(struct swap_info_struct *sis, 3135 struct file *sw 5268 struct file *swap_file, sector_t *span) 3136 { 5269 { 3137 struct cifsFileInfo *cfile = swap_fil 5270 struct cifsFileInfo *cfile = swap_file->private_data; 3138 struct inode *inode = swap_file->f_ma 5271 struct inode *inode = swap_file->f_mapping->host; 3139 unsigned long blocks; 5272 unsigned long blocks; 3140 long long isize; 5273 long long isize; 3141 5274 3142 cifs_dbg(FYI, "swap activate\n"); 5275 cifs_dbg(FYI, "swap activate\n"); 3143 5276 3144 if (!swap_file->f_mapping->a_ops->swa 5277 if (!swap_file->f_mapping->a_ops->swap_rw) 3145 /* Cannot support swap */ 5278 /* Cannot support swap */ 3146 return -EINVAL; 5279 return -EINVAL; 3147 5280 3148 spin_lock(&inode->i_lock); 5281 spin_lock(&inode->i_lock); 3149 blocks = inode->i_blocks; 5282 blocks = inode->i_blocks; 3150 isize = inode->i_size; 5283 isize = inode->i_size; 3151 spin_unlock(&inode->i_lock); 5284 spin_unlock(&inode->i_lock); 3152 if (blocks*512 < isize) { 5285 if (blocks*512 < isize) { 3153 pr_warn("swap activate: swapf 5286 pr_warn("swap activate: swapfile has holes\n"); 3154 return -EINVAL; 5287 return -EINVAL; 3155 } 5288 } 3156 *span = sis->pages; 5289 *span = sis->pages; 3157 5290 3158 pr_warn_once("Swap support over SMB3 5291 pr_warn_once("Swap support over SMB3 is experimental\n"); 3159 5292 3160 /* 5293 /* 3161 * TODO: consider adding ACL (or docu 5294 * TODO: consider adding ACL (or documenting how) to prevent other 3162 * users (on this or other systems) f 5295 * users (on this or other systems) from reading it 3163 */ 5296 */ 3164 5297 3165 5298 3166 /* TODO: add sk_set_memalloc(inet) or 5299 /* TODO: add sk_set_memalloc(inet) or similar */ 3167 5300 3168 if (cfile) 5301 if (cfile) 3169 cfile->swapfile = true; 5302 cfile->swapfile = true; 3170 /* 5303 /* 3171 * TODO: Since file already open, we 5304 * TODO: Since file already open, we can't open with DENY_ALL here 3172 * but we could add call to grab a by 5305 * but we could add call to grab a byte range lock to prevent others 3173 * from reading or writing the file 5306 * from reading or writing the file 3174 */ 5307 */ 3175 5308 3176 sis->flags |= SWP_FS_OPS; 5309 sis->flags |= SWP_FS_OPS; 3177 return add_swap_extent(sis, 0, sis->m 5310 return add_swap_extent(sis, 0, sis->max, 0); 3178 } 5311 } 3179 5312 3180 static void cifs_swap_deactivate(struct file 5313 static void cifs_swap_deactivate(struct file *file) 3181 { 5314 { 3182 struct cifsFileInfo *cfile = file->pr 5315 struct cifsFileInfo *cfile = file->private_data; 3183 5316 3184 cifs_dbg(FYI, "swap deactivate\n"); 5317 cifs_dbg(FYI, "swap deactivate\n"); 3185 5318 3186 /* TODO: undo sk_set_memalloc(inet) w 5319 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */ 3187 5320 3188 if (cfile) 5321 if (cfile) 3189 cfile->swapfile = false; 5322 cfile->swapfile = false; 3190 5323 3191 /* do we need to unpin (or unlock) th 5324 /* do we need to unpin (or unlock) the file */ 3192 } 5325 } 3193 5326 3194 /** !! 5327 /* 3195 * cifs_swap_rw - SMB3 address space operatio !! 5328 * Mark a page as having been made dirty and thus needing writeback. We also 3196 * @iocb: target I/O control block !! 5329 * need to pin the cache object to write back to. 3197 * @iter: I/O buffer << 3198 * << 3199 * Perform IO to the swap-file. This is much << 3200 */ 5330 */ 3201 static int cifs_swap_rw(struct kiocb *iocb, s !! 5331 #ifdef CONFIG_CIFS_FSCACHE >> 5332 static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio) 3202 { 5333 { 3203 ssize_t ret; !! 5334 return fscache_dirty_folio(mapping, folio, 3204 !! 5335 cifs_inode_cookie(mapping->host)); 3205 if (iov_iter_rw(iter) == READ) << 3206 ret = netfs_unbuffered_read_i << 3207 else << 3208 ret = netfs_unbuffered_write_ << 3209 if (ret < 0) << 3210 return ret; << 3211 return 0; << 3212 } 5336 } >> 5337 #else >> 5338 #define cifs_dirty_folio filemap_dirty_folio >> 5339 #endif 3213 5340 3214 const struct address_space_operations cifs_ad 5341 const struct address_space_operations cifs_addr_ops = { 3215 .read_folio = netfs_read_folio, !! 5342 .read_folio = cifs_read_folio, 3216 .readahead = netfs_readahead, !! 5343 .readahead = cifs_readahead, 3217 .writepages = netfs_writepages, !! 5344 .writepage = cifs_writepage, 3218 .dirty_folio = netfs_dirty_folio, !! 5345 .writepages = cifs_writepages, 3219 .release_folio = netfs_release_folio !! 5346 .write_begin = cifs_write_begin, 3220 .direct_IO = noop_direct_IO, !! 5347 .write_end = cifs_write_end, 3221 .invalidate_folio = netfs_invalidate_ !! 5348 .dirty_folio = cifs_dirty_folio, 3222 .migrate_folio = filemap_migrate_fol !! 5349 .release_folio = cifs_release_folio, >> 5350 .direct_IO = cifs_direct_io, >> 5351 .invalidate_folio = cifs_invalidate_folio, >> 5352 .launder_folio = cifs_launder_folio, 3223 /* 5353 /* 3224 * TODO: investigate and if useful we !! 5354 * TODO: investigate and if useful we could add an cifs_migratePage 3225 * helper if needed !! 5355 * helper (under an CONFIG_MIGRATION) in the future, and also >> 5356 * investigate and add an is_dirty_writeback helper if needed 3226 */ 5357 */ 3227 .swap_activate = cifs_swap_activate, !! 5358 .swap_activate = cifs_swap_activate, 3228 .swap_deactivate = cifs_swap_deactiva 5359 .swap_deactivate = cifs_swap_deactivate, 3229 .swap_rw = cifs_swap_rw, << 3230 }; 5360 }; 3231 5361 3232 /* 5362 /* 3233 * cifs_readahead requires the server to supp 5363 * cifs_readahead requires the server to support a buffer large enough to 3234 * contain the header plus one complete page 5364 * contain the header plus one complete page of data. Otherwise, we need 3235 * to leave cifs_readahead out of the address 5365 * to leave cifs_readahead out of the address space operations. 3236 */ 5366 */ 3237 const struct address_space_operations cifs_ad 5367 const struct address_space_operations cifs_addr_ops_smallbuf = { 3238 .read_folio = netfs_read_folio, !! 5368 .read_folio = cifs_read_folio, 3239 .writepages = netfs_writepages, !! 5369 .writepage = cifs_writepage, 3240 .dirty_folio = netfs_dirty_folio, !! 5370 .writepages = cifs_writepages, 3241 .release_folio = netfs_release_folio !! 5371 .write_begin = cifs_write_begin, 3242 .invalidate_folio = netfs_invalidate_ !! 5372 .write_end = cifs_write_end, 3243 .migrate_folio = filemap_migrate_fol !! 5373 .dirty_folio = cifs_dirty_folio, >> 5374 .release_folio = cifs_release_folio, >> 5375 .invalidate_folio = cifs_invalidate_folio, >> 5376 .launder_folio = cifs_launder_folio, 3244 }; 5377 }; 3245 5378
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.