Skip to content

Commit f122121

Browse files
paliSteve French
authored andcommitted
cifs: Fix changing times and read-only attr over SMB1 smb_set_file_info() function
Function CIFSSMBSetPathInfo() is not supported by non-NT servers and returns error. Fallback code via open filehandle and CIFSSMBSetFileInfo() does not work neither because CIFS_open() works also only on NT server. Therefore currently the whole smb_set_file_info() function as a SMB1 callback for the ->set_file_info() does not work with older non-NT SMB servers, like Win9x and others. This change implements fallback code in smb_set_file_info() which will works with any server and allows to change time values and also to set or clear read-only attributes. To make existing fallback code via CIFSSMBSetFileInfo() working with also non-NT servers, it is needed to change open function from CIFS_open() (which is NT specific) to cifs_open_file() which works with any server (this is just a open wrapper function which choose the correct open function supported by the server). CIFSSMBSetFileInfo() is working also on non-NT servers, but zero time values are not treated specially. So first it is needed to fill all time values if some of them are missing, via cifs_query_path_info() call. There is another issue, opening file in write-mode (needed for changing attributes) is not possible when the file has read-only attribute set. The only option how to clear read-only attribute is via SMB_COM_SETATTR command. And opening directory is not possible neither and here the SMB_COM_SETATTR command is the only option how to change attributes. And CIFSSMBSetFileInfo() does not honor setting read-only attribute, so for setting is also needed to use SMB_COM_SETATTR command. Existing code in cifs_query_path_info() is already using SMB_COM_GETATTR as a fallback code path (function SMBQueryInformation()), so introduce a new function SMBSetInformation which will implement SMB_COM_SETATTR command. My testing showed that Windows XP SMB1 client is also using SMB_COM_SETATTR command for setting or clearing read-only attribute against non-NT server. So this can prove that this is the correct way how to do it. With this change it is possible set all 4 time values and all attributes, including clearing and setting read-only bit on non-NT SMB servers. Tested against Win98 SMB1 server. This change fixes "touch" command which was failing when called on existing file. And fixes also "chmod +w" and "chmod -w" commands which were also failing (as they are changing read-only attribute). Note that this change depends on following change "cifs: Improve cifs_query_path_info() and cifs_query_file_info()" as it require to query all 4 time attribute values. Signed-off-by: Pali Rohár <pali@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 1041c11 commit f122121

File tree

4 files changed

+166
-12
lines changed

4 files changed

+166
-12
lines changed

fs/smb/client/cifspdu.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,9 @@ typedef struct smb_com_query_information_rsp {
12661266
typedef struct smb_com_setattr_req {
12671267
struct smb_hdr hdr; /* wct = 8 */
12681268
__le16 attr;
1269-
__le16 time_low;
1270-
__le16 time_high;
1269+
__le32 last_write_time;
12711270
__le16 reserved[5]; /* must be zero */
1272-
__u16 ByteCount;
1271+
__le16 ByteCount;
12731272
__u8 BufferFormat; /* 4 = ASCII */
12741273
unsigned char fileName[];
12751274
} __attribute__((packed)) SETATTR_REQ;

fs/smb/client/cifsproto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ extern int CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon);
395395
extern int CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
396396
struct kstatfs *FSData);
397397

398+
extern int SMBSetInformation(const unsigned int xid, struct cifs_tcon *tcon,
399+
const char *fileName, __le32 attributes, __le64 write_time,
400+
const struct nls_table *nls_codepage,
401+
struct cifs_sb_info *cifs_sb);
398402
extern int CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
399403
const char *fileName, const FILE_BASIC_INFO *data,
400404
const struct nls_table *nls_codepage,

fs/smb/client/cifssmb.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5171,6 +5171,63 @@ CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
51715171
return rc;
51725172
}
51735173

