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