From patchwork Thu Nov 9 23:52:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ronnie Sahlberg X-Patchwork-Id: 10052149 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8153F601EA for ; Thu, 9 Nov 2017 23:52:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73E552B19E for ; Thu, 9 Nov 2017 23:52:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 686402B1FB; Thu, 9 Nov 2017 23:52:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0A5AF2B1E7 for ; Thu, 9 Nov 2017 23:52:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755249AbdKIXw6 (ORCPT ); Thu, 9 Nov 2017 18:52:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46872 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755203AbdKIXw5 (ORCPT ); Thu, 9 Nov 2017 18:52:57 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A759185A02; Thu, 9 Nov 2017 23:52:57 +0000 (UTC) Received: from test1190.test.redhat.com (vpn2-54-27.bne.redhat.com [10.64.54.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FF6317A71; Thu, 9 Nov 2017 23:52:56 +0000 (UTC) From: Ronnie Sahlberg To: linux-cifs Cc: Steve French Subject: [PATCH] cifs: fix return code when failing to rename a file onto a directory Date: Fri, 10 Nov 2017 10:52:49 +1100 Message-Id: <20171109235249.8013-1-lsahlber@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 09 Nov 2017 23:52:57 +0000 (UTC) Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Cifs servers return ACCESS_DENIED when trying to rename onto a non-empty directory. This is different from xfstest where we expect this to return -EEXIST instead. As a workaround, if we failed to rename the file and we got -EACCES then try to open the target file and check the attributes if it is a directory or not and if so remap the error to -EEXIST. This makes us pass xfstest generic/245 Signed-off-by: Ronnie Sahlberg --- fs/cifs/smb2ops.c | 40 ++++++++++++++++++++++++++++++++++++++++ fs/cifs/smb2pdu.c | 12 +++++++++++- fs/cifs/smb2proto.h | 2 ++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c index bdb963d0ba32..5a620c71d91f 100644 --- a/fs/cifs/smb2ops.c +++ b/fs/cifs/smb2ops.c @@ -426,6 +426,46 @@ smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon, return rc; } +int +smb2_is_dir(const unsigned int xid, struct cifs_tcon *tcon, + __le16 *target_file, int *is_dir) +{ + int rc; + u8 oplock = SMB2_OPLOCK_LEVEL_NONE; + struct cifs_open_parms oparms; + struct cifs_fid fid; + struct smb2_file_all_info *smb2_data; + + smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2, + GFP_KERNEL); + if (smb2_data == NULL) + return -ENOMEM; + + oparms.tcon = tcon; + oparms.desired_access = FILE_READ_ATTRIBUTES; + oparms.disposition = FILE_OPEN; + oparms.create_options = 0; + oparms.fid = &fid; + oparms.reconnect = false; + + rc = SMB2_open(xid, &oparms, target_file, &oplock, NULL, NULL); + if (rc) + goto out; + + rc = SMB2_query_info(xid, tcon, fid.persistent_fid, fid.volatile_fid, + smb2_data); + SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid); + if (rc) + goto out; + + *is_dir = !!(le32_to_cpu(smb2_data->Attributes) & + FILE_ATTRIBUTE_DIRECTORY); + +out: + kfree(smb2_data); + return rc; +} + #ifdef CONFIG_CIFS_XATTR static ssize_t move_smb2_ea_to_cifs(char *dst, size_t dst_size, diff --git a/fs/cifs/smb2pdu.c b/fs/cifs/smb2pdu.c index 553d574940b9..ec5de0a23378 100644 --- a/fs/cifs/smb2pdu.c +++ b/fs/cifs/smb2pdu.c @@ -3144,7 +3144,7 @@ SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon, struct smb2_file_rename_info info; void **data; unsigned int size[2]; - int rc; + int rc, is_dir; int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); @@ -3165,6 +3165,16 @@ SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon, rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE, 0, 2, data, size); + + /* SMB2 servers responds with ACCESS_DENIED when trying to rename + * and replace onto a non-empty directory. Check for this and remap + * to EEXIST. + */ + if (rc == -EACCES) { + if (!smb2_is_dir(xid, tcon, target_file, &is_dir) && is_dir) + rc = -EEXIST; + } + kfree(data); return rc; } diff --git a/fs/cifs/smb2proto.h b/fs/cifs/smb2proto.h index e9ab5227e7a8..a7528c0941c6 100644 --- a/fs/cifs/smb2proto.h +++ b/fs/cifs/smb2proto.h @@ -203,4 +203,6 @@ extern int smb3_validate_negotiate(const unsigned int, struct cifs_tcon *); extern enum securityEnum smb2_select_sectype(struct TCP_Server_Info *, enum securityEnum); +extern int smb2_is_dir(const unsigned int xid, struct cifs_tcon *tcon, + __le16 *target_file, int *is_dir); #endif /* _SMB2PROTO_H */