5174+
int
5175+
SMBSetInformation(const unsigned int xid, struct cifs_tcon *tcon,
5176+
const char *fileName, __le32 attributes, __le64 write_time,
5177+
const struct nls_table *nls_codepage,
5178+
struct cifs_sb_info *cifs_sb)
5179+
{
5180+
SETATTR_REQ *pSMB;
5181+
SETATTR_RSP *pSMBr;
5182+
struct timespec64 ts;
5183+
int bytes_returned;
5184+
int name_len;
5185+
int rc;
5186+
5187+
cifs_dbg(FYI, "In %s path %s\n", __func__, fileName);
5188+
5189+
retry:
5190+
rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5191+
(void **) &pSMBr);
5192+
if (rc)
5193+
return rc;
5194+
5195+
if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5196+
name_len =
5197+
cifsConvertToUTF16((__le16 *) pSMB->fileName,
5198+
fileName, PATH_MAX, nls_codepage,
5199+
cifs_remap(cifs_sb));
5200+
name_len++; /* trailing null */
5201+
name_len *= 2;
5202+
} else {
5203+
name_len = copy_path_name(pSMB->fileName, fileName);
5204+
}
5205+
/* Only few attributes can be set by this command, others are not accepted by Win9x. */
5206+
pSMB->attr = cpu_to_le16(le32_to_cpu(attributes) &
5207+
(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_ARCHIVE));
5208+
/* Zero write time value (in both NT and SETATTR formats) means to not change it. */
5209+
if (le64_to_cpu(write_time) != 0) {
5210+
ts = cifs_NTtimeToUnix(write_time);
5211+
pSMB->last_write_time = cpu_to_le32(ts.tv_sec);
5212+
}
5213+
pSMB->BufferFormat = 0x04;
5214+
name_len++; /* account for buffer type byte */
5215+
inc_rfc1001_len(pSMB, (__u16)name_len);
5216+
pSMB->ByteCount = cpu_to_le16(name_len);
5217+
5218+
rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5219+
(struct smb_hdr *) pSMBr, &bytes_returned, 0);
5220+
if (rc)
5221+
cifs_dbg(FYI, "Send error in %s = %d\n", __func__, rc);
5222+
5223+
cifs_buf_release(pSMB);
5224+
5225+
if (rc == -EAGAIN)
5226+
goto retry;
5227+
5228+
return rc;
5229+
}
5230+
51745231
/* Some legacy servers such as NT4 require that the file times be set on
51755232
an open handle, rather than by pathname - this is awkward due to
51765233
potential access conflicts on the open, but it is unavoidable for these

fs/smb/client/smb1ops.c

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -896,27 +896,68 @@ smb_set_file_info(struct inode *inode, const char *full_path,
896896
struct cifs_fid fid;
897897
struct cifs_open_parms oparms;
898898
struct cifsFileInfo *open_file;
899+
FILE_BASIC_INFO new_buf;
900+
struct cifs_open_info_data query_data;
901+
__le64 write_time = buf->LastWriteTime;
899902
struct cifsInodeInfo *cinode = CIFS_I(inode);
900903
struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
901904
struct tcon_link *tlink = NULL;
902905
struct cifs_tcon *tcon;
903906

904907
/* if the file is already open for write, just use that fileid */
905908
open_file = find_writable_file(cinode, FIND_WR_FSUID_ONLY);
909+
906910
if (open_file) {
907911
fid.netfid = open_file->fid.netfid;
908912
netpid = open_file->pid;
909913
tcon = tlink_tcon(open_file->tlink);
910-
goto set_via_filehandle;
914+
} else {
915+
tlink = cifs_sb_tlink(cifs_sb);
916+
if (IS_ERR(tlink)) {
917+
rc = PTR_ERR(tlink);
918+
tlink = NULL;
919+
goto out;
920+
}
921+
tcon = tlink_tcon(tlink);
911922
}
912923

