1 // SPDX-License-Identifier: LGPL-2.1
4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
12 #include <linux/filelock.h>
13 #include <linux/backing-dev.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/pagemap.h>
17 #include <linux/pagevec.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>
25 #include <asm/div64.h>
29 #include "cifsproto.h"
30 #include "smb2proto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
35 #include "smbdirect.h"
36 #include "fs_context.h"
37 #include "cifs_ioctl.h"
38 #include "cached_dir.h"
39 #include <trace/events/netfs.h>
41 static int cifs_reopen_file(struct cifsFileInfo
*cfile
, bool can_flush
);
44 * Prepare a subrequest to upload to the server. We need to allocate credits
45 * so that we know the maximum amount of data that we can include in it.
47 static void cifs_prepare_write(struct netfs_io_subrequest
*subreq
)
49 struct cifs_io_subrequest
*wdata
=
50 container_of(subreq
, struct cifs_io_subrequest
, subreq
);
51 struct cifs_io_request
*req
= wdata
->req
;
52 struct netfs_io_stream
*stream
= &req
->rreq
.io_streams
[subreq
->stream_nr
];
53 struct TCP_Server_Info
*server
;
54 struct cifsFileInfo
*open_file
= req
->cfile
;
55 size_t wsize
= req
->rreq
.wsize
;
58 if (!wdata
->have_xid
) {
59 wdata
->xid
= get_xid();
60 wdata
->have_xid
= true;
63 server
= cifs_pick_channel(tlink_tcon(open_file
->tlink
)->ses
);
64 wdata
->server
= server
;
67 if (open_file
->invalidHandle
) {
68 rc
= cifs_reopen_file(open_file
, false);
73 return netfs_prepare_write_failed(subreq
);
77 rc
= server
->ops
->wait_mtu_credits(server
, wsize
, &stream
->sreq_max_len
,
81 return netfs_prepare_write_failed(subreq
);
84 wdata
->credits
.rreq_debug_id
= subreq
->rreq
->debug_id
;
85 wdata
->credits
.rreq_debug_index
= subreq
->debug_index
;
86 wdata
->credits
.in_flight_check
= 1;
87 trace_smb3_rw_credits(wdata
->rreq
->debug_id
,
88 wdata
->subreq
.debug_index
,
90 server
->credits
, server
->in_flight
,
92 cifs_trace_rw_credits_write_prepare
);
94 #ifdef CONFIG_CIFS_SMB_DIRECT
95 if (server
->smbd_conn
)
96 stream
->sreq_max_segs
= server
->smbd_conn
->max_frmr_depth
;
101 * Issue a subrequest to upload to the server.
103 static void cifs_issue_write(struct netfs_io_subrequest
*subreq
)
105 struct cifs_io_subrequest
*wdata
=
106 container_of(subreq
, struct cifs_io_subrequest
, subreq
);
107 struct cifs_sb_info
*sbi
= CIFS_SB(subreq
->rreq
->inode
->i_sb
);
110 if (cifs_forced_shutdown(sbi
)) {
115 rc
= adjust_credits(wdata
->server
, wdata
, cifs_trace_rw_credits_issue_write_adjust
);
120 if (wdata
->req
->cfile
->invalidHandle
)
123 wdata
->server
->ops
->async_writev(wdata
);
129 trace_netfs_sreq(subreq
, netfs_sreq_trace_retry
);
131 trace_netfs_sreq(subreq
, netfs_sreq_trace_fail
);
132 add_credits_and_wake_if(wdata
->server
, &wdata
->credits
, 0);
133 cifs_write_subrequest_terminated(wdata
, rc
, false);
137 static void cifs_netfs_invalidate_cache(struct netfs_io_request
*wreq
)
139 cifs_invalidate_cache(wreq
->inode
, 0);
143 * Negotiate the size of a read operation on behalf of the netfs library.
145 static int cifs_prepare_read(struct netfs_io_subrequest
*subreq
)
147 struct netfs_io_request
*rreq
= subreq
->rreq
;
148 struct cifs_io_subrequest
*rdata
= container_of(subreq
, struct cifs_io_subrequest
, subreq
);
149 struct cifs_io_request
*req
= container_of(subreq
->rreq
, struct cifs_io_request
, rreq
);
150 struct TCP_Server_Info
*server
= req
->server
;
151 struct cifs_sb_info
*cifs_sb
= CIFS_SB(rreq
->inode
->i_sb
);
155 if (!rdata
->have_xid
) {
156 rdata
->xid
= get_xid();
157 rdata
->have_xid
= true;
159 rdata
->server
= server
;
161 if (cifs_sb
->ctx
->rsize
== 0)
162 cifs_sb
->ctx
->rsize
=
163 server
->ops
->negotiate_rsize(tlink_tcon(req
->cfile
->tlink
),
166 rc
= server
->ops
->wait_mtu_credits(server
, cifs_sb
->ctx
->rsize
,
167 &size
, &rdata
->credits
);
171 rreq
->io_streams
[0].sreq_max_len
= size
;
173 rdata
->credits
.in_flight_check
= 1;
174 rdata
->credits
.rreq_debug_id
= rreq
->debug_id
;
175 rdata
->credits
.rreq_debug_index
= subreq
->debug_index
;
177 trace_smb3_rw_credits(rdata
->rreq
->debug_id
,
178 rdata
->subreq
.debug_index
,
179 rdata
->credits
.value
,
180 server
->credits
, server
->in_flight
, 0,
181 cifs_trace_rw_credits_read_submit
);
183 #ifdef CONFIG_CIFS_SMB_DIRECT
184 if (server
->smbd_conn
)
185 rreq
->io_streams
[0].sreq_max_segs
= server
->smbd_conn
->max_frmr_depth
;
191 * Issue a read operation on behalf of the netfs helper functions. We're asked
192 * to make a read of a certain size at a point in the file. We are permitted
193 * to only read a portion of that, but as long as we read something, the netfs
194 * helper will call us again so that we can issue another read.
196 static void cifs_issue_read(struct netfs_io_subrequest
*subreq
)
198 struct netfs_io_request
*rreq
= subreq
->rreq
;
199 struct cifs_io_subrequest
*rdata
= container_of(subreq
, struct cifs_io_subrequest
, subreq
);
200 struct cifs_io_request
*req
= container_of(subreq
->rreq
, struct cifs_io_request
, rreq
);
201 struct TCP_Server_Info
*server
= req
->server
;
204 cifs_dbg(FYI
, "%s: op=%08x[%x] mapping=%p len=%zu/%zu\n",
205 __func__
, rreq
->debug_id
, subreq
->debug_index
, rreq
->mapping
,
206 subreq
->transferred
, subreq
->len
);
208 rc
= adjust_credits(server
, rdata
, cifs_trace_rw_credits_issue_read_adjust
);
212 if (req
->cfile
->invalidHandle
) {
214 rc
= cifs_reopen_file(req
->cfile
, true);
215 } while (rc
== -EAGAIN
);
220 if (subreq
->rreq
->origin
!= NETFS_DIO_READ
)
221 __set_bit(NETFS_SREQ_CLEAR_TAIL
, &subreq
->flags
);
223 trace_netfs_sreq(subreq
, netfs_sreq_trace_submit
);
224 rc
= rdata
->server
->ops
->async_readv(rdata
);
231 netfs_read_subreq_terminated(subreq
);
235 * Writeback calls this when it finds a folio that needs uploading. This isn't
236 * called if writeback only has copy-to-cache to deal with.
238 static void cifs_begin_writeback(struct netfs_io_request
*wreq
)
240 struct cifs_io_request
*req
= container_of(wreq
, struct cifs_io_request
, rreq
);
243 ret
= cifs_get_writable_file(CIFS_I(wreq
->inode
), FIND_WR_ANY
, &req
->cfile
);
245 cifs_dbg(VFS
, "No writable handle in writepages ret=%d\n", ret
);
249 wreq
->io_streams
[0].avail
= true;
253 * Initialise a request.
255 static int cifs_init_request(struct netfs_io_request
*rreq
, struct file
*file
)
257 struct cifs_io_request
*req
= container_of(rreq
, struct cifs_io_request
, rreq
);
258 struct cifs_sb_info
*cifs_sb
= CIFS_SB(rreq
->inode
->i_sb
);
259 struct cifsFileInfo
*open_file
= NULL
;
261 rreq
->rsize
= cifs_sb
->ctx
->rsize
;
262 rreq
->wsize
= cifs_sb
->ctx
->wsize
;
263 req
->pid
= current
->tgid
; // Ummm... This may be a workqueue
266 open_file
= file
->private_data
;
267 rreq
->netfs_priv
= file
->private_data
;
268 req
->cfile
= cifsFileInfo_get(open_file
);
269 req
->server
= cifs_pick_channel(tlink_tcon(req
->cfile
->tlink
)->ses
);
270 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_RWPIDFORWARD
)
271 req
->pid
= req
->cfile
->pid
;
272 } else if (rreq
->origin
!= NETFS_WRITEBACK
) {
281 * Completion of a request operation.
283 static void cifs_rreq_done(struct netfs_io_request
*rreq
)
285 struct timespec64 atime
, mtime
;
286 struct inode
*inode
= rreq
->inode
;
288 /* we do not want atime to be less than mtime, it broke some apps */
289 atime
= inode_set_atime_to_ts(inode
, current_time(inode
));
290 mtime
= inode_get_mtime(inode
);
291 if (timespec64_compare(&atime
, &mtime
))
292 inode_set_atime_to_ts(inode
, inode_get_mtime(inode
));
295 static void cifs_free_request(struct netfs_io_request
*rreq
)
297 struct cifs_io_request
*req
= container_of(rreq
, struct cifs_io_request
, rreq
);
300 cifsFileInfo_put(req
->cfile
);
303 static void cifs_free_subrequest(struct netfs_io_subrequest
*subreq
)
305 struct cifs_io_subrequest
*rdata
=
306 container_of(subreq
, struct cifs_io_subrequest
, subreq
);
307 int rc
= subreq
->error
;
309 if (rdata
->subreq
.source
== NETFS_DOWNLOAD_FROM_SERVER
) {
310 #ifdef CONFIG_CIFS_SMB_DIRECT
312 smbd_deregister_mr(rdata
->mr
);
318 if (rdata
->credits
.value
!= 0) {
319 trace_smb3_rw_credits(rdata
->rreq
->debug_id
,
320 rdata
->subreq
.debug_index
,
321 rdata
->credits
.value
,
322 rdata
->server
? rdata
->server
->credits
: 0,
323 rdata
->server
? rdata
->server
->in_flight
: 0,
324 -rdata
->credits
.value
,
325 cifs_trace_rw_credits_free_subreq
);
327 add_credits_and_wake_if(rdata
->server
, &rdata
->credits
, 0);
329 rdata
->credits
.value
= 0;
333 free_xid(rdata
->xid
);
336 const struct netfs_request_ops cifs_req_ops
= {
337 .request_pool
= &cifs_io_request_pool
,
338 .subrequest_pool
= &cifs_io_subrequest_pool
,
339 .init_request
= cifs_init_request
,
340 .free_request
= cifs_free_request
,
341 .free_subrequest
= cifs_free_subrequest
,
342 .prepare_read
= cifs_prepare_read
,
343 .issue_read
= cifs_issue_read
,
344 .done
= cifs_rreq_done
,
345 .begin_writeback
= cifs_begin_writeback
,
346 .prepare_write
= cifs_prepare_write
,
347 .issue_write
= cifs_issue_write
,
348 .invalidate_cache
= cifs_netfs_invalidate_cache
,
352 * Mark as invalid, all open files on tree connections since they
353 * were closed when session to server was lost.
356 cifs_mark_open_files_invalid(struct cifs_tcon
*tcon
)
358 struct cifsFileInfo
*open_file
= NULL
;
359 struct list_head
*tmp
;
360 struct list_head
*tmp1
;
362 /* only send once per connect */
363 spin_lock(&tcon
->tc_lock
);
364 if (tcon
->need_reconnect
)
365 tcon
->status
= TID_NEED_RECON
;
367 if (tcon
->status
!= TID_NEED_RECON
) {
368 spin_unlock(&tcon
->tc_lock
);
371 tcon
->status
= TID_IN_FILES_INVALIDATE
;
372 spin_unlock(&tcon
->tc_lock
);
374 /* list all files open on tree connection and mark them invalid */
375 spin_lock(&tcon
->open_file_lock
);
376 list_for_each_safe(tmp
, tmp1
, &tcon
->openFileList
) {
377 open_file
= list_entry(tmp
, struct cifsFileInfo
, tlist
);
378 open_file
->invalidHandle
= true;
379 open_file
->oplock_break_cancelled
= true;
381 spin_unlock(&tcon
->open_file_lock
);
383 invalidate_all_cached_dirs(tcon
);
384 spin_lock(&tcon
->tc_lock
);
385 if (tcon
->status
== TID_IN_FILES_INVALIDATE
)
386 tcon
->status
= TID_NEED_TCON
;
387 spin_unlock(&tcon
->tc_lock
);
390 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
395 static inline int cifs_convert_flags(unsigned int flags
, int rdwr_for_fscache
)
397 if ((flags
& O_ACCMODE
) == O_RDONLY
)
399 else if ((flags
& O_ACCMODE
) == O_WRONLY
)
400 return rdwr_for_fscache
== 1 ? (GENERIC_READ
| GENERIC_WRITE
) : GENERIC_WRITE
;
401 else if ((flags
& O_ACCMODE
) == O_RDWR
) {
402 /* GENERIC_ALL is too much permission to request
403 can cause unnecessary access denied on create */
404 /* return GENERIC_ALL; */
405 return (GENERIC_READ
| GENERIC_WRITE
);
408 return (READ_CONTROL
| FILE_WRITE_ATTRIBUTES
| FILE_READ_ATTRIBUTES
|
409 FILE_WRITE_EA
| FILE_APPEND_DATA
| FILE_WRITE_DATA
|
413 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
414 static u32
cifs_posix_convert_flags(unsigned int flags
)
418 if ((flags
& O_ACCMODE
) == O_RDONLY
)
419 posix_flags
= SMB_O_RDONLY
;
420 else if ((flags
& O_ACCMODE
) == O_WRONLY
)
421 posix_flags
= SMB_O_WRONLY
;
422 else if ((flags
& O_ACCMODE
) == O_RDWR
)
423 posix_flags
= SMB_O_RDWR
;
425 if (flags
& O_CREAT
) {
426 posix_flags
|= SMB_O_CREAT
;
428 posix_flags
|= SMB_O_EXCL
;
429 } else if (flags
& O_EXCL
)
430 cifs_dbg(FYI
, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
431 current
->comm
, current
->tgid
);
434 posix_flags
|= SMB_O_TRUNC
;
435 /* be safe and imply O_SYNC for O_DSYNC */
437 posix_flags
|= SMB_O_SYNC
;
438 if (flags
& O_DIRECTORY
)
439 posix_flags
|= SMB_O_DIRECTORY
;
440 if (flags
& O_NOFOLLOW
)
441 posix_flags
|= SMB_O_NOFOLLOW
;
442 if (flags
& O_DIRECT
)
443 posix_flags
|= SMB_O_DIRECT
;
447 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
449 static inline int cifs_get_disposition(unsigned int flags
)
451 if ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
))
453 else if ((flags
& (O_CREAT
| O_TRUNC
)) == (O_CREAT
| O_TRUNC
))
454 return FILE_OVERWRITE_IF
;
455 else if ((flags
& O_CREAT
) == O_CREAT
)
457 else if ((flags
& O_TRUNC
) == O_TRUNC
)
458 return FILE_OVERWRITE
;
463 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
464 int cifs_posix_open(const char *full_path
, struct inode
**pinode
,
465 struct super_block
*sb
, int mode
, unsigned int f_flags
,
466 __u32
*poplock
, __u16
*pnetfid
, unsigned int xid
)
469 FILE_UNIX_BASIC_INFO
*presp_data
;
470 __u32 posix_flags
= 0;
471 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
472 struct cifs_fattr fattr
;
473 struct tcon_link
*tlink
;
474 struct cifs_tcon
*tcon
;
476 cifs_dbg(FYI
, "posix open %s\n", full_path
);
478 presp_data
= kzalloc(sizeof(FILE_UNIX_BASIC_INFO
), GFP_KERNEL
);
479 if (presp_data
== NULL
)
482 tlink
= cifs_sb_tlink(cifs_sb
);
488 tcon
= tlink_tcon(tlink
);
489 mode
&= ~current_umask();
491 posix_flags
= cifs_posix_convert_flags(f_flags
);
492 rc
= CIFSPOSIXCreate(xid
, tcon
, posix_flags
, mode
, pnetfid
, presp_data
,
493 poplock
, full_path
, cifs_sb
->local_nls
,
494 cifs_remap(cifs_sb
));
495 cifs_put_tlink(tlink
);
500 if (presp_data
->Type
== cpu_to_le32(-1))
501 goto posix_open_ret
; /* open ok, caller does qpathinfo */
504 goto posix_open_ret
; /* caller does not need info */
506 cifs_unix_basic_to_fattr(&fattr
, presp_data
, cifs_sb
);
508 /* get new inode and set it up */
509 if (*pinode
== NULL
) {
510 cifs_fill_uniqueid(sb
, &fattr
);
511 *pinode
= cifs_iget(sb
, &fattr
);
517 cifs_revalidate_mapping(*pinode
);
518 rc
= cifs_fattr_to_inode(*pinode
, &fattr
, false);
525 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
527 static int cifs_nt_open(const char *full_path
, struct inode
*inode
, struct cifs_sb_info
*cifs_sb
,
528 struct cifs_tcon
*tcon
, unsigned int f_flags
, __u32
*oplock
,
529 struct cifs_fid
*fid
, unsigned int xid
, struct cifs_open_info_data
*buf
)
534 int create_options
= CREATE_NOT_DIR
;
535 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
536 struct cifs_open_parms oparms
;
537 int rdwr_for_fscache
= 0;
539 if (!server
->ops
->open
)
542 /* If we're caching, we need to be able to fill in around partial writes. */
543 if (cifs_fscache_enabled(inode
) && (f_flags
& O_ACCMODE
) == O_WRONLY
)
544 rdwr_for_fscache
= 1;
546 desired_access
= cifs_convert_flags(f_flags
, rdwr_for_fscache
);
548 /*********************************************************************
549 * open flag mapping table:
551 * POSIX Flag CIFS Disposition
552 * ---------- ----------------
553 * O_CREAT FILE_OPEN_IF
554 * O_CREAT | O_EXCL FILE_CREATE
555 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
556 * O_TRUNC FILE_OVERWRITE
557 * none of the above FILE_OPEN
559 * Note that there is not a direct match between disposition
560 * FILE_SUPERSEDE (ie create whether or not file exists although
561 * O_CREAT | O_TRUNC is similar but truncates the existing
562 * file rather than creating a new file as FILE_SUPERSEDE does
563 * (which uses the attributes / metadata passed in on open call)
565 *? O_SYNC is a reasonable match to CIFS writethrough flag
566 *? and the read write flags match reasonably. O_LARGEFILE
567 *? is irrelevant because largefile support is always used
568 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
569 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
570 *********************************************************************/
572 disposition
= cifs_get_disposition(f_flags
);
574 /* BB pass O_SYNC flag through on file attributes .. BB */
576 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
577 if (f_flags
& O_SYNC
)
578 create_options
|= CREATE_WRITE_THROUGH
;
580 if (f_flags
& O_DIRECT
)
581 create_options
|= CREATE_NO_BUFFER
;
584 oparms
= (struct cifs_open_parms
) {
587 .desired_access
= desired_access
,
588 .create_options
= cifs_create_options(cifs_sb
, create_options
),
589 .disposition
= disposition
,
594 rc
= server
->ops
->open(xid
, &oparms
, oplock
, buf
);
596 if (rc
== -EACCES
&& rdwr_for_fscache
== 1) {
597 desired_access
= cifs_convert_flags(f_flags
, 0);
598 rdwr_for_fscache
= 2;
603 if (rdwr_for_fscache
== 2)
604 cifs_invalidate_cache(inode
, FSCACHE_INVAL_DIO_WRITE
);
606 /* TODO: Add support for calling posix query info but with passing in fid */
608 rc
= cifs_get_inode_info_unix(&inode
, full_path
, inode
->i_sb
,
611 rc
= cifs_get_inode_info(&inode
, full_path
, buf
, inode
->i_sb
,
615 server
->ops
->close(xid
, tcon
, fid
);
624 cifs_has_mand_locks(struct cifsInodeInfo
*cinode
)
626 struct cifs_fid_locks
*cur
;
627 bool has_locks
= false;
629 down_read(&cinode
->lock_sem
);
630 list_for_each_entry(cur
, &cinode
->llist
, llist
) {
631 if (!list_empty(&cur
->locks
)) {
636 up_read(&cinode
->lock_sem
);
641 cifs_down_write(struct rw_semaphore
*sem
)
643 while (!down_write_trylock(sem
))
647 static void cifsFileInfo_put_work(struct work_struct
*work
);
648 void serverclose_work(struct work_struct
*work
);
650 struct cifsFileInfo
*cifs_new_fileinfo(struct cifs_fid
*fid
, struct file
*file
,
651 struct tcon_link
*tlink
, __u32 oplock
,
652 const char *symlink_target
)
654 struct dentry
*dentry
= file_dentry(file
);
655 struct inode
*inode
= d_inode(dentry
);
656 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
657 struct cifsFileInfo
*cfile
;
658 struct cifs_fid_locks
*fdlocks
;
659 struct cifs_tcon
*tcon
= tlink_tcon(tlink
);
660 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
662 cfile
= kzalloc(sizeof(struct cifsFileInfo
), GFP_KERNEL
);
666 fdlocks
= kzalloc(sizeof(struct cifs_fid_locks
), GFP_KERNEL
);
672 if (symlink_target
) {
673 cfile
->symlink_target
= kstrdup(symlink_target
, GFP_KERNEL
);
674 if (!cfile
->symlink_target
) {
681 INIT_LIST_HEAD(&fdlocks
->locks
);
682 fdlocks
->cfile
= cfile
;
683 cfile
->llist
= fdlocks
;
686 cfile
->pid
= current
->tgid
;
687 cfile
->uid
= current_fsuid();
688 cfile
->dentry
= dget(dentry
);
689 cfile
->f_flags
= file
->f_flags
;
690 cfile
->invalidHandle
= false;
691 cfile
->deferred_close_scheduled
= false;
692 cfile
->tlink
= cifs_get_tlink(tlink
);
693 INIT_WORK(&cfile
->oplock_break
, cifs_oplock_break
);
694 INIT_WORK(&cfile
->put
, cifsFileInfo_put_work
);
695 INIT_WORK(&cfile
->serverclose
, serverclose_work
);
696 INIT_DELAYED_WORK(&cfile
->deferred
, smb2_deferred_work_close
);
697 mutex_init(&cfile
->fh_mutex
);
698 spin_lock_init(&cfile
->file_info_lock
);
700 cifs_sb_active(inode
->i_sb
);
703 * If the server returned a read oplock and we have mandatory brlocks,
704 * set oplock level to None.
706 if (server
->ops
->is_read_op(oplock
) && cifs_has_mand_locks(cinode
)) {
707 cifs_dbg(FYI
, "Reset oplock val from read to None due to mand locks\n");
711 cifs_down_write(&cinode
->lock_sem
);
712 list_add(&fdlocks
->llist
, &cinode
->llist
);
713 up_write(&cinode
->lock_sem
);
715 spin_lock(&tcon
->open_file_lock
);
716 if (fid
->pending_open
->oplock
!= CIFS_OPLOCK_NO_CHANGE
&& oplock
)
717 oplock
= fid
->pending_open
->oplock
;
718 list_del(&fid
->pending_open
->olist
);
720 fid
->purge_cache
= false;
721 server
->ops
->set_fid(cfile
, fid
, oplock
);
723 list_add(&cfile
->tlist
, &tcon
->openFileList
);
724 atomic_inc(&tcon
->num_local_opens
);
726 /* if readable file instance put first in list*/
727 spin_lock(&cinode
->open_file_lock
);
728 if (file
->f_mode
& FMODE_READ
)
729 list_add(&cfile
->flist
, &cinode
->openFileList
);
731 list_add_tail(&cfile
->flist
, &cinode
->openFileList
);
732 spin_unlock(&cinode
->open_file_lock
);
733 spin_unlock(&tcon
->open_file_lock
);
735 if (fid
->purge_cache
)
736 cifs_zap_mapping(inode
);
738 file
->private_data
= cfile
;
742 struct cifsFileInfo
*
743 cifsFileInfo_get(struct cifsFileInfo
*cifs_file
)
745 spin_lock(&cifs_file
->file_info_lock
);
746 cifsFileInfo_get_locked(cifs_file
);
747 spin_unlock(&cifs_file
->file_info_lock
);
751 static void cifsFileInfo_put_final(struct cifsFileInfo
*cifs_file
)
753 struct inode
*inode
= d_inode(cifs_file
->dentry
);
754 struct cifsInodeInfo
*cifsi
= CIFS_I(inode
);
755 struct cifsLockInfo
*li
, *tmp
;
756 struct super_block
*sb
= inode
->i_sb
;
759 * Delete any outstanding lock records. We'll lose them when the file
762 cifs_down_write(&cifsi
->lock_sem
);
763 list_for_each_entry_safe(li
, tmp
, &cifs_file
->llist
->locks
, llist
) {
764 list_del(&li
->llist
);
765 cifs_del_lock_waiters(li
);
768 list_del(&cifs_file
->llist
->llist
);
769 kfree(cifs_file
->llist
);
770 up_write(&cifsi
->lock_sem
);
772 cifs_put_tlink(cifs_file
->tlink
);
773 dput(cifs_file
->dentry
);
774 cifs_sb_deactive(sb
);
775 kfree(cifs_file
->symlink_target
);
779 static void cifsFileInfo_put_work(struct work_struct
*work
)
781 struct cifsFileInfo
*cifs_file
= container_of(work
,
782 struct cifsFileInfo
, put
);
784 cifsFileInfo_put_final(cifs_file
);
787 void serverclose_work(struct work_struct
*work
)
789 struct cifsFileInfo
*cifs_file
= container_of(work
,
790 struct cifsFileInfo
, serverclose
);
792 struct cifs_tcon
*tcon
= tlink_tcon(cifs_file
->tlink
);
794 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
800 if (server
->ops
->close_getattr
)
801 rc
= server
->ops
->close_getattr(0, tcon
, cifs_file
);
802 else if (server
->ops
->close
)
803 rc
= server
->ops
->close(0, tcon
, &cifs_file
->fid
);
805 if (rc
== -EBUSY
|| rc
== -EAGAIN
) {
809 } while ((rc
== -EBUSY
|| rc
== -EAGAIN
) && (retries
< MAX_RETRIES
)
812 if (retries
== MAX_RETRIES
)
813 pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES
);
815 if (cifs_file
->offload
)
816 queue_work(fileinfo_put_wq
, &cifs_file
->put
);
818 cifsFileInfo_put_final(cifs_file
);
822 * cifsFileInfo_put - release a reference of file priv data
824 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
826 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
828 void cifsFileInfo_put(struct cifsFileInfo
*cifs_file
)
830 _cifsFileInfo_put(cifs_file
, true, true);
834 * _cifsFileInfo_put - release a reference of file priv data
836 * This may involve closing the filehandle @cifs_file out on the
837 * server. Must be called without holding tcon->open_file_lock,
838 * cinode->open_file_lock and cifs_file->file_info_lock.
840 * If @wait_for_oplock_handler is true and we are releasing the last
841 * reference, wait for any running oplock break handler of the file
842 * and cancel any pending one.
844 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
845 * @wait_oplock_handler: must be false if called from oplock_break_handler
846 * @offload: not offloaded on close and oplock breaks
849 void _cifsFileInfo_put(struct cifsFileInfo
*cifs_file
,
850 bool wait_oplock_handler
, bool offload
)
852 struct inode
*inode
= d_inode(cifs_file
->dentry
);
853 struct cifs_tcon
*tcon
= tlink_tcon(cifs_file
->tlink
);
854 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
855 struct cifsInodeInfo
*cifsi
= CIFS_I(inode
);
856 struct super_block
*sb
= inode
->i_sb
;
857 struct cifs_sb_info
*cifs_sb
= CIFS_SB(sb
);
858 struct cifs_fid fid
= {};
859 struct cifs_pending_open open
;
860 bool oplock_break_cancelled
;
861 bool serverclose_offloaded
= false;
863 spin_lock(&tcon
->open_file_lock
);
864 spin_lock(&cifsi
->open_file_lock
);
865 spin_lock(&cifs_file
->file_info_lock
);
867 cifs_file
->offload
= offload
;
868 if (--cifs_file
->count
> 0) {
869 spin_unlock(&cifs_file
->file_info_lock
);
870 spin_unlock(&cifsi
->open_file_lock
);
871 spin_unlock(&tcon
->open_file_lock
);
874 spin_unlock(&cifs_file
->file_info_lock
);
876 if (server
->ops
->get_lease_key
)
877 server
->ops
->get_lease_key(inode
, &fid
);
879 /* store open in pending opens to make sure we don't miss lease break */
880 cifs_add_pending_open_locked(&fid
, cifs_file
->tlink
, &open
);
882 /* remove it from the lists */
883 list_del(&cifs_file
->flist
);
884 list_del(&cifs_file
->tlist
);
885 atomic_dec(&tcon
->num_local_opens
);
887 if (list_empty(&cifsi
->openFileList
)) {
888 cifs_dbg(FYI
, "closing last open instance for inode %p\n",
889 d_inode(cifs_file
->dentry
));
891 * In strict cache mode we need invalidate mapping on the last
892 * close because it may cause a error when we open this file
893 * again and get at least level II oplock.
895 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_STRICT_IO
)
896 set_bit(CIFS_INO_INVALID_MAPPING
, &cifsi
->flags
);
897 cifs_set_oplock_level(cifsi
, 0);
900 spin_unlock(&cifsi
->open_file_lock
);
901 spin_unlock(&tcon
->open_file_lock
);
903 oplock_break_cancelled
= wait_oplock_handler
?
904 cancel_work_sync(&cifs_file
->oplock_break
) : false;
906 if (!tcon
->need_reconnect
&& !cifs_file
->invalidHandle
) {
907 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
912 if (server
->ops
->close_getattr
)
913 rc
= server
->ops
->close_getattr(xid
, tcon
, cifs_file
);
914 else if (server
->ops
->close
)
915 rc
= server
->ops
->close(xid
, tcon
, &cifs_file
->fid
);
918 if (rc
== -EBUSY
|| rc
== -EAGAIN
) {
919 // Server close failed, hence offloading it as an async op
920 queue_work(serverclose_wq
, &cifs_file
->serverclose
);
921 serverclose_offloaded
= true;
925 if (oplock_break_cancelled
)
926 cifs_done_oplock_break(cifsi
);
928 cifs_del_pending_open(&open
);
930 // if serverclose has been offloaded to wq (on failure), it will
931 // handle offloading put as well. If serverclose not offloaded,
932 // we need to handle offloading put here.
933 if (!serverclose_offloaded
) {
935 queue_work(fileinfo_put_wq
, &cifs_file
->put
);
937 cifsFileInfo_put_final(cifs_file
);
941 int cifs_open(struct inode
*inode
, struct file
*file
)
947 struct cifs_sb_info
*cifs_sb
;
948 struct TCP_Server_Info
*server
;
949 struct cifs_tcon
*tcon
;
950 struct tcon_link
*tlink
;
951 struct cifsFileInfo
*cfile
= NULL
;
953 const char *full_path
;
954 bool posix_open_ok
= false;
955 struct cifs_fid fid
= {};
956 struct cifs_pending_open open
;
957 struct cifs_open_info_data data
= {};
961 cifs_sb
= CIFS_SB(inode
->i_sb
);
962 if (unlikely(cifs_forced_shutdown(cifs_sb
))) {
967 tlink
= cifs_sb_tlink(cifs_sb
);
970 return PTR_ERR(tlink
);
972 tcon
= tlink_tcon(tlink
);
973 server
= tcon
->ses
->server
;
975 page
= alloc_dentry_path();
976 full_path
= build_path_from_dentry(file_dentry(file
), page
);
977 if (IS_ERR(full_path
)) {
978 rc
= PTR_ERR(full_path
);
982 cifs_dbg(FYI
, "inode = 0x%p file flags are 0x%x for %s\n",
983 inode
, file
->f_flags
, full_path
);
985 if (file
->f_flags
& O_DIRECT
&&
986 cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_STRICT_IO
) {
987 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_BRL
)
988 file
->f_op
= &cifs_file_direct_nobrl_ops
;
990 file
->f_op
= &cifs_file_direct_ops
;
993 /* Get the cached handle as SMB2 close is deferred */
994 if (OPEN_FMODE(file
->f_flags
) & FMODE_WRITE
) {
995 rc
= cifs_get_writable_path(tcon
, full_path
, FIND_WR_FSUID_ONLY
, &cfile
);
997 rc
= cifs_get_readable_path(tcon
, full_path
, &cfile
);
1000 if (file
->f_flags
== cfile
->f_flags
) {
1001 file
->private_data
= cfile
;
1002 spin_lock(&CIFS_I(inode
)->deferred_lock
);
1003 cifs_del_deferred_close(cfile
);
1004 spin_unlock(&CIFS_I(inode
)->deferred_lock
);
1007 _cifsFileInfo_put(cfile
, true, false);
1011 if (server
->oplocks
)
1012 oplock
= REQ_OPLOCK
;
1016 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1017 if (!tcon
->broken_posix_open
&& tcon
->unix_ext
&&
1018 cap_unix(tcon
->ses
) && (CIFS_UNIX_POSIX_PATH_OPS_CAP
&
1019 le64_to_cpu(tcon
->fsUnixInfo
.Capability
))) {
1020 /* can not refresh inode info since size could be stale */
1021 rc
= cifs_posix_open(full_path
, &inode
, inode
->i_sb
,
1022 cifs_sb
->ctx
->file_mode
/* ignored */,
1023 file
->f_flags
, &oplock
, &fid
.netfid
, xid
);
1025 cifs_dbg(FYI
, "posix open succeeded\n");
1026 posix_open_ok
= true;
1027 } else if ((rc
== -EINVAL
) || (rc
== -EOPNOTSUPP
)) {
1028 if (tcon
->ses
->serverNOS
)
1029 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",
1031 tcon
->ses
->serverNOS
);
1032 tcon
->broken_posix_open
= true;
1033 } else if ((rc
!= -EIO
) && (rc
!= -EREMOTE
) &&
1034 (rc
!= -EOPNOTSUPP
)) /* path not found or net err */
1037 * Else fallthrough to retry open the old way on network i/o
1041 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1043 if (server
->ops
->get_lease_key
)
1044 server
->ops
->get_lease_key(inode
, &fid
);
1046 cifs_add_pending_open(&fid
, tlink
, &open
);
1048 if (!posix_open_ok
) {
1049 if (server
->ops
->get_lease_key
)
1050 server
->ops
->get_lease_key(inode
, &fid
);
1052 rc
= cifs_nt_open(full_path
, inode
, cifs_sb
, tcon
, file
->f_flags
, &oplock
, &fid
,
1055 cifs_del_pending_open(&open
);
1060 cfile
= cifs_new_fileinfo(&fid
, file
, tlink
, oplock
, data
.symlink_target
);
1061 if (cfile
== NULL
) {
1062 if (server
->ops
->close
)
1063 server
->ops
->close(xid
, tcon
, &fid
);
1064 cifs_del_pending_open(&open
);
1069 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1070 if ((oplock
& CIFS_CREATE_ACTION
) && !posix_open_ok
&& tcon
->unix_ext
) {
1072 * Time to set mode which we can not set earlier due to
1073 * problems creating new read-only files.
1075 struct cifs_unix_set_info_args args
= {
1076 .mode
= inode
->i_mode
,
1077 .uid
= INVALID_UID
, /* no change */
1078 .gid
= INVALID_GID
, /* no change */
1079 .ctime
= NO_CHANGE_64
,
1080 .atime
= NO_CHANGE_64
,
1081 .mtime
= NO_CHANGE_64
,
1084 CIFSSMBUnixSetFileInfo(xid
, tcon
, &args
, fid
.netfid
,
1087 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1090 fscache_use_cookie(cifs_inode_cookie(file_inode(file
)),
1091 file
->f_mode
& FMODE_WRITE
);
1092 if (!(file
->f_flags
& O_DIRECT
))
1094 if ((file
->f_flags
& (O_ACCMODE
| O_APPEND
)) == O_RDONLY
)
1096 cifs_invalidate_cache(file_inode(file
), FSCACHE_INVAL_DIO_WRITE
);
1099 free_dentry_path(page
);
1101 cifs_put_tlink(tlink
);
1102 cifs_free_open_info(&data
);
1106 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1107 static int cifs_push_posix_locks(struct cifsFileInfo
*cfile
);
1108 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1111 * Try to reacquire byte range locks that were released when session
1112 * to server was lost.
1115 cifs_relock_file(struct cifsFileInfo
*cfile
)
1117 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1118 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
1120 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1121 struct cifs_sb_info
*cifs_sb
= CIFS_SB(cfile
->dentry
->d_sb
);
1122 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1124 down_read_nested(&cinode
->lock_sem
, SINGLE_DEPTH_NESTING
);
1125 if (cinode
->can_cache_brlcks
) {
1126 /* can cache locks - no need to relock */
1127 up_read(&cinode
->lock_sem
);
1131 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1132 if (cap_unix(tcon
->ses
) &&
1133 (CIFS_UNIX_FCNTL_CAP
& le64_to_cpu(tcon
->fsUnixInfo
.Capability
)) &&
1134 ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) == 0))
1135 rc
= cifs_push_posix_locks(cfile
);
1137 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1138 rc
= tcon
->ses
->server
->ops
->push_mand_locks(cfile
);
1140 up_read(&cinode
->lock_sem
);
1145 cifs_reopen_file(struct cifsFileInfo
*cfile
, bool can_flush
)
1150 struct cifs_sb_info
*cifs_sb
;
1151 struct cifs_tcon
*tcon
;
1152 struct TCP_Server_Info
*server
;
1153 struct cifsInodeInfo
*cinode
;
1154 struct inode
*inode
;
1156 const char *full_path
;
1158 int disposition
= FILE_OPEN
;
1159 int create_options
= CREATE_NOT_DIR
;
1160 struct cifs_open_parms oparms
;
1161 int rdwr_for_fscache
= 0;
1164 mutex_lock(&cfile
->fh_mutex
);
1165 if (!cfile
->invalidHandle
) {
1166 mutex_unlock(&cfile
->fh_mutex
);
1171 inode
= d_inode(cfile
->dentry
);
1172 cifs_sb
= CIFS_SB(inode
->i_sb
);
1173 tcon
= tlink_tcon(cfile
->tlink
);
1174 server
= tcon
->ses
->server
;
1177 * Can not grab rename sem here because various ops, including those
1178 * that already have the rename sem can end up causing writepage to get
1179 * called and if the server was down that means we end up here, and we
1180 * can never tell if the caller already has the rename_sem.
1182 page
= alloc_dentry_path();
1183 full_path
= build_path_from_dentry(cfile
->dentry
, page
);
1184 if (IS_ERR(full_path
)) {
1185 mutex_unlock(&cfile
->fh_mutex
);
1186 free_dentry_path(page
);
1188 return PTR_ERR(full_path
);
1191 cifs_dbg(FYI
, "inode = 0x%p file flags 0x%x for %s\n",
1192 inode
, cfile
->f_flags
, full_path
);
1194 if (tcon
->ses
->server
->oplocks
)
1195 oplock
= REQ_OPLOCK
;
1199 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1200 if (tcon
->unix_ext
&& cap_unix(tcon
->ses
) &&
1201 (CIFS_UNIX_POSIX_PATH_OPS_CAP
&
1202 le64_to_cpu(tcon
->fsUnixInfo
.Capability
))) {
1204 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
1205 * original open. Must mask them off for a reopen.
1207 unsigned int oflags
= cfile
->f_flags
&
1208 ~(O_CREAT
| O_EXCL
| O_TRUNC
);
1210 rc
= cifs_posix_open(full_path
, NULL
, inode
->i_sb
,
1211 cifs_sb
->ctx
->file_mode
/* ignored */,
1212 oflags
, &oplock
, &cfile
->fid
.netfid
, xid
);
1214 cifs_dbg(FYI
, "posix reopen succeeded\n");
1215 oparms
.reconnect
= true;
1216 goto reopen_success
;
1219 * fallthrough to retry open the old way on errors, especially
1220 * in the reconnect path it is important to retry hard
1223 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1225 /* If we're caching, we need to be able to fill in around partial writes. */
1226 if (cifs_fscache_enabled(inode
) && (cfile
->f_flags
& O_ACCMODE
) == O_WRONLY
)
1227 rdwr_for_fscache
= 1;
1229 desired_access
= cifs_convert_flags(cfile
->f_flags
, rdwr_for_fscache
);
1231 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
1232 if (cfile
->f_flags
& O_SYNC
)
1233 create_options
|= CREATE_WRITE_THROUGH
;
1235 if (cfile
->f_flags
& O_DIRECT
)
1236 create_options
|= CREATE_NO_BUFFER
;
1238 if (server
->ops
->get_lease_key
)
1239 server
->ops
->get_lease_key(inode
, &cfile
->fid
);
1242 oparms
= (struct cifs_open_parms
) {
1245 .desired_access
= desired_access
,
1246 .create_options
= cifs_create_options(cifs_sb
, create_options
),
1247 .disposition
= disposition
,
1254 * Can not refresh inode by passing in file_info buf to be returned by
1255 * ops->open and then calling get_inode_info with returned buf since
1256 * file might have write behind data that needs to be flushed and server
1257 * version of file size can be stale. If we knew for sure that inode was
1258 * not dirty locally we could do this.
1260 rc
= server
->ops
->open(xid
, &oparms
, &oplock
, NULL
);
1261 if (rc
== -ENOENT
&& oparms
.reconnect
== false) {
1262 /* durable handle timeout is expired - open the file again */
1263 rc
= server
->ops
->open(xid
, &oparms
, &oplock
, NULL
);
1264 /* indicate that we need to relock the file */
1265 oparms
.reconnect
= true;
1267 if (rc
== -EACCES
&& rdwr_for_fscache
== 1) {
1268 desired_access
= cifs_convert_flags(cfile
->f_flags
, 0);
1269 rdwr_for_fscache
= 2;
1274 mutex_unlock(&cfile
->fh_mutex
);
1275 cifs_dbg(FYI
, "cifs_reopen returned 0x%x\n", rc
);
1276 cifs_dbg(FYI
, "oplock: %d\n", oplock
);
1277 goto reopen_error_exit
;
1280 if (rdwr_for_fscache
== 2)
1281 cifs_invalidate_cache(inode
, FSCACHE_INVAL_DIO_WRITE
);
1283 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1285 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1286 cfile
->invalidHandle
= false;
1287 mutex_unlock(&cfile
->fh_mutex
);
1288 cinode
= CIFS_I(inode
);
1291 rc
= filemap_write_and_wait(inode
->i_mapping
);
1292 if (!is_interrupt_error(rc
))
1293 mapping_set_error(inode
->i_mapping
, rc
);
1295 if (tcon
->posix_extensions
) {
1296 rc
= smb311_posix_get_inode_info(&inode
, full_path
,
1297 NULL
, inode
->i_sb
, xid
);
1298 } else if (tcon
->unix_ext
) {
1299 rc
= cifs_get_inode_info_unix(&inode
, full_path
,
1302 rc
= cifs_get_inode_info(&inode
, full_path
, NULL
,
1303 inode
->i_sb
, xid
, NULL
);
1307 * Else we are writing out data to server already and could deadlock if
1308 * we tried to flush data, and since we do not know if we have data that
1309 * would invalidate the current end of file on the server we can not go
1310 * to the server to get the new inode info.
1314 * If the server returned a read oplock and we have mandatory brlocks,
1315 * set oplock level to None.
1317 if (server
->ops
->is_read_op(oplock
) && cifs_has_mand_locks(cinode
)) {
1318 cifs_dbg(FYI
, "Reset oplock val from read to None due to mand locks\n");
1322 server
->ops
->set_fid(cfile
, &cfile
->fid
, oplock
);
1323 if (oparms
.reconnect
)
1324 cifs_relock_file(cfile
);
1327 free_dentry_path(page
);
1332 void smb2_deferred_work_close(struct work_struct
*work
)
1334 struct cifsFileInfo
*cfile
= container_of(work
,
1335 struct cifsFileInfo
, deferred
.work
);
1337 spin_lock(&CIFS_I(d_inode(cfile
->dentry
))->deferred_lock
);
1338 cifs_del_deferred_close(cfile
);
1339 cfile
->deferred_close_scheduled
= false;
1340 spin_unlock(&CIFS_I(d_inode(cfile
->dentry
))->deferred_lock
);
1341 _cifsFileInfo_put(cfile
, true, false);
1345 smb2_can_defer_close(struct inode
*inode
, struct cifs_deferred_close
*dclose
)
1347 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
1348 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
1350 return (cifs_sb
->ctx
->closetimeo
&& cinode
->lease_granted
&& dclose
&&
1351 (cinode
->oplock
== CIFS_CACHE_RHW_FLG
||
1352 cinode
->oplock
== CIFS_CACHE_RH_FLG
) &&
1353 !test_bit(CIFS_INO_CLOSE_ON_LOCK
, &cinode
->flags
));
1357 int cifs_close(struct inode
*inode
, struct file
*file
)
1359 struct cifsFileInfo
*cfile
;
1360 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
1361 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
1362 struct cifs_deferred_close
*dclose
;
1364 cifs_fscache_unuse_inode_cookie(inode
, file
->f_mode
& FMODE_WRITE
);
1366 if (file
->private_data
!= NULL
) {
1367 cfile
= file
->private_data
;
1368 file
->private_data
= NULL
;
1369 dclose
= kmalloc(sizeof(struct cifs_deferred_close
), GFP_KERNEL
);
1370 if ((cfile
->status_file_deleted
== false) &&
1371 (smb2_can_defer_close(inode
, dclose
))) {
1372 if (test_and_clear_bit(NETFS_ICTX_MODIFIED_ATTR
, &cinode
->netfs
.flags
)) {
1373 inode_set_mtime_to_ts(inode
,
1374 inode_set_ctime_current(inode
));
1376 spin_lock(&cinode
->deferred_lock
);
1377 cifs_add_deferred_close(cfile
, dclose
);
1378 if (cfile
->deferred_close_scheduled
&&
1379 delayed_work_pending(&cfile
->deferred
)) {
1381 * If there is no pending work, mod_delayed_work queues new work.
1382 * So, Increase the ref count to avoid use-after-free.
1384 if (!mod_delayed_work(deferredclose_wq
,
1385 &cfile
->deferred
, cifs_sb
->ctx
->closetimeo
))
1386 cifsFileInfo_get(cfile
);
1388 /* Deferred close for files */
1389 queue_delayed_work(deferredclose_wq
,
1390 &cfile
->deferred
, cifs_sb
->ctx
->closetimeo
);
1391 cfile
->deferred_close_scheduled
= true;
1392 spin_unlock(&cinode
->deferred_lock
);
1395 spin_unlock(&cinode
->deferred_lock
);
1396 _cifsFileInfo_put(cfile
, true, false);
1398 _cifsFileInfo_put(cfile
, true, false);
1403 /* return code from the ->release op is always ignored */
1408 cifs_reopen_persistent_handles(struct cifs_tcon
*tcon
)
1410 struct cifsFileInfo
*open_file
, *tmp
;
1411 LIST_HEAD(tmp_list
);
1413 if (!tcon
->use_persistent
|| !tcon
->need_reopen_files
)
1416 tcon
->need_reopen_files
= false;
1418 cifs_dbg(FYI
, "Reopen persistent handles\n");
1420 /* list all files open on tree connection, reopen resilient handles */
1421 spin_lock(&tcon
->open_file_lock
);
1422 list_for_each_entry(open_file
, &tcon
->openFileList
, tlist
) {
1423 if (!open_file
->invalidHandle
)
1425 cifsFileInfo_get(open_file
);
1426 list_add_tail(&open_file
->rlist
, &tmp_list
);
1428 spin_unlock(&tcon
->open_file_lock
);
1430 list_for_each_entry_safe(open_file
, tmp
, &tmp_list
, rlist
) {
1431 if (cifs_reopen_file(open_file
, false /* do not flush */))
1432 tcon
->need_reopen_files
= true;
1433 list_del_init(&open_file
->rlist
);
1434 cifsFileInfo_put(open_file
);
1438 int cifs_closedir(struct inode
*inode
, struct file
*file
)
1442 struct cifsFileInfo
*cfile
= file
->private_data
;
1443 struct cifs_tcon
*tcon
;
1444 struct TCP_Server_Info
*server
;
1447 cifs_dbg(FYI
, "Closedir inode = 0x%p\n", inode
);
1453 tcon
= tlink_tcon(cfile
->tlink
);
1454 server
= tcon
->ses
->server
;
1456 cifs_dbg(FYI
, "Freeing private data in close dir\n");
1457 spin_lock(&cfile
->file_info_lock
);
1458 if (server
->ops
->dir_needs_close(cfile
)) {
1459 cfile
->invalidHandle
= true;
1460 spin_unlock(&cfile
->file_info_lock
);
1461 if (server
->ops
->close_dir
)
1462 rc
= server
->ops
->close_dir(xid
, tcon
, &cfile
->fid
);
1465 cifs_dbg(FYI
, "Closing uncompleted readdir with rc %d\n", rc
);
1466 /* not much we can do if it fails anyway, ignore rc */
1469 spin_unlock(&cfile
->file_info_lock
);
1471 buf
= cfile
->srch_inf
.ntwrk_buf_start
;
1473 cifs_dbg(FYI
, "closedir free smb buf in srch struct\n");
1474 cfile
->srch_inf
.ntwrk_buf_start
= NULL
;
1475 if (cfile
->srch_inf
.smallBuf
)
1476 cifs_small_buf_release(buf
);
1478 cifs_buf_release(buf
);
1481 cifs_put_tlink(cfile
->tlink
);
1482 kfree(file
->private_data
);
1483 file
->private_data
= NULL
;
1484 /* BB can we lock the filestruct while this is going on? */
1489 static struct cifsLockInfo
*
1490 cifs_lock_init(__u64 offset
, __u64 length
, __u8 type
, __u16 flags
)
1492 struct cifsLockInfo
*lock
=
1493 kmalloc(sizeof(struct cifsLockInfo
), GFP_KERNEL
);
1496 lock
->offset
= offset
;
1497 lock
->length
= length
;
1499 lock
->pid
= current
->tgid
;
1500 lock
->flags
= flags
;
1501 INIT_LIST_HEAD(&lock
->blist
);
1502 init_waitqueue_head(&lock
->block_q
);
1507 cifs_del_lock_waiters(struct cifsLockInfo
*lock
)
1509 struct cifsLockInfo
*li
, *tmp
;
1510 list_for_each_entry_safe(li
, tmp
, &lock
->blist
, blist
) {
1511 list_del_init(&li
->blist
);
1512 wake_up(&li
->block_q
);
1516 #define CIFS_LOCK_OP 0
1517 #define CIFS_READ_OP 1
1518 #define CIFS_WRITE_OP 2
1520 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1522 cifs_find_fid_lock_conflict(struct cifs_fid_locks
*fdlocks
, __u64 offset
,
1523 __u64 length
, __u8 type
, __u16 flags
,
1524 struct cifsFileInfo
*cfile
,
1525 struct cifsLockInfo
**conf_lock
, int rw_check
)
1527 struct cifsLockInfo
*li
;
1528 struct cifsFileInfo
*cur_cfile
= fdlocks
->cfile
;
1529 struct TCP_Server_Info
*server
= tlink_tcon(cfile
->tlink
)->ses
->server
;
1531 list_for_each_entry(li
, &fdlocks
->locks
, llist
) {
1532 if (offset
+ length
<= li
->offset
||
1533 offset
>= li
->offset
+ li
->length
)
1535 if (rw_check
!= CIFS_LOCK_OP
&& current
->tgid
== li
->pid
&&
1536 server
->ops
->compare_fids(cfile
, cur_cfile
)) {
1537 /* shared lock prevents write op through the same fid */
1538 if (!(li
->type
& server
->vals
->shared_lock_type
) ||
1539 rw_check
!= CIFS_WRITE_OP
)
1542 if ((type
& server
->vals
->shared_lock_type
) &&
1543 ((server
->ops
->compare_fids(cfile
, cur_cfile
) &&
1544 current
->tgid
== li
->pid
) || type
== li
->type
))
1546 if (rw_check
== CIFS_LOCK_OP
&&
1547 (flags
& FL_OFDLCK
) && (li
->flags
& FL_OFDLCK
) &&
1548 server
->ops
->compare_fids(cfile
, cur_cfile
))
1558 cifs_find_lock_conflict(struct cifsFileInfo
*cfile
, __u64 offset
, __u64 length
,
1559 __u8 type
, __u16 flags
,
1560 struct cifsLockInfo
**conf_lock
, int rw_check
)
1563 struct cifs_fid_locks
*cur
;
1564 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1566 list_for_each_entry(cur
, &cinode
->llist
, llist
) {
1567 rc
= cifs_find_fid_lock_conflict(cur
, offset
, length
, type
,
1568 flags
, cfile
, conf_lock
,
1578 * Check if there is another lock that prevents us to set the lock (mandatory
1579 * style). If such a lock exists, update the flock structure with its
1580 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1581 * or leave it the same if we can't. Returns 0 if we don't need to request to
1582 * the server or 1 otherwise.
1585 cifs_lock_test(struct cifsFileInfo
*cfile
, __u64 offset
, __u64 length
,
1586 __u8 type
, struct file_lock
*flock
)
1589 struct cifsLockInfo
*conf_lock
;
1590 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1591 struct TCP_Server_Info
*server
= tlink_tcon(cfile
->tlink
)->ses
->server
;
1594 down_read(&cinode
->lock_sem
);
1596 exist
= cifs_find_lock_conflict(cfile
, offset
, length
, type
,
1597 flock
->c
.flc_flags
, &conf_lock
,
1600 flock
->fl_start
= conf_lock
->offset
;
1601 flock
->fl_end
= conf_lock
->offset
+ conf_lock
->length
- 1;
1602 flock
->c
.flc_pid
= conf_lock
->pid
;
1603 if (conf_lock
->type
& server
->vals
->shared_lock_type
)
1604 flock
->c
.flc_type
= F_RDLCK
;
1606 flock
->c
.flc_type
= F_WRLCK
;
1607 } else if (!cinode
->can_cache_brlcks
)
1610 flock
->c
.flc_type
= F_UNLCK
;
1612 up_read(&cinode
->lock_sem
);
1617 cifs_lock_add(struct cifsFileInfo
*cfile
, struct cifsLockInfo
*lock
)
1619 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1620 cifs_down_write(&cinode
->lock_sem
);
1621 list_add_tail(&lock
->llist
, &cfile
->llist
->locks
);
1622 up_write(&cinode
->lock_sem
);
1626 * Set the byte-range lock (mandatory style). Returns:
1627 * 1) 0, if we set the lock and don't need to request to the server;
1628 * 2) 1, if no locks prevent us but we need to request to the server;
1629 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1632 cifs_lock_add_if(struct cifsFileInfo
*cfile
, struct cifsLockInfo
*lock
,
1635 struct cifsLockInfo
*conf_lock
;
1636 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1642 cifs_down_write(&cinode
->lock_sem
);
1644 exist
= cifs_find_lock_conflict(cfile
, lock
->offset
, lock
->length
,
1645 lock
->type
, lock
->flags
, &conf_lock
,
1647 if (!exist
&& cinode
->can_cache_brlcks
) {
1648 list_add_tail(&lock
->llist
, &cfile
->llist
->locks
);
1649 up_write(&cinode
->lock_sem
);
1658 list_add_tail(&lock
->blist
, &conf_lock
->blist
);
1659 up_write(&cinode
->lock_sem
);
1660 rc
= wait_event_interruptible(lock
->block_q
,
1661 (lock
->blist
.prev
== &lock
->blist
) &&
1662 (lock
->blist
.next
== &lock
->blist
));
1665 cifs_down_write(&cinode
->lock_sem
);
1666 list_del_init(&lock
->blist
);
1669 up_write(&cinode
->lock_sem
);
1673 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1675 * Check if there is another lock that prevents us to set the lock (posix
1676 * style). If such a lock exists, update the flock structure with its
1677 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1678 * or leave it the same if we can't. Returns 0 if we don't need to request to
1679 * the server or 1 otherwise.
1682 cifs_posix_lock_test(struct file
*file
, struct file_lock
*flock
)
1685 struct cifsInodeInfo
*cinode
= CIFS_I(file_inode(file
));
1686 unsigned char saved_type
= flock
->c
.flc_type
;
1688 if ((flock
->c
.flc_flags
& FL_POSIX
) == 0)
1691 down_read(&cinode
->lock_sem
);
1692 posix_test_lock(file
, flock
);
1694 if (lock_is_unlock(flock
) && !cinode
->can_cache_brlcks
) {
1695 flock
->c
.flc_type
= saved_type
;
1699 up_read(&cinode
->lock_sem
);
1704 * Set the byte-range lock (posix style). Returns:
1705 * 1) <0, if the error occurs while setting the lock;
1706 * 2) 0, if we set the lock and don't need to request to the server;
1707 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1708 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1711 cifs_posix_lock_set(struct file
*file
, struct file_lock
*flock
)
1713 struct cifsInodeInfo
*cinode
= CIFS_I(file_inode(file
));
1714 int rc
= FILE_LOCK_DEFERRED
+ 1;
1716 if ((flock
->c
.flc_flags
& FL_POSIX
) == 0)
1719 cifs_down_write(&cinode
->lock_sem
);
1720 if (!cinode
->can_cache_brlcks
) {
1721 up_write(&cinode
->lock_sem
);
1725 rc
= posix_lock_file(file
, flock
, NULL
);
1726 up_write(&cinode
->lock_sem
);
1731 cifs_push_mandatory_locks(struct cifsFileInfo
*cfile
)
1734 int rc
= 0, stored_rc
;
1735 struct cifsLockInfo
*li
, *tmp
;
1736 struct cifs_tcon
*tcon
;
1737 unsigned int num
, max_num
, max_buf
;
1738 LOCKING_ANDX_RANGE
*buf
, *cur
;
1739 static const int types
[] = {
1740 LOCKING_ANDX_LARGE_FILES
,
1741 LOCKING_ANDX_SHARED_LOCK
| LOCKING_ANDX_LARGE_FILES
1746 tcon
= tlink_tcon(cfile
->tlink
);
1749 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1750 * and check it before using.
1752 max_buf
= tcon
->ses
->server
->maxBuf
;
1753 if (max_buf
< (sizeof(struct smb_hdr
) + sizeof(LOCKING_ANDX_RANGE
))) {
1758 BUILD_BUG_ON(sizeof(struct smb_hdr
) + sizeof(LOCKING_ANDX_RANGE
) >
1760 max_buf
= min_t(unsigned int, max_buf
- sizeof(struct smb_hdr
),
1762 max_num
= (max_buf
- sizeof(struct smb_hdr
)) /
1763 sizeof(LOCKING_ANDX_RANGE
);
1764 buf
= kcalloc(max_num
, sizeof(LOCKING_ANDX_RANGE
), GFP_KERNEL
);
1770 for (i
= 0; i
< 2; i
++) {
1773 list_for_each_entry_safe(li
, tmp
, &cfile
->llist
->locks
, llist
) {
1774 if (li
->type
!= types
[i
])
1776 cur
->Pid
= cpu_to_le16(li
->pid
);
1777 cur
->LengthLow
= cpu_to_le32((u32
)li
->length
);
1778 cur
->LengthHigh
= cpu_to_le32((u32
)(li
->length
>>32));
1779 cur
->OffsetLow
= cpu_to_le32((u32
)li
->offset
);
1780 cur
->OffsetHigh
= cpu_to_le32((u32
)(li
->offset
>>32));
1781 if (++num
== max_num
) {
1782 stored_rc
= cifs_lockv(xid
, tcon
,
1784 (__u8
)li
->type
, 0, num
,
1795 stored_rc
= cifs_lockv(xid
, tcon
, cfile
->fid
.netfid
,
1796 (__u8
)types
[i
], 0, num
, buf
);
1808 hash_lockowner(fl_owner_t owner
)
1810 return cifs_lock_secret
^ hash32_ptr((const void *)owner
);
1812 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1814 struct lock_to_push
{
1815 struct list_head llist
;
1823 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1825 cifs_push_posix_locks(struct cifsFileInfo
*cfile
)
1827 struct inode
*inode
= d_inode(cfile
->dentry
);
1828 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
1829 struct file_lock
*flock
;
1830 struct file_lock_context
*flctx
= locks_inode_context(inode
);
1831 unsigned int count
= 0, i
;
1832 int rc
= 0, xid
, type
;
1833 struct list_head locks_to_send
, *el
;
1834 struct lock_to_push
*lck
, *tmp
;
1842 spin_lock(&flctx
->flc_lock
);
1843 list_for_each(el
, &flctx
->flc_posix
) {
1846 spin_unlock(&flctx
->flc_lock
);
1848 INIT_LIST_HEAD(&locks_to_send
);
1851 * Allocating count locks is enough because no FL_POSIX locks can be
1852 * added to the list while we are holding cinode->lock_sem that
1853 * protects locking operations of this inode.
1855 for (i
= 0; i
< count
; i
++) {
1856 lck
= kmalloc(sizeof(struct lock_to_push
), GFP_KERNEL
);
1861 list_add_tail(&lck
->llist
, &locks_to_send
);
1864 el
= locks_to_send
.next
;
1865 spin_lock(&flctx
->flc_lock
);
1866 for_each_file_lock(flock
, &flctx
->flc_posix
) {
1867 unsigned char ftype
= flock
->c
.flc_type
;
1869 if (el
== &locks_to_send
) {
1871 * The list ended. We don't have enough allocated
1872 * structures - something is really wrong.
1874 cifs_dbg(VFS
, "Can't push all brlocks!\n");
1877 length
= cifs_flock_len(flock
);
1878 if (ftype
== F_RDLCK
|| ftype
== F_SHLCK
)
1882 lck
= list_entry(el
, struct lock_to_push
, llist
);
1883 lck
->pid
= hash_lockowner(flock
->c
.flc_owner
);
1884 lck
->netfid
= cfile
->fid
.netfid
;
1885 lck
->length
= length
;
1887 lck
->offset
= flock
->fl_start
;
1889 spin_unlock(&flctx
->flc_lock
);
1891 list_for_each_entry_safe(lck
, tmp
, &locks_to_send
, llist
) {
1894 stored_rc
= CIFSSMBPosixLock(xid
, tcon
, lck
->netfid
, lck
->pid
,
1895 lck
->offset
, lck
->length
, NULL
,
1899 list_del(&lck
->llist
);
1907 list_for_each_entry_safe(lck
, tmp
, &locks_to_send
, llist
) {
1908 list_del(&lck
->llist
);
1913 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1916 cifs_push_locks(struct cifsFileInfo
*cfile
)
1918 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1919 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
1921 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1922 struct cifs_sb_info
*cifs_sb
= CIFS_SB(cfile
->dentry
->d_sb
);
1923 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1925 /* we are going to update can_cache_brlcks here - need a write access */
1926 cifs_down_write(&cinode
->lock_sem
);
1927 if (!cinode
->can_cache_brlcks
) {
1928 up_write(&cinode
->lock_sem
);
1932 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1933 if (cap_unix(tcon
->ses
) &&
1934 (CIFS_UNIX_FCNTL_CAP
& le64_to_cpu(tcon
->fsUnixInfo
.Capability
)) &&
1935 ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) == 0))
1936 rc
= cifs_push_posix_locks(cfile
);
1938 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1939 rc
= tcon
->ses
->server
->ops
->push_mand_locks(cfile
);
1941 cinode
->can_cache_brlcks
= false;
1942 up_write(&cinode
->lock_sem
);
1947 cifs_read_flock(struct file_lock
*flock
, __u32
*type
, int *lock
, int *unlock
,
1948 bool *wait_flag
, struct TCP_Server_Info
*server
)
1950 if (flock
->c
.flc_flags
& FL_POSIX
)
1951 cifs_dbg(FYI
, "Posix\n");
1952 if (flock
->c
.flc_flags
& FL_FLOCK
)
1953 cifs_dbg(FYI
, "Flock\n");
1954 if (flock
->c
.flc_flags
& FL_SLEEP
) {
1955 cifs_dbg(FYI
, "Blocking lock\n");
1958 if (flock
->c
.flc_flags
& FL_ACCESS
)
1959 cifs_dbg(FYI
, "Process suspended by mandatory locking - not implemented yet\n");
1960 if (flock
->c
.flc_flags
& FL_LEASE
)
1961 cifs_dbg(FYI
, "Lease on file - not implemented yet\n");
1962 if (flock
->c
.flc_flags
&
1963 (~(FL_POSIX
| FL_FLOCK
| FL_SLEEP
|
1964 FL_ACCESS
| FL_LEASE
| FL_CLOSE
| FL_OFDLCK
)))
1965 cifs_dbg(FYI
, "Unknown lock flags 0x%x\n",
1966 flock
->c
.flc_flags
);
1968 *type
= server
->vals
->large_lock_type
;
1969 if (lock_is_write(flock
)) {
1970 cifs_dbg(FYI
, "F_WRLCK\n");
1971 *type
|= server
->vals
->exclusive_lock_type
;
1973 } else if (lock_is_unlock(flock
)) {
1974 cifs_dbg(FYI
, "F_UNLCK\n");
1975 *type
|= server
->vals
->unlock_lock_type
;
1977 /* Check if unlock includes more than one lock range */
1978 } else if (lock_is_read(flock
)) {
1979 cifs_dbg(FYI
, "F_RDLCK\n");
1980 *type
|= server
->vals
->shared_lock_type
;
1982 } else if (flock
->c
.flc_type
== F_EXLCK
) {
1983 cifs_dbg(FYI
, "F_EXLCK\n");
1984 *type
|= server
->vals
->exclusive_lock_type
;
1986 } else if (flock
->c
.flc_type
== F_SHLCK
) {
1987 cifs_dbg(FYI
, "F_SHLCK\n");
1988 *type
|= server
->vals
->shared_lock_type
;
1991 cifs_dbg(FYI
, "Unknown type of lock\n");
1995 cifs_getlk(struct file
*file
, struct file_lock
*flock
, __u32 type
,
1996 bool wait_flag
, bool posix_lck
, unsigned int xid
)
1999 __u64 length
= cifs_flock_len(flock
);
2000 struct cifsFileInfo
*cfile
= (struct cifsFileInfo
*)file
->private_data
;
2001 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
2002 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2003 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2004 __u16 netfid
= cfile
->fid
.netfid
;
2007 int posix_lock_type
;
2009 rc
= cifs_posix_lock_test(file
, flock
);
2013 if (type
& server
->vals
->shared_lock_type
)
2014 posix_lock_type
= CIFS_RDLCK
;
2016 posix_lock_type
= CIFS_WRLCK
;
2017 rc
= CIFSSMBPosixLock(xid
, tcon
, netfid
,
2018 hash_lockowner(flock
->c
.flc_owner
),
2019 flock
->fl_start
, length
, flock
,
2020 posix_lock_type
, wait_flag
);
2023 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2025 rc
= cifs_lock_test(cfile
, flock
->fl_start
, length
, type
, flock
);
2029 /* BB we could chain these into one lock request BB */
2030 rc
= server
->ops
->mand_lock(xid
, cfile
, flock
->fl_start
, length
, type
,
2033 rc
= server
->ops
->mand_lock(xid
, cfile
, flock
->fl_start
, length
,
2035 flock
->c
.flc_type
= F_UNLCK
;
2037 cifs_dbg(VFS
, "Error unlocking previously locked range %d during test of lock\n",
2042 if (type
& server
->vals
->shared_lock_type
) {
2043 flock
->c
.flc_type
= F_WRLCK
;
2047 type
&= ~server
->vals
->exclusive_lock_type
;
2049 rc
= server
->ops
->mand_lock(xid
, cfile
, flock
->fl_start
, length
,
2050 type
| server
->vals
->shared_lock_type
,
2053 rc
= server
->ops
->mand_lock(xid
, cfile
, flock
->fl_start
, length
,
2054 type
| server
->vals
->shared_lock_type
, 0, 1, false);
2055 flock
->c
.flc_type
= F_RDLCK
;
2057 cifs_dbg(VFS
, "Error unlocking previously locked range %d during test of lock\n",
2060 flock
->c
.flc_type
= F_WRLCK
;
2066 cifs_move_llist(struct list_head
*source
, struct list_head
*dest
)
2068 struct list_head
*li
, *tmp
;
2069 list_for_each_safe(li
, tmp
, source
)
2070 list_move(li
, dest
);
2074 cifs_free_llist(struct list_head
*llist
)
2076 struct cifsLockInfo
*li
, *tmp
;
2077 list_for_each_entry_safe(li
, tmp
, llist
, llist
) {
2078 cifs_del_lock_waiters(li
);
2079 list_del(&li
->llist
);
2084 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2086 cifs_unlock_range(struct cifsFileInfo
*cfile
, struct file_lock
*flock
,
2089 int rc
= 0, stored_rc
;
2090 static const int types
[] = {
2091 LOCKING_ANDX_LARGE_FILES
,
2092 LOCKING_ANDX_SHARED_LOCK
| LOCKING_ANDX_LARGE_FILES
2095 unsigned int max_num
, num
, max_buf
;
2096 LOCKING_ANDX_RANGE
*buf
, *cur
;
2097 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
2098 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
2099 struct cifsLockInfo
*li
, *tmp
;
2100 __u64 length
= cifs_flock_len(flock
);
2101 LIST_HEAD(tmp_llist
);
2104 * Accessing maxBuf is racy with cifs_reconnect - need to store value
2105 * and check it before using.
2107 max_buf
= tcon
->ses
->server
->maxBuf
;
2108 if (max_buf
< (sizeof(struct smb_hdr
) + sizeof(LOCKING_ANDX_RANGE
)))
2111 BUILD_BUG_ON(sizeof(struct smb_hdr
) + sizeof(LOCKING_ANDX_RANGE
) >
2113 max_buf
= min_t(unsigned int, max_buf
- sizeof(struct smb_hdr
),
2115 max_num
= (max_buf
- sizeof(struct smb_hdr
)) /
2116 sizeof(LOCKING_ANDX_RANGE
);
2117 buf
= kcalloc(max_num
, sizeof(LOCKING_ANDX_RANGE
), GFP_KERNEL
);
2121 cifs_down_write(&cinode
->lock_sem
);
2122 for (i
= 0; i
< 2; i
++) {
2125 list_for_each_entry_safe(li
, tmp
, &cfile
->llist
->locks
, llist
) {
2126 if (flock
->fl_start
> li
->offset
||
2127 (flock
->fl_start
+ length
) <
2128 (li
->offset
+ li
->length
))
2130 if (current
->tgid
!= li
->pid
)
2132 if (types
[i
] != li
->type
)
2134 if (cinode
->can_cache_brlcks
) {
2136 * We can cache brlock requests - simply remove
2137 * a lock from the file's list.
2139 list_del(&li
->llist
);
2140 cifs_del_lock_waiters(li
);
2144 cur
->Pid
= cpu_to_le16(li
->pid
);
2145 cur
->LengthLow
= cpu_to_le32((u32
)li
->length
);
2146 cur
->LengthHigh
= cpu_to_le32((u32
)(li
->length
>>32));
2147 cur
->OffsetLow
= cpu_to_le32((u32
)li
->offset
);
2148 cur
->OffsetHigh
= cpu_to_le32((u32
)(li
->offset
>>32));
2150 * We need to save a lock here to let us add it again to
2151 * the file's list if the unlock range request fails on
2154 list_move(&li
->llist
, &tmp_llist
);
2155 if (++num
== max_num
) {
2156 stored_rc
= cifs_lockv(xid
, tcon
,
2158 li
->type
, num
, 0, buf
);
2161 * We failed on the unlock range
2162 * request - add all locks from the tmp
2163 * list to the head of the file's list.
2165 cifs_move_llist(&tmp_llist
,
2166 &cfile
->llist
->locks
);
2170 * The unlock range request succeed -
2171 * free the tmp list.
2173 cifs_free_llist(&tmp_llist
);
2180 stored_rc
= cifs_lockv(xid
, tcon
, cfile
->fid
.netfid
,
2181 types
[i
], num
, 0, buf
);
2183 cifs_move_llist(&tmp_llist
,
2184 &cfile
->llist
->locks
);
2187 cifs_free_llist(&tmp_llist
);
2191 up_write(&cinode
->lock_sem
);
2195 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2198 cifs_setlk(struct file
*file
, struct file_lock
*flock
, __u32 type
,
2199 bool wait_flag
, bool posix_lck
, int lock
, int unlock
,
2203 __u64 length
= cifs_flock_len(flock
);
2204 struct cifsFileInfo
*cfile
= (struct cifsFileInfo
*)file
->private_data
;
2205 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
2206 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
2207 struct inode
*inode
= d_inode(cfile
->dentry
);
2209 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2211 int posix_lock_type
;
2213 rc
= cifs_posix_lock_set(file
, flock
);
2214 if (rc
<= FILE_LOCK_DEFERRED
)
2217 if (type
& server
->vals
->shared_lock_type
)
2218 posix_lock_type
= CIFS_RDLCK
;
2220 posix_lock_type
= CIFS_WRLCK
;
2223 posix_lock_type
= CIFS_UNLCK
;
2225 rc
= CIFSSMBPosixLock(xid
, tcon
, cfile
->fid
.netfid
,
2226 hash_lockowner(flock
->c
.flc_owner
),
2227 flock
->fl_start
, length
,
2228 NULL
, posix_lock_type
, wait_flag
);
2231 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2233 struct cifsLockInfo
*lock
;
2235 lock
= cifs_lock_init(flock
->fl_start
, length
, type
,
2236 flock
->c
.flc_flags
);
2240 rc
= cifs_lock_add_if(cfile
, lock
, wait_flag
);
2249 * Windows 7 server can delay breaking lease from read to None
2250 * if we set a byte-range lock on a file - break it explicitly
2251 * before sending the lock to the server to be sure the next
2252 * read won't conflict with non-overlapted locks due to
2255 if (!CIFS_CACHE_WRITE(CIFS_I(inode
)) &&
2256 CIFS_CACHE_READ(CIFS_I(inode
))) {
2257 cifs_zap_mapping(inode
);
2258 cifs_dbg(FYI
, "Set no oplock for inode=%p due to mand locks\n",
2260 CIFS_I(inode
)->oplock
= 0;
2263 rc
= server
->ops
->mand_lock(xid
, cfile
, flock
->fl_start
, length
,
2264 type
, 1, 0, wait_flag
);
2270 cifs_lock_add(cfile
, lock
);
2272 rc
= server
->ops
->mand_unlock_range(cfile
, flock
, xid
);
2275 if ((flock
->c
.flc_flags
& FL_POSIX
) || (flock
->c
.flc_flags
& FL_FLOCK
)) {
2277 * If this is a request to remove all locks because we
2278 * are closing the file, it doesn't matter if the
2279 * unlocking failed as both cifs.ko and the SMB server
2280 * remove the lock on file close
2283 cifs_dbg(VFS
, "%s failed rc=%d\n", __func__
, rc
);
2284 if (!(flock
->c
.flc_flags
& FL_CLOSE
))
2287 rc
= locks_lock_file_wait(file
, flock
);
2292 int cifs_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2295 int lock
= 0, unlock
= 0;
2296 bool wait_flag
= false;
2297 bool posix_lck
= false;
2298 struct cifs_sb_info
*cifs_sb
;
2299 struct cifs_tcon
*tcon
;
2300 struct cifsFileInfo
*cfile
;
2305 if (!(fl
->c
.flc_flags
& FL_FLOCK
)) {
2311 cfile
= (struct cifsFileInfo
*)file
->private_data
;
2312 tcon
= tlink_tcon(cfile
->tlink
);
2314 cifs_read_flock(fl
, &type
, &lock
, &unlock
, &wait_flag
,
2316 cifs_sb
= CIFS_FILE_SB(file
);
2318 if (cap_unix(tcon
->ses
) &&
2319 (CIFS_UNIX_FCNTL_CAP
& le64_to_cpu(tcon
->fsUnixInfo
.Capability
)) &&
2320 ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) == 0))
2323 if (!lock
&& !unlock
) {
2325 * if no lock or unlock then nothing to do since we do not
2333 rc
= cifs_setlk(file
, fl
, type
, wait_flag
, posix_lck
, lock
, unlock
,
2341 int cifs_lock(struct file
*file
, int cmd
, struct file_lock
*flock
)
2344 int lock
= 0, unlock
= 0;
2345 bool wait_flag
= false;
2346 bool posix_lck
= false;
2347 struct cifs_sb_info
*cifs_sb
;
2348 struct cifs_tcon
*tcon
;
2349 struct cifsFileInfo
*cfile
;
2355 cifs_dbg(FYI
, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__
, file
, cmd
,
2356 flock
->c
.flc_flags
, flock
->c
.flc_type
,
2357 (long long)flock
->fl_start
,
2358 (long long)flock
->fl_end
);
2360 cfile
= (struct cifsFileInfo
*)file
->private_data
;
2361 tcon
= tlink_tcon(cfile
->tlink
);
2363 cifs_read_flock(flock
, &type
, &lock
, &unlock
, &wait_flag
,
2365 cifs_sb
= CIFS_FILE_SB(file
);
2366 set_bit(CIFS_INO_CLOSE_ON_LOCK
, &CIFS_I(d_inode(cfile
->dentry
))->flags
);
2368 if (cap_unix(tcon
->ses
) &&
2369 (CIFS_UNIX_FCNTL_CAP
& le64_to_cpu(tcon
->fsUnixInfo
.Capability
)) &&
2370 ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) == 0))
2373 * BB add code here to normalize offset and length to account for
2374 * negative length which we can not accept over the wire.
2376 if (IS_GETLK(cmd
)) {
2377 rc
= cifs_getlk(file
, flock
, type
, wait_flag
, posix_lck
, xid
);
2382 if (!lock
&& !unlock
) {
2384 * if no lock or unlock then nothing to do since we do not
2391 rc
= cifs_setlk(file
, flock
, type
, wait_flag
, posix_lck
, lock
, unlock
,
2397 void cifs_write_subrequest_terminated(struct cifs_io_subrequest
*wdata
, ssize_t result
,
2400 struct netfs_io_request
*wreq
= wdata
->rreq
;
2401 struct netfs_inode
*ictx
= netfs_inode(wreq
->inode
);
2405 wrend
= wdata
->subreq
.start
+ wdata
->subreq
.transferred
+ result
;
2407 if (wrend
> ictx
->zero_point
&&
2408 (wdata
->rreq
->origin
== NETFS_UNBUFFERED_WRITE
||
2409 wdata
->rreq
->origin
== NETFS_DIO_WRITE
))
2410 ictx
->zero_point
= wrend
;
2411 if (wrend
> ictx
->remote_i_size
)
2412 netfs_resize_file(ictx
, wrend
, true);
2415 netfs_write_subrequest_terminated(&wdata
->subreq
, result
, was_async
);
2418 struct cifsFileInfo
*find_readable_file(struct cifsInodeInfo
*cifs_inode
,
2421 struct cifsFileInfo
*open_file
= NULL
;
2422 struct cifs_sb_info
*cifs_sb
= CIFS_SB(cifs_inode
->netfs
.inode
.i_sb
);
2424 /* only filter by fsuid on multiuser mounts */
2425 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MULTIUSER
))
2428 spin_lock(&cifs_inode
->open_file_lock
);
2429 /* we could simply get the first_list_entry since write-only entries
2430 are always at the end of the list but since the first entry might
2431 have a close pending, we go through the whole list */
2432 list_for_each_entry(open_file
, &cifs_inode
->openFileList
, flist
) {
2433 if (fsuid_only
&& !uid_eq(open_file
->uid
, current_fsuid()))
2435 if (OPEN_FMODE(open_file
->f_flags
) & FMODE_READ
) {
2436 if ((!open_file
->invalidHandle
)) {
2437 /* found a good file */
2438 /* lock it so it will not be closed on us */
2439 cifsFileInfo_get(open_file
);
2440 spin_unlock(&cifs_inode
->open_file_lock
);
2442 } /* else might as well continue, and look for
2443 another, or simply have the caller reopen it
2444 again rather than trying to fix this handle */
2445 } else /* write only file */
2446 break; /* write only files are last so must be done */
2448 spin_unlock(&cifs_inode
->open_file_lock
);
2452 /* Return -EBADF if no handle is found and general rc otherwise */
2454 cifs_get_writable_file(struct cifsInodeInfo
*cifs_inode
, int flags
,
2455 struct cifsFileInfo
**ret_file
)
2457 struct cifsFileInfo
*open_file
, *inv_file
= NULL
;
2458 struct cifs_sb_info
*cifs_sb
;
2459 bool any_available
= false;
2461 unsigned int refind
= 0;
2462 bool fsuid_only
= flags
& FIND_WR_FSUID_ONLY
;
2463 bool with_delete
= flags
& FIND_WR_WITH_DELETE
;
2467 * Having a null inode here (because mapping->host was set to zero by
2468 * the VFS or MM) should not happen but we had reports of on oops (due
2469 * to it being zero) during stress testcases so we need to check for it
2472 if (cifs_inode
== NULL
) {
2473 cifs_dbg(VFS
, "Null inode passed to cifs_writeable_file\n");
2478 cifs_sb
= CIFS_SB(cifs_inode
->netfs
.inode
.i_sb
);
2480 /* only filter by fsuid on multiuser mounts */
2481 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MULTIUSER
))
2484 spin_lock(&cifs_inode
->open_file_lock
);
2486 if (refind
> MAX_REOPEN_ATT
) {
2487 spin_unlock(&cifs_inode
->open_file_lock
);
2490 list_for_each_entry(open_file
, &cifs_inode
->openFileList
, flist
) {
2491 if (!any_available
&& open_file
->pid
!= current
->tgid
)
2493 if (fsuid_only
&& !uid_eq(open_file
->uid
, current_fsuid()))
2495 if (with_delete
&& !(open_file
->fid
.access
& DELETE
))
2497 if (OPEN_FMODE(open_file
->f_flags
) & FMODE_WRITE
) {
2498 if (!open_file
->invalidHandle
) {
2499 /* found a good writable file */
2500 cifsFileInfo_get(open_file
);
2501 spin_unlock(&cifs_inode
->open_file_lock
);
2502 *ret_file
= open_file
;
2506 inv_file
= open_file
;
2510 /* couldn't find usable FH with same pid, try any available */
2511 if (!any_available
) {
2512 any_available
= true;
2513 goto refind_writable
;
2517 any_available
= false;
2518 cifsFileInfo_get(inv_file
);
2521 spin_unlock(&cifs_inode
->open_file_lock
);
2524 rc
= cifs_reopen_file(inv_file
, false);
2526 *ret_file
= inv_file
;
2530 spin_lock(&cifs_inode
->open_file_lock
);
2531 list_move_tail(&inv_file
->flist
, &cifs_inode
->openFileList
);
2532 spin_unlock(&cifs_inode
->open_file_lock
);
2533 cifsFileInfo_put(inv_file
);
2536 spin_lock(&cifs_inode
->open_file_lock
);
2537 goto refind_writable
;
2543 struct cifsFileInfo
*
2544 find_writable_file(struct cifsInodeInfo
*cifs_inode
, int flags
)
2546 struct cifsFileInfo
*cfile
;
2549 rc
= cifs_get_writable_file(cifs_inode
, flags
, &cfile
);
2551 cifs_dbg(FYI
, "Couldn't find writable handle rc=%d\n", rc
);
2557 cifs_get_writable_path(struct cifs_tcon
*tcon
, const char *name
,
2559 struct cifsFileInfo
**ret_file
)
2561 struct cifsFileInfo
*cfile
;
2562 void *page
= alloc_dentry_path();
2566 spin_lock(&tcon
->open_file_lock
);
2567 list_for_each_entry(cfile
, &tcon
->openFileList
, tlist
) {
2568 struct cifsInodeInfo
*cinode
;
2569 const char *full_path
= build_path_from_dentry(cfile
->dentry
, page
);
2570 if (IS_ERR(full_path
)) {
2571 spin_unlock(&tcon
->open_file_lock
);
2572 free_dentry_path(page
);
2573 return PTR_ERR(full_path
);
2575 if (strcmp(full_path
, name
))
2578 cinode
= CIFS_I(d_inode(cfile
->dentry
));
2579 spin_unlock(&tcon
->open_file_lock
);
2580 free_dentry_path(page
);
2581 return cifs_get_writable_file(cinode
, flags
, ret_file
);
2584 spin_unlock(&tcon
->open_file_lock
);
2585 free_dentry_path(page
);
2590 cifs_get_readable_path(struct cifs_tcon
*tcon
, const char *name
,
2591 struct cifsFileInfo
**ret_file
)
2593 struct cifsFileInfo
*cfile
;
2594 void *page
= alloc_dentry_path();
2598 spin_lock(&tcon
->open_file_lock
);
2599 list_for_each_entry(cfile
, &tcon
->openFileList
, tlist
) {
2600 struct cifsInodeInfo
*cinode
;
2601 const char *full_path
= build_path_from_dentry(cfile
->dentry
, page
);
2602 if (IS_ERR(full_path
)) {
2603 spin_unlock(&tcon
->open_file_lock
);
2604 free_dentry_path(page
);
2605 return PTR_ERR(full_path
);
2607 if (strcmp(full_path
, name
))
2610 cinode
= CIFS_I(d_inode(cfile
->dentry
));
2611 spin_unlock(&tcon
->open_file_lock
);
2612 free_dentry_path(page
);
2613 *ret_file
= find_readable_file(cinode
, 0);
2614 return *ret_file
? 0 : -ENOENT
;
2617 spin_unlock(&tcon
->open_file_lock
);
2618 free_dentry_path(page
);
2623 * Flush data on a strict file.
2625 int cifs_strict_fsync(struct file
*file
, loff_t start
, loff_t end
,
2630 struct cifs_tcon
*tcon
;
2631 struct TCP_Server_Info
*server
;
2632 struct cifsFileInfo
*smbfile
= file
->private_data
;
2633 struct inode
*inode
= file_inode(file
);
2634 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2636 rc
= file_write_and_wait_range(file
, start
, end
);
2638 trace_cifs_fsync_err(inode
->i_ino
, rc
);
2644 cifs_dbg(FYI
, "Sync file - name: %pD datasync: 0x%x\n",
2647 if (!CIFS_CACHE_READ(CIFS_I(inode
))) {
2648 rc
= cifs_zap_mapping(inode
);
2650 cifs_dbg(FYI
, "rc: %d during invalidate phase\n", rc
);
2651 rc
= 0; /* don't care about it in fsync */
2655 tcon
= tlink_tcon(smbfile
->tlink
);
2656 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOSSYNC
)) {
2657 server
= tcon
->ses
->server
;
2658 if (server
->ops
->flush
== NULL
) {
2660 goto strict_fsync_exit
;
2663 if ((OPEN_FMODE(smbfile
->f_flags
) & FMODE_WRITE
) == 0) {
2664 smbfile
= find_writable_file(CIFS_I(inode
), FIND_WR_ANY
);
2666 rc
= server
->ops
->flush(xid
, tcon
, &smbfile
->fid
);
2667 cifsFileInfo_put(smbfile
);
2669 cifs_dbg(FYI
, "ignore fsync for file not open for write\n");
2671 rc
= server
->ops
->flush(xid
, tcon
, &smbfile
->fid
);
2680 * Flush data on a non-strict data.
2682 int cifs_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
2686 struct cifs_tcon
*tcon
;
2687 struct TCP_Server_Info
*server
;
2688 struct cifsFileInfo
*smbfile
= file
->private_data
;
2689 struct inode
*inode
= file_inode(file
);
2690 struct cifs_sb_info
*cifs_sb
= CIFS_FILE_SB(file
);
2692 rc
= file_write_and_wait_range(file
, start
, end
);
2694 trace_cifs_fsync_err(file_inode(file
)->i_ino
, rc
);
2700 cifs_dbg(FYI
, "Sync file - name: %pD datasync: 0x%x\n",
2703 tcon
= tlink_tcon(smbfile
->tlink
);
2704 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOSSYNC
)) {
2705 server
= tcon
->ses
->server
;
2706 if (server
->ops
->flush
== NULL
) {
2711 if ((OPEN_FMODE(smbfile
->f_flags
) & FMODE_WRITE
) == 0) {
2712 smbfile
= find_writable_file(CIFS_I(inode
), FIND_WR_ANY
);
2714 rc
= server
->ops
->flush(xid
, tcon
, &smbfile
->fid
);
2715 cifsFileInfo_put(smbfile
);
2717 cifs_dbg(FYI
, "ignore fsync for file not open for write\n");
2719 rc
= server
->ops
->flush(xid
, tcon
, &smbfile
->fid
);
2728 * As file closes, flush all cached write data for this inode checking
2729 * for write behind errors.
2731 int cifs_flush(struct file
*file
, fl_owner_t id
)
2733 struct inode
*inode
= file_inode(file
);
2736 if (file
->f_mode
& FMODE_WRITE
)
2737 rc
= filemap_write_and_wait(inode
->i_mapping
);
2739 cifs_dbg(FYI
, "Flush inode %p file %p rc %d\n", inode
, file
, rc
);
2741 /* get more nuanced writeback errors */
2742 rc
= filemap_check_wb_err(file
->f_mapping
, 0);
2743 trace_cifs_flush_err(inode
->i_ino
, rc
);
2749 cifs_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
2751 struct file
*file
= iocb
->ki_filp
;
2752 struct cifsFileInfo
*cfile
= (struct cifsFileInfo
*)file
->private_data
;
2753 struct inode
*inode
= file
->f_mapping
->host
;
2754 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
2755 struct TCP_Server_Info
*server
= tlink_tcon(cfile
->tlink
)->ses
->server
;
2756 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2759 rc
= netfs_start_io_write(inode
);
2764 * We need to hold the sem to be sure nobody modifies lock list
2765 * with a brlock that prevents writing.
2767 down_read(&cinode
->lock_sem
);
2769 rc
= generic_write_checks(iocb
, from
);
2773 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) &&
2774 (cifs_find_lock_conflict(cfile
, iocb
->ki_pos
, iov_iter_count(from
),
2775 server
->vals
->exclusive_lock_type
, 0,
2776 NULL
, CIFS_WRITE_OP
))) {
2781 rc
= netfs_buffered_write_iter_locked(iocb
, from
, NULL
);
2784 up_read(&cinode
->lock_sem
);
2785 netfs_end_io_write(inode
);
2787 rc
= generic_write_sync(iocb
, rc
);
2792 cifs_strict_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
2794 struct inode
*inode
= file_inode(iocb
->ki_filp
);
2795 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
2796 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2797 struct cifsFileInfo
*cfile
= (struct cifsFileInfo
*)
2798 iocb
->ki_filp
->private_data
;
2799 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
2802 written
= cifs_get_writer(cinode
);
2806 if (CIFS_CACHE_WRITE(cinode
)) {
2807 if (cap_unix(tcon
->ses
) &&
2808 (CIFS_UNIX_FCNTL_CAP
& le64_to_cpu(tcon
->fsUnixInfo
.Capability
)) &&
2809 ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) == 0)) {
2810 written
= netfs_file_write_iter(iocb
, from
);
2813 written
= cifs_writev(iocb
, from
);
2817 * For non-oplocked files in strict cache mode we need to write the data
2818 * to the server exactly from the pos to pos+len-1 rather than flush all
2819 * affected pages because it may cause a error with mandatory locks on
2820 * these pages but not on the region from pos to ppos+len-1.
2822 written
= netfs_file_write_iter(iocb
, from
);
2823 if (CIFS_CACHE_READ(cinode
)) {
2825 * We have read level caching and we have just sent a write
2826 * request to the server thus making data in the cache stale.
2827 * Zap the cache and set oplock/lease level to NONE to avoid
2828 * reading stale data from the cache. All subsequent read
2829 * operations will read new data from the server.
2831 cifs_zap_mapping(inode
);
2832 cifs_dbg(FYI
, "Set Oplock/Lease to NONE for inode=%p after write\n",
2837 cifs_put_writer(cinode
);
2841 ssize_t
cifs_loose_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
2844 struct inode
*inode
= file_inode(iocb
->ki_filp
);
2846 if (iocb
->ki_flags
& IOCB_DIRECT
)
2847 return netfs_unbuffered_read_iter(iocb
, iter
);
2849 rc
= cifs_revalidate_mapping(inode
);
2853 return netfs_file_read_iter(iocb
, iter
);
2856 ssize_t
cifs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
2858 struct inode
*inode
= file_inode(iocb
->ki_filp
);
2859 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
2863 if (iocb
->ki_filp
->f_flags
& O_DIRECT
) {
2864 written
= netfs_unbuffered_write_iter(iocb
, from
);
2865 if (written
> 0 && CIFS_CACHE_READ(cinode
)) {
2866 cifs_zap_mapping(inode
);
2868 "Set no oplock for inode=%p after a write operation\n",
2875 written
= cifs_get_writer(cinode
);
2879 written
= netfs_file_write_iter(iocb
, from
);
2881 if (!CIFS_CACHE_WRITE(CIFS_I(inode
))) {
2882 rc
= filemap_fdatawrite(inode
->i_mapping
);
2884 cifs_dbg(FYI
, "cifs_file_write_iter: %d rc on %p inode\n",
2888 cifs_put_writer(cinode
);
2893 cifs_strict_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
2895 struct inode
*inode
= file_inode(iocb
->ki_filp
);
2896 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
2897 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2898 struct cifsFileInfo
*cfile
= (struct cifsFileInfo
*)
2899 iocb
->ki_filp
->private_data
;
2900 struct cifs_tcon
*tcon
= tlink_tcon(cfile
->tlink
);
2904 * In strict cache mode we need to read from the server all the time
2905 * if we don't have level II oplock because the server can delay mtime
2906 * change - so we can't make a decision about inode invalidating.
2907 * And we can also fail with pagereading if there are mandatory locks
2908 * on pages affected by this read but not on the region from pos to
2911 if (!CIFS_CACHE_READ(cinode
))
2912 return netfs_unbuffered_read_iter(iocb
, to
);
2914 if ((cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NOPOSIXBRL
) == 0) {
2915 if (iocb
->ki_flags
& IOCB_DIRECT
)
2916 return netfs_unbuffered_read_iter(iocb
, to
);
2917 return netfs_buffered_read_iter(iocb
, to
);
2921 * We need to hold the sem to be sure nobody modifies lock list
2922 * with a brlock that prevents reading.
2924 if (iocb
->ki_flags
& IOCB_DIRECT
) {
2925 rc
= netfs_start_io_direct(inode
);
2929 down_read(&cinode
->lock_sem
);
2930 if (!cifs_find_lock_conflict(
2931 cfile
, iocb
->ki_pos
, iov_iter_count(to
),
2932 tcon
->ses
->server
->vals
->shared_lock_type
,
2933 0, NULL
, CIFS_READ_OP
))
2934 rc
= netfs_unbuffered_read_iter_locked(iocb
, to
);
2935 up_read(&cinode
->lock_sem
);
2936 netfs_end_io_direct(inode
);
2938 rc
= netfs_start_io_read(inode
);
2942 down_read(&cinode
->lock_sem
);
2943 if (!cifs_find_lock_conflict(
2944 cfile
, iocb
->ki_pos
, iov_iter_count(to
),
2945 tcon
->ses
->server
->vals
->shared_lock_type
,
2946 0, NULL
, CIFS_READ_OP
))
2947 rc
= filemap_read(iocb
, to
, 0);
2948 up_read(&cinode
->lock_sem
);
2949 netfs_end_io_read(inode
);
2955 static vm_fault_t
cifs_page_mkwrite(struct vm_fault
*vmf
)
2957 return netfs_page_mkwrite(vmf
, NULL
);
2960 static const struct vm_operations_struct cifs_file_vm_ops
= {
2961 .fault
= filemap_fault
,
2962 .map_pages
= filemap_map_pages
,
2963 .page_mkwrite
= cifs_page_mkwrite
,
2966 int cifs_file_strict_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2969 struct inode
*inode
= file_inode(file
);
2973 if (!CIFS_CACHE_READ(CIFS_I(inode
)))
2974 rc
= cifs_zap_mapping(inode
);
2976 rc
= generic_file_mmap(file
, vma
);
2978 vma
->vm_ops
= &cifs_file_vm_ops
;
2984 int cifs_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2990 rc
= cifs_revalidate_file(file
);
2992 cifs_dbg(FYI
, "Validation prior to mmap failed, error=%d\n",
2995 rc
= generic_file_mmap(file
, vma
);
2997 vma
->vm_ops
= &cifs_file_vm_ops
;
3003 static int is_inode_writable(struct cifsInodeInfo
*cifs_inode
)
3005 struct cifsFileInfo
*open_file
;
3007 spin_lock(&cifs_inode
->open_file_lock
);
3008 list_for_each_entry(open_file
, &cifs_inode
->openFileList
, flist
) {
3009 if (OPEN_FMODE(open_file
->f_flags
) & FMODE_WRITE
) {
3010 spin_unlock(&cifs_inode
->open_file_lock
);
3014 spin_unlock(&cifs_inode
->open_file_lock
);
3018 /* We do not want to update the file size from server for inodes
3019 open for write - to avoid races with writepage extending
3020 the file - in the future we could consider allowing
3021 refreshing the inode only on increases in the file size
3022 but this is tricky to do without racing with writebehind
3023 page caching in the current Linux kernel design */
3024 bool is_size_safe_to_change(struct cifsInodeInfo
*cifsInode
, __u64 end_of_file
,
3030 if (is_inode_writable(cifsInode
) ||
3031 ((cifsInode
->oplock
& CIFS_CACHE_RW_FLG
) != 0 && from_readdir
)) {
3032 /* This inode is open for write at least once */
3033 struct cifs_sb_info
*cifs_sb
;
3035 cifs_sb
= CIFS_SB(cifsInode
->netfs
.inode
.i_sb
);
3036 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_DIRECT_IO
) {
3037 /* since no page cache to corrupt on directio
3038 we can change size safely */
3042 if (i_size_read(&cifsInode
->netfs
.inode
) < end_of_file
)
3050 void cifs_oplock_break(struct work_struct
*work
)
3052 struct cifsFileInfo
*cfile
= container_of(work
, struct cifsFileInfo
,
3054 struct inode
*inode
= d_inode(cfile
->dentry
);
3055 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
3056 struct cifsInodeInfo
*cinode
= CIFS_I(inode
);
3057 struct cifs_tcon
*tcon
;
3058 struct TCP_Server_Info
*server
;
3059 struct tcon_link
*tlink
;
3061 bool purge_cache
= false, oplock_break_cancelled
;
3062 __u64 persistent_fid
, volatile_fid
;
3065 wait_on_bit(&cinode
->flags
, CIFS_INODE_PENDING_WRITERS
,
3066 TASK_UNINTERRUPTIBLE
);
3068 tlink
= cifs_sb_tlink(cifs_sb
);
3071 tcon
= tlink_tcon(tlink
);
3072 server
= tcon
->ses
->server
;
3074 server
->ops
->downgrade_oplock(server
, cinode
, cfile
->oplock_level
,
3075 cfile
->oplock_epoch
, &purge_cache
);
3077 if (!CIFS_CACHE_WRITE(cinode
) && CIFS_CACHE_READ(cinode
) &&
3078 cifs_has_mand_locks(cinode
)) {
3079 cifs_dbg(FYI
, "Reset oplock to None for inode=%p due to mand locks\n",
3084 if (inode
&& S_ISREG(inode
->i_mode
)) {
3085 if (CIFS_CACHE_READ(cinode
))
3086 break_lease(inode
, O_RDONLY
);
3088 break_lease(inode
, O_WRONLY
);
3089 rc
= filemap_fdatawrite(inode
->i_mapping
);
3090 if (!CIFS_CACHE_READ(cinode
) || purge_cache
) {
3091 rc
= filemap_fdatawait(inode
->i_mapping
);
3092 mapping_set_error(inode
->i_mapping
, rc
);
3093 cifs_zap_mapping(inode
);
3095 cifs_dbg(FYI
, "Oplock flush inode %p rc %d\n", inode
, rc
);
3096 if (CIFS_CACHE_WRITE(cinode
))
3097 goto oplock_break_ack
;
3100 rc
= cifs_push_locks(cfile
);
3102 cifs_dbg(VFS
, "Push locks rc = %d\n", rc
);
3106 * When oplock break is received and there are no active
3107 * file handles but cached, then schedule deferred close immediately.
3108 * So, new open will not use cached handle.
3111 if (!CIFS_CACHE_HANDLE(cinode
) && !list_empty(&cinode
->deferred_closes
))
3112 cifs_close_deferred_file(cinode
);
3114 persistent_fid
= cfile
->fid
.persistent_fid
;
3115 volatile_fid
= cfile
->fid
.volatile_fid
;
3116 net_fid
= cfile
->fid
.netfid
;
3117 oplock_break_cancelled
= cfile
->oplock_break_cancelled
;
3119 _cifsFileInfo_put(cfile
, false /* do not wait for ourself */, false);
3121 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
3122 * an acknowledgment to be sent when the file has already been closed.
3124 spin_lock(&cinode
->open_file_lock
);
3125 /* check list empty since can race with kill_sb calling tree disconnect */
3126 if (!oplock_break_cancelled
&& !list_empty(&cinode
->openFileList
)) {
3127 spin_unlock(&cinode
->open_file_lock
);
3128 rc
= server
->ops
->oplock_response(tcon
, persistent_fid
,
3129 volatile_fid
, net_fid
, cinode
);
3130 cifs_dbg(FYI
, "Oplock release rc = %d\n", rc
);
3132 spin_unlock(&cinode
->open_file_lock
);
3134 cifs_put_tlink(tlink
);
3136 cifs_done_oplock_break(cinode
);
3139 static int cifs_swap_activate(struct swap_info_struct
*sis
,
3140 struct file
*swap_file
, sector_t
*span
)
3142 struct cifsFileInfo
*cfile
= swap_file
->private_data
;
3143 struct inode
*inode
= swap_file
->f_mapping
->host
;
3144 unsigned long blocks
;
3147 cifs_dbg(FYI
, "swap activate\n");
3149 if (!swap_file
->f_mapping
->a_ops
->swap_rw
)
3150 /* Cannot support swap */
3153 spin_lock(&inode
->i_lock
);
3154 blocks
= inode
->i_blocks
;
3155 isize
= inode
->i_size
;
3156 spin_unlock(&inode
->i_lock
);
3157 if (blocks
*512 < isize
) {
3158 pr_warn("swap activate: swapfile has holes\n");
3163 pr_warn_once("Swap support over SMB3 is experimental\n");
3166 * TODO: consider adding ACL (or documenting how) to prevent other
3167 * users (on this or other systems) from reading it
3171 /* TODO: add sk_set_memalloc(inet) or similar */
3174 cfile
->swapfile
= true;
3176 * TODO: Since file already open, we can't open with DENY_ALL here
3177 * but we could add call to grab a byte range lock to prevent others
3178 * from reading or writing the file
3181 sis
->flags
|= SWP_FS_OPS
;
3182 return add_swap_extent(sis
, 0, sis
->max
, 0);
3185 static void cifs_swap_deactivate(struct file
*file
)
3187 struct cifsFileInfo
*cfile
= file
->private_data
;
3189 cifs_dbg(FYI
, "swap deactivate\n");
3191 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
3194 cfile
->swapfile
= false;
3196 /* do we need to unpin (or unlock) the file */
3200 * cifs_swap_rw - SMB3 address space operation for swap I/O
3201 * @iocb: target I/O control block
3204 * Perform IO to the swap-file. This is much like direct IO.
3206 static int cifs_swap_rw(struct kiocb
*iocb
, struct iov_iter
*iter
)
3210 if (iov_iter_rw(iter
) == READ
)
3211 ret
= netfs_unbuffered_read_iter_locked(iocb
, iter
);
3213 ret
= netfs_unbuffered_write_iter_locked(iocb
, iter
, NULL
);
3219 const struct address_space_operations cifs_addr_ops
= {
3220 .read_folio
= netfs_read_folio
,
3221 .readahead
= netfs_readahead
,
3222 .writepages
= netfs_writepages
,
3223 .dirty_folio
= netfs_dirty_folio
,
3224 .release_folio
= netfs_release_folio
,
3225 .direct_IO
= noop_direct_IO
,
3226 .invalidate_folio
= netfs_invalidate_folio
,
3227 .migrate_folio
= filemap_migrate_folio
,
3229 * TODO: investigate and if useful we could add an is_dirty_writeback
3232 .swap_activate
= cifs_swap_activate
,
3233 .swap_deactivate
= cifs_swap_deactivate
,
3234 .swap_rw
= cifs_swap_rw
,
3238 * cifs_readahead requires the server to support a buffer large enough to
3239 * contain the header plus one complete page of data. Otherwise, we need
3240 * to leave cifs_readahead out of the address space operations.
3242 const struct address_space_operations cifs_addr_ops_smallbuf
= {
3243 .read_folio
= netfs_read_folio
,
3244 .writepages
= netfs_writepages
,
3245 .dirty_folio
= netfs_dirty_folio
,
3246 .release_folio
= netfs_release_folio
,
3247 .invalidate_folio
= netfs_invalidate_folio
,
3248 .migrate_folio
= filemap_migrate_folio
,