2021-06-18 00:31:49 -05:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* vfs operations that deal with files
|
2007-07-10 01:16:18 +00:00
|
|
|
*
|
2010-04-21 04:12:10 +00:00
|
|
|
* Copyright (C) International Business Machines Corp., 2002,2010
|
2005-04-16 15:20:36 -07:00
|
|
|
* Author(s): Steve French (sfrench@us.ibm.com)
|
2006-08-02 21:56:33 +00:00
|
|
|
* Jeremy Allison (jra@samba.org)
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/fs.h>
|
2022-11-20 09:15:34 -05:00
|
|
|
#include <linux/filelock.h>
|
2005-10-05 14:50:29 -07:00
|
|
|
#include <linux/backing-dev.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/pagevec.h>
|
2005-10-05 14:50:29 -07:00
|
|
|
#include <linux/writeback.h>
|
2006-12-10 02:19:44 -08:00
|
|
|
#include <linux/task_io_accounting_ops.h>
|
2005-10-20 13:44:56 -07:00
|
|
|
#include <linux/delay.h>
|
2009-09-21 06:47:50 -04:00
|
|
|
#include <linux/mount.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 17:04:11 +09:00
|
|
|
#include <linux/slab.h>
|
2011-10-19 15:30:16 -04:00
|
|
|
#include <linux/swap.h>
|
2019-01-03 15:29:02 -08:00
|
|
|
#include <linux/mm.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
#include <asm/div64.h>
|
|
|
|
#include "cifsfs.h"
|
|
|
|
#include "cifspdu.h"
|
|
|
|
#include "cifsglob.h"
|
|
|
|
#include "cifsproto.h"
|
2022-08-01 11:17:46 -05:00
|
|
|
#include "smb2proto.h"
|
2005-04-16 15:20:36 -07:00
|
|
|
#include "cifs_unicode.h"
|
|
|
|
#include "cifs_debug.h"
|
|
|
|
#include "cifs_fs_sb.h"
|
2010-07-05 18:12:45 +05:30
|
|
|
#include "fscache.h"
|
2017-11-22 17:38:47 -07:00
|
|
|
#include "smbdirect.h"
|
2020-12-12 13:40:50 -06:00
|
|
|
#include "fs_context.h"
|
2021-04-29 00:18:43 -05:00
|
|
|
#include "cifs_ioctl.h"
|
2022-08-10 22:00:08 -05:00
|
|
|
#include "cached_dir.h"
|
2023-10-06 18:16:15 +01:00
|
|
|
#include <trace/events/netfs.h>
|
|
|
|
|
|
|
|
static int cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare a subrequest to upload to the server. We need to allocate credits
|
|
|
|
* so that we know the maximum amount of data that we can include in it.
|
|
|
|
*/
|
|
|
|
static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
|
|
|
|
{
|
|
|
|
struct cifs_io_subrequest *wdata =
|
|
|
|
container_of(subreq, struct cifs_io_subrequest, subreq);
|
|
|
|
struct cifs_io_request *req = wdata->req;
|
2024-06-07 09:02:58 +01:00
|
|
|
struct netfs_io_stream *stream = &req->rreq.io_streams[subreq->stream_nr];
|
2023-10-06 18:16:15 +01:00
|
|
|
struct TCP_Server_Info *server;
|
|
|
|
struct cifsFileInfo *open_file = req->cfile;
|
2025-06-18 16:39:47 +01:00
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(wdata->rreq->inode->i_sb);
|
2023-10-06 18:16:15 +01:00
|
|
|
size_t wsize = req->rreq.wsize;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!wdata->have_xid) {
|
|
|
|
wdata->xid = get_xid();
|
|
|
|
wdata->have_xid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
|
|
|
|
wdata->server = server;
|
|
|
|
|
2025-06-18 16:39:47 +01:00
|
|
|
if (cifs_sb->ctx->wsize == 0)
|
|
|
|
cifs_negotiate_wsize(server, cifs_sb->ctx,
|
|
|
|
tlink_tcon(req->cfile->tlink));
|
|
|
|
|
2023-10-06 18:16:15 +01:00
|
|
|
retry:
|
|
|
|
if (open_file->invalidHandle) {
|
|
|
|
rc = cifs_reopen_file(open_file, false);
|
|
|
|
if (rc < 0) {
|
|
|
|
if (rc == -EAGAIN)
|
|
|
|
goto retry;
|
|
|
|
subreq->error = rc;
|
|
|
|
return netfs_prepare_write_failed(subreq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 09:02:58 +01:00
|
|
|
rc = server->ops->wait_mtu_credits(server, wsize, &stream->sreq_max_len,
|
2023-10-06 18:16:15 +01:00
|
|
|
&wdata->credits);
|
|
|
|
if (rc < 0) {
|
|
|
|
subreq->error = rc;
|
|
|
|
return netfs_prepare_write_failed(subreq);
|
|
|
|
}
|
|
|
|
|
2024-05-23 10:01:08 +01:00
|
|
|
wdata->credits.rreq_debug_id = subreq->rreq->debug_id;
|
|
|
|
wdata->credits.rreq_debug_index = subreq->debug_index;
|
|
|
|
wdata->credits.in_flight_check = 1;
|
|
|
|
trace_smb3_rw_credits(wdata->rreq->debug_id,
|
|
|
|
wdata->subreq.debug_index,
|
|
|
|
wdata->credits.value,
|
|
|
|
server->credits, server->in_flight,
|
|
|
|
wdata->credits.value,
|
|
|
|
cifs_trace_rw_credits_write_prepare);
|
|
|
|
|
2023-10-06 18:16:15 +01:00
|
|
|
#ifdef CONFIG_CIFS_SMB_DIRECT
|
|
|
|
if (server->smbd_conn)
|
2024-06-07 09:02:58 +01:00
|
|
|
stream->sreq_max_segs = server->smbd_conn->max_frmr_depth;
|
2023-10-06 18:16:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue a subrequest to upload to the server.
|
|
|
|
*/
|
|
|
|
static void cifs_issue_write(struct netfs_io_subrequest *subreq)
|
|
|
|
{
|
|
|
|
struct cifs_io_subrequest *wdata =
|
|
|
|
container_of(subreq, struct cifs_io_subrequest, subreq);
|
|
|
|
struct cifs_sb_info *sbi = CIFS_SB(subreq->rreq->inode->i_sb);
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (cifs_forced_shutdown(sbi)) {
|
|
|
|
rc = -EIO;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2024-05-23 10:01:08 +01:00
|
|
|
rc = adjust_credits(wdata->server, wdata, cifs_trace_rw_credits_issue_write_adjust);
|
2023-10-06 18:16:15 +01:00
|
|
|
if (rc)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = -EAGAIN;
|
|
|
|
if (wdata->req->cfile->invalidHandle)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
wdata->server->ops->async_writev(wdata);
|
|
|
|
out:
|
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (rc == -EAGAIN)
|
|
|
|
trace_netfs_sreq(subreq, netfs_sreq_trace_retry);
|
|
|
|
else
|
|
|
|
trace_netfs_sreq(subreq, netfs_sreq_trace_fail);
|
|
|
|
add_credits_and_wake_if(wdata->server, &wdata->credits, 0);
|
2025-05-19 10:07:03 +01:00
|
|
|
cifs_write_subrequest_terminated(wdata, rc);
|
2023-10-06 18:16:15 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-07-19 16:01:48 +01:00
|
|
|
static void cifs_netfs_invalidate_cache(struct netfs_io_request *wreq)
|
|
|
|
{
|
|
|
|
cifs_invalidate_cache(wreq->inode, 0);
|
|
|
|
}
|
|
|
|
|
2023-10-06 18:16:15 +01:00
|
|
|
/*
|
2024-07-02 00:40:22 +01:00
|
|
|
* Negotiate the size of a read operation on behalf of the netfs library.
|
2023-10-06 18:16:15 +01:00
|
|
|
*/
|
2024-07-02 00:40:22 +01:00
|
|
|
static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
|
2023-10-06 18:16:15 +01:00
|
|
|
{
|
|
|
|
struct netfs_io_request *rreq = subreq->rreq;
|
|
|
|
struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq);
|
|
|
|
struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq);
|
2025-02-11 10:00:25 +00:00
|
|
|
struct TCP_Server_Info *server;
|
2023-10-06 18:16:15 +01:00
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb);
|
2024-07-02 00:40:22 +01:00
|
|
|
size_t size;
|
|
|
|
int rc = 0;
|
2023-10-06 18:16:15 +01:00
|
|
|
|
2024-07-02 00:40:22 +01:00
|
|
|
if (!rdata->have_xid) {
|
|
|
|
rdata->xid = get_xid();
|
|
|
|
rdata->have_xid = true;
|
|
|
|
}
|
2025-02-11 10:00:25 +00:00
|
|
|
|
|
|
|
server = cifs_pick_channel(tlink_tcon(req->cfile->tlink)->ses);
|
2023-10-06 18:16:15 +01:00
|
|
|
rdata->server = server;
|
|
|
|
|
2025-06-18 16:39:47 +01:00
|
|
|
if (cifs_sb->ctx->rsize == 0)
|
2025-05-12 14:58:36 -03:00
|
|
|
cifs_negotiate_rsize(server, cifs_sb->ctx,
|
|
|
|
tlink_tcon(req->cfile->tlink));
|
2023-10-06 18:16:15 +01:00
|
|
|
|
cifs: Fix lack of credit renegotiation on read retry
When netfslib asks cifs to issue a read operation, it prefaces this with a
call to ->clamp_length() which cifs uses to negotiate credits, providing
receive capacity on the server; however, in the event that a read op needs
reissuing, netfslib doesn't call ->clamp_length() again as that could
shorten the subrequest, leaving a gap.
This causes the retried read to be done with zero credits which causes the
server to reject it with STATUS_INVALID_PARAMETER. This is a problem for a
DIO read that is requested that would go over the EOF. The short read will
be retried, causing EINVAL to be returned to the user when it fails.
Fix this by making cifs_req_issue_read() negotiate new credits if retrying
(NETFS_SREQ_RETRYING now gets set in the read side as well as the write
side in this instance).
This isn't sufficient, however: the new credits might not be sufficient to
complete the remainder of the read, so also add an additional field,
rreq->actual_len, that holds the actual size of the op we want to perform
without having to alter subreq->len.
We then rely on repeated short reads being retried until we finish the read
or reach the end of file and make a zero-length read.
Also fix a couple of places where the subrequest start and length need to
be altered by the amount so far transferred when being used.
Fixes: 69c3c023af25 ("cifs: Implement netfslib hooks")
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.com>
cc: Jeff Layton <jlayton@kernel.org>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Steve French <stfrench@microsoft.com>
2024-08-22 23:06:48 +01:00
|
|
|
rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
|
2024-07-02 00:40:22 +01:00
|
|
|
&size, &rdata->credits);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
rreq->io_streams[0].sreq_max_len = size;
|
2023-10-06 18:16:15 +01:00
|
|
|
|
2024-05-23 10:01:08 +01:00
|
|
|
rdata->credits.in_flight_check = 1;
|
|
|
|
rdata->credits.rreq_debug_id = rreq->debug_id;
|
|
|
|
rdata->credits.rreq_debug_index = subreq->debug_index;
|
|
|
|
|
|
|
|
trace_smb3_rw_credits(rdata->rreq->debug_id,
|
|
|
|
rdata->subreq.debug_index,
|
|
|
|
rdata->credits.value,
|
|
|
|
server->credits, server->in_flight, 0,
|
|
|
|
cifs_trace_rw_credits_read_submit);
|
|
|
|
|
2023-10-06 18:16:15 +01:00
|
|
|
#ifdef CONFIG_CIFS_SMB_DIRECT
|
|
|
|
if (server->smbd_conn)
|
2024-07-02 00:40:22 +01:00
|
|
|
rreq->io_streams[0].sreq_max_segs = server->smbd_conn->max_frmr_depth;
|
2023-10-06 18:16:15 +01:00
|
|
|
#endif
|
2024-07-02 00:40:22 +01:00
|
|
|
return 0;
|
2023-10-06 18:16:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue a read operation on behalf of the netfs helper functions. We're asked
|
|
|
|
* to make a read of a certain size at a point in the file. We are permitted
|
|
|
|
* to only read a portion of that, but as long as we read something, the netfs
|
|
|
|
* helper will call us again so that we can issue another read.
|
|
|
|
*/
|
2024-07-02 00:40:22 +01:00
|
|
|
static void cifs_issue_read(struct netfs_io_subrequest *subreq)
|
2023-10-06 18:16:15 +01:00
|
|
|
{
|
|
|
|
struct netfs_io_request *rreq = subreq->rreq;
|
|
|
|
struct cifs_io_subrequest *rdata = container_of(subreq, struct cifs_io_subrequest, subreq);
|
|
|
|
struct cifs_io_request *req = container_of(subreq->rreq, struct cifs_io_request, rreq);
|
2025-02-11 10:00:25 +00:00
|
|
|
struct TCP_Server_Info *server = rdata->server;
|
2023-10-06 18:16:15 +01:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
cifs_dbg(FYI, "%s: op=%08x[%x] mapping=%p len=%zu/%zu\n",
|
|
|
|
__func__, rreq->debug_id, subreq->debug_index, rreq->mapping,
|
|
|
|
subreq->transferred, subreq->len);
|
|
|
|
|
2024-07-02 00:40:22 +01:00
|
|
|
rc = adjust_credits(server, rdata, cifs_trace_rw_credits_issue_read_adjust);
|
|
|
|
if (rc)
|
|
|
|
goto failed;
|
cifs: Fix lack of credit renegotiation on read retry
When netfslib asks cifs to issue a read operation, it prefaces this with a
call to ->clamp_length() which cifs uses to negotiate credits, providing
receive capacity on the server; however, in the event that a read op needs
reissuing, netfslib doesn't call ->clamp_length() again as that could
shorten the subrequest, leaving a gap.
This causes the retried read to be done with zero credits which causes the
server to reject it with STATUS_INVALID_PARAMETER. This is a problem for a
DIO read that is requested that would go over the EOF. The short read will
be retried, causing EINVAL to be returned to the user when it fails.
Fix this by making cifs_req_issue_read() negotiate new credits if retrying
(NETFS_SREQ_RETRYING now gets set in the read side as well as the write
side in this instance).
This isn't sufficient, however: the new credits might not be sufficient to
complete the remainder of the read, so also add an additional field,
rreq->actual_len, that holds the actual size of the op we want to perform
without having to alter subreq->len.
We then rely on repeated short reads being retried until we finish the read
or reach the end of file and make a zero-length read.
Also fix a couple of places where the subrequest start and length need to
be altered by the amount so far transferred when being used.
Fixes: 69c3c023af25 ("cifs: Implement netfslib hooks")
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.com>
cc: Jeff Layton <jlayton@kernel.org>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Steve French <stfrench@microsoft.com>
2024-08-22 23:06:48 +01:00
|
|
|
|
2023-10-06 18:16:15 +01:00
|
|
|
if (req->cfile->invalidHandle) {
|
|
|
|
do {
|
|
|
|
rc = cifs_reopen_file(req->cfile, true);
|
|
|
|
} while (rc == -EAGAIN);
|
|
|
|
if (rc)
|
2024-07-02 00:40:22 +01:00
|
|
|
goto failed;
|
2023-10-06 18:16:15 +01:00
|
|
|
}
|
|
|
|
|
2025-05-23 08:57:52 +01:00
|
|
|
if (subreq->rreq->origin != NETFS_UNBUFFERED_READ &&
|
|
|
|
subreq->rreq->origin != NETFS_DIO_READ)
|
2024-08-08 14:29:38 +01:00
|
|
|
__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
|
2023-10-06 18:16:15 +01:00
|
|
|
|
2024-07-02 00:40:22 +01:00
|
|
|
trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
|
2024-06-20 18:31:28 +01:00
|
|
|
rc = rdata->server->ops->async_readv(rdata);
|
2023-10-06 18:16:15 +01:00
|
|
|
if (rc)
|
2024-07-02 00:40:22 +01:00
|
|
|
goto failed;
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
2024-12-16 20:40:58 +00:00
|
|
|
subreq->error = rc;
|
2024-12-16 20:40:59 +00:00
|
|
|
netfs_read_subreq_terminated(subreq);
|
2023-10-06 18:16:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Writeback calls this when it finds a folio that needs uploading. This isn't
|
|
|
|
* called if writeback only has copy-to-cache to deal with.
|
|
|
|
*/
|
|
|
|
static void cifs_begin_writeback(struct netfs_io_request *wreq)
|
|
|
|
{
|
|
|
|
struct cifs_io_request *req = container_of(wreq, struct cifs_io_request, rreq);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = cifs_get_writable_file(CIFS_I(wreq->inode), FIND_WR_ANY, &req->cfile);
|
|
|
|
if (ret) {
|
|
|
|
cifs_dbg(VFS, "No writable handle in writepages ret=%d\n", ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wreq->io_streams[0].avail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise a request.
|
|
|
|
*/
|
|
|
|
static int cifs_init_request(struct netfs_io_request *rreq, struct file *file)
|
|
|
|
{
|
|
|
|
struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq);
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(rreq->inode->i_sb);
|
|
|
|
struct cifsFileInfo *open_file = NULL;
|
|
|
|
|
|
|
|
rreq->rsize = cifs_sb->ctx->rsize;
|
|
|
|
rreq->wsize = cifs_sb->ctx->wsize;
|
2024-06-20 18:31:29 +01:00
|
|
|
req->pid = current->tgid; // Ummm... This may be a workqueue
|
2023-10-06 18:16:15 +01:00
|
|
|
|
|
|
|
if (file) {
|
|
|
|
open_file = file->private_data;
|
|
|
|
rreq->netfs_priv = file->private_data;
|
|
|
|
req->cfile = cifsFileInfo_get(open_file);
|
2024-06-20 18:31:29 +01:00
|
|
|
if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
|
|
|
|
req->pid = req->cfile->pid;
|
2023-10-06 18:16:15 +01:00
|
|
|
} else if (rreq->origin != NETFS_WRITEBACK) {
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Completion of a request operation.
|
|
|
|
*/
|
|
|
|
static void cifs_rreq_done(struct netfs_io_request *rreq)
|
|
|
|
{
|
|
|
|
struct timespec64 atime, mtime;
|
|
|
|
struct inode *inode = rreq->inode;
|
|
|
|
|
|
|
|
/* we do not want atime to be less than mtime, it broke some apps */
|
|
|
|
atime = inode_set_atime_to_ts(inode, current_time(inode));
|
|
|
|
mtime = inode_get_mtime(inode);
|
|
|
|
if (timespec64_compare(&atime, &mtime))
|
|
|
|
inode_set_atime_to_ts(inode, inode_get_mtime(inode));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cifs_free_request(struct netfs_io_request *rreq)
|
|
|
|
{
|
|
|
|
struct cifs_io_request *req = container_of(rreq, struct cifs_io_request, rreq);
|
|
|
|
|
|
|
|
if (req->cfile)
|
|
|
|
cifsFileInfo_put(req->cfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cifs_free_subrequest(struct netfs_io_subrequest *subreq)
|
|
|
|
{
|
|
|
|
struct cifs_io_subrequest *rdata =
|
|
|
|
container_of(subreq, struct cifs_io_subrequest, subreq);
|
|
|
|
int rc = subreq->error;
|
|
|
|
|
|
|
|
if (rdata->subreq.source == NETFS_DOWNLOAD_FROM_SERVER) {
|
|
|
|
#ifdef CONFIG_CIFS_SMB_DIRECT
|
|
|
|
if (rdata->mr) {
|
|
|
|
smbd_deregister_mr(rdata->mr);
|
|
|
|
rdata->mr = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-08-08 20:23:32 +08:00
|
|
|
if (rdata->credits.value != 0) {
|
2024-05-23 10:01:08 +01:00
|
|
|
trace_smb3_rw_credits(rdata->rreq->debug_id,
|
|
|
|
rdata->subreq.debug_index,
|
|
|
|
rdata->credits.value,
|
|
|
|
rdata->server ? rdata->server->credits : 0,
|
|
|
|
rdata->server ? rdata->server->in_flight : 0,
|
|
|
|
-rdata->credits.value,
|
|
|
|
cifs_trace_rw_credits_free_subreq);
|
2024-08-08 20:23:32 +08:00
|
|
|
if (rdata->server)
|
|
|
|
add_credits_and_wake_if(rdata->server, &rdata->credits, 0);
|
|
|
|
else
|
|
|
|
rdata->credits.value = 0;
|
|
|
|
}
|
2024-05-23 10:01:08 +01:00
|
|
|
|
2023-10-06 18:16:15 +01:00
|
|
|
if (rdata->have_xid)
|
|
|
|
free_xid(rdata->xid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct netfs_request_ops cifs_req_ops = {
|
|
|
|
.request_pool = &cifs_io_request_pool,
|
|
|
|
.subrequest_pool = &cifs_io_subrequest_pool,
|
|
|
|
.init_request = cifs_init_request,
|
|
|
|
.free_request = cifs_free_request,
|
|
|
|
.free_subrequest = cifs_free_subrequest,
|
2024-07-02 00:40:22 +01:00
|
|
|
.prepare_read = cifs_prepare_read,
|
|
|
|
.issue_read = cifs_issue_read,
|
2023-10-06 18:16:15 +01:00
|
|
|
.done = cifs_rreq_done,
|
|
|
|
.begin_writeback = cifs_begin_writeback,
|
|
|
|
.prepare_write = cifs_prepare_write,
|
|
|
|
.issue_write = cifs_issue_write,
|
2024-07-19 16:01:48 +01:00
|
|
|
.invalidate_cache = cifs_netfs_invalidate_cache,
|
2023-10-06 18:16:15 +01:00
|
|
|
};
|
2013-02-18 10:34:26 -06:00
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
/*
|
|
|
|
* Mark as invalid, all open files on tree connections since they
|
|
|
|
* were closed when session to server was lost.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *open_file = NULL;
|
|
|
|
struct list_head *tmp;
|
|
|
|
struct list_head *tmp1;
|
|
|
|
|
|
|
|
/* only send once per connect */
|
2023-03-16 10:45:12 +00:00
|
|
|
spin_lock(&tcon->tc_lock);
|
2024-02-06 15:00:46 +00:00
|
|
|
if (tcon->need_reconnect)
|
|
|
|
tcon->status = TID_NEED_RECON;
|
|
|
|
|
2023-03-16 10:45:12 +00:00
|
|
|
if (tcon->status != TID_NEED_RECON) {
|
|
|
|
spin_unlock(&tcon->tc_lock);
|
2022-08-01 11:17:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tcon->status = TID_IN_FILES_INVALIDATE;
|
2023-03-16 10:45:12 +00:00
|
|
|
spin_unlock(&tcon->tc_lock);
|
2022-08-01 11:17:46 -05:00
|
|
|
|
|
|
|
/* list all files open on tree connection and mark them invalid */
|
|
|
|
spin_lock(&tcon->open_file_lock);
|
|
|
|
list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
|
|
|
|
open_file = list_entry(tmp, struct cifsFileInfo, tlist);
|
|
|
|
open_file->invalidHandle = true;
|
|
|
|
open_file->oplock_break_cancelled = true;
|
|
|
|
}
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
|
|
|
|
2022-08-10 22:00:08 -05:00
|
|
|
invalidate_all_cached_dirs(tcon);
|
2022-08-01 11:17:46 -05:00
|
|
|
spin_lock(&tcon->tc_lock);
|
|
|
|
if (tcon->status == TID_IN_FILES_INVALIDATE)
|
|
|
|
tcon->status = TID_NEED_TCON;
|
|
|
|
spin_unlock(&tcon->tc_lock);
|
|
|
|
|
|
|
|
/*
|
2025-03-07 15:43:19 +01:00
|
|
|
* BB Add call to evict_inodes(sb) for all superblocks mounted
|
2022-08-01 11:17:46 -05:00
|
|
|
* to this tcon.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:11:35 +01:00
|
|
|
static inline int cifs_convert_flags(unsigned int flags, int rdwr_for_fscache)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
if ((flags & O_ACCMODE) == O_RDONLY)
|
|
|
|
return GENERIC_READ;
|
|
|
|
else if ((flags & O_ACCMODE) == O_WRONLY)
|
2024-04-02 10:11:35 +01:00
|
|
|
return rdwr_for_fscache == 1 ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE;
|
2005-04-16 15:20:36 -07:00
|
|
|
else if ((flags & O_ACCMODE) == O_RDWR) {
|
|
|
|
/* GENERIC_ALL is too much permission to request
|
|
|
|
can cause unnecessary access denied on create */
|
|
|
|
/* return GENERIC_ALL; */
|
|
|
|
return (GENERIC_READ | GENERIC_WRITE);
|
|
|
|
}
|
|
|
|
|
2008-05-14 10:21:33 -07:00
|
|
|
return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
|
|
|
|
FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
|
|
|
|
FILE_READ_DATA);
|
2009-02-23 20:43:11 +00:00
|
|
|
}
|
2008-05-14 10:21:33 -07:00
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2010-10-15 15:33:56 -04:00
|
|
|
static u32 cifs_posix_convert_flags(unsigned int flags)
|
2009-02-23 20:43:11 +00:00
|
|
|
{
|
2010-10-15 15:33:56 -04:00
|
|
|
u32 posix_flags = 0;
|
2008-05-14 10:21:33 -07:00
|
|
|
|
2009-02-23 20:43:11 +00:00
|
|
|
if ((flags & O_ACCMODE) == O_RDONLY)
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags = SMB_O_RDONLY;
|
2009-02-23 20:43:11 +00:00
|
|
|
else if ((flags & O_ACCMODE) == O_WRONLY)
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags = SMB_O_WRONLY;
|
|
|
|
else if ((flags & O_ACCMODE) == O_RDWR)
|
|
|
|
posix_flags = SMB_O_RDWR;
|
|
|
|
|
2013-02-18 10:34:26 -06:00
|
|
|
if (flags & O_CREAT) {
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags |= SMB_O_CREAT;
|
2013-02-18 10:34:26 -06:00
|
|
|
if (flags & O_EXCL)
|
|
|
|
posix_flags |= SMB_O_EXCL;
|
|
|
|
} else if (flags & O_EXCL)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
|
|
|
|
current->comm, current->tgid);
|
2013-02-18 10:34:26 -06:00
|
|
|
|
2010-10-15 15:33:56 -04:00
|
|
|
if (flags & O_TRUNC)
|
|
|
|
posix_flags |= SMB_O_TRUNC;
|
|
|
|
/* be safe and imply O_SYNC for O_DSYNC */
|
2009-10-27 11:05:28 +01:00
|
|
|
if (flags & O_DSYNC)
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags |= SMB_O_SYNC;
|
2009-02-23 20:43:11 +00:00
|
|
|
if (flags & O_DIRECTORY)
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags |= SMB_O_DIRECTORY;
|
2009-02-23 20:43:11 +00:00
|
|
|
if (flags & O_NOFOLLOW)
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags |= SMB_O_NOFOLLOW;
|
2009-02-23 20:43:11 +00:00
|
|
|
if (flags & O_DIRECT)
|
2010-10-15 15:33:56 -04:00
|
|
|
posix_flags |= SMB_O_DIRECT;
|
2009-02-23 20:43:11 +00:00
|
|
|
|
|
|
|
return posix_flags;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
static inline int cifs_get_disposition(unsigned int flags)
|
|
|
|
{
|
|
|
|
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
|
|
|
return FILE_CREATE;
|
|
|
|
else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
|
|
|
|
return FILE_OVERWRITE_IF;
|
|
|
|
else if ((flags & O_CREAT) == O_CREAT)
|
|
|
|
return FILE_OPEN_IF;
|
2006-05-30 18:09:31 +00:00
|
|
|
else if ((flags & O_TRUNC) == O_TRUNC)
|
|
|
|
return FILE_OVERWRITE;
|
2005-04-16 15:20:36 -07:00
|
|
|
else
|
|
|
|
return FILE_OPEN;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2021-03-18 15:44:05 -04:00
|
|
|
int cifs_posix_open(const char *full_path, struct inode **pinode,
|
2010-10-15 15:33:56 -04:00
|
|
|
struct super_block *sb, int mode, unsigned int f_flags,
|
2012-06-20 11:21:16 +04:00
|
|
|
__u32 *poplock, __u16 *pnetfid, unsigned int xid)
|
2010-10-15 15:33:56 -04:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
FILE_UNIX_BASIC_INFO *presp_data;
|
|
|
|
__u32 posix_flags = 0;
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
|
|
|
|
struct cifs_fattr fattr;
|
|
|
|
struct tcon_link *tlink;
|
2011-05-27 04:34:02 +00:00
|
|
|
struct cifs_tcon *tcon;
|
2010-10-15 15:33:56 -04:00
|
|
|
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "posix open %s\n", full_path);
|
2010-10-15 15:33:56 -04:00
|
|
|
|
|
|
|
presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
|
|
|
|
if (presp_data == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
tlink = cifs_sb_tlink(cifs_sb);
|
|
|
|
if (IS_ERR(tlink)) {
|
|
|
|
rc = PTR_ERR(tlink);
|
|
|
|
goto posix_open_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcon = tlink_tcon(tlink);
|
|
|
|
mode &= ~current_umask();
|
|
|
|
|
|
|
|
posix_flags = cifs_posix_convert_flags(f_flags);
|
|
|
|
rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
|
|
|
|
poplock, full_path, cifs_sb->local_nls,
|
2015-02-13 15:35:58 +09:00
|
|
|
cifs_remap(cifs_sb));
|
2010-10-15 15:33:56 -04:00
|
|
|
cifs_put_tlink(tlink);
|
|
|
|
|
|
|
|
if (rc)
|
|
|
|
goto posix_open_ret;
|
|
|
|
|
|
|
|
if (presp_data->Type == cpu_to_le32(-1))
|
|
|
|
goto posix_open_ret; /* open ok, caller does qpathinfo */
|
|
|
|
|
|
|
|
if (!pinode)
|
|
|
|
goto posix_open_ret; /* caller does not need info */
|
|
|
|
|
|
|
|
cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
|
|
|
|
|
|
|
|
/* get new inode and set it up */
|
|
|
|
if (*pinode == NULL) {
|
|
|
|
cifs_fill_uniqueid(sb, &fattr);
|
|
|
|
*pinode = cifs_iget(sb, &fattr);
|
|
|
|
if (!*pinode) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto posix_open_ret;
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-25 16:26:35 +10:00
|
|
|
cifs_revalidate_mapping(*pinode);
|
2024-02-29 23:09:52 +05:30
|
|
|
rc = cifs_fattr_to_inode(*pinode, &fattr, false);
|
2010-10-15 15:33:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
posix_open_ret:
|
|
|
|
kfree(presp_data);
|
|
|
|
return rc;
|
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2010-10-15 15:33:56 -04:00
|
|
|
|
2022-10-03 18:43:50 -03:00
|
|
|
static int 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)
|
2010-11-25 15:12:39 +03:00
|
|
|
{
|
|
|
|
int rc;
|
2012-09-18 16:20:26 -07:00
|
|
|
int desired_access;
|
2010-11-25 15:12:39 +03:00
|
|
|
int disposition;
|
2011-09-26 09:56:44 -05:00
|
|
|
int create_options = CREATE_NOT_DIR;
|
2012-09-19 06:22:44 -07:00
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
2013-07-05 12:00:30 +04:00
|
|
|
struct cifs_open_parms oparms;
|
2024-04-02 10:11:35 +01:00
|
|
|
int rdwr_for_fscache = 0;
|
2010-11-25 15:12:39 +03:00
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
if (!server->ops->open)
|
2012-09-18 16:20:26 -07:00
|
|
|
return -ENOSYS;
|
|
|
|
|
2024-04-02 10:11:35 +01:00
|
|
|
/* If we're caching, we need to be able to fill in around partial writes. */
|
|
|
|
if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY)
|
|
|
|
rdwr_for_fscache = 1;
|
|
|
|
|
|
|
|
desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache);
|
2010-11-25 15:12:39 +03:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* open flag mapping table:
|
|
|
|
*
|
|
|
|
* POSIX Flag CIFS Disposition
|
|
|
|
* ---------- ----------------
|
|
|
|
* O_CREAT FILE_OPEN_IF
|
|
|
|
* O_CREAT | O_EXCL FILE_CREATE
|
|
|
|
* O_CREAT | O_TRUNC FILE_OVERWRITE_IF
|
|
|
|
* O_TRUNC FILE_OVERWRITE
|
|
|
|
* none of the above FILE_OPEN
|
|
|
|
*
|
|
|
|
* Note that there is not a direct match between disposition
|
|
|
|
* FILE_SUPERSEDE (ie create whether or not file exists although
|
|
|
|
* O_CREAT | O_TRUNC is similar but truncates the existing
|
|
|
|
* file rather than creating a new file as FILE_SUPERSEDE does
|
|
|
|
* (which uses the attributes / metadata passed in on open call)
|
|
|
|
*?
|
|
|
|
*? O_SYNC is a reasonable match to CIFS writethrough flag
|
|
|
|
*? and the read write flags match reasonably. O_LARGEFILE
|
|
|
|
*? is irrelevant because largefile support is always used
|
|
|
|
*? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
|
|
|
|
* O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
disposition = cifs_get_disposition(f_flags);
|
|
|
|
|
|
|
|
/* BB pass O_SYNC flag through on file attributes .. BB */
|
|
|
|
|
2017-09-22 01:40:27 -05:00
|
|
|
/* O_SYNC also has bit for O_DSYNC so following check picks up either */
|
|
|
|
if (f_flags & O_SYNC)
|
|
|
|
create_options |= CREATE_WRITE_THROUGH;
|
|
|
|
|
|
|
|
if (f_flags & O_DIRECT)
|
|
|
|
create_options |= CREATE_NO_BUFFER;
|
|
|
|
|
2024-04-02 10:11:35 +01:00
|
|
|
retry_open:
|
2023-01-11 12:37:58 +01:00
|
|
|
oparms = (struct cifs_open_parms) {
|
|
|
|
.tcon = tcon,
|
|
|
|
.cifs_sb = cifs_sb,
|
|
|
|
.desired_access = desired_access,
|
|
|
|
.create_options = cifs_create_options(cifs_sb, create_options),
|
|
|
|
.disposition = disposition,
|
|
|
|
.path = full_path,
|
|
|
|
.fid = fid,
|
|
|
|
};
|
2013-07-05 12:00:30 +04:00
|
|
|
|
|
|
|
rc = server->ops->open(xid, &oparms, oplock, buf);
|
2024-04-02 10:11:35 +01:00
|
|
|
if (rc) {
|
|
|
|
if (rc == -EACCES && rdwr_for_fscache == 1) {
|
|
|
|
desired_access = cifs_convert_flags(f_flags, 0);
|
|
|
|
rdwr_for_fscache = 2;
|
|
|
|
goto retry_open;
|
|
|
|
}
|
2022-10-03 18:43:50 -03:00
|
|
|
return rc;
|
2024-04-02 10:11:35 +01:00
|
|
|
}
|
|
|
|
if (rdwr_for_fscache == 2)
|
|
|
|
cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
|
2010-11-25 15:12:39 +03:00
|
|
|
|
2020-06-11 22:43:01 -05:00
|
|
|
/* TODO: Add support for calling posix query info but with passing in fid */
|
2010-11-25 15:12:39 +03:00
|
|
|
if (tcon->unix_ext)
|
|
|
|
rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
|
|
|
|
xid);
|
|
|
|
else
|
|
|
|
rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
|
2014-02-10 14:08:16 -06:00
|
|
|
xid, fid);
|
2010-11-25 15:12:39 +03:00
|
|
|
|
2019-09-30 10:06:18 -07:00
|
|
|
if (rc) {
|
|
|
|
server->ops->close(xid, tcon, fid);
|
|
|
|
if (rc == -ESTALE)
|
|
|
|
rc = -EOPENSTALE;
|
|
|
|
}
|
|
|
|
|
2010-11-25 15:12:39 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-12-24 14:41:19 +04:00
|
|
|
static bool
|
|
|
|
cifs_has_mand_locks(struct cifsInodeInfo *cinode)
|
|
|
|
{
|
|
|
|
struct cifs_fid_locks *cur;
|
|
|
|
bool has_locks = false;
|
|
|
|
|
|
|
|
down_read(&cinode->lock_sem);
|
|
|
|
list_for_each_entry(cur, &cinode->llist, llist) {
|
|
|
|
if (!list_empty(&cur->locks)) {
|
|
|
|
has_locks = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up_read(&cinode->lock_sem);
|
|
|
|
return has_locks;
|
|
|
|
}
|
|
|
|
|
2019-10-23 05:02:33 -04:00
|
|
|
void
|
|
|
|
cifs_down_write(struct rw_semaphore *sem)
|
|
|
|
{
|
|
|
|
while (!down_write_trylock(sem))
|
|
|
|
msleep(10);
|
|
|
|
}
|
|
|
|
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
static void cifsFileInfo_put_work(struct work_struct *work);
|
2024-04-02 14:01:28 -05:00
|
|
|
void serverclose_work(struct work_struct *work);
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
|
2022-10-03 18:43:50 -03:00
|
|
|
struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
|
|
|
|
struct tcon_link *tlink, __u32 oplock,
|
|
|
|
const char *symlink_target)
|
2010-10-15 15:34:02 -04:00
|
|
|
{
|
2016-04-18 06:41:52 -05:00
|
|
|
struct dentry *dentry = file_dentry(file);
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2012-09-18 16:20:26 -07:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
struct cifsFileInfo *cfile;
|
2012-09-19 06:22:43 -07:00
|
|
|
struct cifs_fid_locks *fdlocks;
|
2012-09-19 06:22:45 -07:00
|
|
|
struct cifs_tcon *tcon = tlink_tcon(tlink);
|
2012-12-24 14:41:19 +04:00
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
2012-09-18 16:20:26 -07:00
|
|
|
|
|
|
|
cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
|
|
|
|
if (cfile == NULL)
|
|
|
|
return cfile;
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
|
|
|
|
if (!fdlocks) {
|
|
|
|
kfree(cfile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:43:50 -03:00
|
|
|
if (symlink_target) {
|
|
|
|
cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
|
|
|
|
if (!cfile->symlink_target) {
|
|
|
|
kfree(fdlocks);
|
|
|
|
kfree(cfile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
INIT_LIST_HEAD(&fdlocks->locks);
|
|
|
|
fdlocks->cfile = cfile;
|
|
|
|
cfile->llist = fdlocks;
|
|
|
|
|
2012-09-18 16:20:26 -07:00
|
|
|
cfile->count = 1;
|
|
|
|
cfile->pid = current->tgid;
|
|
|
|
cfile->uid = current_fsuid();
|
|
|
|
cfile->dentry = dget(dentry);
|
|
|
|
cfile->f_flags = file->f_flags;
|
|
|
|
cfile->invalidHandle = false;
|
2021-05-05 10:56:47 +00:00
|
|
|
cfile->deferred_close_scheduled = false;
|
2012-09-18 16:20:26 -07:00
|
|
|
cfile->tlink = cifs_get_tlink(tlink);
|
|
|
|
INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
INIT_WORK(&cfile->put, cifsFileInfo_put_work);
|
2024-04-02 14:01:28 -05:00
|
|
|
INIT_WORK(&cfile->serverclose, serverclose_work);
|
2021-04-13 00:26:42 -05:00
|
|
|
INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
|
2012-09-19 06:22:43 -07:00
|
|
|
mutex_init(&cfile->fh_mutex);
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_lock_init(&cfile->file_info_lock);
|
2010-10-15 15:34:02 -04:00
|
|
|
|
2013-03-08 16:30:03 +01:00
|
|
|
cifs_sb_active(inode->i_sb);
|
|
|
|
|
2012-12-24 14:41:19 +04:00
|
|
|
/*
|
|
|
|
* If the server returned a read oplock and we have mandatory brlocks,
|
|
|
|
* set oplock level to None.
|
|
|
|
*/
|
2013-09-05 16:11:28 +04:00
|
|
|
if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
|
2012-12-24 14:41:19 +04:00
|
|
|
oplock = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:18:39 -08:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
|
|
|
list_add(&fdlocks->llist, &cinode->llist);
|
|
|
|
up_write(&cinode->lock_sem);
|
|
|
|
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_lock(&tcon->open_file_lock);
|
2012-12-24 14:41:19 +04:00
|
|
|
if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
|
2012-09-19 06:22:45 -07:00
|
|
|
oplock = fid->pending_open->oplock;
|
|
|
|
list_del(&fid->pending_open->olist);
|
|
|
|
|
2013-09-05 21:30:16 +04:00
|
|
|
fid->purge_cache = false;
|
2012-12-24 14:41:19 +04:00
|
|
|
server->ops->set_fid(cfile, fid, oplock);
|
2012-09-19 06:22:45 -07:00
|
|
|
|
|
|
|
list_add(&cfile->tlist, &tcon->openFileList);
|
2018-10-19 17:14:32 -05:00
|
|
|
atomic_inc(&tcon->num_local_opens);
|
2016-09-22 18:58:16 -05:00
|
|
|
|
2010-10-15 15:34:02 -04:00
|
|
|
/* if readable file instance put first in list*/
|
2019-06-05 10:38:38 +10:00
|
|
|
spin_lock(&cinode->open_file_lock);
|
2010-10-15 15:34:02 -04:00
|
|
|
if (file->f_mode & FMODE_READ)
|
2012-09-18 16:20:26 -07:00
|
|
|
list_add(&cfile->flist, &cinode->openFileList);
|
2010-10-15 15:34:02 -04:00
|
|
|
else
|
2012-09-18 16:20:26 -07:00
|
|
|
list_add_tail(&cfile->flist, &cinode->openFileList);
|
2019-06-05 10:38:38 +10:00
|
|
|
spin_unlock(&cinode->open_file_lock);
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2010-10-15 15:34:02 -04:00
|
|
|
|
2013-09-05 21:30:16 +04:00
|
|
|
if (fid->purge_cache)
|
2014-04-30 09:31:47 -04:00
|
|
|
cifs_zap_mapping(inode);
|
2013-09-05 21:30:16 +04:00
|
|
|
|
2012-09-18 16:20:26 -07:00
|
|
|
file->private_data = cfile;
|
|
|
|
return cfile;
|
2010-10-15 15:34:02 -04:00
|
|
|
}
|
|
|
|
|
2012-07-25 14:59:54 -04:00
|
|
|
struct cifsFileInfo *
|
|
|
|
cifsFileInfo_get(struct cifsFileInfo *cifs_file)
|
|
|
|
{
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_lock(&cifs_file->file_info_lock);
|
2012-07-25 14:59:54 -04:00
|
|
|
cifsFileInfo_get_locked(cifs_file);
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&cifs_file->file_info_lock);
|
2012-07-25 14:59:54 -04:00
|
|
|
return cifs_file;
|
|
|
|
}
|
|
|
|
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
|
|
|
|
{
|
|
|
|
struct inode *inode = d_inode(cifs_file->dentry);
|
|
|
|
struct cifsInodeInfo *cifsi = CIFS_I(inode);
|
|
|
|
struct cifsLockInfo *li, *tmp;
|
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete any outstanding lock records. We'll lose them when the file
|
|
|
|
* is closed anyway.
|
|
|
|
*/
|
|
|
|
cifs_down_write(&cifsi->lock_sem);
|
|
|
|
list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
|
|
|
|
list_del(&li->llist);
|
|
|
|
cifs_del_lock_waiters(li);
|
|
|
|
kfree(li);
|
|
|
|
}
|
|
|
|
list_del(&cifs_file->llist->llist);
|
|
|
|
kfree(cifs_file->llist);
|
|
|
|
up_write(&cifsi->lock_sem);
|
|
|
|
|
|
|
|
cifs_put_tlink(cifs_file->tlink);
|
|
|
|
dput(cifs_file->dentry);
|
|
|
|
cifs_sb_deactive(sb);
|
2022-10-03 18:43:50 -03:00
|
|
|
kfree(cifs_file->symlink_target);
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
kfree(cifs_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cifsFileInfo_put_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cifs_file = container_of(work,
|
|
|
|
struct cifsFileInfo, put);
|
|
|
|
|
|
|
|
cifsFileInfo_put_final(cifs_file);
|
|
|
|
}
|
|
|
|
|
2024-04-02 14:01:28 -05:00
|
|
|
void serverclose_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cifs_file = container_of(work,
|
|
|
|
struct cifsFileInfo, serverclose);
|
|
|
|
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
|
|
|
|
|
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
|
|
|
int rc = 0;
|
|
|
|
int retries = 0;
|
|
|
|
int MAX_RETRIES = 4;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (server->ops->close_getattr)
|
|
|
|
rc = server->ops->close_getattr(0, tcon, cifs_file);
|
|
|
|
else if (server->ops->close)
|
|
|
|
rc = server->ops->close(0, tcon, &cifs_file->fid);
|
|
|
|
|
|
|
|
if (rc == -EBUSY || rc == -EAGAIN) {
|
|
|
|
retries++;
|
|
|
|
msleep(250);
|
|
|
|
}
|
|
|
|
} while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (retries == MAX_RETRIES)
|
|
|
|
pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES);
|
|
|
|
|
|
|
|
if (cifs_file->offload)
|
|
|
|
queue_work(fileinfo_put_wq, &cifs_file->put);
|
|
|
|
else
|
|
|
|
cifsFileInfo_put_final(cifs_file);
|
|
|
|
}
|
|
|
|
|
2019-03-29 10:49:12 +01:00
|
|
|
/**
|
|
|
|
* cifsFileInfo_put - release a reference of file priv data
|
|
|
|
*
|
|
|
|
* Always potentially wait for oplock handler. See _cifsFileInfo_put().
|
2020-12-12 12:08:58 -06:00
|
|
|
*
|
|
|
|
* @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
|
2010-10-21 22:46:14 +00:00
|
|
|
*/
|
2010-10-15 15:34:04 -04:00
|
|
|
void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
|
2019-03-29 10:49:12 +01:00
|
|
|
{
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
_cifsFileInfo_put(cifs_file, true, true);
|
2019-03-29 10:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* _cifsFileInfo_put - release a reference of file priv data
|
|
|
|
*
|
|
|
|
* This may involve closing the filehandle @cifs_file out on the
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
* server. Must be called without holding tcon->open_file_lock,
|
|
|
|
* cinode->open_file_lock and cifs_file->file_info_lock.
|
2019-03-29 10:49:12 +01:00
|
|
|
*
|
|
|
|
* If @wait_for_oplock_handler is true and we are releasing the last
|
|
|
|
* reference, wait for any running oplock break handler of the file
|
2020-12-12 12:08:58 -06:00
|
|
|
* and cancel any pending one.
|
|
|
|
*
|
|
|
|
* @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
|
|
|
|
* @wait_oplock_handler: must be false if called from oplock_break_handler
|
|
|
|
* @offload: not offloaded on close and oplock breaks
|
2019-03-29 10:49:12 +01:00
|
|
|
*
|
|
|
|
*/
|
cifs: move cifsFileInfo_put logic into a work-queue
This patch moves the final part of the cifsFileInfo_put() logic where we
need a write lock on lock_sem to be processed in a separate thread that
holds no other locks.
This is to prevent deadlocks like the one below:
> there are 6 processes looping to while trying to down_write
> cinode->lock_sem, 5 of them from _cifsFileInfo_put, and one from
> cifs_new_fileinfo
>
> and there are 5 other processes which are blocked, several of them
> waiting on either PG_writeback or PG_locked (which are both set), all
> for the same page of the file
>
> 2 inode_lock() (inode->i_rwsem) for the file
> 1 wait_on_page_writeback() for the page
> 1 down_read(inode->i_rwsem) for the inode of the directory
> 1 inode_lock()(inode->i_rwsem) for the inode of the directory
> 1 __lock_page
>
>
> so processes are blocked waiting on:
> page flags PG_locked and PG_writeback for one specific page
> inode->i_rwsem for the directory
> inode->i_rwsem for the file
> cifsInodeInflock_sem
>
>
>
> here are the more gory details (let me know if I need to provide
> anything more/better):
>
> [0 00:48:22.765] [UN] PID: 8863 TASK: ffff8c691547c5c0 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff9965007e3ba8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007e3c38] schedule at ffffffff9b6e64df
> #2 [ffff9965007e3c48] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965007e3cb8] legitimize_path at ffffffff9b0f975d
> #4 [ffff9965007e3d08] path_openat at ffffffff9b0fe55d
> #5 [ffff9965007e3dd8] do_filp_open at ffffffff9b100a33
> #6 [ffff9965007e3ee0] do_sys_open at ffffffff9b0eb2d6
> #7 [ffff9965007e3f38] do_syscall_64 at ffffffff9ae04315
> * (I think legitimize_path is bogus)
>
> in path_openat
> } else {
> const char *s = path_init(nd, flags);
> while (!(error = link_path_walk(s, nd)) &&
> (error = do_last(nd, file, op)) > 0) { <<<<
>
> do_last:
> if (open_flag & O_CREAT)
> inode_lock(dir->d_inode); <<<<
> else
> so it's trying to take inode->i_rwsem for the directory
>
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68bb8e79c0 ffff8c691158ef20 ffff8c6915bf9000 DIR /mnt/vm1_smb/
> inode.i_rwsem is ffff8c691158efc0
>
> <struct rw_semaphore 0xffff8c691158efc0>:
> owner: <struct task_struct 0xffff8c6914275d00> (UN - 8856 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 2
> 0xffff9965007e3c90 8863 reopen_file UN 0 1:29:22.926
> RWSEM_WAITING_FOR_WRITE
> 0xffff996500393e00 9802 ls UN 0 1:17:26.700
> RWSEM_WAITING_FOR_READ
>
>
> the owner of the inode.i_rwsem of the directory is:
>
> [0 00:00:00.109] [UN] PID: 8856 TASK: ffff8c6914275d00 CPU: 3
> COMMAND: "reopen_file"
> #0 [ffff99650065b828] __schedule at ffffffff9b6e6095
> #1 [ffff99650065b8b8] schedule at ffffffff9b6e64df
> #2 [ffff99650065b8c8] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff99650065b940] msleep at ffffffff9af573a9
> #4 [ffff99650065b948] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff99650065ba38] cifs_writepage_locked at ffffffffc0a0b8f3 [cifs]
> #6 [ffff99650065bab0] cifs_launder_page at ffffffffc0a0bb72 [cifs]
> #7 [ffff99650065bb30] invalidate_inode_pages2_range at ffffffff9b04d4bd
> #8 [ffff99650065bcb8] cifs_invalidate_mapping at ffffffffc0a11339 [cifs]
> #9 [ffff99650065bcd0] cifs_revalidate_mapping at ffffffffc0a1139a [cifs]
> #10 [ffff99650065bcf0] cifs_d_revalidate at ffffffffc0a014f6 [cifs]
> #11 [ffff99650065bd08] path_openat at ffffffff9b0fe7f7
> #12 [ffff99650065bdd8] do_filp_open at ffffffff9b100a33
> #13 [ffff99650065bee0] do_sys_open at ffffffff9b0eb2d6
> #14 [ffff99650065bf38] do_syscall_64 at ffffffff9ae04315
>
> cifs_launder_page is for page 0xffffd1e2c07d2480
>
> crash> page.index,mapping,flags 0xffffd1e2c07d2480
> index = 0x8
> mapping = 0xffff8c68f3cd0db0
> flags = 0xfffffc0008095
>
> PAGE-FLAG BIT VALUE
> PG_locked 0 0000001
> PG_uptodate 2 0000004
> PG_lru 4 0000010
> PG_waiters 7 0000080
> PG_writeback 15 0008000
>
>
> inode is ffff8c68f3cd0c40
> inode.i_rwsem is ffff8c68f3cd0ce0
> DENTRY INODE SUPERBLK TYPE PATH
> ffff8c68a1f1b480 ffff8c68f3cd0c40 ffff8c6915bf9000 REG
> /mnt/vm1_smb/testfile.8853
>
>
> this process holds the inode->i_rwsem for the parent directory, is
> laundering a page attached to the inode of the file it's opening, and in
> _cifsFileInfo_put is trying to down_write the cifsInodeInflock_sem
> for the file itself.
>
>
> <struct rw_semaphore 0xffff8c68f3cd0ce0>:
> owner: <struct task_struct 0xffff8c6914272e80> (UN - 8854 -
> reopen_file), counter: 0x0000000000000003
> waitlist: 1
> 0xffff9965005dfd80 8855 reopen_file UN 0 1:29:22.912
> RWSEM_WAITING_FOR_WRITE
>
> this is the inode.i_rwsem for the file
>
> the owner:
>
> [0 00:48:22.739] [UN] PID: 8854 TASK: ffff8c6914272e80 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff99650054fb38] __schedule at ffffffff9b6e6095
> #1 [ffff99650054fbc8] schedule at ffffffff9b6e64df
> #2 [ffff99650054fbd8] io_schedule at ffffffff9b6e68e2
> #3 [ffff99650054fbe8] __lock_page at ffffffff9b03c56f
> #4 [ffff99650054fc80] pagecache_get_page at ffffffff9b03dcdf
> #5 [ffff99650054fcc0] grab_cache_page_write_begin at ffffffff9b03ef4c
> #6 [ffff99650054fcd0] cifs_write_begin at ffffffffc0a064ec [cifs]
> #7 [ffff99650054fd30] generic_perform_write at ffffffff9b03bba4
> #8 [ffff99650054fda8] __generic_file_write_iter at ffffffff9b04060a
> #9 [ffff99650054fdf0] cifs_strict_writev.cold.70 at ffffffffc0a4469b [cifs]
> #10 [ffff99650054fe48] new_sync_write at ffffffff9b0ec1dd
> #11 [ffff99650054fed0] vfs_write at ffffffff9b0eed35
> #12 [ffff99650054ff00] ksys_write at ffffffff9b0eefd9
> #13 [ffff99650054ff38] do_syscall_64 at ffffffff9ae04315
>
> the process holds the inode->i_rwsem for the file to which it's writing,
> and is trying to __lock_page for the same page as in the other processes
>
>
> the other tasks:
> [0 00:00:00.028] [UN] PID: 8859 TASK: ffff8c6915479740 CPU: 2
> COMMAND: "reopen_file"
> #0 [ffff9965007b39d8] __schedule at ffffffff9b6e6095
> #1 [ffff9965007b3a68] schedule at ffffffff9b6e64df
> #2 [ffff9965007b3a78] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007b3af0] msleep at ffffffff9af573a9
> #4 [ffff9965007b3af8] cifs_new_fileinfo.cold.61 at ffffffffc0a42a07 [cifs]
> #5 [ffff9965007b3b78] cifs_open at ffffffffc0a0709d [cifs]
> #6 [ffff9965007b3cd8] do_dentry_open at ffffffff9b0e9b7a
> #7 [ffff9965007b3d08] path_openat at ffffffff9b0fe34f
> #8 [ffff9965007b3dd8] do_filp_open at ffffffff9b100a33
> #9 [ffff9965007b3ee0] do_sys_open at ffffffff9b0eb2d6
> #10 [ffff9965007b3f38] do_syscall_64 at ffffffff9ae04315
>
> this is opening the file, and is trying to down_write cinode->lock_sem
>
>
> [0 00:00:00.041] [UN] PID: 8860 TASK: ffff8c691547ae80 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.057] [UN] PID: 8861 TASK: ffff8c6915478000 CPU: 3
> COMMAND: "reopen_file"
> [0 00:00:00.059] [UN] PID: 8858 TASK: ffff8c6914271740 CPU: 2
> COMMAND: "reopen_file"
> [0 00:00:00.109] [UN] PID: 8862 TASK: ffff8c691547dd00 CPU: 6
> COMMAND: "reopen_file"
> #0 [ffff9965007c3c78] __schedule at ffffffff9b6e6095
> #1 [ffff9965007c3d08] schedule at ffffffff9b6e64df
> #2 [ffff9965007c3d18] schedule_timeout at ffffffff9b6e9f89
> #3 [ffff9965007c3d90] msleep at ffffffff9af573a9
> #4 [ffff9965007c3d98] _cifsFileInfo_put.cold.63 at ffffffffc0a42dd6 [cifs]
> #5 [ffff9965007c3e88] cifs_close at ffffffffc0a07aaf [cifs]
> #6 [ffff9965007c3ea0] __fput at ffffffff9b0efa6e
> #7 [ffff9965007c3ee8] task_work_run at ffffffff9aef1614
> #8 [ffff9965007c3f20] exit_to_usermode_loop at ffffffff9ae03d6f
> #9 [ffff9965007c3f38] do_syscall_64 at ffffffff9ae0444c
>
> closing the file, and trying to down_write cifsi->lock_sem
>
>
> [0 00:48:22.839] [UN] PID: 8857 TASK: ffff8c6914270000 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965006a7cc8] __schedule at ffffffff9b6e6095
> #1 [ffff9965006a7d58] schedule at ffffffff9b6e64df
> #2 [ffff9965006a7d68] io_schedule at ffffffff9b6e68e2
> #3 [ffff9965006a7d78] wait_on_page_bit at ffffffff9b03cac6
> #4 [ffff9965006a7e10] __filemap_fdatawait_range at ffffffff9b03b028
> #5 [ffff9965006a7ed8] filemap_write_and_wait at ffffffff9b040165
> #6 [ffff9965006a7ef0] cifs_flush at ffffffffc0a0c2fa [cifs]
> #7 [ffff9965006a7f10] filp_close at ffffffff9b0e93f1
> #8 [ffff9965006a7f30] __x64_sys_close at ffffffff9b0e9a0e
> #9 [ffff9965006a7f38] do_syscall_64 at ffffffff9ae04315
>
> in __filemap_fdatawait_range
> wait_on_page_writeback(page);
> for the same page of the file
>
>
>
> [0 00:48:22.718] [UN] PID: 8855 TASK: ffff8c69142745c0 CPU: 7
> COMMAND: "reopen_file"
> #0 [ffff9965005dfc98] __schedule at ffffffff9b6e6095
> #1 [ffff9965005dfd28] schedule at ffffffff9b6e64df
> #2 [ffff9965005dfd38] rwsem_down_write_slowpath at ffffffff9af283d7
> #3 [ffff9965005dfdf0] cifs_strict_writev at ffffffffc0a0c40a [cifs]
> #4 [ffff9965005dfe48] new_sync_write at ffffffff9b0ec1dd
> #5 [ffff9965005dfed0] vfs_write at ffffffff9b0eed35
> #6 [ffff9965005dff00] ksys_write at ffffffff9b0eefd9
> #7 [ffff9965005dff38] do_syscall_64 at ffffffff9ae04315
>
> inode_lock(inode);
>
>
> and one 'ls' later on, to see whether the rest of the mount is available
> (the test file is in the root, so we get blocked up on the directory
> ->i_rwsem), so the entire mount is unavailable
>
> [0 00:36:26.473] [UN] PID: 9802 TASK: ffff8c691436ae80 CPU: 4
> COMMAND: "ls"
> #0 [ffff996500393d28] __schedule at ffffffff9b6e6095
> #1 [ffff996500393db8] schedule at ffffffff9b6e64df
> #2 [ffff996500393dc8] rwsem_down_read_slowpath at ffffffff9b6e9421
> #3 [ffff996500393e78] down_read_killable at ffffffff9b6e95e2
> #4 [ffff996500393e88] iterate_dir at ffffffff9b103c56
> #5 [ffff996500393ec8] ksys_getdents64 at ffffffff9b104b0c
> #6 [ffff996500393f30] __x64_sys_getdents64 at ffffffff9b104bb6
> #7 [ffff996500393f38] do_syscall_64 at ffffffff9ae04315
>
> in iterate_dir:
> if (shared)
> res = down_read_killable(&inode->i_rwsem); <<<<
> else
> res = down_write_killable(&inode->i_rwsem);
>
Reported-by: Frank Sorenson <sorenson@redhat.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2019-11-03 13:06:37 +10:00
|
|
|
void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
|
|
|
|
bool wait_oplock_handler, bool offload)
|
2010-10-15 15:34:04 -04:00
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(cifs_file->dentry);
|
2011-05-27 04:34:02 +00:00
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
|
2012-09-19 06:22:45 -07:00
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
2010-11-02 12:00:42 +03:00
|
|
|
struct cifsInodeInfo *cifsi = CIFS_I(inode);
|
2013-03-08 16:30:03 +01:00
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
|
2022-10-14 20:00:32 -05:00
|
|
|
struct cifs_fid fid = {};
|
2012-09-19 06:22:45 -07:00
|
|
|
struct cifs_pending_open open;
|
2015-01-15 12:22:04 +00:00
|
|
|
bool oplock_break_cancelled;
|
2024-04-02 14:01:28 -05:00
|
|
|
bool serverclose_offloaded = false;
|
2010-10-21 22:46:14 +00:00
|
|
|
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_lock(&tcon->open_file_lock);
|
2019-10-23 15:37:19 -07:00
|
|
|
spin_lock(&cifsi->open_file_lock);
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_lock(&cifs_file->file_info_lock);
|
2024-04-02 14:01:28 -05:00
|
|
|
|
|
|
|
cifs_file->offload = offload;
|
2010-10-15 15:34:06 -04:00
|
|
|
if (--cifs_file->count > 0) {
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&cifs_file->file_info_lock);
|
2019-10-23 15:37:19 -07:00
|
|
|
spin_unlock(&cifsi->open_file_lock);
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2010-10-21 22:46:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&cifs_file->file_info_lock);
|
2010-10-21 22:46:14 +00:00
|
|
|
|
2012-09-19 06:22:45 -07:00
|
|
|
if (server->ops->get_lease_key)
|
|
|
|
server->ops->get_lease_key(inode, &fid);
|
|
|
|
|
|
|
|
/* store open in pending opens to make sure we don't miss lease break */
|
|
|
|
cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
|
|
|
|
|
2010-10-21 22:46:14 +00:00
|
|
|
/* remove it from the lists */
|
|
|
|
list_del(&cifs_file->flist);
|
|
|
|
list_del(&cifs_file->tlist);
|
2018-10-19 17:14:32 -05:00
|
|
|
atomic_dec(&tcon->num_local_opens);
|
2010-10-21 22:46:14 +00:00
|
|
|
|
|
|
|
if (list_empty(&cifsi->openFileList)) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "closing last open instance for inode %p\n",
|
2015-03-17 22:25:59 +00:00
|
|
|
d_inode(cifs_file->dentry));
|
2012-09-18 16:20:27 -07:00
|
|
|
/*
|
|
|
|
* In strict cache mode we need invalidate mapping on the last
|
|
|
|
* close because it may cause a error when we open this file
|
|
|
|
* again and get at least level II oplock.
|
|
|
|
*/
|
2010-11-21 22:36:12 +03:00
|
|
|
if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
|
2014-04-30 09:31:45 -04:00
|
|
|
set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
|
2010-11-03 10:58:57 +03:00
|
|
|
cifs_set_oplock_level(cifsi, 0);
|
2010-10-21 22:46:14 +00:00
|
|
|
}
|
2016-09-22 18:58:16 -05:00
|
|
|
|
2019-10-23 15:37:19 -07:00
|
|
|
spin_unlock(&cifsi->open_file_lock);
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2010-10-21 22:46:14 +00:00
|
|
|
|
2019-03-29 10:49:12 +01:00
|
|
|
oplock_break_cancelled = wait_oplock_handler ?
|
|
|
|
cancel_work_sync(&cifs_file->oplock_break) : false;
|
2011-07-26 12:20:17 -04:00
|
|
|
|
2010-10-21 22:46:14 +00:00
|
|
|
if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
|
2012-09-18 16:20:26 -07:00
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
2024-04-02 14:01:28 -05:00
|
|
|
int rc = 0;
|
2012-09-18 16:20:26 -07:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2019-12-02 21:46:54 -06:00
|
|
|
if (server->ops->close_getattr)
|
2024-04-02 14:01:28 -05:00
|
|
|
rc = server->ops->close_getattr(xid, tcon, cifs_file);
|
2019-12-02 21:46:54 -06:00
|
|
|
else if (server->ops->close)
|
2024-04-02 14:01:28 -05:00
|
|
|
rc = server->ops->close(xid, tcon, &cifs_file->fid);
|
2012-09-25 11:00:07 +04:00
|
|
|
_free_xid(xid);
|
2024-04-02 14:01:28 -05:00
|
|
|
|
|
|
|
if (rc == -EBUSY || rc == -EAGAIN) {
|
|
|
|
// Server close failed, hence offloading it as an async op
|
|
|
|
queue_work(serverclose_wq, &cifs_file->serverclose);
|
|
|
|
serverclose_offloaded = true;
|
|
|
|
}
|
2010-10-21 22:46:14 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 12:22:04 +00:00
|
|
|
if (oplock_break_cancelled)
|
|
|
|
cifs_done_oplock_break(cifsi);
|
|
|
|
|
2012-09-19 06:22:45 -07:00
|
|
|
cifs_del_pending_open(&open);
|
|
|
|
|
2024-04-02 14:01:28 -05:00
|
|
|
// if serverclose has been offloaded to wq (on failure), it will
|
|
|
|
// handle offloading put as well. If serverclose not offloaded,
|
|
|
|
// we need to handle offloading put here.
|
|
|
|
if (!serverclose_offloaded) {
|
|
|
|
if (offload)
|
|
|
|
queue_work(fileinfo_put_wq, &cifs_file->put);
|
|
|
|
else
|
|
|
|
cifsFileInfo_put_final(cifs_file);
|
|
|
|
}
|
2010-10-15 15:34:04 -04:00
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
int cifs_open(struct inode *inode, struct file *file)
|
2012-09-19 06:22:45 -07:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
int rc = -EACCES;
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
2009-09-12 11:54:28 -04:00
|
|
|
__u32 oplock;
|
2005-04-16 15:20:36 -07:00
|
|
|
struct cifs_sb_info *cifs_sb;
|
2012-09-19 06:22:44 -07:00
|
|
|
struct TCP_Server_Info *server;
|
2011-05-27 04:34:02 +00:00
|
|
|
struct cifs_tcon *tcon;
|
2010-09-29 19:51:11 -04:00
|
|
|
struct tcon_link *tlink;
|
2012-09-18 16:20:26 -07:00
|
|
|
struct cifsFileInfo *cfile = NULL;
|
2021-03-05 17:36:04 -05:00
|
|
|
void *page;
|
|
|
|
const char *full_path;
|
2010-11-25 17:20:20 +03:00
|
|
|
bool posix_open_ok = false;
|
2022-10-14 20:00:32 -05:00
|
|
|
struct cifs_fid fid = {};
|
2012-09-19 06:22:45 -07:00
|
|
|
struct cifs_pending_open open;
|
2022-10-03 18:43:50 -03:00
|
|
|
struct cifs_open_info_data data = {};
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
cifs_sb = CIFS_SB(inode->i_sb);
|
2021-04-29 00:18:43 -05:00
|
|
|
if (unlikely(cifs_forced_shutdown(cifs_sb))) {
|
|
|
|
free_xid(xid);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2010-09-29 19:51:11 -04:00
|
|
|
tlink = cifs_sb_tlink(cifs_sb);
|
|
|
|
if (IS_ERR(tlink)) {
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2010-09-29 19:51:11 -04:00
|
|
|
return PTR_ERR(tlink);
|
|
|
|
}
|
|
|
|
tcon = tlink_tcon(tlink);
|
2012-09-19 06:22:44 -07:00
|
|
|
server = tcon->ses->server;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2021-03-05 17:36:04 -05:00
|
|
|
page = alloc_dentry_path();
|
|
|
|
full_path = build_path_from_dentry(file_dentry(file), page);
|
|
|
|
if (IS_ERR(full_path)) {
|
|
|
|
rc = PTR_ERR(full_path);
|
2010-08-05 13:58:38 -04:00
|
|
|
goto out;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
|
2010-04-21 03:50:45 +00:00
|
|
|
inode, file->f_flags, full_path);
|
2009-03-03 18:00:34 +00:00
|
|
|
|
2014-08-22 14:22:51 +09:00
|
|
|
if (file->f_flags & O_DIRECT &&
|
|
|
|
cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
|
|
|
|
if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
|
|
|
|
file->f_op = &cifs_file_direct_nobrl_ops;
|
|
|
|
else
|
|
|
|
file->f_op = &cifs_file_direct_ops;
|
|
|
|
}
|
|
|
|
|
2021-04-13 00:26:42 -05:00
|
|
|
/* Get the cached handle as SMB2 close is deferred */
|
2024-12-13 22:50:21 +05:30
|
|
|
if (OPEN_FMODE(file->f_flags) & FMODE_WRITE) {
|
|
|
|
rc = cifs_get_writable_path(tcon, full_path, FIND_WR_FSUID_ONLY, &cfile);
|
|
|
|
} else {
|
|
|
|
rc = cifs_get_readable_path(tcon, full_path, &cfile);
|
|
|
|
}
|
2021-04-13 00:26:42 -05:00
|
|
|
if (rc == 0) {
|
smb: client: fix perf regression with deferred closes
Customer reported that one of their applications started failing to
open files with STATUS_INSUFFICIENT_RESOURCES due to NetApp server
hitting the maximum number of opens to same file that it would allow
for a single client connection.
It turned out the client was failing to reuse open handles with
deferred closes because matching ->f_flags directly without masking
off O_CREAT|O_EXCL|O_TRUNC bits first broke the comparision and then
client ended up with thousands of deferred closes to same file. Those
bits are already satisfied on the original open, so no need to check
them against existing open handles.
Reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#define NR_THREADS 4
#define NR_ITERATIONS 2500
#define TEST_FILE "/mnt/1/test/dir/foo"
static char buf[64];
static void *worker(void *arg)
{
int i, j;
int fd;
for (i = 0; i < NR_ITERATIONS; i++) {
fd = open(TEST_FILE, O_WRONLY|O_CREAT|O_APPEND, 0666);
for (j = 0; j < 16; j++)
write(fd, buf, sizeof(buf));
close(fd);
}
}
int main(int argc, char *argv[])
{
pthread_t t[NR_THREADS];
int fd;
int i;
fd = open(TEST_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
close(fd);
memset(buf, 'a', sizeof(buf));
for (i = 0; i < NR_THREADS; i++)
pthread_create(&t[i], NULL, worker, NULL);
for (i = 0; i < NR_THREADS; i++)
pthread_join(t[i], NULL);
return 0;
}
Before patch:
$ mount.cifs //srv/share /mnt/1 -o ...
$ mkdir -p /mnt/1/test/dir
$ gcc repro.c && ./a.out
...
number of opens: 1391
After patch:
$ mount.cifs //srv/share /mnt/1 -o ...
$ mkdir -p /mnt/1/test/dir
$ gcc repro.c && ./a.out
...
number of opens: 1
Cc: linux-cifs@vger.kernel.org
Cc: David Howells <dhowells@redhat.com>
Cc: Jay Shin <jaeshin@redhat.com>
Cc: Pierguido Lambri <plambri@redhat.com>
Fixes: b8ea3b1ff544 ("smb: enable reuse of deferred file handles for write operations")
Acked-by: Shyam Prasad N <sprasad@microsoft.com>
Signed-off-by: Paulo Alcantara (Red Hat) <pc@manguebit.org>
Signed-off-by: Steve French <stfrench@microsoft.com>
2025-06-12 12:45:04 -03:00
|
|
|
unsigned int oflags = file->f_flags & ~(O_CREAT|O_EXCL|O_TRUNC);
|
|
|
|
unsigned int cflags = cfile->f_flags & ~(O_CREAT|O_EXCL|O_TRUNC);
|
|
|
|
|
|
|
|
if (cifs_convert_flags(oflags, 0) == cifs_convert_flags(cflags, 0) &&
|
|
|
|
(oflags & (O_SYNC|O_DIRECT)) == (cflags & (O_SYNC|O_DIRECT))) {
|
2021-04-13 00:26:42 -05:00
|
|
|
file->private_data = cfile;
|
2021-05-05 10:56:47 +00:00
|
|
|
spin_lock(&CIFS_I(inode)->deferred_lock);
|
2021-04-13 00:26:42 -05:00
|
|
|
cifs_del_deferred_close(cfile);
|
|
|
|
spin_unlock(&CIFS_I(inode)->deferred_lock);
|
cifs: Support fscache indexing rewrite
Change the cifs filesystem to take account of the changes to fscache's
indexing rewrite and reenable caching in cifs.
The following changes have been made:
(1) The fscache_netfs struct is no more, and there's no need to register
the filesystem as a whole.
(2) The session cookie is now an fscache_volume cookie, allocated with
fscache_acquire_volume(). That takes three parameters: a string
representing the "volume" in the index, a string naming the cache to
use (or NULL) and a u64 that conveys coherency metadata for the
volume.
For cifs, I've made it render the volume name string as:
"cifs,<ipaddress>,<sharename>"
where the sharename has '/' characters replaced with ';'.
This probably needs rethinking a bit as the total name could exceed
the maximum filename component length.
Further, the coherency data is currently just set to 0. It needs
something else doing with it - I wonder if it would suffice simply to
sum the resource_id, vol_create_time and vol_serial_number or maybe
hash them.
(3) The fscache_cookie_def is no more and needed information is passed
directly to fscache_acquire_cookie(). The cache no longer calls back
into the filesystem, but rather metadata changes are indicated at
other times.
fscache_acquire_cookie() is passed the same keying and coherency
information as before.
(4) The functions to set/reset cookies are removed and
fscache_use_cookie() and fscache_unuse_cookie() are used instead.
fscache_use_cookie() is passed a flag to indicate if the cookie is
opened for writing. fscache_unuse_cookie() is passed updates for the
metadata if we changed it (ie. if the file was opened for writing).
These are called when the file is opened or closed.
(5) cifs_setattr_*() are made to call fscache_resize() to change the size
of the cache object.
(6) The functions to read and write data are stubbed out pending a
conversion to use netfslib.
Changes
=======
ver #8:
- Abstract cache invalidation into a helper function.
- Fix some checkpatch warnings[3].
ver #7:
- Removed the accidentally added-back call to get the super cookie in
cifs_root_iget().
- Fixed the right call to cifs_fscache_get_super_cookie() to take account
of the "-o fsc" mount flag.
ver #6:
- Moved the change of gfpflags_allow_blocking() to current_is_kswapd() for
cifs here.
- Fixed one of the error paths in cifs_atomic_open() to jump around the
call to use the cookie.
- Fixed an additional successful return in the middle of cifs_open() to
use the cookie on the way out.
- Only get a volume cookie (and thus inode cookies) when "-o fsc" is
supplied to mount.
ver #5:
- Fixed a couple of bits of cookie handling[2]:
- The cookie should be released in cifs_evict_inode(), not
cifsFileInfo_put_final(). The cookie needs to persist beyond file
closure so that writepages will be able to write to it.
- fscache_use_cookie() needs to be called in cifs_atomic_open() as it is
for cifs_open().
ver #4:
- Fixed the use of sizeof with memset.
- tcon->vol_create_time is __le64 so doesn't need cpu_to_le64().
ver #3:
- Canonicalise the cifs coherency data to make the cache portable.
- Set volume coherency data.
ver #2:
- Use gfpflags_allow_blocking() rather than using flag directly.
- Upgraded to -rc4 to allow for upstream changes[1].
- fscache_acquire_volume() now returns errors.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Jeff Layton <jlayton@kernel.org>
cc: Steve French <smfrench@gmail.com>
cc: Shyam Prasad N <nspmangalore@gmail.com>
cc: linux-cifs@vger.kernel.org
cc: linux-cachefs@redhat.com
Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=23b55d673d7527b093cd97b7c217c82e70cd1af0 [1]
Link: https://lore.kernel.org/r/3419813.1641592362@warthog.procyon.org.uk/ [2]
Link: https://lore.kernel.org/r/CAH2r5muTanw9pJqzAHd01d9A8keeChkzGsCEH6=0rHutVLAF-A@mail.gmail.com/ [3]
Link: https://lore.kernel.org/r/163819671009.215744.11230627184193298714.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906982979.143852.10672081929614953210.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/163967187187.1823006.247415138444991444.stgit@warthog.procyon.org.uk/ # v3
Link: https://lore.kernel.org/r/164021579335.640689.2681324337038770579.stgit@warthog.procyon.org.uk/ # v4
Link: https://lore.kernel.org/r/3462849.1641593783@warthog.procyon.org.uk/ # v5
Link: https://lore.kernel.org/r/1318953.1642024578@warthog.procyon.org.uk/ # v6
Signed-off-by: Steve French <stfrench@microsoft.com>
2020-11-17 15:56:59 +00:00
|
|
|
goto use_cache;
|
2021-04-13 00:26:42 -05:00
|
|
|
}
|
smb: client: fix perf regression with deferred closes
Customer reported that one of their applications started failing to
open files with STATUS_INSUFFICIENT_RESOURCES due to NetApp server
hitting the maximum number of opens to same file that it would allow
for a single client connection.
It turned out the client was failing to reuse open handles with
deferred closes because matching ->f_flags directly without masking
off O_CREAT|O_EXCL|O_TRUNC bits first broke the comparision and then
client ended up with thousands of deferred closes to same file. Those
bits are already satisfied on the original open, so no need to check
them against existing open handles.
Reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#define NR_THREADS 4
#define NR_ITERATIONS 2500
#define TEST_FILE "/mnt/1/test/dir/foo"
static char buf[64];
static void *worker(void *arg)
{
int i, j;
int fd;
for (i = 0; i < NR_ITERATIONS; i++) {
fd = open(TEST_FILE, O_WRONLY|O_CREAT|O_APPEND, 0666);
for (j = 0; j < 16; j++)
write(fd, buf, sizeof(buf));
close(fd);
}
}
int main(int argc, char *argv[])
{
pthread_t t[NR_THREADS];
int fd;
int i;
fd = open(TEST_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
close(fd);
memset(buf, 'a', sizeof(buf));
for (i = 0; i < NR_THREADS; i++)
pthread_create(&t[i], NULL, worker, NULL);
for (i = 0; i < NR_THREADS; i++)
pthread_join(t[i], NULL);
return 0;
}
Before patch:
$ mount.cifs //srv/share /mnt/1 -o ...
$ mkdir -p /mnt/1/test/dir
$ gcc repro.c && ./a.out
...
number of opens: 1391
After patch:
$ mount.cifs //srv/share /mnt/1 -o ...
$ mkdir -p /mnt/1/test/dir
$ gcc repro.c && ./a.out
...
number of opens: 1
Cc: linux-cifs@vger.kernel.org
Cc: David Howells <dhowells@redhat.com>
Cc: Jay Shin <jaeshin@redhat.com>
Cc: Pierguido Lambri <plambri@redhat.com>
Fixes: b8ea3b1ff544 ("smb: enable reuse of deferred file handles for write operations")
Acked-by: Shyam Prasad N <sprasad@microsoft.com>
Signed-off-by: Paulo Alcantara (Red Hat) <pc@manguebit.org>
Signed-off-by: Steve French <stfrench@microsoft.com>
2025-06-12 12:45:04 -03:00
|
|
|
_cifsFileInfo_put(cfile, true, false);
|
smb3 client: fix open hardlink on deferred close file error
The following Python script results in unexpected behaviour when run on
a CIFS filesystem against a Windows Server:
# Create file
fd = os.open('test', os.O_WRONLY|os.O_CREAT)
os.write(fd, b'foo')
os.close(fd)
# Open and close the file to leave a pending deferred close
fd = os.open('test', os.O_RDONLY|os.O_DIRECT)
os.close(fd)
# Try to open the file via a hard link
os.link('test', 'new')
newfd = os.open('new', os.O_RDONLY|os.O_DIRECT)
The final open returns EINVAL due to the server returning
STATUS_INVALID_PARAMETER. The root cause of this is that the client
caches lease keys per inode, but the spec requires them to be related to
the filename which causes problems when hard links are involved:
From MS-SMB2 section 3.3.5.9.11:
"The server MUST attempt to locate a Lease by performing a lookup in the
LeaseTable.LeaseList using the LeaseKey in the
SMB2_CREATE_REQUEST_LEASE_V2 as the lookup key. If a lease is found,
Lease.FileDeleteOnClose is FALSE, and Lease.Filename does not match the
file name for the incoming request, the request MUST be failed with
STATUS_INVALID_PARAMETER"
On client side, we first check the context of file open, if it hits above
conditions, we first close all opening files which are belong to the same
inode, then we do open the hard link file.
Cc: stable@vger.kernel.org
Signed-off-by: Chunjie Zhu <chunjie.zhu@cloud.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2025-04-12 21:15:55 -05:00
|
|
|
} else {
|
|
|
|
/* hard link on the defeered close file */
|
|
|
|
rc = cifs_get_hardlink_path(tcon, inode, file);
|
|
|
|
if (rc)
|
|
|
|
cifs_close_deferred_file(CIFS_I(inode));
|
2021-04-13 00:26:42 -05:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:45 -07:00
|
|
|
if (server->oplocks)
|
2009-03-03 18:00:34 +00:00
|
|
|
oplock = REQ_OPLOCK;
|
|
|
|
else
|
|
|
|
oplock = 0;
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2009-03-04 19:54:08 +00:00
|
|
|
if (!tcon->broken_posix_open && tcon->unix_ext &&
|
2012-07-13 13:58:14 +04:00
|
|
|
cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
|
|
|
|
le64_to_cpu(tcon->fsUnixInfo.Capability))) {
|
2009-03-03 18:00:34 +00:00
|
|
|
/* can not refresh inode info since size could be stale */
|
2010-06-16 13:40:16 -04:00
|
|
|
rc = cifs_posix_open(full_path, &inode, inode->i_sb,
|
2020-12-12 13:40:50 -06:00
|
|
|
cifs_sb->ctx->file_mode /* ignored */,
|
2012-09-18 16:20:26 -07:00
|
|
|
file->f_flags, &oplock, &fid.netfid, xid);
|
2009-03-03 18:00:34 +00:00
|
|
|
if (rc == 0) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "posix open succeeded\n");
|
2010-11-25 17:20:20 +03:00
|
|
|
posix_open_ok = true;
|
2009-03-04 19:54:08 +00:00
|
|
|
} else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
|
|
|
|
if (tcon->ses->serverNOS)
|
2013-05-04 22:12:25 -05:00
|
|
|
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",
|
2021-02-20 19:24:11 -06:00
|
|
|
tcon->ses->ip_addr,
|
2013-05-04 22:12:25 -05:00
|
|
|
tcon->ses->serverNOS);
|
2009-03-04 19:54:08 +00:00
|
|
|
tcon->broken_posix_open = true;
|
2009-03-03 18:00:34 +00:00
|
|
|
} else if ((rc != -EIO) && (rc != -EREMOTE) &&
|
|
|
|
(rc != -EOPNOTSUPP)) /* path not found or net err */
|
|
|
|
goto out;
|
2012-09-18 16:20:26 -07:00
|
|
|
/*
|
|
|
|
* Else fallthrough to retry open the old way on network i/o
|
|
|
|
* or DFS errors.
|
|
|
|
*/
|
2009-03-03 18:00:34 +00:00
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2009-03-03 18:00:34 +00:00
|
|
|
|
2012-09-19 06:22:45 -07:00
|
|
|
if (server->ops->get_lease_key)
|
|
|
|
server->ops->get_lease_key(inode, &fid);
|
|
|
|
|
|
|
|
cifs_add_pending_open(&fid, tlink, &open);
|
|
|
|
|
2010-11-25 17:20:20 +03:00
|
|
|
if (!posix_open_ok) {
|
2012-09-19 06:22:44 -07:00
|
|
|
if (server->ops->get_lease_key)
|
|
|
|
server->ops->get_lease_key(inode, &fid);
|
|
|
|
|
2022-10-03 18:43:50 -03:00
|
|
|
rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
|
|
|
|
xid, &data);
|
2012-09-19 06:22:45 -07:00
|
|
|
if (rc) {
|
|
|
|
cifs_del_pending_open(&open);
|
2010-11-25 17:20:20 +03:00
|
|
|
goto out;
|
2012-09-19 06:22:45 -07:00
|
|
|
}
|
2010-11-25 17:20:20 +03:00
|
|
|
}
|
2010-06-16 13:40:17 -04:00
|
|
|
|
2022-10-03 18:43:50 -03:00
|
|
|
cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
|
2012-09-18 16:20:26 -07:00
|
|
|
if (cfile == NULL) {
|
2012-09-19 06:22:44 -07:00
|
|
|
if (server->ops->close)
|
|
|
|
server->ops->close(xid, tcon, &fid);
|
2012-09-19 06:22:45 -07:00
|
|
|
cifs_del_pending_open(&open);
|
2005-04-16 15:20:36 -07:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2010-11-25 17:20:20 +03:00
|
|
|
if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
|
2012-09-18 16:20:26 -07:00
|
|
|
/*
|
|
|
|
* Time to set mode which we can not set earlier due to
|
|
|
|
* problems creating new read-only files.
|
|
|
|
*/
|
2010-11-25 17:20:20 +03:00
|
|
|
struct cifs_unix_set_info_args args = {
|
|
|
|
.mode = inode->i_mode,
|
2013-02-06 00:57:56 -08:00
|
|
|
.uid = INVALID_UID, /* no change */
|
|
|
|
.gid = INVALID_GID, /* no change */
|
2010-11-25 17:20:20 +03:00
|
|
|
.ctime = NO_CHANGE_64,
|
|
|
|
.atime = NO_CHANGE_64,
|
|
|
|
.mtime = NO_CHANGE_64,
|
|
|
|
.device = 0,
|
|
|
|
};
|
2012-09-18 16:20:26 -07:00
|
|
|
CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
|
|
|
|
cfile->pid);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2005-04-16 15:20:36 -07:00
|
|
|
|
cifs: Support fscache indexing rewrite
Change the cifs filesystem to take account of the changes to fscache's
indexing rewrite and reenable caching in cifs.
The following changes have been made:
(1) The fscache_netfs struct is no more, and there's no need to register
the filesystem as a whole.
(2) The session cookie is now an fscache_volume cookie, allocated with
fscache_acquire_volume(). That takes three parameters: a string
representing the "volume" in the index, a string naming the cache to
use (or NULL) and a u64 that conveys coherency metadata for the
volume.
For cifs, I've made it render the volume name string as:
"cifs,<ipaddress>,<sharename>"
where the sharename has '/' characters replaced with ';'.
This probably needs rethinking a bit as the total name could exceed
the maximum filename component length.
Further, the coherency data is currently just set to 0. It needs
something else doing with it - I wonder if it would suffice simply to
sum the resource_id, vol_create_time and vol_serial_number or maybe
hash them.
(3) The fscache_cookie_def is no more and needed information is passed
directly to fscache_acquire_cookie(). The cache no longer calls back
into the filesystem, but rather metadata changes are indicated at
other times.
fscache_acquire_cookie() is passed the same keying and coherency
information as before.
(4) The functions to set/reset cookies are removed and
fscache_use_cookie() and fscache_unuse_cookie() are used instead.
fscache_use_cookie() is passed a flag to indicate if the cookie is
opened for writing. fscache_unuse_cookie() is passed updates for the
metadata if we changed it (ie. if the file was opened for writing).
These are called when the file is opened or closed.
(5) cifs_setattr_*() are made to call fscache_resize() to change the size
of the cache object.
(6) The functions to read and write data are stubbed out pending a
conversion to use netfslib.
Changes
=======
ver #8:
- Abstract cache invalidation into a helper function.
- Fix some checkpatch warnings[3].
ver #7:
- Removed the accidentally added-back call to get the super cookie in
cifs_root_iget().
- Fixed the right call to cifs_fscache_get_super_cookie() to take account
of the "-o fsc" mount flag.
ver #6:
- Moved the change of gfpflags_allow_blocking() to current_is_kswapd() for
cifs here.
- Fixed one of the error paths in cifs_atomic_open() to jump around the
call to use the cookie.
- Fixed an additional successful return in the middle of cifs_open() to
use the cookie on the way out.
- Only get a volume cookie (and thus inode cookies) when "-o fsc" is
supplied to mount.
ver #5:
- Fixed a couple of bits of cookie handling[2]:
- The cookie should be released in cifs_evict_inode(), not
cifsFileInfo_put_final(). The cookie needs to persist beyond file
closure so that writepages will be able to write to it.
- fscache_use_cookie() needs to be called in cifs_atomic_open() as it is
for cifs_open().
ver #4:
- Fixed the use of sizeof with memset.
- tcon->vol_create_time is __le64 so doesn't need cpu_to_le64().
ver #3:
- Canonicalise the cifs coherency data to make the cache portable.
- Set volume coherency data.
ver #2:
- Use gfpflags_allow_blocking() rather than using flag directly.
- Upgraded to -rc4 to allow for upstream changes[1].
- fscache_acquire_volume() now returns errors.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Jeff Layton <jlayton@kernel.org>
cc: Steve French <smfrench@gmail.com>
cc: Shyam Prasad N <nspmangalore@gmail.com>
cc: linux-cifs@vger.kernel.org
cc: linux-cachefs@redhat.com
Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=23b55d673d7527b093cd97b7c217c82e70cd1af0 [1]
Link: https://lore.kernel.org/r/3419813.1641592362@warthog.procyon.org.uk/ [2]
Link: https://lore.kernel.org/r/CAH2r5muTanw9pJqzAHd01d9A8keeChkzGsCEH6=0rHutVLAF-A@mail.gmail.com/ [3]
Link: https://lore.kernel.org/r/163819671009.215744.11230627184193298714.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906982979.143852.10672081929614953210.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/163967187187.1823006.247415138444991444.stgit@warthog.procyon.org.uk/ # v3
Link: https://lore.kernel.org/r/164021579335.640689.2681324337038770579.stgit@warthog.procyon.org.uk/ # v4
Link: https://lore.kernel.org/r/3462849.1641593783@warthog.procyon.org.uk/ # v5
Link: https://lore.kernel.org/r/1318953.1642024578@warthog.procyon.org.uk/ # v6
Signed-off-by: Steve French <stfrench@microsoft.com>
2020-11-17 15:56:59 +00:00
|
|
|
use_cache:
|
|
|
|
fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
|
|
|
|
file->f_mode & FMODE_WRITE);
|
2024-04-02 10:11:35 +01:00
|
|
|
if (!(file->f_flags & O_DIRECT))
|
|
|
|
goto out;
|
|
|
|
if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY)
|
|
|
|
goto out;
|
|
|
|
cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE);
|
cifs: Support fscache indexing rewrite
Change the cifs filesystem to take account of the changes to fscache's
indexing rewrite and reenable caching in cifs.
The following changes have been made:
(1) The fscache_netfs struct is no more, and there's no need to register
the filesystem as a whole.
(2) The session cookie is now an fscache_volume cookie, allocated with
fscache_acquire_volume(). That takes three parameters: a string
representing the "volume" in the index, a string naming the cache to
use (or NULL) and a u64 that conveys coherency metadata for the
volume.
For cifs, I've made it render the volume name string as:
"cifs,<ipaddress>,<sharename>"
where the sharename has '/' characters replaced with ';'.
This probably needs rethinking a bit as the total name could exceed
the maximum filename component length.
Further, the coherency data is currently just set to 0. It needs
something else doing with it - I wonder if it would suffice simply to
sum the resource_id, vol_create_time and vol_serial_number or maybe
hash them.
(3) The fscache_cookie_def is no more and needed information is passed
directly to fscache_acquire_cookie(). The cache no longer calls back
into the filesystem, but rather metadata changes are indicated at
other times.
fscache_acquire_cookie() is passed the same keying and coherency
information as before.
(4) The functions to set/reset cookies are removed and
fscache_use_cookie() and fscache_unuse_cookie() are used instead.
fscache_use_cookie() is passed a flag to indicate if the cookie is
opened for writing. fscache_unuse_cookie() is passed updates for the
metadata if we changed it (ie. if the file was opened for writing).
These are called when the file is opened or closed.
(5) cifs_setattr_*() are made to call fscache_resize() to change the size
of the cache object.
(6) The functions to read and write data are stubbed out pending a
conversion to use netfslib.
Changes
=======
ver #8:
- Abstract cache invalidation into a helper function.
- Fix some checkpatch warnings[3].
ver #7:
- Removed the accidentally added-back call to get the super cookie in
cifs_root_iget().
- Fixed the right call to cifs_fscache_get_super_cookie() to take account
of the "-o fsc" mount flag.
ver #6:
- Moved the change of gfpflags_allow_blocking() to current_is_kswapd() for
cifs here.
- Fixed one of the error paths in cifs_atomic_open() to jump around the
call to use the cookie.
- Fixed an additional successful return in the middle of cifs_open() to
use the cookie on the way out.
- Only get a volume cookie (and thus inode cookies) when "-o fsc" is
supplied to mount.
ver #5:
- Fixed a couple of bits of cookie handling[2]:
- The cookie should be released in cifs_evict_inode(), not
cifsFileInfo_put_final(). The cookie needs to persist beyond file
closure so that writepages will be able to write to it.
- fscache_use_cookie() needs to be called in cifs_atomic_open() as it is
for cifs_open().
ver #4:
- Fixed the use of sizeof with memset.
- tcon->vol_create_time is __le64 so doesn't need cpu_to_le64().
ver #3:
- Canonicalise the cifs coherency data to make the cache portable.
- Set volume coherency data.
ver #2:
- Use gfpflags_allow_blocking() rather than using flag directly.
- Upgraded to -rc4 to allow for upstream changes[1].
- fscache_acquire_volume() now returns errors.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Jeff Layton <jlayton@kernel.org>
cc: Steve French <smfrench@gmail.com>
cc: Shyam Prasad N <nspmangalore@gmail.com>
cc: linux-cifs@vger.kernel.org
cc: linux-cachefs@redhat.com
Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=23b55d673d7527b093cd97b7c217c82e70cd1af0 [1]
Link: https://lore.kernel.org/r/3419813.1641592362@warthog.procyon.org.uk/ [2]
Link: https://lore.kernel.org/r/CAH2r5muTanw9pJqzAHd01d9A8keeChkzGsCEH6=0rHutVLAF-A@mail.gmail.com/ [3]
Link: https://lore.kernel.org/r/163819671009.215744.11230627184193298714.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906982979.143852.10672081929614953210.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/163967187187.1823006.247415138444991444.stgit@warthog.procyon.org.uk/ # v3
Link: https://lore.kernel.org/r/164021579335.640689.2681324337038770579.stgit@warthog.procyon.org.uk/ # v4
Link: https://lore.kernel.org/r/3462849.1641593783@warthog.procyon.org.uk/ # v5
Link: https://lore.kernel.org/r/1318953.1642024578@warthog.procyon.org.uk/ # v6
Signed-off-by: Steve French <stfrench@microsoft.com>
2020-11-17 15:56:59 +00:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
out:
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2010-09-29 19:51:11 -04:00
|
|
|
cifs_put_tlink(tlink);
|
2022-10-03 18:43:50 -03:00
|
|
|
cifs_free_open_info(&data);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2012-11-22 17:10:57 +04:00
|
|
|
static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2012-11-22 17:10:57 +04:00
|
|
|
|
2012-09-18 16:20:27 -07:00
|
|
|
/*
|
|
|
|
* Try to reacquire byte range locks that were released when session
|
2012-11-22 17:10:57 +04:00
|
|
|
* to server was lost.
|
2012-09-18 16:20:27 -07:00
|
|
|
*/
|
2012-11-22 17:10:57 +04:00
|
|
|
static int
|
|
|
|
cifs_relock_file(struct cifsFileInfo *cfile)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2012-11-22 17:10:57 +04:00
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2005-04-16 15:20:36 -07:00
|
|
|
int rc = 0;
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
|
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2017-05-03 17:17:21 +02:00
|
|
|
down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
|
2012-11-22 17:10:57 +04:00
|
|
|
if (cinode->can_cache_brlcks) {
|
2013-07-11 11:17:45 +04:00
|
|
|
/* can cache locks - no need to relock */
|
|
|
|
up_read(&cinode->lock_sem);
|
2012-11-22 17:10:57 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2012-11-22 17:10:57 +04:00
|
|
|
if (cap_unix(tcon->ses) &&
|
|
|
|
(CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
|
|
|
|
((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
|
|
|
|
rc = cifs_push_posix_locks(cfile);
|
|
|
|
else
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2012-11-22 17:10:57 +04:00
|
|
|
rc = tcon->ses->server->ops->push_mand_locks(cfile);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-07-11 11:17:45 +04:00
|
|
|
up_read(&cinode->lock_sem);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-09-18 16:20:27 -07:00
|
|
|
static int
|
|
|
|
cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
int rc = -EACCES;
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
2009-09-12 11:54:28 -04:00
|
|
|
__u32 oplock;
|
2005-04-16 15:20:36 -07:00
|
|
|
struct cifs_sb_info *cifs_sb;
|
2011-05-27 04:34:02 +00:00
|
|
|
struct cifs_tcon *tcon;
|
2012-09-18 16:20:27 -07:00
|
|
|
struct TCP_Server_Info *server;
|
|
|
|
struct cifsInodeInfo *cinode;
|
2007-07-10 01:16:18 +00:00
|
|
|
struct inode *inode;
|
2021-03-05 17:36:04 -05:00
|
|
|
void *page;
|
|
|
|
const char *full_path;
|
2012-09-18 16:20:27 -07:00
|
|
|
int desired_access;
|
2005-04-16 15:20:36 -07:00
|
|
|
int disposition = FILE_OPEN;
|
2011-09-26 09:56:44 -05:00
|
|
|
int create_options = CREATE_NOT_DIR;
|
2013-07-05 12:00:30 +04:00
|
|
|
struct cifs_open_parms oparms;
|
2024-04-02 10:11:35 +01:00
|
|
|
int rdwr_for_fscache = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2012-09-18 16:20:27 -07:00
|
|
|
mutex_lock(&cfile->fh_mutex);
|
|
|
|
if (!cfile->invalidHandle) {
|
|
|
|
mutex_unlock(&cfile->fh_mutex);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2021-03-05 17:36:04 -05:00
|
|
|
return 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:25:59 +00:00
|
|
|
inode = d_inode(cfile->dentry);
|
2005-04-16 15:20:36 -07:00
|
|
|
cifs_sb = CIFS_SB(inode->i_sb);
|
2012-09-18 16:20:27 -07:00
|
|
|
tcon = tlink_tcon(cfile->tlink);
|
|
|
|
server = tcon->ses->server;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can not grab rename sem here because various ops, including those
|
|
|
|
* that already have the rename sem can end up causing writepage to get
|
|
|
|
* called and if the server was down that means we end up here, and we
|
|
|
|
* can never tell if the caller already has the rename_sem.
|
|
|
|
*/
|
2021-03-05 17:36:04 -05:00
|
|
|
page = alloc_dentry_path();
|
|
|
|
full_path = build_path_from_dentry(cfile->dentry, page);
|
|
|
|
if (IS_ERR(full_path)) {
|
2012-09-18 16:20:27 -07:00
|
|
|
mutex_unlock(&cfile->fh_mutex);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2021-03-05 17:36:04 -05:00
|
|
|
return PTR_ERR(full_path);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
|
|
|
|
inode, cfile->f_flags, full_path);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-03-20 12:55:09 +03:00
|
|
|
if (tcon->ses->server->oplocks)
|
2005-04-16 15:20:36 -07:00
|
|
|
oplock = REQ_OPLOCK;
|
|
|
|
else
|
2008-04-29 00:06:05 +00:00
|
|
|
oplock = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2012-07-13 13:58:14 +04:00
|
|
|
if (tcon->unix_ext && cap_unix(tcon->ses) &&
|
2009-02-23 20:43:11 +00:00
|
|
|
(CIFS_UNIX_POSIX_PATH_OPS_CAP &
|
2012-07-13 13:58:14 +04:00
|
|
|
le64_to_cpu(tcon->fsUnixInfo.Capability))) {
|
2010-10-15 15:33:56 -04:00
|
|
|
/*
|
|
|
|
* O_CREAT, O_EXCL and O_TRUNC already had their effect on the
|
|
|
|
* original open. Must mask them off for a reopen.
|
|
|
|
*/
|
2012-09-18 16:20:27 -07:00
|
|
|
unsigned int oflags = cfile->f_flags &
|
2010-10-15 15:33:59 -04:00
|
|
|
~(O_CREAT | O_EXCL | O_TRUNC);
|
2010-10-15 15:33:56 -04:00
|
|
|
|
2010-06-16 13:40:16 -04:00
|
|
|
rc = cifs_posix_open(full_path, NULL, inode->i_sb,
|
2020-12-12 13:40:50 -06:00
|
|
|
cifs_sb->ctx->file_mode /* ignored */,
|
2013-07-09 18:40:58 +04:00
|
|
|
oflags, &oplock, &cfile->fid.netfid, xid);
|
2009-02-23 20:43:11 +00:00
|
|
|
if (rc == 0) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "posix reopen succeeded\n");
|
2013-07-29 20:04:35 +02:00
|
|
|
oparms.reconnect = true;
|
2009-02-23 20:43:11 +00:00
|
|
|
goto reopen_success;
|
|
|
|
}
|
2012-09-18 16:20:27 -07:00
|
|
|
/*
|
|
|
|
* fallthrough to retry open the old way on errors, especially
|
|
|
|
* in the reconnect path it is important to retry hard
|
|
|
|
*/
|
2009-02-23 20:43:11 +00:00
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2009-02-23 20:43:11 +00:00
|
|
|
|
2024-04-02 10:11:35 +01:00
|
|
|
/* If we're caching, we need to be able to fill in around partial writes. */
|
|
|
|
if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY)
|
|
|
|
rdwr_for_fscache = 1;
|
|
|
|
|
|
|
|
desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache);
|
2009-02-23 20:43:11 +00:00
|
|
|
|
2019-11-12 17:16:35 -08:00
|
|
|
/* O_SYNC also has bit for O_DSYNC so following check picks up either */
|
|
|
|
if (cfile->f_flags & O_SYNC)
|
|
|
|
create_options |= CREATE_WRITE_THROUGH;
|
|
|
|
|
|
|
|
if (cfile->f_flags & O_DIRECT)
|
|
|
|
create_options |= CREATE_NO_BUFFER;
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
if (server->ops->get_lease_key)
|
2013-07-09 18:40:58 +04:00
|
|
|
server->ops->get_lease_key(inode, &cfile->fid);
|
2012-09-19 06:22:44 -07:00
|
|
|
|
2024-04-02 10:11:35 +01:00
|
|
|
retry_open:
|
2023-01-11 12:37:58 +01:00
|
|
|
oparms = (struct cifs_open_parms) {
|
|
|
|
.tcon = tcon,
|
|
|
|
.cifs_sb = cifs_sb,
|
|
|
|
.desired_access = desired_access,
|
|
|
|
.create_options = cifs_create_options(cifs_sb, create_options),
|
|
|
|
.disposition = disposition,
|
|
|
|
.path = full_path,
|
|
|
|
.fid = &cfile->fid,
|
|
|
|
.reconnect = true,
|
|
|
|
};
|
2013-07-05 12:00:30 +04:00
|
|
|
|
2012-09-18 16:20:27 -07:00
|
|
|
/*
|
|
|
|
* Can not refresh inode by passing in file_info buf to be returned by
|
2014-01-16 15:53:36 +04:00
|
|
|
* ops->open and then calling get_inode_info with returned buf since
|
2012-09-18 16:20:27 -07:00
|
|
|
* file might have write behind data that needs to be flushed and server
|
|
|
|
* version of file size can be stale. If we knew for sure that inode was
|
|
|
|
* not dirty locally we could do this.
|
|
|
|
*/
|
2013-07-05 12:00:30 +04:00
|
|
|
rc = server->ops->open(xid, &oparms, &oplock, NULL);
|
2013-07-11 10:58:30 +04:00
|
|
|
if (rc == -ENOENT && oparms.reconnect == false) {
|
|
|
|
/* durable handle timeout is expired - open the file again */
|
|
|
|
rc = server->ops->open(xid, &oparms, &oplock, NULL);
|
|
|
|
/* indicate that we need to relock the file */
|
|
|
|
oparms.reconnect = true;
|
|
|
|
}
|
2024-04-02 10:11:35 +01:00
|
|
|
if (rc == -EACCES && rdwr_for_fscache == 1) {
|
|
|
|
desired_access = cifs_convert_flags(cfile->f_flags, 0);
|
|
|
|
rdwr_for_fscache = 2;
|
|
|
|
goto retry_open;
|
|
|
|
}
|
2013-07-11 10:58:30 +04:00
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
if (rc) {
|
2012-09-18 16:20:27 -07:00
|
|
|
mutex_unlock(&cfile->fh_mutex);
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
|
|
|
|
cifs_dbg(FYI, "oplock: %d\n", oplock);
|
2010-10-15 15:33:59 -04:00
|
|
|
goto reopen_error_exit;
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:11:35 +01:00
|
|
|
if (rdwr_for_fscache == 2)
|
|
|
|
cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE);
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2009-02-23 20:43:11 +00:00
|
|
|
reopen_success:
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2012-09-18 16:20:27 -07:00
|
|
|
cfile->invalidHandle = false;
|
|
|
|
mutex_unlock(&cfile->fh_mutex);
|
|
|
|
cinode = CIFS_I(inode);
|
2010-10-15 15:33:59 -04:00
|
|
|
|
|
|
|
if (can_flush) {
|
|
|
|
rc = filemap_write_and_wait(inode->i_mapping);
|
2019-01-08 11:15:28 -08:00
|
|
|
if (!is_interrupt_error(rc))
|
|
|
|
mapping_set_error(inode->i_mapping, rc);
|
2010-10-15 15:33:59 -04:00
|
|
|
|
2023-11-25 23:55:03 -03:00
|
|
|
if (tcon->posix_extensions) {
|
|
|
|
rc = smb311_posix_get_inode_info(&inode, full_path,
|
|
|
|
NULL, inode->i_sb, xid);
|
|
|
|
} else if (tcon->unix_ext) {
|
2012-09-18 16:20:27 -07:00
|
|
|
rc = cifs_get_inode_info_unix(&inode, full_path,
|
|
|
|
inode->i_sb, xid);
|
2023-11-25 23:55:03 -03:00
|
|
|
} else {
|
2012-09-18 16:20:27 -07:00
|
|
|
rc = cifs_get_inode_info(&inode, full_path, NULL,
|
|
|
|
inode->i_sb, xid, NULL);
|
2023-11-25 23:55:03 -03:00
|
|
|
}
|
2012-09-18 16:20:27 -07:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Else we are writing out data to server already and could deadlock if
|
|
|
|
* we tried to flush data, and since we do not know if we have data that
|
|
|
|
* would invalidate the current end of file on the server we can not go
|
|
|
|
* to the server to get the new inode info.
|
|
|
|
*/
|
|
|
|
|
2016-10-11 15:34:07 -07:00
|
|
|
/*
|
|
|
|
* If the server returned a read oplock and we have mandatory brlocks,
|
|
|
|
* set oplock level to None.
|
|
|
|
*/
|
|
|
|
if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
|
|
|
|
cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
|
|
|
|
oplock = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-09 18:40:58 +04:00
|
|
|
server->ops->set_fid(cfile, &cfile->fid, oplock);
|
|
|
|
if (oparms.reconnect)
|
|
|
|
cifs_relock_file(cfile);
|
2010-10-15 15:33:59 -04:00
|
|
|
|
|
|
|
reopen_error_exit:
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-04-13 00:26:42 -05:00
|
|
|
void smb2_deferred_work_close(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile = container_of(work,
|
|
|
|
struct cifsFileInfo, deferred.work);
|
|
|
|
|
|
|
|
spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
|
|
|
|
cifs_del_deferred_close(cfile);
|
2021-05-05 10:56:47 +00:00
|
|
|
cfile->deferred_close_scheduled = false;
|
2021-04-13 00:26:42 -05:00
|
|
|
spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
|
|
|
|
_cifsFileInfo_put(cfile, true, false);
|
|
|
|
}
|
|
|
|
|
2024-03-12 21:21:41 -05:00
|
|
|
static bool
|
|
|
|
smb2_can_defer_close(struct inode *inode, struct cifs_deferred_close *dclose)
|
|
|
|
{
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
|
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
|
|
|
|
return (cifs_sb->ctx->closetimeo && cinode->lease_granted && dclose &&
|
|
|
|
(cinode->oplock == CIFS_CACHE_RHW_FLG ||
|
|
|
|
cinode->oplock == CIFS_CACHE_RH_FLG) &&
|
|
|
|
!test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
int cifs_close(struct inode *inode, struct file *file)
|
|
|
|
{
|
2021-04-13 00:26:42 -05:00
|
|
|
struct cifsFileInfo *cfile;
|
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
|
|
|
|
struct cifs_deferred_close *dclose;
|
|
|
|
|
cifs: Support fscache indexing rewrite
Change the cifs filesystem to take account of the changes to fscache's
indexing rewrite and reenable caching in cifs.
The following changes have been made:
(1) The fscache_netfs struct is no more, and there's no need to register
the filesystem as a whole.
(2) The session cookie is now an fscache_volume cookie, allocated with
fscache_acquire_volume(). That takes three parameters: a string
representing the "volume" in the index, a string naming the cache to
use (or NULL) and a u64 that conveys coherency metadata for the
volume.
For cifs, I've made it render the volume name string as:
"cifs,<ipaddress>,<sharename>"
where the sharename has '/' characters replaced with ';'.
This probably needs rethinking a bit as the total name could exceed
the maximum filename component length.
Further, the coherency data is currently just set to 0. It needs
something else doing with it - I wonder if it would suffice simply to
sum the resource_id, vol_create_time and vol_serial_number or maybe
hash them.
(3) The fscache_cookie_def is no more and needed information is passed
directly to fscache_acquire_cookie(). The cache no longer calls back
into the filesystem, but rather metadata changes are indicated at
other times.
fscache_acquire_cookie() is passed the same keying and coherency
information as before.
(4) The functions to set/reset cookies are removed and
fscache_use_cookie() and fscache_unuse_cookie() are used instead.
fscache_use_cookie() is passed a flag to indicate if the cookie is
opened for writing. fscache_unuse_cookie() is passed updates for the
metadata if we changed it (ie. if the file was opened for writing).
These are called when the file is opened or closed.
(5) cifs_setattr_*() are made to call fscache_resize() to change the size
of the cache object.
(6) The functions to read and write data are stubbed out pending a
conversion to use netfslib.
Changes
=======
ver #8:
- Abstract cache invalidation into a helper function.
- Fix some checkpatch warnings[3].
ver #7:
- Removed the accidentally added-back call to get the super cookie in
cifs_root_iget().
- Fixed the right call to cifs_fscache_get_super_cookie() to take account
of the "-o fsc" mount flag.
ver #6:
- Moved the change of gfpflags_allow_blocking() to current_is_kswapd() for
cifs here.
- Fixed one of the error paths in cifs_atomic_open() to jump around the
call to use the cookie.
- Fixed an additional successful return in the middle of cifs_open() to
use the cookie on the way out.
- Only get a volume cookie (and thus inode cookies) when "-o fsc" is
supplied to mount.
ver #5:
- Fixed a couple of bits of cookie handling[2]:
- The cookie should be released in cifs_evict_inode(), not
cifsFileInfo_put_final(). The cookie needs to persist beyond file
closure so that writepages will be able to write to it.
- fscache_use_cookie() needs to be called in cifs_atomic_open() as it is
for cifs_open().
ver #4:
- Fixed the use of sizeof with memset.
- tcon->vol_create_time is __le64 so doesn't need cpu_to_le64().
ver #3:
- Canonicalise the cifs coherency data to make the cache portable.
- Set volume coherency data.
ver #2:
- Use gfpflags_allow_blocking() rather than using flag directly.
- Upgraded to -rc4 to allow for upstream changes[1].
- fscache_acquire_volume() now returns errors.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Jeff Layton <jlayton@kernel.org>
cc: Steve French <smfrench@gmail.com>
cc: Shyam Prasad N <nspmangalore@gmail.com>
cc: linux-cifs@vger.kernel.org
cc: linux-cachefs@redhat.com
Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=23b55d673d7527b093cd97b7c217c82e70cd1af0 [1]
Link: https://lore.kernel.org/r/3419813.1641592362@warthog.procyon.org.uk/ [2]
Link: https://lore.kernel.org/r/CAH2r5muTanw9pJqzAHd01d9A8keeChkzGsCEH6=0rHutVLAF-A@mail.gmail.com/ [3]
Link: https://lore.kernel.org/r/163819671009.215744.11230627184193298714.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906982979.143852.10672081929614953210.stgit@warthog.procyon.org.uk/ # v2
Link: https://lore.kernel.org/r/163967187187.1823006.247415138444991444.stgit@warthog.procyon.org.uk/ # v3
Link: https://lore.kernel.org/r/164021579335.640689.2681324337038770579.stgit@warthog.procyon.org.uk/ # v4
Link: https://lore.kernel.org/r/3462849.1641593783@warthog.procyon.org.uk/ # v5
Link: https://lore.kernel.org/r/1318953.1642024578@warthog.procyon.org.uk/ # v6
Signed-off-by: Steve French <stfrench@microsoft.com>
2020-11-17 15:56:59 +00:00
|
|
|
cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
|
|
|
|
|
2011-04-05 16:23:47 -07:00
|
|
|
if (file->private_data != NULL) {
|
2021-04-13 00:26:42 -05:00
|
|
|
cfile = file->private_data;
|
2011-04-05 16:23:47 -07:00
|
|
|
file->private_data = NULL;
|
2021-04-13 00:26:42 -05:00
|
|
|
dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
|
2024-03-12 21:21:41 -05:00
|
|
|
if ((cfile->status_file_deleted == false) &&
|
|
|
|
(smb2_can_defer_close(inode, dclose))) {
|
2024-06-05 11:26:24 +01:00
|
|
|
if (test_and_clear_bit(NETFS_ICTX_MODIFIED_ATTR, &cinode->netfs.flags)) {
|
2023-10-04 14:52:53 -04:00
|
|
|
inode_set_mtime_to_ts(inode,
|
|
|
|
inode_set_ctime_current(inode));
|
2021-08-10 10:22:28 +00:00
|
|
|
}
|
2021-04-13 00:26:42 -05:00
|
|
|
spin_lock(&cinode->deferred_lock);
|
|
|
|
cifs_add_deferred_close(cfile, dclose);
|
2021-05-05 10:56:47 +00:00
|
|
|
if (cfile->deferred_close_scheduled &&
|
|
|
|
delayed_work_pending(&cfile->deferred)) {
|
2021-05-20 16:45:01 +00:00
|
|
|
/*
|
|
|
|
* If there is no pending work, mod_delayed_work queues new work.
|
|
|
|
* So, Increase the ref count to avoid use-after-free.
|
|
|
|
*/
|
|
|
|
if (!mod_delayed_work(deferredclose_wq,
|
2022-08-11 00:53:00 -05:00
|
|
|
&cfile->deferred, cifs_sb->ctx->closetimeo))
|
2021-05-20 16:45:01 +00:00
|
|
|
cifsFileInfo_get(cfile);
|
2021-04-13 00:26:42 -05:00
|
|
|
} else {
|
|
|
|
/* Deferred close for files */
|
|
|
|
queue_delayed_work(deferredclose_wq,
|
2022-08-11 00:53:00 -05:00
|
|
|
&cfile->deferred, cifs_sb->ctx->closetimeo);
|
2021-05-05 10:56:47 +00:00
|
|
|
cfile->deferred_close_scheduled = true;
|
2021-04-13 00:26:42 -05:00
|
|
|
spin_unlock(&cinode->deferred_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
spin_unlock(&cinode->deferred_lock);
|
|
|
|
_cifsFileInfo_put(cfile, true, false);
|
|
|
|
} else {
|
|
|
|
_cifsFileInfo_put(cfile, true, false);
|
|
|
|
kfree(dclose);
|
|
|
|
}
|
2011-04-05 16:23:47 -07:00
|
|
|
}
|
2006-08-02 21:56:33 +00:00
|
|
|
|
2010-10-21 22:46:14 +00:00
|
|
|
/* return code from the ->release op is always ignored */
|
|
|
|
return 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2016-09-22 19:23:56 -05:00
|
|
|
void
|
|
|
|
cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
|
|
|
|
{
|
2022-07-22 14:02:59 -03:00
|
|
|
struct cifsFileInfo *open_file, *tmp;
|
2024-08-21 14:56:37 +08:00
|
|
|
LIST_HEAD(tmp_list);
|
2016-10-07 17:26:36 -07:00
|
|
|
|
2016-11-29 11:31:23 -08:00
|
|
|
if (!tcon->use_persistent || !tcon->need_reopen_files)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tcon->need_reopen_files = false;
|
|
|
|
|
2020-04-14 22:42:53 -07:00
|
|
|
cifs_dbg(FYI, "Reopen persistent handles\n");
|
2016-09-22 19:23:56 -05:00
|
|
|
|
|
|
|
/* list all files open on tree connection, reopen resilient handles */
|
|
|
|
spin_lock(&tcon->open_file_lock);
|
2022-07-22 14:02:59 -03:00
|
|
|
list_for_each_entry(open_file, &tcon->openFileList, tlist) {
|
2016-10-07 17:26:36 -07:00
|
|
|
if (!open_file->invalidHandle)
|
|
|
|
continue;
|
|
|
|
cifsFileInfo_get(open_file);
|
|
|
|
list_add_tail(&open_file->rlist, &tmp_list);
|
2016-09-22 19:23:56 -05:00
|
|
|
}
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2016-10-07 17:26:36 -07:00
|
|
|
|
2022-07-22 14:02:59 -03:00
|
|
|
list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
|
2016-11-29 11:31:23 -08:00
|
|
|
if (cifs_reopen_file(open_file, false /* do not flush */))
|
|
|
|
tcon->need_reopen_files = true;
|
2016-10-07 17:26:36 -07:00
|
|
|
list_del_init(&open_file->rlist);
|
|
|
|
cifsFileInfo_put(open_file);
|
|
|
|
}
|
2016-09-22 19:23:56 -05:00
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
int cifs_closedir(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
2012-09-18 16:20:26 -07:00
|
|
|
struct cifsFileInfo *cfile = file->private_data;
|
2012-09-18 16:20:32 -07:00
|
|
|
struct cifs_tcon *tcon;
|
|
|
|
struct TCP_Server_Info *server;
|
|
|
|
char *buf;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-09-18 16:20:32 -07:00
|
|
|
if (cfile == NULL)
|
|
|
|
return rc;
|
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2012-09-18 16:20:32 -07:00
|
|
|
tcon = tlink_tcon(cfile->tlink);
|
|
|
|
server = tcon->ses->server;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Freeing private data in close dir\n");
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_lock(&cfile->file_info_lock);
|
2014-08-18 20:49:57 +04:00
|
|
|
if (server->ops->dir_needs_close(cfile)) {
|
2012-09-18 16:20:32 -07:00
|
|
|
cfile->invalidHandle = true;
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&cfile->file_info_lock);
|
2012-09-18 16:20:32 -07:00
|
|
|
if (server->ops->close_dir)
|
|
|
|
rc = server->ops->close_dir(xid, tcon, &cfile->fid);
|
|
|
|
else
|
|
|
|
rc = -ENOSYS;
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
|
2012-09-18 16:20:32 -07:00
|
|
|
/* not much we can do if it fails anyway, ignore rc */
|
|
|
|
rc = 0;
|
|
|
|
} else
|
2016-09-22 18:58:16 -05:00
|
|
|
spin_unlock(&cfile->file_info_lock);
|
2012-09-18 16:20:32 -07:00
|
|
|
|
|
|
|
buf = cfile->srch_inf.ntwrk_buf_start;
|
|
|
|
if (buf) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
|
2012-09-18 16:20:32 -07:00
|
|
|
cfile->srch_inf.ntwrk_buf_start = NULL;
|
|
|
|
if (cfile->srch_inf.smallBuf)
|
|
|
|
cifs_small_buf_release(buf);
|
|
|
|
else
|
|
|
|
cifs_buf_release(buf);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2012-09-18 16:20:32 -07:00
|
|
|
|
|
|
|
cifs_put_tlink(cfile->tlink);
|
|
|
|
kfree(file->private_data);
|
|
|
|
file->private_data = NULL;
|
2005-04-16 15:20:36 -07:00
|
|
|
/* BB can we lock the filestruct while this is going on? */
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:33:29 +04:00
|
|
|
static struct cifsLockInfo *
|
2018-10-04 09:24:38 +10:00
|
|
|
cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
|
2006-08-02 21:56:33 +00:00
|
|
|
{
|
2011-10-29 17:17:59 +04:00
|
|
|
struct cifsLockInfo *lock =
|
2007-07-10 01:16:18 +00:00
|
|
|
kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
|
2011-10-29 17:17:59 +04:00
|
|
|
if (!lock)
|
|
|
|
return lock;
|
|
|
|
lock->offset = offset;
|
|
|
|
lock->length = length;
|
|
|
|
lock->type = type;
|
|
|
|
lock->pid = current->tgid;
|
2018-10-04 09:24:38 +10:00
|
|
|
lock->flags = flags;
|
2011-10-29 17:17:59 +04:00
|
|
|
INIT_LIST_HEAD(&lock->blist);
|
|
|
|
init_waitqueue_head(&lock->block_q);
|
|
|
|
return lock;
|
2011-10-22 15:33:29 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
void
|
2011-10-22 15:33:29 +04:00
|
|
|
cifs_del_lock_waiters(struct cifsLockInfo *lock)
|
|
|
|
{
|
|
|
|
struct cifsLockInfo *li, *tmp;
|
|
|
|
list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
|
|
|
|
list_del_init(&li->blist);
|
|
|
|
wake_up(&li->block_q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 18:38:53 +04:00
|
|
|
#define CIFS_LOCK_OP 0
|
|
|
|
#define CIFS_READ_OP 1
|
|
|
|
#define CIFS_WRITE_OP 2
|
|
|
|
|
|
|
|
/* @rw_check : 0 - no op, 1 - read, 2 - write */
|
2011-10-22 15:33:29 +04:00
|
|
|
static bool
|
2012-09-19 06:22:43 -07:00
|
|
|
cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
|
2018-10-04 09:24:38 +10:00
|
|
|
__u64 length, __u8 type, __u16 flags,
|
|
|
|
struct cifsFileInfo *cfile,
|
2012-11-27 18:38:53 +04:00
|
|
|
struct cifsLockInfo **conf_lock, int rw_check)
|
2011-10-22 15:33:29 +04:00
|
|
|
{
|
2012-02-24 15:41:06 +03:00
|
|
|
struct cifsLockInfo *li;
|
2012-09-19 06:22:43 -07:00
|
|
|
struct cifsFileInfo *cur_cfile = fdlocks->cfile;
|
2012-02-28 14:23:34 +03:00
|
|
|
struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
|
2011-10-22 15:33:29 +04:00
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
list_for_each_entry(li, &fdlocks->locks, llist) {
|
2011-10-22 15:33:29 +04:00
|
|
|
if (offset + length <= li->offset ||
|
|
|
|
offset >= li->offset + li->length)
|
|
|
|
continue;
|
2012-11-27 18:38:53 +04:00
|
|
|
if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
|
|
|
|
server->ops->compare_fids(cfile, cur_cfile)) {
|
|
|
|
/* shared lock prevents write op through the same fid */
|
|
|
|
if (!(li->type & server->vals->shared_lock_type) ||
|
|
|
|
rw_check != CIFS_WRITE_OP)
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-19 06:22:43 -07:00
|
|
|
if ((type & server->vals->shared_lock_type) &&
|
|
|
|
((server->ops->compare_fids(cfile, cur_cfile) &&
|
|
|
|
current->tgid == li->pid) || type == li->type))
|
2011-10-22 15:33:29 +04:00
|
|
|
continue;
|
2018-10-04 09:24:38 +10:00
|
|
|
if (rw_check == CIFS_LOCK_OP &&
|
|
|
|
(flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
|
|
|
|
server->ops->compare_fids(cfile, cur_cfile))
|
|
|
|
continue;
|
2012-09-19 06:22:44 -07:00
|
|
|
if (conf_lock)
|
|
|
|
*conf_lock = li;
|
2012-09-19 06:22:43 -07:00
|
|
|
return true;
|
2011-10-22 15:33:29 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
bool
|
2012-02-28 14:04:17 +03:00
|
|
|
cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
|
2018-10-04 09:24:38 +10:00
|
|
|
__u8 type, __u16 flags,
|
|
|
|
struct cifsLockInfo **conf_lock, int rw_check)
|
2011-10-29 17:17:58 +04:00
|
|
|
{
|
2012-02-24 15:41:06 +03:00
|
|
|
bool rc = false;
|
2012-09-19 06:22:43 -07:00
|
|
|
struct cifs_fid_locks *cur;
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2012-02-24 15:41:06 +03:00
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
list_for_each_entry(cur, &cinode->llist, llist) {
|
|
|
|
rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
|
2018-10-04 09:24:38 +10:00
|
|
|
flags, cfile, conf_lock,
|
|
|
|
rw_check);
|
2012-02-24 15:41:06 +03:00
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2011-10-29 17:17:58 +04:00
|
|
|
}
|
|
|
|
|
2011-11-07 16:11:24 +03:00
|
|
|
/*
|
|
|
|
* Check if there is another lock that prevents us to set the lock (mandatory
|
|
|
|
* style). If such a lock exists, update the flock structure with its
|
|
|
|
* properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
|
|
|
|
* or leave it the same if we can't. Returns 0 if we don't need to request to
|
|
|
|
* the server or 1 otherwise.
|
|
|
|
*/
|
2011-10-22 15:33:29 +04:00
|
|
|
static int
|
2012-02-24 15:41:06 +03:00
|
|
|
cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
|
|
|
|
__u8 type, struct file_lock *flock)
|
2011-10-22 15:33:29 +04:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
struct cifsLockInfo *conf_lock;
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2012-02-28 14:23:34 +03:00
|
|
|
struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
|
2011-10-22 15:33:29 +04:00
|
|
|
bool exist;
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
down_read(&cinode->lock_sem);
|
2011-10-22 15:33:29 +04:00
|
|
|
|
2012-02-28 14:04:17 +03:00
|
|
|
exist = cifs_find_lock_conflict(cfile, offset, length, type,
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_flags, &conf_lock,
|
2018-10-04 09:24:38 +10:00
|
|
|
CIFS_LOCK_OP);
|
2011-10-22 15:33:29 +04:00
|
|
|
if (exist) {
|
|
|
|
flock->fl_start = conf_lock->offset;
|
|
|
|
flock->fl_end = conf_lock->offset + conf_lock->length - 1;
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_pid = conf_lock->pid;
|
2012-02-28 14:23:34 +03:00
|
|
|
if (conf_lock->type & server->vals->shared_lock_type)
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_RDLCK;
|
2011-10-22 15:33:29 +04:00
|
|
|
else
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_WRLCK;
|
2011-10-22 15:33:29 +04:00
|
|
|
} else if (!cinode->can_cache_brlcks)
|
|
|
|
rc = 1;
|
|
|
|
else
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_UNLCK;
|
2011-10-22 15:33:29 +04:00
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
up_read(&cinode->lock_sem);
|
2011-10-22 15:33:29 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-10-29 17:17:58 +04:00
|
|
|
static void
|
2012-02-24 15:41:06 +03:00
|
|
|
cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
|
2011-10-22 15:33:29 +04:00
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2019-10-23 05:02:33 -04:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2012-09-19 06:22:43 -07:00
|
|
|
list_add_tail(&lock->llist, &cfile->llist->locks);
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2006-08-02 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
2011-11-07 16:11:24 +03:00
|
|
|
/*
|
|
|
|
* Set the byte-range lock (mandatory style). Returns:
|
|
|
|
* 1) 0, if we set the lock and don't need to request to the server;
|
|
|
|
* 2) 1, if no locks prevent us but we need to request to the server;
|
2018-10-26 19:07:21 +01:00
|
|
|
* 3) -EACCES, if there is a lock that prevents us and wait is false.
|
2011-11-07 16:11:24 +03:00
|
|
|
*/
|
2011-10-22 15:33:29 +04:00
|
|
|
static int
|
2012-02-24 15:41:06 +03:00
|
|
|
cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
|
2011-10-29 17:17:58 +04:00
|
|
|
bool wait)
|
2011-10-22 15:33:29 +04:00
|
|
|
{
|
2011-10-29 17:17:58 +04:00
|
|
|
struct cifsLockInfo *conf_lock;
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2011-10-22 15:33:29 +04:00
|
|
|
bool exist;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
try_again:
|
|
|
|
exist = false;
|
2019-10-23 05:02:33 -04:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2011-10-22 15:33:29 +04:00
|
|
|
|
2012-02-28 14:04:17 +03:00
|
|
|
exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
|
2018-10-04 09:24:38 +10:00
|
|
|
lock->type, lock->flags, &conf_lock,
|
|
|
|
CIFS_LOCK_OP);
|
2011-10-22 15:33:29 +04:00
|
|
|
if (!exist && cinode->can_cache_brlcks) {
|
2012-09-19 06:22:43 -07:00
|
|
|
list_add_tail(&lock->llist, &cfile->llist->locks);
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2011-10-22 15:33:29 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exist)
|
|
|
|
rc = 1;
|
|
|
|
else if (!wait)
|
|
|
|
rc = -EACCES;
|
|
|
|
else {
|
|
|
|
list_add_tail(&lock->blist, &conf_lock->blist);
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2011-10-22 15:33:29 +04:00
|
|
|
rc = wait_event_interruptible(lock->block_q,
|
|
|
|
(lock->blist.prev == &lock->blist) &&
|
|
|
|
(lock->blist.next == &lock->blist));
|
|
|
|
if (!rc)
|
|
|
|
goto try_again;
|
2019-10-23 05:02:33 -04:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2011-10-29 17:17:59 +04:00
|
|
|
list_del_init(&lock->blist);
|
2011-10-22 15:33:29 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2011-10-22 15:33:29 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2011-11-07 16:11:24 +03:00
|
|
|
/*
|
|
|
|
* Check if there is another lock that prevents us to set the lock (posix
|
|
|
|
* style). If such a lock exists, update the flock structure with its
|
|
|
|
* properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
|
|
|
|
* or leave it the same if we can't. Returns 0 if we don't need to request to
|
|
|
|
* the server or 1 otherwise.
|
|
|
|
*/
|
2011-10-22 15:33:29 +04:00
|
|
|
static int
|
2011-10-22 15:33:30 +04:00
|
|
|
cifs_posix_lock_test(struct file *file, struct file_lock *flock)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
2013-01-23 17:07:38 -05:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
|
2024-01-31 18:02:25 -05:00
|
|
|
unsigned char saved_type = flock->c.flc_type;
|
2011-10-22 15:33:30 +04:00
|
|
|
|
2024-01-31 18:02:25 -05:00
|
|
|
if ((flock->c.flc_flags & FL_POSIX) == 0)
|
2011-10-29 17:17:57 +04:00
|
|
|
return 1;
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
down_read(&cinode->lock_sem);
|
2011-10-22 15:33:30 +04:00
|
|
|
posix_test_lock(file, flock);
|
|
|
|
|
2024-01-31 18:01:55 -05:00
|
|
|
if (lock_is_unlock(flock) && !cinode->can_cache_brlcks) {
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = saved_type;
|
2011-10-22 15:33:30 +04:00
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
up_read(&cinode->lock_sem);
|
2011-10-22 15:33:30 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-11-07 16:11:24 +03:00
|
|
|
/*
|
|
|
|
* Set the byte-range lock (posix style). Returns:
|
2020-07-02 15:25:26 +08:00
|
|
|
* 1) <0, if the error occurs while setting the lock;
|
|
|
|
* 2) 0, if we set the lock and don't need to request to the server;
|
|
|
|
* 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
|
|
|
|
* 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
|
2011-11-07 16:11:24 +03:00
|
|
|
*/
|
2011-10-22 15:33:30 +04:00
|
|
|
static int
|
|
|
|
cifs_posix_lock_set(struct file *file, struct file_lock *flock)
|
|
|
|
{
|
2013-01-23 17:07:38 -05:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
|
2020-07-02 15:25:26 +08:00
|
|
|
int rc = FILE_LOCK_DEFERRED + 1;
|
2011-10-29 17:17:57 +04:00
|
|
|
|
2024-01-31 18:02:25 -05:00
|
|
|
if ((flock->c.flc_flags & FL_POSIX) == 0)
|
2011-10-29 17:17:57 +04:00
|
|
|
return rc;
|
2011-10-22 15:33:30 +04:00
|
|
|
|
2019-10-23 05:02:33 -04:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2011-10-22 15:33:30 +04:00
|
|
|
if (!cinode->can_cache_brlcks) {
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2011-10-29 17:17:57 +04:00
|
|
|
return rc;
|
2011-10-22 15:33:30 +04:00
|
|
|
}
|
2012-03-28 21:56:19 +04:00
|
|
|
|
|
|
|
rc = posix_lock_file(file, flock, NULL);
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2012-04-01 13:52:54 -05:00
|
|
|
return rc;
|
2011-10-22 15:33:30 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
int
|
2011-10-22 15:33:30 +04:00
|
|
|
cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
|
2011-10-22 15:33:29 +04:00
|
|
|
{
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
|
|
|
int rc = 0, stored_rc;
|
2011-10-22 15:33:29 +04:00
|
|
|
struct cifsLockInfo *li, *tmp;
|
|
|
|
struct cifs_tcon *tcon;
|
2012-05-31 13:03:26 +04:00
|
|
|
unsigned int num, max_num, max_buf;
|
2011-10-22 15:33:32 +04:00
|
|
|
LOCKING_ANDX_RANGE *buf, *cur;
|
2017-09-19 16:27:39 +01:00
|
|
|
static const int types[] = {
|
|
|
|
LOCKING_ANDX_LARGE_FILES,
|
|
|
|
LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
|
|
|
|
};
|
2011-10-22 15:33:32 +04:00
|
|
|
int i;
|
2011-10-22 15:33:29 +04:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2011-10-22 15:33:29 +04:00
|
|
|
tcon = tlink_tcon(cfile->tlink);
|
|
|
|
|
2012-05-31 13:03:26 +04:00
|
|
|
/*
|
|
|
|
* Accessing maxBuf is racy with cifs_reconnect - need to store value
|
2019-01-08 18:30:57 +00:00
|
|
|
* and check it before using.
|
2012-05-31 13:03:26 +04:00
|
|
|
*/
|
|
|
|
max_buf = tcon->ses->server->maxBuf;
|
2019-01-08 18:30:57 +00:00
|
|
|
if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2012-05-31 13:03:26 +04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:30:56 +00:00
|
|
|
BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
|
|
|
|
PAGE_SIZE);
|
|
|
|
max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
|
|
|
|
PAGE_SIZE);
|
2012-05-31 13:03:26 +04:00
|
|
|
max_num = (max_buf - sizeof(struct smb_hdr)) /
|
|
|
|
sizeof(LOCKING_ANDX_RANGE);
|
2014-12-10 15:41:17 -08:00
|
|
|
buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
|
2011-10-22 15:33:32 +04:00
|
|
|
if (!buf) {
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2012-08-29 21:13:38 +04:00
|
|
|
return -ENOMEM;
|
2011-10-22 15:33:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
cur = buf;
|
|
|
|
num = 0;
|
2012-09-19 06:22:43 -07:00
|
|
|
list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
|
2011-10-22 15:33:32 +04:00
|
|
|
if (li->type != types[i])
|
|
|
|
continue;
|
|
|
|
cur->Pid = cpu_to_le16(li->pid);
|
|
|
|
cur->LengthLow = cpu_to_le32((u32)li->length);
|
|
|
|
cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
|
|
|
|
cur->OffsetLow = cpu_to_le32((u32)li->offset);
|
|
|
|
cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
|
|
|
|
if (++num == max_num) {
|
2012-09-18 16:20:26 -07:00
|
|
|
stored_rc = cifs_lockv(xid, tcon,
|
|
|
|
cfile->fid.netfid,
|
2012-02-28 14:16:55 +03:00
|
|
|
(__u8)li->type, 0, num,
|
|
|
|
buf);
|
2011-10-22 15:33:32 +04:00
|
|
|
if (stored_rc)
|
|
|
|
rc = stored_rc;
|
|
|
|
cur = buf;
|
|
|
|
num = 0;
|
|
|
|
} else
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num) {
|
2012-09-18 16:20:26 -07:00
|
|
|
stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
|
2012-02-28 14:16:55 +03:00
|
|
|
(__u8)types[i], 0, num, buf);
|
2011-10-22 15:33:32 +04:00
|
|
|
if (stored_rc)
|
|
|
|
rc = stored_rc;
|
|
|
|
}
|
2011-10-22 15:33:29 +04:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:33:32 +04:00
|
|
|
kfree(buf);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2011-10-22 15:33:29 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-05-24 06:27:44 -04:00
|
|
|
static __u32
|
|
|
|
hash_lockowner(fl_owner_t owner)
|
|
|
|
{
|
|
|
|
return cifs_lock_secret ^ hash32_ptr((const void *)owner);
|
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2016-05-24 06:27:44 -04:00
|
|
|
|
2012-03-05 09:39:20 +03:00
|
|
|
struct lock_to_push {
|
|
|
|
struct list_head llist;
|
|
|
|
__u64 offset;
|
|
|
|
__u64 length;
|
|
|
|
__u32 pid;
|
|
|
|
__u16 netfid;
|
|
|
|
__u8 type;
|
|
|
|
};
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2011-10-22 15:33:30 +04:00
|
|
|
static int
|
2012-11-22 17:07:16 +04:00
|
|
|
cifs_push_posix_locks(struct cifsFileInfo *cfile)
|
2011-10-22 15:33:30 +04:00
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(cfile->dentry);
|
2011-10-22 15:33:30 +04:00
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2015-01-16 15:05:55 -05:00
|
|
|
struct file_lock *flock;
|
2022-11-16 08:49:25 -05:00
|
|
|
struct file_lock_context *flctx = locks_inode_context(inode);
|
2015-02-16 14:32:03 -05:00
|
|
|
unsigned int count = 0, i;
|
2011-10-22 15:33:30 +04:00
|
|
|
int rc = 0, xid, type;
|
2012-03-05 09:39:20 +03:00
|
|
|
struct list_head locks_to_send, *el;
|
|
|
|
struct lock_to_push *lck, *tmp;
|
2011-10-22 15:33:30 +04:00
|
|
|
__u64 length;
|
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2011-10-22 15:33:30 +04:00
|
|
|
|
2015-01-16 15:05:55 -05:00
|
|
|
if (!flctx)
|
|
|
|
goto out;
|
2012-03-05 09:39:20 +03:00
|
|
|
|
2015-02-16 14:32:03 -05:00
|
|
|
spin_lock(&flctx->flc_lock);
|
|
|
|
list_for_each(el, &flctx->flc_posix) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
spin_unlock(&flctx->flc_lock);
|
|
|
|
|
2011-10-22 15:33:30 +04:00
|
|
|
INIT_LIST_HEAD(&locks_to_send);
|
|
|
|
|
2012-03-05 09:39:20 +03:00
|
|
|
/*
|
2015-02-16 14:32:03 -05:00
|
|
|
* Allocating count locks is enough because no FL_POSIX locks can be
|
|
|
|
* added to the list while we are holding cinode->lock_sem that
|
2012-03-17 09:46:55 +03:00
|
|
|
* protects locking operations of this inode.
|
2012-03-05 09:39:20 +03:00
|
|
|
*/
|
2015-02-16 14:32:03 -05:00
|
|
|
for (i = 0; i < count; i++) {
|
2012-03-05 09:39:20 +03:00
|
|
|
lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
|
|
|
|
if (!lck) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
list_add_tail(&lck->llist, &locks_to_send);
|
|
|
|
}
|
|
|
|
|
|
|
|
el = locks_to_send.next;
|
2015-01-16 15:05:57 -05:00
|
|
|
spin_lock(&flctx->flc_lock);
|
2024-01-31 18:01:55 -05:00
|
|
|
for_each_file_lock(flock, &flctx->flc_posix) {
|
2024-01-31 18:02:25 -05:00
|
|
|
unsigned char ftype = flock->c.flc_type;
|
|
|
|
|
2012-03-05 09:39:20 +03:00
|
|
|
if (el == &locks_to_send) {
|
2012-03-17 09:46:55 +03:00
|
|
|
/*
|
|
|
|
* The list ended. We don't have enough allocated
|
|
|
|
* structures - something is really wrong.
|
|
|
|
*/
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(VFS, "Can't push all brlocks!\n");
|
2012-03-05 09:39:20 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-05-19 12:18:37 -03:00
|
|
|
length = cifs_flock_len(flock);
|
2024-01-31 18:02:25 -05:00
|
|
|
if (ftype == F_RDLCK || ftype == F_SHLCK)
|
2011-10-22 15:33:30 +04:00
|
|
|
type = CIFS_RDLCK;
|
|
|
|
else
|
|
|
|
type = CIFS_WRLCK;
|
2012-03-05 09:39:20 +03:00
|
|
|
lck = list_entry(el, struct lock_to_push, llist);
|
2024-01-31 18:02:25 -05:00
|
|
|
lck->pid = hash_lockowner(flock->c.flc_owner);
|
2012-09-18 16:20:26 -07:00
|
|
|
lck->netfid = cfile->fid.netfid;
|
2012-03-05 09:39:20 +03:00
|
|
|
lck->length = length;
|
|
|
|
lck->type = type;
|
|
|
|
lck->offset = flock->fl_start;
|
2011-10-22 15:33:30 +04:00
|
|
|
}
|
2015-01-16 15:05:57 -05:00
|
|
|
spin_unlock(&flctx->flc_lock);
|
2011-10-22 15:33:30 +04:00
|
|
|
|
|
|
|
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
|
|
|
|
int stored_rc;
|
|
|
|
|
|
|
|
stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
|
2012-07-23 13:28:37 -04:00
|
|
|
lck->offset, lck->length, NULL,
|
2011-10-22 15:33:30 +04:00
|
|
|
lck->type, 0);
|
|
|
|
if (stored_rc)
|
|
|
|
rc = stored_rc;
|
|
|
|
list_del(&lck->llist);
|
|
|
|
kfree(lck);
|
|
|
|
}
|
|
|
|
|
2012-03-05 09:39:20 +03:00
|
|
|
out:
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2011-10-22 15:33:30 +04:00
|
|
|
return rc;
|
2012-03-05 09:39:20 +03:00
|
|
|
err_out:
|
|
|
|
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
|
|
|
|
list_del(&lck->llist);
|
|
|
|
kfree(lck);
|
|
|
|
}
|
|
|
|
goto out;
|
2011-10-22 15:33:30 +04:00
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2011-10-22 15:33:30 +04:00
|
|
|
|
2012-11-22 17:00:10 +04:00
|
|
|
static int
|
2012-11-22 17:07:16 +04:00
|
|
|
cifs_push_locks(struct cifsFileInfo *cfile)
|
2012-11-22 17:00:10 +04:00
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2012-11-22 17:07:16 +04:00
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2012-11-22 17:00:10 +04:00
|
|
|
int rc = 0;
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
|
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2012-11-22 17:00:10 +04:00
|
|
|
|
|
|
|
/* we are going to update can_cache_brlcks here - need a write access */
|
2019-10-23 05:02:33 -04:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2012-11-22 17:00:10 +04:00
|
|
|
if (!cinode->can_cache_brlcks) {
|
|
|
|
up_write(&cinode->lock_sem);
|
|
|
|
return rc;
|
|
|
|
}
|
2011-10-22 15:33:30 +04:00
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2012-07-13 13:58:14 +04:00
|
|
|
if (cap_unix(tcon->ses) &&
|
2011-10-22 15:33:30 +04:00
|
|
|
(CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
|
|
|
|
((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
|
2012-11-22 17:07:16 +04:00
|
|
|
rc = cifs_push_posix_locks(cfile);
|
|
|
|
else
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2012-11-22 17:07:16 +04:00
|
|
|
rc = tcon->ses->server->ops->push_mand_locks(cfile);
|
2011-10-22 15:33:30 +04:00
|
|
|
|
2012-11-22 17:07:16 +04:00
|
|
|
cinode->can_cache_brlcks = false;
|
|
|
|
up_write(&cinode->lock_sem);
|
|
|
|
return rc;
|
2011-10-22 15:33:30 +04:00
|
|
|
}
|
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
static void
|
2012-02-28 14:16:55 +03:00
|
|
|
cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
|
2012-02-28 14:23:34 +03:00
|
|
|
bool *wait_flag, struct TCP_Server_Info *server)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2024-01-31 18:02:25 -05:00
|
|
|
if (flock->c.flc_flags & FL_POSIX)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Posix\n");
|
2024-01-31 18:02:25 -05:00
|
|
|
if (flock->c.flc_flags & FL_FLOCK)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Flock\n");
|
2024-01-31 18:02:25 -05:00
|
|
|
if (flock->c.flc_flags & FL_SLEEP) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Blocking lock\n");
|
2010-08-17 11:26:00 +04:00
|
|
|
*wait_flag = true;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2024-01-31 18:02:25 -05:00
|
|
|
if (flock->c.flc_flags & FL_ACCESS)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
|
2024-01-31 18:02:25 -05:00
|
|
|
if (flock->c.flc_flags & FL_LEASE)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Lease on file - not implemented yet\n");
|
2024-01-31 18:02:25 -05:00
|
|
|
if (flock->c.flc_flags &
|
2012-09-19 06:22:46 -07:00
|
|
|
(~(FL_POSIX | FL_FLOCK | FL_SLEEP |
|
2018-10-04 09:24:38 +10:00
|
|
|
FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
|
2024-01-31 18:02:25 -05:00
|
|
|
cifs_dbg(FYI, "Unknown lock flags 0x%x\n",
|
|
|
|
flock->c.flc_flags);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-02-28 14:23:34 +03:00
|
|
|
*type = server->vals->large_lock_type;
|
2024-01-31 18:01:55 -05:00
|
|
|
if (lock_is_write(flock)) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "F_WRLCK\n");
|
2012-02-28 14:23:34 +03:00
|
|
|
*type |= server->vals->exclusive_lock_type;
|
2010-08-17 11:26:00 +04:00
|
|
|
*lock = 1;
|
2024-01-31 18:01:55 -05:00
|
|
|
} else if (lock_is_unlock(flock)) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "F_UNLCK\n");
|
2012-02-28 14:23:34 +03:00
|
|
|
*type |= server->vals->unlock_lock_type;
|
2010-08-17 11:26:00 +04:00
|
|
|
*unlock = 1;
|
|
|
|
/* Check if unlock includes more than one lock range */
|
2024-01-31 18:01:55 -05:00
|
|
|
} else if (lock_is_read(flock)) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "F_RDLCK\n");
|
2012-02-28 14:23:34 +03:00
|
|
|
*type |= server->vals->shared_lock_type;
|
2010-08-17 11:26:00 +04:00
|
|
|
*lock = 1;
|
2024-01-31 18:02:25 -05:00
|
|
|
} else if (flock->c.flc_type == F_EXLCK) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "F_EXLCK\n");
|
2012-02-28 14:23:34 +03:00
|
|
|
*type |= server->vals->exclusive_lock_type;
|
2010-08-17 11:26:00 +04:00
|
|
|
*lock = 1;
|
2024-01-31 18:02:25 -05:00
|
|
|
} else if (flock->c.flc_type == F_SHLCK) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "F_SHLCK\n");
|
2012-02-28 14:23:34 +03:00
|
|
|
*type |= server->vals->shared_lock_type;
|
2010-08-17 11:26:00 +04:00
|
|
|
*lock = 1;
|
2005-04-16 15:20:36 -07:00
|
|
|
} else
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Unknown type of lock\n");
|
2010-08-17 11:26:00 +04:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
static int
|
2012-02-28 14:16:55 +03:00
|
|
|
cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
|
2012-06-20 11:21:16 +04:00
|
|
|
bool wait_flag, bool posix_lck, unsigned int xid)
|
2010-08-17 11:26:00 +04:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2022-05-19 12:18:37 -03:00
|
|
|
__u64 length = cifs_flock_len(flock);
|
2011-10-22 15:33:30 +04:00
|
|
|
struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2012-02-28 14:23:34 +03:00
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2012-09-18 16:20:26 -07:00
|
|
|
__u16 netfid = cfile->fid.netfid;
|
2010-04-05 09:59:14 +04:00
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
if (posix_lck) {
|
|
|
|
int posix_lock_type;
|
2011-10-22 15:33:30 +04:00
|
|
|
|
|
|
|
rc = cifs_posix_lock_test(file, flock);
|
|
|
|
if (!rc)
|
|
|
|
return rc;
|
|
|
|
|
2012-02-28 14:23:34 +03:00
|
|
|
if (type & server->vals->shared_lock_type)
|
2010-08-17 11:26:00 +04:00
|
|
|
posix_lock_type = CIFS_RDLCK;
|
|
|
|
else
|
|
|
|
posix_lock_type = CIFS_WRLCK;
|
2016-05-24 06:27:44 -04:00
|
|
|
rc = CIFSSMBPosixLock(xid, tcon, netfid,
|
2024-01-31 18:02:25 -05:00
|
|
|
hash_lockowner(flock->c.flc_owner),
|
2012-07-23 13:28:37 -04:00
|
|
|
flock->fl_start, length, flock,
|
2011-10-22 15:33:30 +04:00
|
|
|
posix_lock_type, wait_flag);
|
2010-08-17 11:26:00 +04:00
|
|
|
return rc;
|
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-02-24 15:41:06 +03:00
|
|
|
rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
|
2011-10-22 15:33:29 +04:00
|
|
|
if (!rc)
|
|
|
|
return rc;
|
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
/* BB we could chain these into one lock request BB */
|
2012-09-19 06:22:43 -07:00
|
|
|
rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
|
|
|
|
1, 0, false);
|
2010-08-17 11:26:00 +04:00
|
|
|
if (rc == 0) {
|
2012-09-19 06:22:43 -07:00
|
|
|
rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
|
|
|
|
type, 0, 1, false);
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_UNLCK;
|
2010-08-17 11:26:00 +04:00
|
|
|
if (rc != 0)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
|
|
|
|
rc);
|
2011-10-29 17:17:59 +04:00
|
|
|
return 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2006-08-02 21:56:33 +00:00
|
|
|
|
2012-02-28 14:23:34 +03:00
|
|
|
if (type & server->vals->shared_lock_type) {
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_WRLCK;
|
2011-10-29 17:17:59 +04:00
|
|
|
return 0;
|
2006-08-02 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
type &= ~server->vals->exclusive_lock_type;
|
|
|
|
|
|
|
|
rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
|
|
|
|
type | server->vals->shared_lock_type,
|
|
|
|
1, 0, false);
|
2010-08-17 11:26:00 +04:00
|
|
|
if (rc == 0) {
|
2012-09-19 06:22:43 -07:00
|
|
|
rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
|
|
|
|
type | server->vals->shared_lock_type, 0, 1, false);
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_RDLCK;
|
2010-08-17 11:26:00 +04:00
|
|
|
if (rc != 0)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
|
|
|
|
rc);
|
2010-08-17 11:26:00 +04:00
|
|
|
} else
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_type = F_WRLCK;
|
2010-08-17 11:26:00 +04:00
|
|
|
|
2011-10-29 17:17:59 +04:00
|
|
|
return 0;
|
2010-08-17 11:26:00 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
void
|
2011-10-22 15:33:31 +04:00
|
|
|
cifs_move_llist(struct list_head *source, struct list_head *dest)
|
|
|
|
{
|
|
|
|
struct list_head *li, *tmp;
|
|
|
|
list_for_each_safe(li, tmp, source)
|
|
|
|
list_move(li, dest);
|
|
|
|
}
|
|
|
|
|
smb3 client: fix open hardlink on deferred close file error
The following Python script results in unexpected behaviour when run on
a CIFS filesystem against a Windows Server:
# Create file
fd = os.open('test', os.O_WRONLY|os.O_CREAT)
os.write(fd, b'foo')
os.close(fd)
# Open and close the file to leave a pending deferred close
fd = os.open('test', os.O_RDONLY|os.O_DIRECT)
os.close(fd)
# Try to open the file via a hard link
os.link('test', 'new')
newfd = os.open('new', os.O_RDONLY|os.O_DIRECT)
The final open returns EINVAL due to the server returning
STATUS_INVALID_PARAMETER. The root cause of this is that the client
caches lease keys per inode, but the spec requires them to be related to
the filename which causes problems when hard links are involved:
From MS-SMB2 section 3.3.5.9.11:
"The server MUST attempt to locate a Lease by performing a lookup in the
LeaseTable.LeaseList using the LeaseKey in the
SMB2_CREATE_REQUEST_LEASE_V2 as the lookup key. If a lease is found,
Lease.FileDeleteOnClose is FALSE, and Lease.Filename does not match the
file name for the incoming request, the request MUST be failed with
STATUS_INVALID_PARAMETER"
On client side, we first check the context of file open, if it hits above
conditions, we first close all opening files which are belong to the same
inode, then we do open the hard link file.
Cc: stable@vger.kernel.org
Signed-off-by: Chunjie Zhu <chunjie.zhu@cloud.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2025-04-12 21:15:55 -05:00
|
|
|
int
|
|
|
|
cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
|
|
|
|
struct file *file)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *open_file = NULL;
|
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
spin_lock(&tcon->open_file_lock);
|
|
|
|
spin_lock(&cinode->open_file_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(open_file, &cinode->openFileList, flist) {
|
|
|
|
if (file->f_flags == open_file->f_flags) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&cinode->open_file_lock);
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
void
|
2011-10-22 15:33:31 +04:00
|
|
|
cifs_free_llist(struct list_head *llist)
|
|
|
|
{
|
|
|
|
struct cifsLockInfo *li, *tmp;
|
|
|
|
list_for_each_entry_safe(li, tmp, llist, llist) {
|
|
|
|
cifs_del_lock_waiters(li);
|
|
|
|
list_del(&li->llist);
|
|
|
|
kfree(li);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2012-09-19 06:22:43 -07:00
|
|
|
int
|
2012-06-20 11:21:16 +04:00
|
|
|
cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
|
|
|
|
unsigned int xid)
|
2011-10-22 15:33:31 +04:00
|
|
|
{
|
|
|
|
int rc = 0, stored_rc;
|
2017-09-19 16:27:39 +01:00
|
|
|
static const int types[] = {
|
|
|
|
LOCKING_ANDX_LARGE_FILES,
|
|
|
|
LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
|
|
|
|
};
|
2011-10-22 15:33:31 +04:00
|
|
|
unsigned int i;
|
2012-05-31 13:03:26 +04:00
|
|
|
unsigned int max_num, num, max_buf;
|
2011-10-22 15:33:31 +04:00
|
|
|
LOCKING_ANDX_RANGE *buf, *cur;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2015-03-17 22:25:59 +00:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
|
2011-10-22 15:33:31 +04:00
|
|
|
struct cifsLockInfo *li, *tmp;
|
2022-05-19 12:18:37 -03:00
|
|
|
__u64 length = cifs_flock_len(flock);
|
2024-08-21 14:56:37 +08:00
|
|
|
LIST_HEAD(tmp_llist);
|
2011-10-22 15:33:31 +04:00
|
|
|
|
2012-05-31 13:03:26 +04:00
|
|
|
/*
|
|
|
|
* Accessing maxBuf is racy with cifs_reconnect - need to store value
|
2019-01-08 18:30:57 +00:00
|
|
|
* and check it before using.
|
2012-05-31 13:03:26 +04:00
|
|
|
*/
|
|
|
|
max_buf = tcon->ses->server->maxBuf;
|
2019-01-08 18:30:57 +00:00
|
|
|
if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
|
2012-05-31 13:03:26 +04:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-01-08 18:30:56 +00:00
|
|
|
BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
|
|
|
|
PAGE_SIZE);
|
|
|
|
max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
|
|
|
|
PAGE_SIZE);
|
2012-05-31 13:03:26 +04:00
|
|
|
max_num = (max_buf - sizeof(struct smb_hdr)) /
|
|
|
|
sizeof(LOCKING_ANDX_RANGE);
|
2014-12-10 15:41:17 -08:00
|
|
|
buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
|
2011-10-22 15:33:31 +04:00
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-10-23 05:02:33 -04:00
|
|
|
cifs_down_write(&cinode->lock_sem);
|
2011-10-22 15:33:31 +04:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
cur = buf;
|
|
|
|
num = 0;
|
2012-09-19 06:22:43 -07:00
|
|
|
list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
|
2011-10-22 15:33:31 +04:00
|
|
|
if (flock->fl_start > li->offset ||
|
|
|
|
(flock->fl_start + length) <
|
|
|
|
(li->offset + li->length))
|
|
|
|
continue;
|
|
|
|
if (current->tgid != li->pid)
|
|
|
|
continue;
|
|
|
|
if (types[i] != li->type)
|
|
|
|
continue;
|
2012-05-31 13:59:36 +04:00
|
|
|
if (cinode->can_cache_brlcks) {
|
2011-10-22 15:33:31 +04:00
|
|
|
/*
|
|
|
|
* We can cache brlock requests - simply remove
|
2012-02-24 15:41:06 +03:00
|
|
|
* a lock from the file's list.
|
2011-10-22 15:33:31 +04:00
|
|
|
*/
|
|
|
|
list_del(&li->llist);
|
|
|
|
cifs_del_lock_waiters(li);
|
|
|
|
kfree(li);
|
2012-05-31 13:59:36 +04:00
|
|
|
continue;
|
2011-10-22 15:33:31 +04:00
|
|
|
}
|
2012-05-31 13:59:36 +04:00
|
|
|
cur->Pid = cpu_to_le16(li->pid);
|
|
|
|
cur->LengthLow = cpu_to_le32((u32)li->length);
|
|
|
|
cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
|
|
|
|
cur->OffsetLow = cpu_to_le32((u32)li->offset);
|
|
|
|
cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
|
|
|
|
/*
|
|
|
|
* We need to save a lock here to let us add it again to
|
|
|
|
* the file's list if the unlock range request fails on
|
|
|
|
* the server.
|
|
|
|
*/
|
|
|
|
list_move(&li->llist, &tmp_llist);
|
|
|
|
if (++num == max_num) {
|
2012-09-18 16:20:26 -07:00
|
|
|
stored_rc = cifs_lockv(xid, tcon,
|
|
|
|
cfile->fid.netfid,
|
2012-05-31 13:59:36 +04:00
|
|
|
li->type, num, 0, buf);
|
|
|
|
if (stored_rc) {
|
|
|
|
/*
|
|
|
|
* We failed on the unlock range
|
|
|
|
* request - add all locks from the tmp
|
|
|
|
* list to the head of the file's list.
|
|
|
|
*/
|
|
|
|
cifs_move_llist(&tmp_llist,
|
2012-09-19 06:22:43 -07:00
|
|
|
&cfile->llist->locks);
|
2012-05-31 13:59:36 +04:00
|
|
|
rc = stored_rc;
|
|
|
|
} else
|
|
|
|
/*
|
|
|
|
* The unlock range request succeed -
|
|
|
|
* free the tmp list.
|
|
|
|
*/
|
|
|
|
cifs_free_llist(&tmp_llist);
|
|
|
|
cur = buf;
|
|
|
|
num = 0;
|
|
|
|
} else
|
|
|
|
cur++;
|
2011-10-22 15:33:31 +04:00
|
|
|
}
|
|
|
|
if (num) {
|
2012-09-18 16:20:26 -07:00
|
|
|
stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
|
2011-10-22 15:33:31 +04:00
|
|
|
types[i], num, 0, buf);
|
|
|
|
if (stored_rc) {
|
2012-09-19 06:22:43 -07:00
|
|
|
cifs_move_llist(&tmp_llist,
|
|
|
|
&cfile->llist->locks);
|
2011-10-22 15:33:31 +04:00
|
|
|
rc = stored_rc;
|
|
|
|
} else
|
|
|
|
cifs_free_llist(&tmp_llist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
up_write(&cinode->lock_sem);
|
2011-10-22 15:33:31 +04:00
|
|
|
kfree(buf);
|
|
|
|
return rc;
|
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2011-10-22 15:33:31 +04:00
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
static int
|
2012-09-19 06:22:43 -07:00
|
|
|
cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
|
2012-06-20 11:21:16 +04:00
|
|
|
bool wait_flag, bool posix_lck, int lock, int unlock,
|
|
|
|
unsigned int xid)
|
2010-08-17 11:26:00 +04:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2022-05-19 12:18:37 -03:00
|
|
|
__u64 length = cifs_flock_len(flock);
|
2010-08-17 11:26:00 +04:00
|
|
|
struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2012-02-28 14:23:34 +03:00
|
|
|
struct TCP_Server_Info *server = tcon->ses->server;
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(cfile->dentry);
|
2010-08-17 11:26:00 +04:00
|
|
|
|
2022-08-01 11:17:46 -05:00
|
|
|
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
|
2010-08-17 11:26:00 +04:00
|
|
|
if (posix_lck) {
|
2006-02-28 22:39:25 +00:00
|
|
|
int posix_lock_type;
|
2011-10-22 15:33:30 +04:00
|
|
|
|
|
|
|
rc = cifs_posix_lock_set(file, flock);
|
2020-07-02 15:25:26 +08:00
|
|
|
if (rc <= FILE_LOCK_DEFERRED)
|
2011-10-22 15:33:30 +04:00
|
|
|
return rc;
|
|
|
|
|
2012-02-28 14:23:34 +03:00
|
|
|
if (type & server->vals->shared_lock_type)
|
2006-02-28 22:39:25 +00:00
|
|
|
posix_lock_type = CIFS_RDLCK;
|
|
|
|
else
|
|
|
|
posix_lock_type = CIFS_WRLCK;
|
2007-07-13 00:33:32 +00:00
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
if (unlock == 1)
|
2006-03-03 23:36:34 +00:00
|
|
|
posix_lock_type = CIFS_UNLCK;
|
2006-08-02 21:56:33 +00:00
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
|
2024-01-31 18:02:25 -05:00
|
|
|
hash_lockowner(flock->c.flc_owner),
|
2016-05-24 06:27:44 -04:00
|
|
|
flock->fl_start, length,
|
2012-09-19 06:22:43 -07:00
|
|
|
NULL, posix_lock_type, wait_flag);
|
2010-08-17 11:26:00 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2022-08-01 11:17:46 -05:00
|
|
|
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
|
2010-08-17 11:26:00 +04:00
|
|
|
if (lock) {
|
2011-10-29 17:17:58 +04:00
|
|
|
struct cifsLockInfo *lock;
|
|
|
|
|
2018-10-04 09:24:38 +10:00
|
|
|
lock = cifs_lock_init(flock->fl_start, length, type,
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_flags);
|
2011-10-29 17:17:58 +04:00
|
|
|
if (!lock)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-02-24 15:41:06 +03:00
|
|
|
rc = cifs_lock_add_if(cfile, lock, wait_flag);
|
2012-11-22 18:56:39 +04:00
|
|
|
if (rc < 0) {
|
2011-10-29 17:17:58 +04:00
|
|
|
kfree(lock);
|
2012-11-22 18:56:39 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (!rc)
|
2011-10-22 15:33:29 +04:00
|
|
|
goto out;
|
|
|
|
|
2012-12-24 14:41:19 +04:00
|
|
|
/*
|
|
|
|
* Windows 7 server can delay breaking lease from read to None
|
|
|
|
* if we set a byte-range lock on a file - break it explicitly
|
|
|
|
* before sending the lock to the server to be sure the next
|
|
|
|
* read won't conflict with non-overlapted locks due to
|
|
|
|
* pagereading.
|
|
|
|
*/
|
2013-09-05 13:01:06 +04:00
|
|
|
if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
|
|
|
|
CIFS_CACHE_READ(CIFS_I(inode))) {
|
2014-04-30 09:31:47 -04:00
|
|
|
cifs_zap_mapping(inode);
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
|
|
|
|
inode);
|
2013-09-05 13:01:06 +04:00
|
|
|
CIFS_I(inode)->oplock = 0;
|
2012-12-24 14:41:19 +04:00
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:43 -07:00
|
|
|
rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
|
|
|
|
type, 1, 0, wait_flag);
|
2011-10-29 17:17:58 +04:00
|
|
|
if (rc) {
|
|
|
|
kfree(lock);
|
2012-11-22 18:56:39 +04:00
|
|
|
return rc;
|
2010-08-17 11:26:00 +04:00
|
|
|
}
|
2011-10-29 17:17:58 +04:00
|
|
|
|
2012-02-24 15:41:06 +03:00
|
|
|
cifs_lock_add(cfile, lock);
|
2011-10-22 15:33:31 +04:00
|
|
|
} else if (unlock)
|
2012-09-19 06:22:43 -07:00
|
|
|
rc = server->ops->mand_unlock_range(cfile, flock, xid);
|
2010-08-17 11:26:00 +04:00
|
|
|
|
|
|
|
out:
|
2024-01-31 18:02:25 -05:00
|
|
|
if ((flock->c.flc_flags & FL_POSIX) || (flock->c.flc_flags & FL_FLOCK)) {
|
CIFS: fix POSIX lock leak and invalid ptr deref
We have a customer reporting crashes in lock_get_status() with many
"Leaked POSIX lock" messages preceeding the crash.
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
POSIX: fl_owner=ffff8900e7b79380 fl_flags=0x1 fl_type=0x1 fl_pid=20709
Leaked POSIX lock on dev=0x0:0x4b ino...
Leaked locks on dev=0x0:0x4b ino=0xf911400000029:
POSIX: fl_owner=ffff89f41c870e00 fl_flags=0x1 fl_type=0x1 fl_pid=19592
stack segment: 0000 [#1] SMP
Modules linked in: binfmt_misc msr tcp_diag udp_diag inet_diag unix_diag af_packet_diag netlink_diag rpcsec_gss_krb5 arc4 ecb auth_rpcgss nfsv4 md4 nfs nls_utf8 lockd grace cifs sunrpc ccm dns_resolver fscache af_packet iscsi_ibft iscsi_boot_sysfs vmw_vsock_vmci_transport vsock xfs libcrc32c sb_edac edac_core crct10dif_pclmul crc32_pclmul ghash_clmulni_intel drbg ansi_cprng vmw_balloon aesni_intel aes_x86_64 lrw gf128mul glue_helper ablk_helper cryptd joydev pcspkr vmxnet3 i2c_piix4 vmw_vmci shpchp fjes processor button ac btrfs xor raid6_pq sr_mod cdrom ata_generic sd_mod ata_piix vmwgfx crc32c_intel drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm serio_raw ahci libahci drm libata vmw_pvscsi sg dm_multipath dm_mod scsi_dh_rdac scsi_dh_emc scsi_dh_alua scsi_mod autofs4
Supported: Yes
CPU: 6 PID: 28250 Comm: lsof Not tainted 4.4.156-94.64-default #1
Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/05/2016
task: ffff88a345f28740 ti: ffff88c74005c000 task.ti: ffff88c74005c000
RIP: 0010:[<ffffffff8125dcab>] [<ffffffff8125dcab>] lock_get_status+0x9b/0x3b0
RSP: 0018:ffff88c74005fd90 EFLAGS: 00010202
RAX: ffff89bde83e20ae RBX: ffff89e870003d18 RCX: 0000000049534f50
RDX: ffffffff81a3541f RSI: ffffffff81a3544e RDI: ffff89bde83e20ae
RBP: 0026252423222120 R08: 0000000020584953 R09: 000000000000ffff
R10: 0000000000000000 R11: ffff88c74005fc70 R12: ffff89e5ca7b1340
R13: 00000000000050e5 R14: ffff89e870003d30 R15: ffff89e5ca7b1340
FS: 00007fafd64be800(0000) GS:ffff89f41fd00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000001c80018 CR3: 000000a522048000 CR4: 0000000000360670
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Stack:
0000000000000208 ffffffff81a3d6b6 ffff89e870003d30 ffff89e870003d18
ffff89e5ca7b1340 ffff89f41738d7c0 ffff89e870003d30 ffff89e5ca7b1340
ffffffff8125e08f 0000000000000000 ffff89bc22b67d00 ffff88c74005ff28
Call Trace:
[<ffffffff8125e08f>] locks_show+0x2f/0x70
[<ffffffff81230ad1>] seq_read+0x251/0x3a0
[<ffffffff81275bbc>] proc_reg_read+0x3c/0x70
[<ffffffff8120e456>] __vfs_read+0x26/0x140
[<ffffffff8120e9da>] vfs_read+0x7a/0x120
[<ffffffff8120faf2>] SyS_read+0x42/0xa0
[<ffffffff8161cbc3>] entry_SYSCALL_64_fastpath+0x1e/0xb7
When Linux closes a FD (close(), close-on-exec, dup2(), ...) it calls
filp_close() which also removes all posix locks.
The lock struct is initialized like so in filp_close() and passed
down to cifs
...
lock.fl_type = F_UNLCK;
lock.fl_flags = FL_POSIX | FL_CLOSE;
lock.fl_start = 0;
lock.fl_end = OFFSET_MAX;
...
Note the FL_CLOSE flag, which hints the VFS code that this unlocking
is done for closing the fd.
filp_close()
locks_remove_posix(filp, id);
vfs_lock_file(filp, F_SETLK, &lock, NULL);
return filp->f_op->lock(filp, cmd, fl) => cifs_lock()
rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, xid);
rc = server->ops->mand_unlock_range(cfile, flock, xid);
if (flock->fl_flags & FL_POSIX && !rc)
rc = locks_lock_file_wait(file, flock)
Notice how we don't call locks_lock_file_wait() which does the
generic VFS lock/unlock/wait work on the inode if rc != 0.
If we are closing the handle, the SMB server is supposed to remove any
locks associated with it. Similarly, cifs.ko frees and wakes up any
lock and lock waiter when closing the file:
cifs_close()
cifsFileInfo_put(file->private_data)
/*
* Delete any outstanding lock records. We'll lose them when the file
* is closed anyway.
*/
down_write(&cifsi->lock_sem);
list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
list_del(&li->llist);
cifs_del_lock_waiters(li);
kfree(li);
}
list_del(&cifs_file->llist->llist);
kfree(cifs_file->llist);
up_write(&cifsi->lock_sem);
So we can safely ignore unlocking failures in cifs_lock() if they
happen with the FL_CLOSE flag hint set as both the server and the
client take care of it during the actual closing.
This is not a proper fix for the unlocking failure but it's safe and
it seems to prevent the lock leakages and crashes the customer
experiences.
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: NeilBrown <neil@brown.name>
Signed-off-by: Steve French <stfrench@microsoft.com>
Acked-by: Pavel Shilovsky <pshilov@microsoft.com>
2019-03-14 18:44:16 +01:00
|
|
|
/*
|
|
|
|
* If this is a request to remove all locks because we
|
|
|
|
* are closing the file, it doesn't matter if the
|
|
|
|
* unlocking failed as both cifs.ko and the SMB server
|
|
|
|
* remove the lock on file close
|
|
|
|
*/
|
|
|
|
if (rc) {
|
|
|
|
cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
|
2024-01-31 18:02:25 -05:00
|
|
|
if (!(flock->c.flc_flags & FL_CLOSE))
|
CIFS: fix POSIX lock leak and invalid ptr deref
We have a customer reporting crashes in lock_get_status() with many
"Leaked POSIX lock" messages preceeding the crash.
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
POSIX: fl_owner=ffff8900e7b79380 fl_flags=0x1 fl_type=0x1 fl_pid=20709
Leaked POSIX lock on dev=0x0:0x4b ino...
Leaked locks on dev=0x0:0x4b ino=0xf911400000029:
POSIX: fl_owner=ffff89f41c870e00 fl_flags=0x1 fl_type=0x1 fl_pid=19592
stack segment: 0000 [#1] SMP
Modules linked in: binfmt_misc msr tcp_diag udp_diag inet_diag unix_diag af_packet_diag netlink_diag rpcsec_gss_krb5 arc4 ecb auth_rpcgss nfsv4 md4 nfs nls_utf8 lockd grace cifs sunrpc ccm dns_resolver fscache af_packet iscsi_ibft iscsi_boot_sysfs vmw_vsock_vmci_transport vsock xfs libcrc32c sb_edac edac_core crct10dif_pclmul crc32_pclmul ghash_clmulni_intel drbg ansi_cprng vmw_balloon aesni_intel aes_x86_64 lrw gf128mul glue_helper ablk_helper cryptd joydev pcspkr vmxnet3 i2c_piix4 vmw_vmci shpchp fjes processor button ac btrfs xor raid6_pq sr_mod cdrom ata_generic sd_mod ata_piix vmwgfx crc32c_intel drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm serio_raw ahci libahci drm libata vmw_pvscsi sg dm_multipath dm_mod scsi_dh_rdac scsi_dh_emc scsi_dh_alua scsi_mod autofs4
Supported: Yes
CPU: 6 PID: 28250 Comm: lsof Not tainted 4.4.156-94.64-default #1
Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/05/2016
task: ffff88a345f28740 ti: ffff88c74005c000 task.ti: ffff88c74005c000
RIP: 0010:[<ffffffff8125dcab>] [<ffffffff8125dcab>] lock_get_status+0x9b/0x3b0
RSP: 0018:ffff88c74005fd90 EFLAGS: 00010202
RAX: ffff89bde83e20ae RBX: ffff89e870003d18 RCX: 0000000049534f50
RDX: ffffffff81a3541f RSI: ffffffff81a3544e RDI: ffff89bde83e20ae
RBP: 0026252423222120 R08: 0000000020584953 R09: 000000000000ffff
R10: 0000000000000000 R11: ffff88c74005fc70 R12: ffff89e5ca7b1340
R13: 00000000000050e5 R14: ffff89e870003d30 R15: ffff89e5ca7b1340
FS: 00007fafd64be800(0000) GS:ffff89f41fd00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000001c80018 CR3: 000000a522048000 CR4: 0000000000360670
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Stack:
0000000000000208 ffffffff81a3d6b6 ffff89e870003d30 ffff89e870003d18
ffff89e5ca7b1340 ffff89f41738d7c0 ffff89e870003d30 ffff89e5ca7b1340
ffffffff8125e08f 0000000000000000 ffff89bc22b67d00 ffff88c74005ff28
Call Trace:
[<ffffffff8125e08f>] locks_show+0x2f/0x70
[<ffffffff81230ad1>] seq_read+0x251/0x3a0
[<ffffffff81275bbc>] proc_reg_read+0x3c/0x70
[<ffffffff8120e456>] __vfs_read+0x26/0x140
[<ffffffff8120e9da>] vfs_read+0x7a/0x120
[<ffffffff8120faf2>] SyS_read+0x42/0xa0
[<ffffffff8161cbc3>] entry_SYSCALL_64_fastpath+0x1e/0xb7
When Linux closes a FD (close(), close-on-exec, dup2(), ...) it calls
filp_close() which also removes all posix locks.
The lock struct is initialized like so in filp_close() and passed
down to cifs
...
lock.fl_type = F_UNLCK;
lock.fl_flags = FL_POSIX | FL_CLOSE;
lock.fl_start = 0;
lock.fl_end = OFFSET_MAX;
...
Note the FL_CLOSE flag, which hints the VFS code that this unlocking
is done for closing the fd.
filp_close()
locks_remove_posix(filp, id);
vfs_lock_file(filp, F_SETLK, &lock, NULL);
return filp->f_op->lock(filp, cmd, fl) => cifs_lock()
rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, xid);
rc = server->ops->mand_unlock_range(cfile, flock, xid);
if (flock->fl_flags & FL_POSIX && !rc)
rc = locks_lock_file_wait(file, flock)
Notice how we don't call locks_lock_file_wait() which does the
generic VFS lock/unlock/wait work on the inode if rc != 0.
If we are closing the handle, the SMB server is supposed to remove any
locks associated with it. Similarly, cifs.ko frees and wakes up any
lock and lock waiter when closing the file:
cifs_close()
cifsFileInfo_put(file->private_data)
/*
* Delete any outstanding lock records. We'll lose them when the file
* is closed anyway.
*/
down_write(&cifsi->lock_sem);
list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
list_del(&li->llist);
cifs_del_lock_waiters(li);
kfree(li);
}
list_del(&cifs_file->llist->llist);
kfree(cifs_file->llist);
up_write(&cifsi->lock_sem);
So we can safely ignore unlocking failures in cifs_lock() if they
happen with the FL_CLOSE flag hint set as both the server and the
client take care of it during the actual closing.
This is not a proper fix for the unlocking failure but it's safe and
it seems to prevent the lock leakages and crashes the customer
experiences.
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: NeilBrown <neil@brown.name>
Signed-off-by: Steve French <stfrench@microsoft.com>
Acked-by: Pavel Shilovsky <pshilov@microsoft.com>
2019-03-14 18:44:16 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2015-10-22 13:38:14 -04:00
|
|
|
rc = locks_lock_file_wait(file, flock);
|
CIFS: fix POSIX lock leak and invalid ptr deref
We have a customer reporting crashes in lock_get_status() with many
"Leaked POSIX lock" messages preceeding the crash.
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x56 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
Leaked POSIX lock on dev=0x0:0x53 ...
POSIX: fl_owner=ffff8900e7b79380 fl_flags=0x1 fl_type=0x1 fl_pid=20709
Leaked POSIX lock on dev=0x0:0x4b ino...
Leaked locks on dev=0x0:0x4b ino=0xf911400000029:
POSIX: fl_owner=ffff89f41c870e00 fl_flags=0x1 fl_type=0x1 fl_pid=19592
stack segment: 0000 [#1] SMP
Modules linked in: binfmt_misc msr tcp_diag udp_diag inet_diag unix_diag af_packet_diag netlink_diag rpcsec_gss_krb5 arc4 ecb auth_rpcgss nfsv4 md4 nfs nls_utf8 lockd grace cifs sunrpc ccm dns_resolver fscache af_packet iscsi_ibft iscsi_boot_sysfs vmw_vsock_vmci_transport vsock xfs libcrc32c sb_edac edac_core crct10dif_pclmul crc32_pclmul ghash_clmulni_intel drbg ansi_cprng vmw_balloon aesni_intel aes_x86_64 lrw gf128mul glue_helper ablk_helper cryptd joydev pcspkr vmxnet3 i2c_piix4 vmw_vmci shpchp fjes processor button ac btrfs xor raid6_pq sr_mod cdrom ata_generic sd_mod ata_piix vmwgfx crc32c_intel drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm serio_raw ahci libahci drm libata vmw_pvscsi sg dm_multipath dm_mod scsi_dh_rdac scsi_dh_emc scsi_dh_alua scsi_mod autofs4
Supported: Yes
CPU: 6 PID: 28250 Comm: lsof Not tainted 4.4.156-94.64-default #1
Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 04/05/2016
task: ffff88a345f28740 ti: ffff88c74005c000 task.ti: ffff88c74005c000
RIP: 0010:[<ffffffff8125dcab>] [<ffffffff8125dcab>] lock_get_status+0x9b/0x3b0
RSP: 0018:ffff88c74005fd90 EFLAGS: 00010202
RAX: ffff89bde83e20ae RBX: ffff89e870003d18 RCX: 0000000049534f50
RDX: ffffffff81a3541f RSI: ffffffff81a3544e RDI: ffff89bde83e20ae
RBP: 0026252423222120 R08: 0000000020584953 R09: 000000000000ffff
R10: 0000000000000000 R11: ffff88c74005fc70 R12: ffff89e5ca7b1340
R13: 00000000000050e5 R14: ffff89e870003d30 R15: ffff89e5ca7b1340
FS: 00007fafd64be800(0000) GS:ffff89f41fd00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000001c80018 CR3: 000000a522048000 CR4: 0000000000360670
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Stack:
0000000000000208 ffffffff81a3d6b6 ffff89e870003d30 ffff89e870003d18
ffff89e5ca7b1340 ffff89f41738d7c0 ffff89e870003d30 ffff89e5ca7b1340
ffffffff8125e08f 0000000000000000 ffff89bc22b67d00 ffff88c74005ff28
Call Trace:
[<ffffffff8125e08f>] locks_show+0x2f/0x70
[<ffffffff81230ad1>] seq_read+0x251/0x3a0
[<ffffffff81275bbc>] proc_reg_read+0x3c/0x70
[<ffffffff8120e456>] __vfs_read+0x26/0x140
[<ffffffff8120e9da>] vfs_read+0x7a/0x120
[<ffffffff8120faf2>] SyS_read+0x42/0xa0
[<ffffffff8161cbc3>] entry_SYSCALL_64_fastpath+0x1e/0xb7
When Linux closes a FD (close(), close-on-exec, dup2(), ...) it calls
filp_close() which also removes all posix locks.
The lock struct is initialized like so in filp_close() and passed
down to cifs
...
lock.fl_type = F_UNLCK;
lock.fl_flags = FL_POSIX | FL_CLOSE;
lock.fl_start = 0;
lock.fl_end = OFFSET_MAX;
...
Note the FL_CLOSE flag, which hints the VFS code that this unlocking
is done for closing the fd.
filp_close()
locks_remove_posix(filp, id);
vfs_lock_file(filp, F_SETLK, &lock, NULL);
return filp->f_op->lock(filp, cmd, fl) => cifs_lock()
rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, xid);
rc = server->ops->mand_unlock_range(cfile, flock, xid);
if (flock->fl_flags & FL_POSIX && !rc)
rc = locks_lock_file_wait(file, flock)
Notice how we don't call locks_lock_file_wait() which does the
generic VFS lock/unlock/wait work on the inode if rc != 0.
If we are closing the handle, the SMB server is supposed to remove any
locks associated with it. Similarly, cifs.ko frees and wakes up any
lock and lock waiter when closing the file:
cifs_close()
cifsFileInfo_put(file->private_data)
/*
* Delete any outstanding lock records. We'll lose them when the file
* is closed anyway.
*/
down_write(&cifsi->lock_sem);
list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
list_del(&li->llist);
cifs_del_lock_waiters(li);
kfree(li);
}
list_del(&cifs_file->llist->llist);
kfree(cifs_file->llist);
up_write(&cifsi->lock_sem);
So we can safely ignore unlocking failures in cifs_lock() if they
happen with the FL_CLOSE flag hint set as both the server and the
client take care of it during the actual closing.
This is not a proper fix for the unlocking failure but it's safe and
it seems to prevent the lock leakages and crashes the customer
experiences.
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: NeilBrown <neil@brown.name>
Signed-off-by: Steve French <stfrench@microsoft.com>
Acked-by: Pavel Shilovsky <pshilov@microsoft.com>
2019-03-14 18:44:16 +01:00
|
|
|
}
|
2010-08-17 11:26:00 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:55:38 -05:00
|
|
|
int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
|
|
|
|
{
|
|
|
|
int rc, xid;
|
|
|
|
int lock = 0, unlock = 0;
|
|
|
|
bool wait_flag = false;
|
|
|
|
bool posix_lck = false;
|
|
|
|
struct cifs_sb_info *cifs_sb;
|
|
|
|
struct cifs_tcon *tcon;
|
|
|
|
struct cifsFileInfo *cfile;
|
|
|
|
__u32 type;
|
|
|
|
|
|
|
|
xid = get_xid();
|
|
|
|
|
2024-01-31 18:02:25 -05:00
|
|
|
if (!(fl->c.flc_flags & FL_FLOCK)) {
|
2022-10-17 22:45:23 +08:00
|
|
|
rc = -ENOLCK;
|
|
|
|
free_xid(xid);
|
|
|
|
return rc;
|
|
|
|
}
|
2019-07-16 18:55:38 -05:00
|
|
|
|
|
|
|
cfile = (struct cifsFileInfo *)file->private_data;
|
|
|
|
tcon = tlink_tcon(cfile->tlink);
|
|
|
|
|
|
|
|
cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
|
|
|
|
tcon->ses->server);
|
|
|
|
cifs_sb = CIFS_FILE_SB(file);
|
|
|
|
|
|
|
|
if (cap_unix(tcon->ses) &&
|
|
|
|
(CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
|
|
|
|
((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
|
|
|
|
posix_lck = true;
|
|
|
|
|
|
|
|
if (!lock && !unlock) {
|
|
|
|
/*
|
|
|
|
* if no lock or unlock then nothing to do since we do not
|
|
|
|
* know what it is
|
|
|
|
*/
|
2022-10-17 22:45:23 +08:00
|
|
|
rc = -EOPNOTSUPP;
|
2019-07-16 18:55:38 -05:00
|
|
|
free_xid(xid);
|
2022-10-17 22:45:23 +08:00
|
|
|
return rc;
|
2019-07-16 18:55:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
|
|
|
|
xid);
|
|
|
|
free_xid(xid);
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
|
|
|
|
{
|
|
|
|
int rc, xid;
|
|
|
|
int lock = 0, unlock = 0;
|
|
|
|
bool wait_flag = false;
|
|
|
|
bool posix_lck = false;
|
|
|
|
struct cifs_sb_info *cifs_sb;
|
|
|
|
struct cifs_tcon *tcon;
|
|
|
|
struct cifsFileInfo *cfile;
|
2012-02-28 14:16:55 +03:00
|
|
|
__u32 type;
|
2010-08-17 11:26:00 +04:00
|
|
|
|
|
|
|
rc = -EACCES;
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2010-08-17 11:26:00 +04:00
|
|
|
|
cifs: fix lock length calculation
The lock length was wrongly set to 0 when fl_end == OFFSET_MAX, thus
failing to lock the whole file when l_start=0 and l_len=0.
This fixes test 2 from cthon04.
Before patch:
$ ./cthon04/lock/tlocklfs -t 2 /mnt
Creating parent/child synchronization pipes.
Test #1 - Test regions of an unlocked file.
Parent: 1.1 - F_TEST [ 0, 1] PASSED.
Parent: 1.2 - F_TEST [ 0, ENDING] PASSED.
Parent: 1.3 - F_TEST [ 0,7fffffffffffffff] PASSED.
Parent: 1.4 - F_TEST [ 1, 1] PASSED.
Parent: 1.5 - F_TEST [ 1, ENDING] PASSED.
Parent: 1.6 - F_TEST [ 1,7fffffffffffffff] PASSED.
Parent: 1.7 - F_TEST [7fffffffffffffff, 1] PASSED.
Parent: 1.8 - F_TEST [7fffffffffffffff, ENDING] PASSED.
Parent: 1.9 - F_TEST [7fffffffffffffff,7fffffffffffffff] PASSED.
Test #2 - Try to lock the whole file.
Parent: 2.0 - F_TLOCK [ 0, ENDING] PASSED.
Child: 2.1 - F_TEST [ 0, 1] FAILED!
Child: **** Expected EACCES, returned success...
Child: **** Probably implementation error.
** CHILD pass 1 results: 0/0 pass, 0/0 warn, 1/1 fail (pass/total).
Parent: Child died
** PARENT pass 1 results: 10/10 pass, 0/0 warn, 0/0 fail (pass/total).
After patch:
$ ./cthon04/lock/tlocklfs -t 2 /mnt
Creating parent/child synchronization pipes.
Test #2 - Try to lock the whole file.
Parent: 2.0 - F_TLOCK [ 0, ENDING] PASSED.
Child: 2.1 - F_TEST [ 0, 1] PASSED.
Child: 2.2 - F_TEST [ 0, ENDING] PASSED.
Child: 2.3 - F_TEST [ 0,7fffffffffffffff] PASSED.
Child: 2.4 - F_TEST [ 1, 1] PASSED.
Child: 2.5 - F_TEST [ 1, ENDING] PASSED.
Child: 2.6 - F_TEST [ 1,7fffffffffffffff] PASSED.
Child: 2.7 - F_TEST [7fffffffffffffff, 1] PASSED.
Child: 2.8 - F_TEST [7fffffffffffffff, ENDING] PASSED.
Child: 2.9 - F_TEST [7fffffffffffffff,7fffffffffffffff] PASSED.
Parent: 2.10 - F_ULOCK [ 0, ENDING] PASSED.
** PARENT pass 1 results: 2/2 pass, 0/0 warn, 0/0 fail (pass/total).
** CHILD pass 1 results: 9/9 pass, 0/0 warn, 0/0 fail (pass/total).
Fixes: d80c69846ddf ("cifs: fix signed integer overflow when fl_end is OFFSET_MAX")
Reported-by: Xiaoli Feng <xifeng@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
Reviewed-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2022-08-08 13:41:18 -03:00
|
|
|
cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
|
2024-01-31 18:02:25 -05:00
|
|
|
flock->c.flc_flags, flock->c.flc_type,
|
|
|
|
(long long)flock->fl_start,
|
cifs: fix lock length calculation
The lock length was wrongly set to 0 when fl_end == OFFSET_MAX, thus
failing to lock the whole file when l_start=0 and l_len=0.
This fixes test 2 from cthon04.
Before patch:
$ ./cthon04/lock/tlocklfs -t 2 /mnt
Creating parent/child synchronization pipes.
Test #1 - Test regions of an unlocked file.
Parent: 1.1 - F_TEST [ 0, 1] PASSED.
Parent: 1.2 - F_TEST [ 0, ENDING] PASSED.
Parent: 1.3 - F_TEST [ 0,7fffffffffffffff] PASSED.
Parent: 1.4 - F_TEST [ 1, 1] PASSED.
Parent: 1.5 - F_TEST [ 1, ENDING] PASSED.
Parent: 1.6 - F_TEST [ 1,7fffffffffffffff] PASSED.
Parent: 1.7 - F_TEST [7fffffffffffffff, 1] PASSED.
Parent: 1.8 - F_TEST [7fffffffffffffff, ENDING] PASSED.
Parent: 1.9 - F_TEST [7fffffffffffffff,7fffffffffffffff] PASSED.
Test #2 - Try to lock the whole file.
Parent: 2.0 - F_TLOCK [ 0, ENDING] PASSED.
Child: 2.1 - F_TEST [ 0, 1] FAILED!
Child: **** Expected EACCES, returned success...
Child: **** Probably implementation error.
** CHILD pass 1 results: 0/0 pass, 0/0 warn, 1/1 fail (pass/total).
Parent: Child died
** PARENT pass 1 results: 10/10 pass, 0/0 warn, 0/0 fail (pass/total).
After patch:
$ ./cthon04/lock/tlocklfs -t 2 /mnt
Creating parent/child synchronization pipes.
Test #2 - Try to lock the whole file.
Parent: 2.0 - F_TLOCK [ 0, ENDING] PASSED.
Child: 2.1 - F_TEST [ 0, 1] PASSED.
Child: 2.2 - F_TEST [ 0, ENDING] PASSED.
Child: 2.3 - F_TEST [ 0,7fffffffffffffff] PASSED.
Child: 2.4 - F_TEST [ 1, 1] PASSED.
Child: 2.5 - F_TEST [ 1, ENDING] PASSED.
Child: 2.6 - F_TEST [ 1,7fffffffffffffff] PASSED.
Child: 2.7 - F_TEST [7fffffffffffffff, 1] PASSED.
Child: 2.8 - F_TEST [7fffffffffffffff, ENDING] PASSED.
Child: 2.9 - F_TEST [7fffffffffffffff,7fffffffffffffff] PASSED.
Parent: 2.10 - F_ULOCK [ 0, ENDING] PASSED.
** PARENT pass 1 results: 2/2 pass, 0/0 warn, 0/0 fail (pass/total).
** CHILD pass 1 results: 9/9 pass, 0/0 warn, 0/0 fail (pass/total).
Fixes: d80c69846ddf ("cifs: fix signed integer overflow when fl_end is OFFSET_MAX")
Reported-by: Xiaoli Feng <xifeng@redhat.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
Reviewed-by: Ronnie Sahlberg <lsahlber@redhat.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
2022-08-08 13:41:18 -03:00
|
|
|
(long long)flock->fl_end);
|
2010-08-17 11:26:00 +04:00
|
|
|
|
|
|
|
cfile = (struct cifsFileInfo *)file->private_data;
|
|
|
|
tcon = tlink_tcon(cfile->tlink);
|
2012-02-28 14:23:34 +03:00
|
|
|
|
|
|
|
cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
|
|
|
|
tcon->ses->server);
|
2014-10-22 00:25:12 -04:00
|
|
|
cifs_sb = CIFS_FILE_SB(file);
|
2021-09-17 16:50:40 -05:00
|
|
|
set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
|
2010-08-17 11:26:00 +04:00
|
|
|
|
2012-07-13 13:58:14 +04:00
|
|
|
if (cap_unix(tcon->ses) &&
|
2010-08-17 11:26:00 +04:00
|
|
|
(CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
|
|
|
|
((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
|
|
|
|
posix_lck = true;
|
|
|
|
/*
|
|
|
|
* BB add code here to normalize offset and length to account for
|
|
|
|
* negative length which we can not accept over the wire.
|
|
|
|
*/
|
|
|
|
if (IS_GETLK(cmd)) {
|
2011-10-22 15:33:30 +04:00
|
|
|
rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2010-08-17 11:26:00 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lock && !unlock) {
|
|
|
|
/*
|
|
|
|
* if no lock or unlock then nothing to do since we do not
|
|
|
|
* know what it is
|
|
|
|
*/
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2010-08-17 11:26:00 +04:00
|
|
|
return -EOPNOTSUPP;
|
2006-08-02 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 11:26:00 +04:00
|
|
|
rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
|
|
|
|
xid);
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2025-05-19 10:07:03 +01:00
|
|
|
void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata, ssize_t result)
|
2009-04-03 13:44:00 -04:00
|
|
|
{
|
2023-10-06 18:29:59 +01:00
|
|
|
struct netfs_io_request *wreq = wdata->rreq;
|
2024-07-19 14:07:07 +01:00
|
|
|
struct netfs_inode *ictx = netfs_inode(wreq->inode);
|
|
|
|
loff_t wrend;
|
2009-04-03 13:44:00 -04:00
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
if (result > 0) {
|
2024-07-19 14:07:07 +01:00
|
|
|
wrend = wdata->subreq.start + wdata->subreq.transferred + result;
|
|
|
|
|
|
|
|
if (wrend > ictx->zero_point &&
|
|
|
|
(wdata->rreq->origin == NETFS_UNBUFFERED_WRITE ||
|
|
|
|
wdata->rreq->origin == NETFS_DIO_WRITE))
|
|
|
|
ictx->zero_point = wrend;
|
|
|
|
if (wrend > ictx->remote_i_size)
|
|
|
|
netfs_resize_file(ictx, wrend, true);
|
2023-10-06 18:29:59 +01:00
|
|
|
}
|
|
|
|
|
2025-05-19 10:07:03 +01:00
|
|
|
netfs_write_subrequest_terminated(&wdata->subreq, result);
|
2009-04-03 13:44:00 -04:00
|
|
|
}
|
|
|
|
|
2010-09-29 19:51:11 -04:00
|
|
|
struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
|
|
|
|
bool fsuid_only)
|
2007-10-25 21:17:17 +00:00
|
|
|
{
|
|
|
|
struct cifsFileInfo *open_file = NULL;
|
netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
While randstruct was satisfied with using an open-coded "void *" offset
cast for the netfs_i_context <-> inode casting, __builtin_object_size() as
used by FORTIFY_SOURCE was not as easily fooled. This was causing the
following complaint[1] from gcc v12:
In file included from include/linux/string.h:253,
from include/linux/ceph/ceph_debug.h:7,
from fs/ceph/inode.c:2:
In function 'fortify_memset_chk',
inlined from 'netfs_i_context_init' at include/linux/netfs.h:326:2,
inlined from 'ceph_alloc_inode' at fs/ceph/inode.c:463:2:
include/linux/fortify-string.h:242:25: warning: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]
242 | __write_overflow_field(p_size_field, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fix this by embedding a struct inode into struct netfs_i_context (which
should perhaps be renamed to struct netfs_inode). The struct inode
vfs_inode fields are then removed from the 9p, afs, ceph and cifs inode
structs and vfs_inode is then simply changed to "netfs.inode" in those
filesystems.
Further, rename netfs_i_context to netfs_inode, get rid of the
netfs_inode() function that converted a netfs_i_context pointer to an
inode pointer (that can now be done with &ctx->inode) and rename the
netfs_i_context() function to netfs_inode() (which is now a wrapper
around container_of()).
Most of the changes were done with:
perl -p -i -e 's/vfs_inode/netfs.inode/'g \
`git grep -l 'vfs_inode' -- fs/{9p,afs,ceph,cifs}/*.[ch]`
Kees suggested doing it with a pair structure[2] and a special
declarator to insert that into the network filesystem's inode
wrapper[3], but I think it's cleaner to embed it - and then it doesn't
matter if struct randomisation reorders things.
Dave Chinner suggested using a filesystem-specific VFS_I() function in
each filesystem to convert that filesystem's own inode wrapper struct
into the VFS inode struct[4].
Version #2:
- Fix a couple of missed name changes due to a disabled cifs option.
- Rename nfs_i_context to nfs_inode
- Use "netfs" instead of "nic" as the member name in per-fs inode wrapper
structs.
[ This also undoes commit 507160f46c55 ("netfs: gcc-12: temporarily
disable '-Wattribute-warning' for now") that is no longer needed ]
Fixes: bc899ee1c898 ("netfs: Add a netfs inode context")
Reported-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
cc: Jonathan Corbet <corbet@lwn.net>
cc: Eric Van Hensbergen <ericvh@gmail.com>
cc: Latchesar Ionkov <lucho@ionkov.net>
cc: Dominique Martinet <asmadeus@codewreck.org>
cc: Christian Schoenebeck <linux_oss@crudebyte.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Ilya Dryomov <idryomov@gmail.com>
cc: Steve French <smfrench@gmail.com>
cc: William Kucharski <william.kucharski@oracle.com>
cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
cc: Dave Chinner <david@fromorbit.com>
cc: linux-doc@vger.kernel.org
cc: v9fs-developer@lists.sourceforge.net
cc: linux-afs@lists.infradead.org
cc: ceph-devel@vger.kernel.org
cc: linux-cifs@vger.kernel.org
cc: samba-technical@lists.samba.org
cc: linux-fsdevel@vger.kernel.org
cc: linux-hardening@vger.kernel.org
Link: https://lore.kernel.org/r/d2ad3a3d7bdd794c6efb562d2f2b655fb67756b9.camel@kernel.org/ [1]
Link: https://lore.kernel.org/r/20220517210230.864239-1-keescook@chromium.org/ [2]
Link: https://lore.kernel.org/r/20220518202212.2322058-1-keescook@chromium.org/ [3]
Link: https://lore.kernel.org/r/20220524101205.GI2306852@dread.disaster.area/ [4]
Link: https://lore.kernel.org/r/165296786831.3591209.12111293034669289733.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/165305805651.4094995.7763502506786714216.stgit@warthog.procyon.org.uk # v2
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2022-06-09 21:46:04 +01:00
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
|
2010-09-29 19:51:11 -04:00
|
|
|
|
|
|
|
/* only filter by fsuid on multiuser mounts */
|
|
|
|
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
|
|
|
|
fsuid_only = false;
|
2007-10-25 21:17:17 +00:00
|
|
|
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_lock(&cifs_inode->open_file_lock);
|
2007-10-25 21:17:17 +00:00
|
|
|
/* we could simply get the first_list_entry since write-only entries
|
|
|
|
are always at the end of the list but since the first entry might
|
|
|
|
have a close pending, we go through the whole list */
|
|
|
|
list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
|
2013-02-06 02:23:02 -08:00
|
|
|
if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
|
2010-09-29 19:51:11 -04:00
|
|
|
continue;
|
2010-10-15 15:34:01 -04:00
|
|
|
if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
|
2021-05-05 10:56:47 +00:00
|
|
|
if ((!open_file->invalidHandle)) {
|
2007-10-25 21:17:17 +00:00
|
|
|
/* found a good file */
|
|
|
|
/* lock it so it will not be closed on us */
|
2016-09-22 18:58:16 -05:00
|
|
|
cifsFileInfo_get(open_file);
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2007-10-25 21:17:17 +00:00
|
|
|
return open_file;
|
|
|
|
} /* else might as well continue, and look for
|
|
|
|
another, or simply have the caller reopen it
|
|
|
|
again rather than trying to fix this handle */
|
|
|
|
} else /* write only file */
|
|
|
|
break; /* write only files are last so must be done */
|
|
|
|
}
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2007-10-25 21:17:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-29 12:15:11 -08:00
|
|
|
/* Return -EBADF if no handle is found and general rc otherwise */
|
|
|
|
int
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
|
2019-01-29 12:15:11 -08:00
|
|
|
struct cifsFileInfo **ret_file)
|
2005-10-05 12:23:19 -07:00
|
|
|
{
|
2012-05-21 09:20:12 -05:00
|
|
|
struct cifsFileInfo *open_file, *inv_file = NULL;
|
2010-11-02 16:22:50 -04:00
|
|
|
struct cifs_sb_info *cifs_sb;
|
2008-09-22 21:33:33 -04:00
|
|
|
bool any_available = false;
|
2019-01-29 12:15:11 -08:00
|
|
|
int rc = -EBADF;
|
2012-05-21 09:20:12 -05:00
|
|
|
unsigned int refind = 0;
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
|
|
|
|
bool with_delete = flags & FIND_WR_WITH_DELETE;
|
2019-01-29 12:15:11 -08:00
|
|
|
*ret_file = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Having a null inode here (because mapping->host was set to zero by
|
|
|
|
* the VFS or MM) should not happen but we had reports of on oops (due
|
|
|
|
* to it being zero) during stress testcases so we need to check for it
|
|
|
|
*/
|
2006-04-22 15:53:05 +00:00
|
|
|
|
2007-07-10 01:16:18 +00:00
|
|
|
if (cifs_inode == NULL) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
|
2006-04-22 15:53:05 +00:00
|
|
|
dump_stack();
|
2019-01-29 12:15:11 -08:00
|
|
|
return rc;
|
2006-04-22 15:53:05 +00:00
|
|
|
}
|
|
|
|
|
netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
While randstruct was satisfied with using an open-coded "void *" offset
cast for the netfs_i_context <-> inode casting, __builtin_object_size() as
used by FORTIFY_SOURCE was not as easily fooled. This was causing the
following complaint[1] from gcc v12:
In file included from include/linux/string.h:253,
from include/linux/ceph/ceph_debug.h:7,
from fs/ceph/inode.c:2:
In function 'fortify_memset_chk',
inlined from 'netfs_i_context_init' at include/linux/netfs.h:326:2,
inlined from 'ceph_alloc_inode' at fs/ceph/inode.c:463:2:
include/linux/fortify-string.h:242:25: warning: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]
242 | __write_overflow_field(p_size_field, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fix this by embedding a struct inode into struct netfs_i_context (which
should perhaps be renamed to struct netfs_inode). The struct inode
vfs_inode fields are then removed from the 9p, afs, ceph and cifs inode
structs and vfs_inode is then simply changed to "netfs.inode" in those
filesystems.
Further, rename netfs_i_context to netfs_inode, get rid of the
netfs_inode() function that converted a netfs_i_context pointer to an
inode pointer (that can now be done with &ctx->inode) and rename the
netfs_i_context() function to netfs_inode() (which is now a wrapper
around container_of()).
Most of the changes were done with:
perl -p -i -e 's/vfs_inode/netfs.inode/'g \
`git grep -l 'vfs_inode' -- fs/{9p,afs,ceph,cifs}/*.[ch]`
Kees suggested doing it with a pair structure[2] and a special
declarator to insert that into the network filesystem's inode
wrapper[3], but I think it's cleaner to embed it - and then it doesn't
matter if struct randomisation reorders things.
Dave Chinner suggested using a filesystem-specific VFS_I() function in
each filesystem to convert that filesystem's own inode wrapper struct
into the VFS inode struct[4].
Version #2:
- Fix a couple of missed name changes due to a disabled cifs option.
- Rename nfs_i_context to nfs_inode
- Use "netfs" instead of "nic" as the member name in per-fs inode wrapper
structs.
[ This also undoes commit 507160f46c55 ("netfs: gcc-12: temporarily
disable '-Wattribute-warning' for now") that is no longer needed ]
Fixes: bc899ee1c898 ("netfs: Add a netfs inode context")
Reported-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
cc: Jonathan Corbet <corbet@lwn.net>
cc: Eric Van Hensbergen <ericvh@gmail.com>
cc: Latchesar Ionkov <lucho@ionkov.net>
cc: Dominique Martinet <asmadeus@codewreck.org>
cc: Christian Schoenebeck <linux_oss@crudebyte.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Ilya Dryomov <idryomov@gmail.com>
cc: Steve French <smfrench@gmail.com>
cc: William Kucharski <william.kucharski@oracle.com>
cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
cc: Dave Chinner <david@fromorbit.com>
cc: linux-doc@vger.kernel.org
cc: v9fs-developer@lists.sourceforge.net
cc: linux-afs@lists.infradead.org
cc: ceph-devel@vger.kernel.org
cc: linux-cifs@vger.kernel.org
cc: samba-technical@lists.samba.org
cc: linux-fsdevel@vger.kernel.org
cc: linux-hardening@vger.kernel.org
Link: https://lore.kernel.org/r/d2ad3a3d7bdd794c6efb562d2f2b655fb67756b9.camel@kernel.org/ [1]
Link: https://lore.kernel.org/r/20220517210230.864239-1-keescook@chromium.org/ [2]
Link: https://lore.kernel.org/r/20220518202212.2322058-1-keescook@chromium.org/ [3]
Link: https://lore.kernel.org/r/20220524101205.GI2306852@dread.disaster.area/ [4]
Link: https://lore.kernel.org/r/165296786831.3591209.12111293034669289733.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/165305805651.4094995.7763502506786714216.stgit@warthog.procyon.org.uk # v2
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2022-06-09 21:46:04 +01:00
|
|
|
cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
|
2010-11-02 16:22:50 -04:00
|
|
|
|
2010-09-29 19:51:11 -04:00
|
|
|
/* only filter by fsuid on multiuser mounts */
|
|
|
|
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
|
|
|
|
fsuid_only = false;
|
|
|
|
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_lock(&cifs_inode->open_file_lock);
|
2007-10-02 01:11:08 +00:00
|
|
|
refind_writable:
|
2012-05-21 09:20:12 -05:00
|
|
|
if (refind > MAX_REOPEN_ATT) {
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2019-01-29 12:15:11 -08:00
|
|
|
return rc;
|
2012-05-21 09:20:12 -05:00
|
|
|
}
|
2005-10-05 12:23:19 -07:00
|
|
|
list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
|
2010-09-29 19:51:11 -04:00
|
|
|
if (!any_available && open_file->pid != current->tgid)
|
|
|
|
continue;
|
2013-02-06 02:23:02 -08:00
|
|
|
if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
|
2005-10-05 12:23:19 -07:00
|
|
|
continue;
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
if (with_delete && !(open_file->fid.access & DELETE))
|
|
|
|
continue;
|
2010-10-15 15:34:01 -04:00
|
|
|
if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
|
2007-10-02 01:11:08 +00:00
|
|
|
if (!open_file->invalidHandle) {
|
|
|
|
/* found a good writable file */
|
2016-09-22 18:58:16 -05:00
|
|
|
cifsFileInfo_get(open_file);
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2019-01-29 12:15:11 -08:00
|
|
|
*ret_file = open_file;
|
|
|
|
return 0;
|
2012-05-21 09:20:12 -05:00
|
|
|
} else {
|
|
|
|
if (!inv_file)
|
|
|
|
inv_file = open_file;
|
2007-10-02 01:11:08 +00:00
|
|
|
}
|
2005-10-05 12:23:19 -07:00
|
|
|
}
|
|
|
|
}
|
2024-09-25 14:55:43 +08:00
|
|
|
/* couldn't find usable FH with same pid, try any available */
|
2008-09-22 21:33:33 -04:00
|
|
|
if (!any_available) {
|
|
|
|
any_available = true;
|
|
|
|
goto refind_writable;
|
|
|
|
}
|
2012-05-21 09:20:12 -05:00
|
|
|
|
|
|
|
if (inv_file) {
|
|
|
|
any_available = false;
|
2016-09-22 18:58:16 -05:00
|
|
|
cifsFileInfo_get(inv_file);
|
2012-05-21 09:20:12 -05:00
|
|
|
}
|
|
|
|
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2012-05-21 09:20:12 -05:00
|
|
|
|
|
|
|
if (inv_file) {
|
|
|
|
rc = cifs_reopen_file(inv_file, false);
|
2019-01-29 12:15:11 -08:00
|
|
|
if (!rc) {
|
|
|
|
*ret_file = inv_file;
|
|
|
|
return 0;
|
2012-05-21 09:20:12 -05:00
|
|
|
}
|
2019-01-29 12:15:11 -08:00
|
|
|
|
2019-06-05 10:38:38 +10:00
|
|
|
spin_lock(&cifs_inode->open_file_lock);
|
2019-01-29 12:15:11 -08:00
|
|
|
list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
|
2019-06-05 10:38:38 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2019-01-29 12:15:11 -08:00
|
|
|
cifsFileInfo_put(inv_file);
|
|
|
|
++refind;
|
|
|
|
inv_file = NULL;
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_lock(&cifs_inode->open_file_lock);
|
2019-01-29 12:15:11 -08:00
|
|
|
goto refind_writable;
|
2012-05-21 09:20:12 -05:00
|
|
|
}
|
|
|
|
|
2019-01-29 12:15:11 -08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cifsFileInfo *
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
|
2019-01-29 12:15:11 -08:00
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile;
|
|
|
|
int rc;
|
|
|
|
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
|
2019-01-29 12:15:11 -08:00
|
|
|
if (rc)
|
2020-04-14 22:42:53 -07:00
|
|
|
cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
|
2019-01-29 12:15:11 -08:00
|
|
|
|
|
|
|
return cfile;
|
2005-10-05 12:23:19 -07:00
|
|
|
}
|
|
|
|
|
2019-08-30 08:25:46 +10:00
|
|
|
int
|
|
|
|
cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
int flags,
|
2019-08-30 08:25:46 +10:00
|
|
|
struct cifsFileInfo **ret_file)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile;
|
2021-03-05 17:36:04 -05:00
|
|
|
void *page = alloc_dentry_path();
|
2019-08-30 08:25:46 +10:00
|
|
|
|
|
|
|
*ret_file = NULL;
|
|
|
|
|
|
|
|
spin_lock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
list_for_each_entry(cfile, &tcon->openFileList, tlist) {
|
|
|
|
struct cifsInodeInfo *cinode;
|
|
|
|
const char *full_path = build_path_from_dentry(cfile->dentry, page);
|
|
|
|
if (IS_ERR(full_path)) {
|
2019-08-30 08:25:46 +10:00
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
|
|
|
return PTR_ERR(full_path);
|
2019-08-30 08:25:46 +10:00
|
|
|
}
|
2021-03-05 17:36:04 -05:00
|
|
|
if (strcmp(full_path, name))
|
2019-08-30 08:25:46 +10:00
|
|
|
continue;
|
|
|
|
|
|
|
|
cinode = CIFS_I(d_inode(cfile->dentry));
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
cifs: fix rename() by ensuring source handle opened with DELETE bit
To rename a file in SMB2 we open it with the DELETE access and do a
special SetInfo on it. If the handle is missing the DELETE bit the
server will fail the SetInfo with STATUS_ACCESS_DENIED.
We currently try to reuse any existing opened handle we have with
cifs_get_writable_path(). That function looks for handles with WRITE
access but doesn't check for DELETE, making rename() fail if it finds
a handle to reuse. Simple reproducer below.
To select handles with the DELETE bit, this patch adds a flag argument
to cifs_get_writable_path() and find_writable_file() and the existing
'bool fsuid_only' argument is converted to a flag.
The cifsFileInfo struct only stores the UNIX open mode but not the
original SMB access flags. Since the DELETE bit is not mapped in that
mode, this patch stores the access mask in cifs_fid on file open,
which is accessible from cifsFileInfo.
Simple reproducer:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define E(s) perror(s), exit(1)
int main(int argc, char *argv[])
{
int fd, ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s A B\n"
"create&open A in write mode, "
"rename A to B, close A\n", argv[0]);
return 0;
}
fd = openat(AT_FDCWD, argv[1], O_WRONLY|O_CREAT|O_SYNC, 0666);
if (fd == -1) E("openat()");
ret = rename(argv[1], argv[2]);
if (ret) E("rename()");
ret = close(fd);
if (ret) E("close()");
return ret;
}
$ gcc -o bugrename bugrename.c
$ ./bugrename /mnt/a /mnt/b
rename(): Permission denied
Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name")
CC: Stable <stable@vger.kernel.org>
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Reviewed-by: Pavel Shilovsky <pshilov@microsoft.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
2020-02-21 11:19:06 +01:00
|
|
|
return cifs_get_writable_file(cinode, flags, ret_file);
|
2019-08-30 08:25:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
2019-08-30 08:25:46 +10:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:30:00 +10:00
|
|
|
int
|
|
|
|
cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
|
|
|
|
struct cifsFileInfo **ret_file)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile;
|
2021-03-05 17:36:04 -05:00
|
|
|
void *page = alloc_dentry_path();
|
2019-09-09 15:30:00 +10:00
|
|
|
|
|
|
|
*ret_file = NULL;
|
|
|
|
|
|
|
|
spin_lock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
list_for_each_entry(cfile, &tcon->openFileList, tlist) {
|
|
|
|
struct cifsInodeInfo *cinode;
|
|
|
|
const char *full_path = build_path_from_dentry(cfile->dentry, page);
|
|
|
|
if (IS_ERR(full_path)) {
|
2019-09-09 15:30:00 +10:00
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
|
|
|
return PTR_ERR(full_path);
|
2019-09-09 15:30:00 +10:00
|
|
|
}
|
2021-03-05 17:36:04 -05:00
|
|
|
if (strcmp(full_path, name))
|
2019-09-09 15:30:00 +10:00
|
|
|
continue;
|
|
|
|
|
|
|
|
cinode = CIFS_I(d_inode(cfile->dentry));
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
2019-09-09 15:30:00 +10:00
|
|
|
*ret_file = find_readable_file(cinode, 0);
|
|
|
|
return *ret_file ? 0 : -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&tcon->open_file_lock);
|
2021-03-05 17:36:04 -05:00
|
|
|
free_dentry_path(page);
|
2019-09-09 15:30:00 +10:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
/*
|
|
|
|
* Flush data on a strict file.
|
|
|
|
*/
|
2011-07-16 20:44:56 -04:00
|
|
|
int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
|
|
|
|
int datasync)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
2005-04-16 15:20:36 -07:00
|
|
|
int rc = 0;
|
2011-05-27 04:34:02 +00:00
|
|
|
struct cifs_tcon *tcon;
|
2012-09-18 16:20:27 -07:00
|
|
|
struct TCP_Server_Info *server;
|
2010-07-12 13:50:14 -07:00
|
|
|
struct cifsFileInfo *smbfile = file->private_data;
|
2013-01-23 17:07:38 -05:00
|
|
|
struct inode *inode = file_inode(file);
|
2010-12-12 13:11:13 +03:00
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2017-07-07 15:20:52 -04:00
|
|
|
rc = file_write_and_wait_range(file, start, end);
|
2020-02-06 16:04:59 -06:00
|
|
|
if (rc) {
|
|
|
|
trace_cifs_fsync_err(inode->i_ino, rc);
|
2011-07-16 20:44:56 -04:00
|
|
|
return rc;
|
2020-02-06 16:04:59 -06:00
|
|
|
}
|
2011-07-16 20:44:56 -04:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2014-08-19 20:25:34 -04:00
|
|
|
cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
|
|
|
|
file, datasync);
|
2007-07-13 00:33:32 +00:00
|
|
|
|
2013-09-05 13:01:06 +04:00
|
|
|
if (!CIFS_CACHE_READ(CIFS_I(inode))) {
|
2014-04-30 09:31:47 -04:00
|
|
|
rc = cifs_zap_mapping(inode);
|
2011-04-07 18:18:11 +04:00
|
|
|
if (rc) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
|
2011-04-07 18:18:11 +04:00
|
|
|
rc = 0; /* don't care about it in fsync */
|
|
|
|
}
|
|
|
|
}
|
2010-10-22 14:52:29 -04:00
|
|
|
|
2010-12-12 13:11:13 +03:00
|
|
|
tcon = tlink_tcon(smbfile->tlink);
|
2012-09-18 16:20:27 -07:00
|
|
|
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
|
|
|
|
server = tcon->ses->server;
|
2021-11-10 01:47:48 -06:00
|
|
|
if (server->ops->flush == NULL) {
|
2012-09-18 16:20:27 -07:00
|
|
|
rc = -ENOSYS;
|
2021-11-10 01:47:48 -06:00
|
|
|
goto strict_fsync_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
|
|
|
|
smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
|
|
|
|
if (smbfile) {
|
|
|
|
rc = server->ops->flush(xid, tcon, &smbfile->fid);
|
|
|
|
cifsFileInfo_put(smbfile);
|
|
|
|
} else
|
|
|
|
cifs_dbg(FYI, "ignore fsync for file not open for write\n");
|
|
|
|
} else
|
|
|
|
rc = server->ops->flush(xid, tcon, &smbfile->fid);
|
2012-09-18 16:20:27 -07:00
|
|
|
}
|
2010-12-12 13:11:13 +03:00
|
|
|
|
2021-11-10 01:47:48 -06:00
|
|
|
strict_fsync_exit:
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2010-12-12 13:11:13 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
/*
|
|
|
|
* Flush data on a non-strict data.
|
|
|
|
*/
|
2011-07-16 20:44:56 -04:00
|
|
|
int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
|
2010-12-12 13:11:13 +03:00
|
|
|
{
|
2012-06-20 11:21:16 +04:00
|
|
|
unsigned int xid;
|
2010-12-12 13:11:13 +03:00
|
|
|
int rc = 0;
|
2011-05-27 04:34:02 +00:00
|
|
|
struct cifs_tcon *tcon;
|
2012-09-18 16:20:27 -07:00
|
|
|
struct TCP_Server_Info *server;
|
2010-12-12 13:11:13 +03:00
|
|
|
struct cifsFileInfo *smbfile = file->private_data;
|
2021-11-10 01:47:48 -06:00
|
|
|
struct inode *inode = file_inode(file);
|
2014-10-22 00:25:12 -04:00
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
|
2011-07-16 20:44:56 -04:00
|
|
|
|
2017-07-07 15:20:52 -04:00
|
|
|
rc = file_write_and_wait_range(file, start, end);
|
2020-02-05 18:22:37 -06:00
|
|
|
if (rc) {
|
|
|
|
trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
|
2011-07-16 20:44:56 -04:00
|
|
|
return rc;
|
2020-02-05 18:22:37 -06:00
|
|
|
}
|
2010-12-12 13:11:13 +03:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2010-12-12 13:11:13 +03:00
|
|
|
|
2014-08-19 20:25:34 -04:00
|
|
|
cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
|
|
|
|
file, datasync);
|
2010-12-12 13:11:13 +03:00
|
|
|
|
|
|
|
tcon = tlink_tcon(smbfile->tlink);
|
2012-09-18 16:20:27 -07:00
|
|
|
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
|
|
|
|
server = tcon->ses->server;
|
2021-11-10 01:47:48 -06:00
|
|
|
if (server->ops->flush == NULL) {
|
2012-09-18 16:20:27 -07:00
|
|
|
rc = -ENOSYS;
|
2021-11-10 01:47:48 -06:00
|
|
|
goto fsync_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
|
|
|
|
smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
|
|
|
|
if (smbfile) {
|
|
|
|
rc = server->ops->flush(xid, tcon, &smbfile->fid);
|
|
|
|
cifsFileInfo_put(smbfile);
|
|
|
|
} else
|
|
|
|
cifs_dbg(FYI, "ignore fsync for file not open for write\n");
|
|
|
|
} else
|
|
|
|
rc = server->ops->flush(xid, tcon, &smbfile->fid);
|
2012-09-18 16:20:27 -07:00
|
|
|
}
|
2009-02-21 21:17:43 +00:00
|
|
|
|
2021-11-10 01:47:48 -06:00
|
|
|
fsync_exit:
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As file closes, flush all cached write data for this inode checking
|
|
|
|
* for write behind errors.
|
|
|
|
*/
|
2006-06-23 02:05:12 -07:00
|
|
|
int cifs_flush(struct file *file, fl_owner_t id)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2013-01-23 17:07:38 -05:00
|
|
|
struct inode *inode = file_inode(file);
|
2005-04-16 15:20:36 -07:00
|
|
|
int rc = 0;
|
|
|
|
|
2010-10-22 14:52:29 -04:00
|
|
|
if (file->f_mode & FMODE_WRITE)
|
2010-10-15 15:34:07 -04:00
|
|
|
rc = filemap_write_and_wait(inode->i_mapping);
|
2007-07-13 00:33:32 +00:00
|
|
|
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
|
2022-05-18 22:56:49 +08:00
|
|
|
if (rc) {
|
|
|
|
/* get more nuanced writeback errors */
|
|
|
|
rc = filemap_check_wb_err(file->f_mapping, 0);
|
2020-02-05 18:22:37 -06:00
|
|
|
trace_cifs_flush_err(inode->i_ino, rc);
|
2022-05-18 22:56:49 +08:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
static ssize_t
|
2014-04-03 12:05:17 -04:00
|
|
|
cifs_writev(struct kiocb *iocb, struct iov_iter *from)
|
2011-01-24 14:16:35 -05:00
|
|
|
{
|
2012-09-19 06:22:44 -07:00
|
|
|
struct file *file = iocb->ki_filp;
|
|
|
|
struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
|
|
|
|
struct inode *inode = file->f_mapping->host;
|
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
|
2024-08-15 14:03:43 -05:00
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
|
2015-04-07 11:28:12 -04:00
|
|
|
ssize_t rc;
|
2011-01-24 14:16:35 -05:00
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
rc = netfs_start_io_write(inode);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
/*
|
|
|
|
* We need to hold the sem to be sure nobody modifies lock list
|
|
|
|
* with a brlock that prevents writing.
|
|
|
|
*/
|
|
|
|
down_read(&cinode->lock_sem);
|
2015-04-07 11:28:12 -04:00
|
|
|
|
2015-04-09 12:55:47 -04:00
|
|
|
rc = generic_write_checks(iocb, from);
|
|
|
|
if (rc <= 0)
|
2015-04-07 11:28:12 -04:00
|
|
|
goto out;
|
|
|
|
|
2024-08-15 14:03:43 -05:00
|
|
|
if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) &&
|
|
|
|
(cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
|
2018-10-04 09:24:38 +10:00
|
|
|
server->vals->exclusive_lock_type, 0,
|
2024-08-15 14:03:43 -05:00
|
|
|
NULL, CIFS_WRITE_OP))) {
|
2015-04-07 11:28:12 -04:00
|
|
|
rc = -EACCES;
|
2024-08-15 14:03:43 -05:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = netfs_buffered_write_iter_locked(iocb, from, NULL);
|
|
|
|
|
2015-04-07 11:28:12 -04:00
|
|
|
out:
|
CIFS: fix circular locking dependency
When a CIFS filesystem is mounted with the forcemand option and the
following command is run on it, lockdep warns about a circular locking
dependency between CifsInodeInfo::lock_sem and the inode lock.
while echo foo > hello; do :; done & while touch -c hello; do :; done
cifs_writev() takes the locks in the wrong order, but note that we can't
only flip the order around because it releases the inode lock before the
call to generic_write_sync() while it holds the lock_sem across that
call.
But, AFAICS, there is no need to hold the CifsInodeInfo::lock_sem across
the generic_write_sync() call either, so we can release both the locks
before generic_write_sync(), and change the order.
======================================================
WARNING: possible circular locking dependency detected
4.12.0-rc7+ #9 Not tainted
------------------------------------------------------
touch/487 is trying to acquire lock:
(&cifsi->lock_sem){++++..}, at: cifsFileInfo_put+0x88f/0x16a0
but task is already holding lock:
(&sb->s_type->i_mutex_key#11){+.+.+.}, at: utimes_common+0x3ad/0x870
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&sb->s_type->i_mutex_key#11){+.+.+.}:
__lock_acquire+0x1f74/0x38f0
lock_acquire+0x1cc/0x600
down_write+0x74/0x110
cifs_strict_writev+0x3cb/0x8c0
__vfs_write+0x4c1/0x930
vfs_write+0x14c/0x2d0
SyS_write+0xf7/0x240
entry_SYSCALL_64_fastpath+0x1f/0xbe
-> #0 (&cifsi->lock_sem){++++..}:
check_prevs_add+0xfa0/0x1d10
__lock_acquire+0x1f74/0x38f0
lock_acquire+0x1cc/0x600
down_write+0x74/0x110
cifsFileInfo_put+0x88f/0x16a0
cifs_setattr+0x992/0x1680
notify_change+0x61a/0xa80
utimes_common+0x3d4/0x870
do_utimes+0x1c1/0x220
SyS_utimensat+0x84/0x1a0
entry_SYSCALL_64_fastpath+0x1f/0xbe
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&sb->s_type->i_mutex_key#11);
lock(&cifsi->lock_sem);
lock(&sb->s_type->i_mutex_key#11);
lock(&cifsi->lock_sem);
*** DEADLOCK ***
2 locks held by touch/487:
#0: (sb_writers#10){.+.+.+}, at: mnt_want_write+0x41/0xb0
#1: (&sb->s_type->i_mutex_key#11){+.+.+.}, at: utimes_common+0x3ad/0x870
stack backtrace:
CPU: 0 PID: 487 Comm: touch Not tainted 4.12.0-rc7+ #9
Call Trace:
dump_stack+0xdb/0x185
print_circular_bug+0x45b/0x790
__lock_acquire+0x1f74/0x38f0
lock_acquire+0x1cc/0x600
down_write+0x74/0x110
cifsFileInfo_put+0x88f/0x16a0
cifs_setattr+0x992/0x1680
notify_change+0x61a/0xa80
utimes_common+0x3d4/0x870
do_utimes+0x1c1/0x220
SyS_utimensat+0x84/0x1a0
entry_SYSCALL_64_fastpath+0x1f/0xbe
Fixes: 19dfc1f5f2ef03a52 ("cifs: fix the race in cifs_writev()")
Signed-off-by: Rabin Vincent <rabinv@axis.com>
Signed-off-by: Steve French <smfrench@gmail.com>
Acked-by: Pavel Shilovsky <pshilov@microsoft.com>
2017-06-29 16:01:42 +02:00
|
|
|
up_read(&cinode->lock_sem);
|
2023-10-06 18:29:59 +01:00
|
|
|
netfs_end_io_write(inode);
|
2016-04-07 08:52:01 -07:00
|
|
|
if (rc > 0)
|
|
|
|
rc = generic_write_sync(iocb, rc);
|
2012-09-19 06:22:44 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2014-04-03 12:05:17 -04:00
|
|
|
cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
|
2012-09-19 06:22:44 -07:00
|
|
|
{
|
2013-01-23 17:07:38 -05:00
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
2012-09-19 06:22:44 -07:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
|
|
|
|
struct cifsFileInfo *cfile = (struct cifsFileInfo *)
|
|
|
|
iocb->ki_filp->private_data;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
2012-12-21 15:07:52 +04:00
|
|
|
ssize_t written;
|
2012-12-21 15:05:47 +04:00
|
|
|
|
2014-03-11 16:11:47 +00:00
|
|
|
written = cifs_get_writer(cinode);
|
|
|
|
if (written)
|
|
|
|
return written;
|
|
|
|
|
2013-09-05 13:01:06 +04:00
|
|
|
if (CIFS_CACHE_WRITE(cinode)) {
|
2012-12-21 15:07:52 +04:00
|
|
|
if (cap_unix(tcon->ses) &&
|
2023-10-06 18:29:59 +01:00
|
|
|
(CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
|
|
|
|
((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
|
|
|
|
written = netfs_file_write_iter(iocb, from);
|
2014-03-11 16:11:47 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-04-03 12:05:17 -04:00
|
|
|
written = cifs_writev(iocb, from);
|
2014-03-11 16:11:47 +00:00
|
|
|
goto out;
|
2012-09-19 06:22:45 -07:00
|
|
|
}
|
|
|
|
/*
|
2012-12-21 15:05:47 +04:00
|
|
|
* For non-oplocked files in strict cache mode we need to write the data
|
|
|
|
* to the server exactly from the pos to pos+len-1 rather than flush all
|
|
|
|
* affected pages because it may cause a error with mandatory locks on
|
|
|
|
* these pages but not on the region from pos to ppos+len-1.
|
2011-01-24 14:16:35 -05:00
|
|
|
*/
|
2023-10-06 18:29:59 +01:00
|
|
|
written = netfs_file_write_iter(iocb, from);
|
2019-03-04 17:48:01 -08:00
|
|
|
if (CIFS_CACHE_READ(cinode)) {
|
2012-12-21 15:07:52 +04:00
|
|
|
/*
|
2019-03-04 17:48:01 -08:00
|
|
|
* We have read level caching and we have just sent a write
|
|
|
|
* request to the server thus making data in the cache stale.
|
|
|
|
* Zap the cache and set oplock/lease level to NONE to avoid
|
|
|
|
* reading stale data from the cache. All subsequent read
|
|
|
|
* operations will read new data from the server.
|
2012-12-21 15:07:52 +04:00
|
|
|
*/
|
2014-04-30 09:31:47 -04:00
|
|
|
cifs_zap_mapping(inode);
|
2019-03-04 17:48:01 -08:00
|
|
|
cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
|
2013-05-04 22:12:25 -05:00
|
|
|
inode);
|
2013-09-05 13:01:06 +04:00
|
|
|
cinode->oplock = 0;
|
2012-12-21 15:07:52 +04:00
|
|
|
}
|
2014-03-11 16:11:47 +00:00
|
|
|
out:
|
|
|
|
cifs_put_writer(cinode);
|
2012-12-21 15:07:52 +04:00
|
|
|
return written;
|
2011-01-24 14:16:35 -05:00
|
|
|
}
|
|
|
|
|
2023-10-12 08:36:44 +01:00
|
|
|
ssize_t cifs_loose_read_iter(struct kiocb *iocb, struct iov_iter *iter)
|
|
|
|
{
|
|
|
|
ssize_t rc;
|
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
|
|
|
|
|
|
|
if (iocb->ki_flags & IOCB_DIRECT)
|
2023-10-06 18:29:59 +01:00
|
|
|
return netfs_unbuffered_read_iter(iocb, iter);
|
2023-10-12 08:36:44 +01:00
|
|
|
|
|
|
|
rc = cifs_revalidate_mapping(inode);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
return netfs_file_read_iter(iocb, iter);
|
2023-10-12 08:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t cifs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
|
|
|
|
{
|
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
ssize_t written;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (iocb->ki_filp->f_flags & O_DIRECT) {
|
2023-10-06 18:29:59 +01:00
|
|
|
written = netfs_unbuffered_write_iter(iocb, from);
|
2023-10-12 08:36:44 +01:00
|
|
|
if (written > 0 && CIFS_CACHE_READ(cinode)) {
|
|
|
|
cifs_zap_mapping(inode);
|
|
|
|
cifs_dbg(FYI,
|
|
|
|
"Set no oplock for inode=%p after a write operation\n",
|
|
|
|
inode);
|
|
|
|
cinode->oplock = 0;
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
written = cifs_get_writer(cinode);
|
|
|
|
if (written)
|
|
|
|
return written;
|
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
written = netfs_file_write_iter(iocb, from);
|
2023-10-12 08:36:44 +01:00
|
|
|
|
2023-10-06 18:29:59 +01:00
|
|
|
if (!CIFS_CACHE_WRITE(CIFS_I(inode))) {
|
|
|
|
rc = filemap_fdatawrite(inode->i_mapping);
|
|
|
|
if (rc)
|
|
|
|
cifs_dbg(FYI, "cifs_file_write_iter: %d rc on %p inode\n",
|
|
|
|
rc, inode);
|
|
|
|
}
|
2023-10-12 08:36:44 +01:00
|
|
|
|
|
|
|
cifs_put_writer(cinode);
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2012-09-19 06:22:44 -07:00
|
|
|
ssize_t
|
2014-04-02 19:53:36 -04:00
|
|
|
cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
|
2010-12-14 11:50:41 +03:00
|
|
|
{
|
2013-01-23 17:07:38 -05:00
|
|
|
struct inode *inode = file_inode(iocb->ki_filp);
|
2012-09-19 06:22:44 -07:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
|
|
|
|
struct cifsFileInfo *cfile = (struct cifsFileInfo *)
|
|
|
|
iocb->ki_filp->private_data;
|
|
|
|
struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
|
|
|
|
int rc = -EACCES;
|
2010-12-14 11:50:41 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In strict cache mode we need to read from the server all the time
|
|
|
|
* if we don't have level II oplock because the server can delay mtime
|
|
|
|
* change - so we can't make a decision about inode invalidating.
|
|
|
|
* And we can also fail with pagereading if there are mandatory locks
|
|
|
|
* on pages affected by this read but not on the region from pos to
|
|
|
|
* pos+len-1.
|
|
|
|
*/
|
2013-09-05 13:01:06 +04:00
|
|
|
if (!CIFS_CACHE_READ(cinode))
|
2023-10-06 18:29:59 +01:00
|
|
|
return netfs_unbuffered_read_iter(iocb, to);
|
2010-12-14 11:50:41 +03:00
|
|
|
|
2024-08-15 18:31:36 -05:00
|
|
|
if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0) {
|
2023-10-06 18:29:59 +01:00
|
|
|
if (iocb->ki_flags & IOCB_DIRECT)
|
|
|
|
return netfs_unbuffered_read_iter(iocb, to);
|
|
|
|
return netfs_buffered_read_iter(iocb, to);
|
|
|
|
}
|
2012-09-19 06:22:44 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to hold the sem to be sure nobody modifies lock list
|
|
|
|
* with a brlock that prevents reading.
|
|
|
|
*/
|
2024-05-13 17:02:05 -05:00
|
|
|
if (iocb->ki_flags & IOCB_DIRECT) {
|
|
|
|
rc = netfs_start_io_direct(inode);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out;
|
2024-07-18 20:55:20 +01:00
|
|
|
rc = -EACCES;
|
2024-05-13 17:02:05 -05:00
|
|
|
down_read(&cinode->lock_sem);
|
|
|
|
if (!cifs_find_lock_conflict(
|
|
|
|
cfile, iocb->ki_pos, iov_iter_count(to),
|
|
|
|
tcon->ses->server->vals->shared_lock_type,
|
|
|
|
0, NULL, CIFS_READ_OP))
|
|
|
|
rc = netfs_unbuffered_read_iter_locked(iocb, to);
|
|
|
|
up_read(&cinode->lock_sem);
|
|
|
|
netfs_end_io_direct(inode);
|
|
|
|
} else {
|
|
|
|
rc = netfs_start_io_read(inode);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out;
|
2024-07-18 20:55:20 +01:00
|
|
|
rc = -EACCES;
|
2024-05-13 17:02:05 -05:00
|
|
|
down_read(&cinode->lock_sem);
|
|
|
|
if (!cifs_find_lock_conflict(
|
|
|
|
cfile, iocb->ki_pos, iov_iter_count(to),
|
|
|
|
tcon->ses->server->vals->shared_lock_type,
|
|
|
|
0, NULL, CIFS_READ_OP))
|
|
|
|
rc = filemap_read(iocb, to, 0);
|
|
|
|
up_read(&cinode->lock_sem);
|
|
|
|
netfs_end_io_read(inode);
|
2023-10-06 18:29:59 +01:00
|
|
|
}
|
2024-05-13 17:02:05 -05:00
|
|
|
out:
|
2012-09-19 06:22:44 -07:00
|
|
|
return rc;
|
2010-12-14 11:50:41 +03:00
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2023-02-08 14:56:10 +00:00
|
|
|
static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf)
|
2011-04-12 09:13:44 -04:00
|
|
|
{
|
2023-10-06 18:29:59 +01:00
|
|
|
return netfs_page_mkwrite(vmf, NULL);
|
2011-04-12 09:13:44 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 15:39:26 -07:00
|
|
|
static const struct vm_operations_struct cifs_file_vm_ops = {
|
2011-04-12 09:13:44 -04:00
|
|
|
.fault = filemap_fault,
|
2014-04-07 15:37:19 -07:00
|
|
|
.map_pages = filemap_map_pages,
|
2011-04-12 09:13:44 -04:00
|
|
|
.page_mkwrite = cifs_page_mkwrite,
|
|
|
|
};
|
|
|
|
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
int cifs_file_strict_mmap_prepare(struct vm_area_desc *desc)
|
2010-12-14 11:29:51 +03:00
|
|
|
{
|
2017-12-15 12:48:32 -08:00
|
|
|
int xid, rc = 0;
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
struct inode *inode = file_inode(desc->file);
|
2010-12-14 11:29:51 +03:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2010-12-14 11:29:51 +03:00
|
|
|
|
2017-12-15 12:48:32 -08:00
|
|
|
if (!CIFS_CACHE_READ(CIFS_I(inode)))
|
2014-04-30 09:31:47 -04:00
|
|
|
rc = cifs_zap_mapping(inode);
|
2017-12-15 12:48:32 -08:00
|
|
|
if (!rc)
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
rc = generic_file_mmap_prepare(desc);
|
2017-12-15 12:48:32 -08:00
|
|
|
if (!rc)
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
desc->vm_ops = &cifs_file_vm_ops;
|
2017-12-15 12:48:32 -08:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2010-12-14 11:29:51 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
int cifs_file_mmap_prepare(struct vm_area_desc *desc)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
int rc, xid;
|
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
xid = get_xid();
|
2017-12-15 12:48:32 -08:00
|
|
|
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
rc = cifs_revalidate_file(desc->file);
|
2017-12-15 12:48:32 -08:00
|
|
|
if (rc)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
|
|
|
|
rc);
|
2017-12-15 12:48:32 -08:00
|
|
|
if (!rc)
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
rc = generic_file_mmap_prepare(desc);
|
2017-12-15 12:48:32 -08:00
|
|
|
if (!rc)
|
fs: convert most other generic_file_*mmap() users to .mmap_prepare()
Update nearly all generic_file_mmap() and generic_file_readonly_mmap()
callers to use generic_file_mmap_prepare() and
generic_file_readonly_mmap_prepare() respectively.
We update blkdev, 9p, afs, erofs, ext2, nfs, ntfs3, smb, ubifs and vboxsf
file systems this way.
Remaining users we cannot yet update are ecryptfs, fuse and cramfs. The
former two are nested file systems that must support any underlying file
ssytem, and cramfs inserts a mixed mapping which currently requires a VMA.
Once all file systems have been converted to mmap_prepare(), we can then
update nested file systems.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/08db85970d89b17a995d2cffae96fb4cc462377f.1750099179.git.lorenzo.stoakes@oracle.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2025-06-16 20:33:28 +01:00
|
|
|
desc->vm_ops = &cifs_file_vm_ops;
|
2017-12-15 12:48:32 -08:00
|
|
|
|
2012-06-20 11:21:16 +04:00
|
|
|
free_xid(xid);
|
2005-04-16 15:20:36 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-07-26 15:54:16 +00:00
|
|
|
static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *open_file;
|
|
|
|
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_lock(&cifs_inode->open_file_lock);
|
2007-07-26 15:54:16 +00:00
|
|
|
list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
|
2010-10-15 15:34:01 -04:00
|
|
|
if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2007-07-26 15:54:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 15:16:27 +10:00
|
|
|
spin_unlock(&cifs_inode->open_file_lock);
|
2007-07-26 15:54:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/* We do not want to update the file size from server for inodes
|
|
|
|
open for write - to avoid races with writepage extending
|
|
|
|
the file - in the future we could consider allowing
|
2007-07-10 01:16:18 +00:00
|
|
|
refreshing the inode only on increases in the file size
|
2005-04-16 15:20:36 -07:00
|
|
|
but this is tricky to do without racing with writebehind
|
|
|
|
page caching in the current Linux kernel design */
|
2024-02-29 23:09:52 +05:30
|
|
|
bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
|
|
|
|
bool from_readdir)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2007-07-26 15:54:16 +00:00
|
|
|
if (!cifsInode)
|
2008-04-29 00:06:05 +00:00
|
|
|
return true;
|
2007-07-13 00:33:32 +00:00
|
|
|
|
2024-02-29 23:09:52 +05:30
|
|
|
if (is_inode_writable(cifsInode) ||
|
|
|
|
((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
|
2007-07-26 15:54:16 +00:00
|
|
|
/* This inode is open for write at least once */
|
2006-01-12 14:41:28 -08:00
|
|
|
struct cifs_sb_info *cifs_sb;
|
|
|
|
|
netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
While randstruct was satisfied with using an open-coded "void *" offset
cast for the netfs_i_context <-> inode casting, __builtin_object_size() as
used by FORTIFY_SOURCE was not as easily fooled. This was causing the
following complaint[1] from gcc v12:
In file included from include/linux/string.h:253,
from include/linux/ceph/ceph_debug.h:7,
from fs/ceph/inode.c:2:
In function 'fortify_memset_chk',
inlined from 'netfs_i_context_init' at include/linux/netfs.h:326:2,
inlined from 'ceph_alloc_inode' at fs/ceph/inode.c:463:2:
include/linux/fortify-string.h:242:25: warning: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]
242 | __write_overflow_field(p_size_field, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fix this by embedding a struct inode into struct netfs_i_context (which
should perhaps be renamed to struct netfs_inode). The struct inode
vfs_inode fields are then removed from the 9p, afs, ceph and cifs inode
structs and vfs_inode is then simply changed to "netfs.inode" in those
filesystems.
Further, rename netfs_i_context to netfs_inode, get rid of the
netfs_inode() function that converted a netfs_i_context pointer to an
inode pointer (that can now be done with &ctx->inode) and rename the
netfs_i_context() function to netfs_inode() (which is now a wrapper
around container_of()).
Most of the changes were done with:
perl -p -i -e 's/vfs_inode/netfs.inode/'g \
`git grep -l 'vfs_inode' -- fs/{9p,afs,ceph,cifs}/*.[ch]`
Kees suggested doing it with a pair structure[2] and a special
declarator to insert that into the network filesystem's inode
wrapper[3], but I think it's cleaner to embed it - and then it doesn't
matter if struct randomisation reorders things.
Dave Chinner suggested using a filesystem-specific VFS_I() function in
each filesystem to convert that filesystem's own inode wrapper struct
into the VFS inode struct[4].
Version #2:
- Fix a couple of missed name changes due to a disabled cifs option.
- Rename nfs_i_context to nfs_inode
- Use "netfs" instead of "nic" as the member name in per-fs inode wrapper
structs.
[ This also undoes commit 507160f46c55 ("netfs: gcc-12: temporarily
disable '-Wattribute-warning' for now") that is no longer needed ]
Fixes: bc899ee1c898 ("netfs: Add a netfs inode context")
Reported-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
cc: Jonathan Corbet <corbet@lwn.net>
cc: Eric Van Hensbergen <ericvh@gmail.com>
cc: Latchesar Ionkov <lucho@ionkov.net>
cc: Dominique Martinet <asmadeus@codewreck.org>
cc: Christian Schoenebeck <linux_oss@crudebyte.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Ilya Dryomov <idryomov@gmail.com>
cc: Steve French <smfrench@gmail.com>
cc: William Kucharski <william.kucharski@oracle.com>
cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
cc: Dave Chinner <david@fromorbit.com>
cc: linux-doc@vger.kernel.org
cc: v9fs-developer@lists.sourceforge.net
cc: linux-afs@lists.infradead.org
cc: ceph-devel@vger.kernel.org
cc: linux-cifs@vger.kernel.org
cc: samba-technical@lists.samba.org
cc: linux-fsdevel@vger.kernel.org
cc: linux-hardening@vger.kernel.org
Link: https://lore.kernel.org/r/d2ad3a3d7bdd794c6efb562d2f2b655fb67756b9.camel@kernel.org/ [1]
Link: https://lore.kernel.org/r/20220517210230.864239-1-keescook@chromium.org/ [2]
Link: https://lore.kernel.org/r/20220518202212.2322058-1-keescook@chromium.org/ [3]
Link: https://lore.kernel.org/r/20220524101205.GI2306852@dread.disaster.area/ [4]
Link: https://lore.kernel.org/r/165296786831.3591209.12111293034669289733.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/165305805651.4094995.7763502506786714216.stgit@warthog.procyon.org.uk # v2
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2022-06-09 21:46:04 +01:00
|
|
|
cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
|
2008-02-07 23:25:02 +00:00
|
|
|
if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
|
2007-07-10 01:16:18 +00:00
|
|
|
/* since no page cache to corrupt on directio
|
2006-01-12 14:41:28 -08:00
|
|
|
we can change size safely */
|
2008-04-29 00:06:05 +00:00
|
|
|
return true;
|
2006-01-12 14:41:28 -08:00
|
|
|
}
|
|
|
|
|
netfs: Fix gcc-12 warning by embedding vfs inode in netfs_i_context
While randstruct was satisfied with using an open-coded "void *" offset
cast for the netfs_i_context <-> inode casting, __builtin_object_size() as
used by FORTIFY_SOURCE was not as easily fooled. This was causing the
following complaint[1] from gcc v12:
In file included from include/linux/string.h:253,
from include/linux/ceph/ceph_debug.h:7,
from fs/ceph/inode.c:2:
In function 'fortify_memset_chk',
inlined from 'netfs_i_context_init' at include/linux/netfs.h:326:2,
inlined from 'ceph_alloc_inode' at fs/ceph/inode.c:463:2:
include/linux/fortify-string.h:242:25: warning: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Wattribute-warning]
242 | __write_overflow_field(p_size_field, size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fix this by embedding a struct inode into struct netfs_i_context (which
should perhaps be renamed to struct netfs_inode). The struct inode
vfs_inode fields are then removed from the 9p, afs, ceph and cifs inode
structs and vfs_inode is then simply changed to "netfs.inode" in those
filesystems.
Further, rename netfs_i_context to netfs_inode, get rid of the
netfs_inode() function that converted a netfs_i_context pointer to an
inode pointer (that can now be done with &ctx->inode) and rename the
netfs_i_context() function to netfs_inode() (which is now a wrapper
around container_of()).
Most of the changes were done with:
perl -p -i -e 's/vfs_inode/netfs.inode/'g \
`git grep -l 'vfs_inode' -- fs/{9p,afs,ceph,cifs}/*.[ch]`
Kees suggested doing it with a pair structure[2] and a special
declarator to insert that into the network filesystem's inode
wrapper[3], but I think it's cleaner to embed it - and then it doesn't
matter if struct randomisation reorders things.
Dave Chinner suggested using a filesystem-specific VFS_I() function in
each filesystem to convert that filesystem's own inode wrapper struct
into the VFS inode struct[4].
Version #2:
- Fix a couple of missed name changes due to a disabled cifs option.
- Rename nfs_i_context to nfs_inode
- Use "netfs" instead of "nic" as the member name in per-fs inode wrapper
structs.
[ This also undoes commit 507160f46c55 ("netfs: gcc-12: temporarily
disable '-Wattribute-warning' for now") that is no longer needed ]
Fixes: bc899ee1c898 ("netfs: Add a netfs inode context")
Reported-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
cc: Jonathan Corbet <corbet@lwn.net>
cc: Eric Van Hensbergen <ericvh@gmail.com>
cc: Latchesar Ionkov <lucho@ionkov.net>
cc: Dominique Martinet <asmadeus@codewreck.org>
cc: Christian Schoenebeck <linux_oss@crudebyte.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Ilya Dryomov <idryomov@gmail.com>
cc: Steve French <smfrench@gmail.com>
cc: William Kucharski <william.kucharski@oracle.com>
cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
cc: Dave Chinner <david@fromorbit.com>
cc: linux-doc@vger.kernel.org
cc: v9fs-developer@lists.sourceforge.net
cc: linux-afs@lists.infradead.org
cc: ceph-devel@vger.kernel.org
cc: linux-cifs@vger.kernel.org
cc: samba-technical@lists.samba.org
cc: linux-fsdevel@vger.kernel.org
cc: linux-hardening@vger.kernel.org
Link: https://lore.kernel.org/r/d2ad3a3d7bdd794c6efb562d2f2b655fb67756b9.camel@kernel.org/ [1]
Link: https://lore.kernel.org/r/20220517210230.864239-1-keescook@chromium.org/ [2]
Link: https://lore.kernel.org/r/20220518202212.2322058-1-keescook@chromium.org/ [3]
Link: https://lore.kernel.org/r/20220524101205.GI2306852@dread.disaster.area/ [4]
Link: https://lore.kernel.org/r/165296786831.3591209.12111293034669289733.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/165305805651.4094995.7763502506786714216.stgit@warthog.procyon.org.uk # v2
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2022-06-09 21:46:04 +01:00
|
|
|
if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
|
2008-04-29 00:06:05 +00:00
|
|
|
return true;
|
2007-02-08 18:14:13 +00:00
|
|
|
|
2008-04-29 00:06:05 +00:00
|
|
|
return false;
|
2005-10-20 13:44:56 -07:00
|
|
|
} else
|
2008-04-29 00:06:05 +00:00
|
|
|
return true;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
|
|
|
|
2010-07-20 22:09:02 +02:00
|
|
|
void cifs_oplock_break(struct work_struct *work)
|
2009-09-21 06:47:50 -04:00
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
|
|
|
|
oplock_break);
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(cfile->dentry);
|
2025-07-07 09:09:26 +08:00
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
|
2009-09-21 06:47:50 -04:00
|
|
|
struct cifsInodeInfo *cinode = CIFS_I(inode);
|
2023-08-10 15:34:21 -05:00
|
|
|
struct cifs_tcon *tcon;
|
|
|
|
struct TCP_Server_Info *server;
|
|
|
|
struct tcon_link *tlink;
|
2010-10-22 14:52:29 -04:00
|
|
|
int rc = 0;
|
2023-05-15 21:25:12 +00:00
|
|
|
bool purge_cache = false, oplock_break_cancelled;
|
|
|
|
__u64 persistent_fid, volatile_fid;
|
|
|
|
__u16 net_fid;
|
2009-09-21 06:47:50 -04:00
|
|
|
|
2025-07-07 09:09:26 +08:00
|
|
|
/*
|
|
|
|
* Hold a reference to the superblock to prevent it and its inodes from
|
|
|
|
* being freed while we are accessing cinode. Otherwise, _cifsFileInfo_put()
|
|
|
|
* may release the last reference to the sb and trigger inode eviction.
|
|
|
|
*/
|
|
|
|
cifs_sb_active(sb);
|
2014-03-11 16:11:47 +00:00
|
|
|
wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
|
sched: Remove proliferation of wait_on_bit() action functions
The current "wait_on_bit" interface requires an 'action'
function to be provided which does the actual waiting.
There are over 20 such functions, many of them identical.
Most cases can be satisfied by one of just two functions, one
which uses io_schedule() and one which just uses schedule().
So:
Rename wait_on_bit and wait_on_bit_lock to
wait_on_bit_action and wait_on_bit_lock_action
to make it explicit that they need an action function.
Introduce new wait_on_bit{,_lock} and wait_on_bit{,_lock}_io
which are *not* given an action function but implicitly use
a standard one.
The decision to error-out if a signal is pending is now made
based on the 'mode' argument rather than being encoded in the action
function.
All instances of the old wait_on_bit and wait_on_bit_lock which
can use the new version have been changed accordingly and their
action functions have been discarded.
wait_on_bit{_lock} does not return any specific error code in the
event of a signal so the caller must check for non-zero and
interpolate their own error code as appropriate.
The wait_on_bit() call in __fscache_wait_on_invalidate() was
ambiguous as it specified TASK_UNINTERRUPTIBLE but used
fscache_wait_bit_interruptible as an action function.
David Howells confirms this should be uniformly
"uninterruptible"
The main remaining user of wait_on_bit{,_lock}_action is NFS
which needs to use a freezer-aware schedule() call.
A comment in fs/gfs2/glock.c notes that having multiple 'action'
functions is useful as they display differently in the 'wchan'
field of 'ps'. (and /proc/$PID/wchan).
As the new bit_wait{,_io} functions are tagged "__sched", they
will not show up at all, but something higher in the stack. So
the distinction will still be visible, only with different
function names (gds2_glock_wait versus gfs2_glock_dq_wait in the
gfs2/glock.c case).
Since first version of this patch (against 3.15) two new action
functions appeared, on in NFS and one in CIFS. CIFS also now
uses an action function that makes the same freezer aware
schedule call as NFS.
Signed-off-by: NeilBrown <neilb@suse.de>
Acked-by: David Howells <dhowells@redhat.com> (fscache, keys)
Acked-by: Steven Whitehouse <swhiteho@redhat.com> (gfs2)
Acked-by: Peter Zijlstra <peterz@infradead.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Steve French <sfrench@samba.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/20140707051603.28027.72349.stgit@notabene.brown
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-07-07 15:16:04 +10:00
|
|
|
TASK_UNINTERRUPTIBLE);
|
2014-03-11 16:11:47 +00:00
|
|
|
|
2023-08-10 15:34:21 -05:00
|
|
|
tlink = cifs_sb_tlink(cifs_sb);
|
|
|
|
if (IS_ERR(tlink))
|
|
|
|
goto out;
|
|
|
|
tcon = tlink_tcon(tlink);
|
|
|
|
server = tcon->ses->server;
|
|
|
|
|
2019-10-29 16:51:19 -07:00
|
|
|
server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
|
|
|
|
cfile->oplock_epoch, &purge_cache);
|
2014-03-11 16:11:47 +00:00
|
|
|
|
2013-09-05 13:01:06 +04:00
|
|
|
if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
|
2012-12-24 14:41:19 +04:00
|
|
|
cifs_has_mand_locks(cinode)) {
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
|
|
|
|
inode);
|
2013-09-05 13:01:06 +04:00
|
|
|
cinode->oplock = 0;
|
2012-12-24 14:41:19 +04:00
|
|
|
}
|
|
|
|
|
2025-03-10 17:04:06 +03:00
|
|
|
if (S_ISREG(inode->i_mode)) {
|
2013-09-05 13:01:06 +04:00
|
|
|
if (CIFS_CACHE_READ(cinode))
|
2009-12-24 06:47:55 -05:00
|
|
|
break_lease(inode, O_RDONLY);
|
2010-04-27 04:38:15 +00:00
|
|
|
else
|
2009-12-24 06:47:55 -05:00
|
|
|
break_lease(inode, O_WRONLY);
|
2009-09-21 06:47:50 -04:00
|
|
|
rc = filemap_fdatawrite(inode->i_mapping);
|
2019-10-29 16:51:19 -07:00
|
|
|
if (!CIFS_CACHE_READ(cinode) || purge_cache) {
|
2010-10-22 14:52:29 -04:00
|
|
|
rc = filemap_fdatawait(inode->i_mapping);
|
|
|
|
mapping_set_error(inode->i_mapping, rc);
|
2014-04-30 09:31:47 -04:00
|
|
|
cifs_zap_mapping(inode);
|
2009-09-21 06:47:50 -04:00
|
|
|
}
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
|
2019-10-29 16:51:19 -07:00
|
|
|
if (CIFS_CACHE_WRITE(cinode))
|
|
|
|
goto oplock_break_ack;
|
2009-09-21 06:47:50 -04:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:33:29 +04:00
|
|
|
rc = cifs_push_locks(cfile);
|
|
|
|
if (rc)
|
2013-05-04 22:12:25 -05:00
|
|
|
cifs_dbg(VFS, "Push locks rc = %d\n", rc);
|
2011-10-22 15:33:29 +04:00
|
|
|
|
2019-10-29 16:51:19 -07:00
|
|
|
oplock_break_ack:
|
2023-04-26 14:05:16 +00:00
|
|
|
/*
|
|
|
|
* When oplock break is received and there are no active
|
|
|
|
* file handles but cached, then schedule deferred close immediately.
|
|
|
|
* So, new open will not use cached handle.
|
|
|
|
*/
|
|
|
|
|
2023-05-03 14:38:35 +00:00
|
|
|
if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes))
|
2023-04-26 14:05:16 +00:00
|
|
|
cifs_close_deferred_file(cinode);
|
|
|
|
|
2023-05-15 21:25:12 +00:00
|
|
|
persistent_fid = cfile->fid.persistent_fid;
|
|
|
|
volatile_fid = cfile->fid.volatile_fid;
|
|
|
|
net_fid = cfile->fid.netfid;
|
|
|
|
oplock_break_cancelled = cfile->oplock_break_cancelled;
|
|
|
|
|
|
|
|
_cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
|
2021-08-09 09:32:46 +00:00
|
|
|
/*
|
2023-06-18 19:02:24 +00:00
|
|
|
* MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
|
|
|
|
* an acknowledgment to be sent when the file has already been closed.
|
2021-08-09 09:32:46 +00:00
|
|
|
*/
|
2023-06-18 19:02:24 +00:00
|
|
|
spin_lock(&cinode->open_file_lock);
|
2023-08-10 15:34:21 -05:00
|
|
|
/* check list empty since can race with kill_sb calling tree disconnect */
|
|
|
|
if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
|
2023-06-18 19:02:24 +00:00
|
|
|
spin_unlock(&cinode->open_file_lock);
|
2023-08-10 15:34:21 -05:00
|
|
|
rc = server->ops->oplock_response(tcon, persistent_fid,
|
|
|
|
volatile_fid, net_fid, cinode);
|
2023-06-18 19:02:24 +00:00
|
|
|
cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
|
|
|
|
} else
|
|
|
|
spin_unlock(&cinode->open_file_lock);
|
2022-08-11 19:46:11 +00:00
|
|
|
|
2023-08-10 15:34:21 -05:00
|
|
|
cifs_put_tlink(tlink);
|
|
|
|
out:
|
2014-03-11 16:11:47 +00:00
|
|
|
cifs_done_oplock_break(cinode);
|
2025-07-07 09:09:26 +08:00
|
|
|
cifs_sb_deactive(sb);
|
2009-09-21 06:47:50 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 21:42:18 -05:00
|
|
|
static int cifs_swap_activate(struct swap_info_struct *sis,
|
|
|
|
struct file *swap_file, sector_t *span)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile = swap_file->private_data;
|
|
|
|
struct inode *inode = swap_file->f_mapping->host;
|
|
|
|
unsigned long blocks;
|
|
|
|
long long isize;
|
|
|
|
|
|
|
|
cifs_dbg(FYI, "swap activate\n");
|
|
|
|
|
2022-05-09 18:20:48 -07:00
|
|
|
if (!swap_file->f_mapping->a_ops->swap_rw)
|
|
|
|
/* Cannot support swap */
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-04-09 21:42:18 -05:00
|
|
|
spin_lock(&inode->i_lock);
|
|
|
|
blocks = inode->i_blocks;
|
|
|
|
isize = inode->i_size;
|
|
|
|
spin_unlock(&inode->i_lock);
|
|
|
|
if (blocks*512 < isize) {
|
|
|
|
pr_warn("swap activate: swapfile has holes\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*span = sis->pages;
|
|
|
|
|
2020-04-14 22:42:53 -07:00
|
|
|
pr_warn_once("Swap support over SMB3 is experimental\n");
|
2020-04-09 21:42:18 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: consider adding ACL (or documenting how) to prevent other
|
|
|
|
* users (on this or other systems) from reading it
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: add sk_set_memalloc(inet) or similar */
|
|
|
|
|
|
|
|
if (cfile)
|
|
|
|
cfile->swapfile = true;
|
|
|
|
/*
|
|
|
|
* TODO: Since file already open, we can't open with DENY_ALL here
|
|
|
|
* but we could add call to grab a byte range lock to prevent others
|
|
|
|
* from reading or writing the file
|
|
|
|
*/
|
|
|
|
|
2022-05-09 18:20:48 -07:00
|
|
|
sis->flags |= SWP_FS_OPS;
|
|
|
|
return add_swap_extent(sis, 0, sis->max, 0);
|
2020-04-09 21:42:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cifs_swap_deactivate(struct file *file)
|
|
|
|
{
|
|
|
|
struct cifsFileInfo *cfile = file->private_data;
|
|
|
|
|
|
|
|
cifs_dbg(FYI, "swap deactivate\n");
|
|
|
|
|
|
|
|
/* TODO: undo sk_set_memalloc(inet) will eventually be needed */
|
|
|
|
|
|
|
|
if (cfile)
|
|
|
|
cfile->swapfile = false;
|
|
|
|
|
|
|
|
/* do we need to unpin (or unlock) the file */
|
|
|
|
}
|
2013-11-11 16:42:37 -06:00
|
|
|
|
2024-05-15 18:06:03 -05:00
|
|
|
/**
|
|
|
|
* cifs_swap_rw - SMB3 address space operation for swap I/O
|
|
|
|
* @iocb: target I/O control block
|
|
|
|
* @iter: I/O buffer
|
|
|
|
*
|
|
|
|
* Perform IO to the swap-file. This is much like direct IO.
|
|
|
|
*/
|
|
|
|
static int cifs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (iov_iter_rw(iter) == READ)
|
|
|
|
ret = netfs_unbuffered_read_iter_locked(iocb, iter);
|
|
|
|
else
|
|
|
|
ret = netfs_unbuffered_write_iter_locked(iocb, iter, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-28 04:26:44 -07:00
|
|
|
const struct address_space_operations cifs_addr_ops = {
|
2023-10-06 18:29:59 +01:00
|
|
|
.read_folio = netfs_read_folio,
|
|
|
|
.readahead = netfs_readahead,
|
|
|
|
.writepages = netfs_writepages,
|
|
|
|
.dirty_folio = netfs_dirty_folio,
|
|
|
|
.release_folio = netfs_release_folio,
|
|
|
|
.direct_IO = noop_direct_IO,
|
|
|
|
.invalidate_folio = netfs_invalidate_folio,
|
|
|
|
.migrate_folio = filemap_migrate_folio,
|
2020-04-09 21:42:18 -05:00
|
|
|
/*
|
2022-11-16 14:18:33 +01:00
|
|
|
* TODO: investigate and if useful we could add an is_dirty_writeback
|
|
|
|
* helper if needed
|
2020-04-09 21:42:18 -05:00
|
|
|
*/
|
2023-10-06 18:29:59 +01:00
|
|
|
.swap_activate = cifs_swap_activate,
|
2020-04-09 21:42:18 -05:00
|
|
|
.swap_deactivate = cifs_swap_deactivate,
|
2024-05-15 18:06:03 -05:00
|
|
|
.swap_rw = cifs_swap_rw,
|
2005-04-16 15:20:36 -07:00
|
|
|
};
|
2006-06-01 19:41:23 +00:00
|
|
|
|
|
|
|
/*
|
2022-04-29 11:12:16 -04:00
|
|
|
* cifs_readahead requires the server to support a buffer large enough to
|
2006-06-01 19:41:23 +00:00
|
|
|
* contain the header plus one complete page of data. Otherwise, we need
|
2022-04-29 11:12:16 -04:00
|
|
|
* to leave cifs_readahead out of the address space operations.
|
2006-06-01 19:41:23 +00:00
|
|
|
*/
|
2006-06-28 04:26:44 -07:00
|
|
|
const struct address_space_operations cifs_addr_ops_smallbuf = {
|
2023-10-06 18:29:59 +01:00
|
|
|
.read_folio = netfs_read_folio,
|
|
|
|
.writepages = netfs_writepages,
|
|
|
|
.dirty_folio = netfs_dirty_folio,
|
|
|
|
.release_folio = netfs_release_folio,
|
|
|
|
.invalidate_folio = netfs_invalidate_folio,
|
|
|
|
.migrate_folio = filemap_migrate_folio,
|
2006-06-01 19:41:23 +00:00
|
|
|
};
|