913-
tlink = cifs_sb_tlink(cifs_sb);
914-
if (IS_ERR(tlink)) {
915-
rc = PTR_ERR(tlink);
916-
tlink = NULL;
917-
goto out;
924+
/*
925+
* Non-NT servers interprets zero time value in SMB_SET_FILE_BASIC_INFO
926+
* over TRANS2_SET_FILE_INFORMATION as a valid time value. NT servers
927+
* interprets zero time value as do not change existing value on server.
928+
* API of ->set_file_info() callback expects that zero time value has
929+
* the NT meaning - do not change. Therefore if server is non-NT and
930+
* some time values in "buf" are zero, then fetch missing time values.
931+
*/
932+
if (!(tcon->ses->capabilities & CAP_NT_SMBS) &&
933+
(!buf->CreationTime || !buf->LastAccessTime ||
934+
!buf->LastWriteTime || !buf->ChangeTime)) {
935+
rc = cifs_query_path_info(xid, tcon, cifs_sb, full_path, &query_data);
936+
if (rc) {
937+
if (open_file) {
938+
cifsFileInfo_put(open_file);
939+
open_file = NULL;
940+
}
941+
goto out;
942+
}
943+
/*
944+
* Original write_time from buf->LastWriteTime is preserved
945+
* as SMBSetInformation() interprets zero as do not change.
946+
*/
947+
new_buf = *buf;
948+
buf = &new_buf;
949+
if (!buf->CreationTime)
950+
buf->CreationTime = query_data.fi.CreationTime;
951+
if (!buf->LastAccessTime)
952+
buf->LastAccessTime = query_data.fi.LastAccessTime;
953+
if (!buf->LastWriteTime)
954+
buf->LastWriteTime = query_data.fi.LastWriteTime;
955+
if (!buf->ChangeTime)
956+
buf->ChangeTime = query_data.fi.ChangeTime;
918957
}
919-
tcon = tlink_tcon(tlink);
958+
959+
if (open_file)
960+
goto set_via_filehandle;
920961

921962
rc = CIFSSMBSetPathInfo(xid, tcon, full_path, buf, cifs_sb->local_nls,
922963
cifs_sb);
@@ -937,15 +978,53 @@ smb_set_file_info(struct inode *inode, const char *full_path,
937978
.fid = &fid,
938979
};
939980

940-
cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for times not supported by this server\n");
941-
rc = CIFS_open(xid, &oparms, &oplock, NULL);
981+
if (S_ISDIR(inode->i_mode) && !(tcon->ses->capabilities & CAP_NT_SMBS)) {
982+
/* Opening directory path is not possible on non-NT servers. */
983+
rc = -EOPNOTSUPP;
984+
} else {
985+
/*
986+
* Use cifs_open_file() instead of CIFS_open() as the
987+
* cifs_open_file() selects the correct function which
988+
* works also on non-NT servers.
989+
*/
990+
rc = cifs_open_file(xid, &oparms, &oplock, NULL);
991+
/*
992+
* Opening path for writing on non-NT servers is not
993+
* possible when the read-only attribute is already set.
994+
* Non-NT server in this case returns -EACCES. For those
995+
* servers the only possible way how to clear the read-only
996+
* bit is via SMB_COM_SETATTR command.
997+
*/
998+
if (rc == -EACCES &&
999+
(cinode->cifsAttrs & ATTR_READONLY) &&
1000+
le32_to_cpu(buf->Attributes) != 0 && /* 0 = do not change attrs */
1001+
!(le32_to_cpu(buf->Attributes) & ATTR_READONLY) &&
1002+
!(tcon->ses->capabilities & CAP_NT_SMBS))
1003+
rc = -EOPNOTSUPP;
1004+
}
1005+
1006+
/* Fallback to SMB_COM_SETATTR command when absolutelty needed. */
1007+
if (rc == -EOPNOTSUPP) {
1008+
cifs_dbg(FYI, "calling SetInformation since SetPathInfo for attrs/times not supported by this server\n");
1009+
rc = SMBSetInformation(xid, tcon, full_path,
1010+
buf->Attributes != 0 ? buf->Attributes : cpu_to_le32(cinode->cifsAttrs),
1011+
write_time,
1012+
cifs_sb->local_nls, cifs_sb);
1013+
if (rc == 0)
1014+
cinode->cifsAttrs = le32_to_cpu(buf->Attributes);
1015+
else
1016+
rc = -EACCES;
1017+
goto out;
1018+
}
1019+
9421020
if (rc != 0) {
9431021
if (rc == -EIO)
9441022
rc = -EINVAL;
9451023
goto out;
9461024
}
9471025

9481026
netpid = current->tgid;
1027+
cifs_dbg(FYI, "calling SetFileInfo since SetPathInfo for attrs/times not supported by this server\n");
9491028

9501029
set_via_filehandle:
9511030
rc = CIFSSMBSetFileInfo(xid, tcon, buf, fid.netfid, netpid);
@@ -956,6 +1035,21 @@ smb_set_file_info(struct inode *inode, const char *full_path,
9561035
CIFSSMBClose(xid, tcon, fid.netfid);
9571036
else
9581037
cifsFileInfo_put(open_file);
1038+
1039+
/*
1040+
* Setting the read-only bit is not honered on non-NT servers when done
1041+
* via open-semantics. So for setting it, use SMB_COM_SETATTR command.
1042+
* This command works only after the file is closed, so use it only when
1043+
* operation was called without the filehandle.
1044+
*/
1045+
if (open_file == NULL &&
1046+
!(tcon->ses->capabilities & CAP_NT_SMBS) &&
1047+
le32_to_cpu(buf->Attributes) & ATTR_READONLY) {
1048+
SMBSetInformation(xid, tcon, full_path,
1049+
buf->Attributes,
1050+
0 /* do not change write time */,
1051+
cifs_sb->local_nls, cifs_sb);
1052+
}
9591053
out:
9601054
if (tlink != NULL)
9611055
cifs_put_tlink(tlink);

0 commit comments

Comments
 (0)