Skip to content

Commit f0df737

Browse files
piastrysmfrench
authored andcommitted
CIFS: Add open/close file support for SMB2
Signed-off-by: Pavel Shilovsky <pshilovsky@samba.org> Signed-off-by: Steve French <smfrench@gmail.com>
1 parent 0ff78a2 commit f0df737

8 files changed

Lines changed: 172 additions & 17 deletions

File tree

‎fs/cifs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ cifs-$(CONFIG_CIFS_DFS_UPCALL) += dns_resolve.o cifs_dfs_ref.o
1717
cifs-$(CONFIG_CIFS_FSCACHE) += fscache.o cache.o
1818

1919
cifs-$(CONFIG_CIFS_SMB2) += smb2ops.o smb2maperror.o smb2transport.o \
20-
smb2misc.o smb2pdu.o smb2inode.o
20+
smb2misc.o smb2pdu.o smb2inode.o smb2file.o

‎fs/cifs/cifsglob.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ struct cifs_search_info {
757757

758758
struct cifs_fid {
759759
__u16 netfid;
760+
#ifdef CONFIG_CIFS_SMB2
761+
__u64 persistent_fid; /* persist file id for smb2 */
762+
__u64 volatile_fid; /* volatile file id for smb2 */
763+
#endif
760764
};
761765

762766
struct cifsFileInfo {

‎fs/cifs/smb2file.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* fs/cifs/smb2file.c
3+
*
4+
* Copyright (C) International Business Machines Corp., 2002, 2011
5+
* Author(s): Steve French (sfrench@us.ibm.com),
6+
* Pavel Shilovsky ((pshilovsky@samba.org) 2012
7+
*
8+
* This library is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published
10+
* by the Free Software Foundation; either version 2.1 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16+
* the GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
*/
22+
#include <linux/fs.h>
23+
#include <linux/stat.h>
24+
#include <linux/slab.h>
25+
#include <linux/pagemap.h>
26+
#include <asm/div64.h>
27+
#include "cifsfs.h"
28+
#include "cifspdu.h"
29+
#include "cifsglob.h"
30+
#include "cifsproto.h"
31+
#include "cifs_debug.h"
32+
#include "cifs_fs_sb.h"
33+
#include "cifs_unicode.h"
34+
#include "fscache.h"
35+
#include "smb2proto.h"
36+
37+
int
38+
smb2_open_file(const unsigned int xid, struct cifs_tcon *tcon, const char *path,
39+
int disposition, int desired_access, int create_options,
40+
struct cifs_fid *fid, __u32 *oplock, FILE_ALL_INFO *buf,
41+
struct cifs_sb_info *cifs_sb)
42+
{
43+
int rc;
44+
__le16 *smb2_path;
45+
struct smb2_file_all_info *smb2_data = NULL;
46+
47+
smb2_path = cifs_convert_path_to_utf16(path, cifs_sb);
48+
if (smb2_path == NULL) {
49+
rc = -ENOMEM;
50+
goto out;
51+
}
52+
53+
smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
54+
GFP_KERNEL);
55+
if (smb2_data == NULL) {
56+
rc = -ENOMEM;
57+
goto out;
58+
}
59+
60+
desired_access |= FILE_READ_ATTRIBUTES;
61+
62+
rc = SMB2_open(xid, tcon, smb2_path, &fid->persistent_fid,
63+
&fid->volatile_fid, desired_access, disposition,
64+
0, 0, smb2_data);
65+
if (rc)
66+
goto out;
67+
68+
if (buf) {
69+
/* open response does not have IndexNumber field - get it */
70+
rc = SMB2_get_srv_num(xid, tcon, fid->persistent_fid,
71+
fid->volatile_fid,
72+
&smb2_data->IndexNumber);
73+
if (rc) {
74+
/* let get_inode_info disable server inode numbers */
75+
smb2_data->IndexNumber = 0;
76+
rc = 0;
77+
}
78+
move_smb2_info_to_cifs(buf, smb2_data);
79+
}
80+
81+
out:
82+
*oplock = 0;
83+
kfree(smb2_data);
84+
kfree(smb2_path);
85+
return rc;
86+
}

‎fs/cifs/smb2inode.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
5454

5555
rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
5656
desired_access, create_disposition, file_attributes,
57-
create_options);
57+
create_options, NULL);
5858
if (rc) {
5959
kfree(utf16_path);
6060
return rc;
@@ -86,7 +86,7 @@ smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
8686
return rc;
8787
}
8888

89-
static void
89+
void
9090
move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
9191
{
9292
memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);

‎fs/cifs/smb2ops.c‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
170170
return -ENOMEM;
171171

172172
rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
173-
FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0);
173+
FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, NULL);
174174
if (rc) {
175175
kfree(utf16_path);
176176
return rc;
@@ -292,6 +292,23 @@ smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
292292
#endif
293293
}
294294

295+
static void
296+
smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
297+
{
298+
/* struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); */
299+
cfile->fid.persistent_fid = fid->persistent_fid;
300+
cfile->fid.volatile_fid = fid->volatile_fid;
301+
/* cifs_set_oplock_level(cinode, oplock); */
302+
/* cinode->can_cache_brlcks = cinode->clientCanCacheAll; */
303+
}
304+
305+
static int
306+
smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
307+
struct cifs_fid *fid)
308+
{
309+
return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
310+
}
311+
295312
struct smb_version_operations smb21_operations = {
296313
.setup_request = smb2_setup_request,
297314
.setup_async_request = smb2_setup_async_request,
@@ -322,6 +339,9 @@ struct smb_version_operations smb21_operations = {
322339
.mkdir_setinfo = smb2_mkdir_setinfo,
323340
.rmdir = smb2_rmdir,
324341
.unlink = smb2_unlink,
342+
.open = smb2_open_file,
343+
.set_fid = smb2_set_fid,
344+
.close = smb2_close_file,
325345
};
326346

327347
struct smb_version_values smb21_values = {

‎fs/cifs/smb2pdu.c‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
833833
int
834834
SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
835835
u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access,
836-
__u32 create_disposition, __u32 file_attributes, __u32 create_options)
836+
__u32 create_disposition, __u32 file_attributes, __u32 create_options,
837+
struct smb2_file_all_info *buf)
837838
{
838839
struct smb2_create_req *req;
839840
struct smb2_create_rsp *rsp;
@@ -856,9 +857,9 @@ SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
856857
if (rc)
857858
return rc;
858859

859-
if (enable_oplocks)
860+
/* if (server->oplocks)
860861
req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_BATCH;
861-
else
862+
else */
862863
req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
863864
req->ImpersonationLevel = IL_IMPERSONATION;
864865
req->DesiredAccess = cpu_to_le32(desired_access);
@@ -906,6 +907,15 @@ SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path,
906907
}
907908
*persistent_fid = rsp->PersistentFileId;
908909
*volatile_fid = rsp->VolatileFileId;
910+
911+
if (buf) {
912+
memcpy(buf, &rsp->CreationTime, 32);
913+
buf->AllocationSize = rsp->AllocationSize;
914+
buf->EndOfFile = rsp->EndofFile;
915+
buf->Attributes = rsp->FileAttributes;
916+
buf->NumberOfLinks = cpu_to_le32(1);
917+
buf->DeletePending = 0;
918+
}
909919
creat_exit:
910920
free_rsp_buf(resp_buftype, rsp);
911921
return rc;
@@ -1019,10 +1029,10 @@ validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
10191029
return 0;
10201030
}
10211031

1022-
int
1023-
SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1024-
u64 persistent_fid, u64 volatile_fid,
1025-
struct smb2_file_all_info *data)
1032+
static int
1033+
query_info(const unsigned int xid, struct cifs_tcon *tcon,
1034+
u64 persistent_fid, u64 volatile_fid, u8 info_class,
1035+
size_t output_len, size_t min_len, void *data)
10261036
{
10271037
struct smb2_query_info_req *req;
10281038
struct smb2_query_info_rsp *rsp = NULL;
@@ -1044,14 +1054,13 @@ SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
10441054
return rc;
10451055

10461056
req->InfoType = SMB2_O_INFO_FILE;
1047-
req->FileInfoClass = FILE_ALL_INFORMATION;
1057+
req->FileInfoClass = info_class;
10481058
req->PersistentFileId = persistent_fid;
10491059
req->VolatileFileId = volatile_fid;
10501060
/* 4 for rfc1002 length field and 1 for Buffer */
10511061
req->InputBufferOffset =
10521062
cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1053-
req->OutputBufferLength =
1054-
cpu_to_le32(sizeof(struct smb2_file_all_info) + MAX_NAME * 2);
1063+
req->OutputBufferLength = cpu_to_le32(output_len);
10551064

10561065
iov[0].iov_base = (char *)req;
10571066
/* 4 for rfc1002 length field */
@@ -1067,14 +1076,34 @@ SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
10671076

10681077
rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
10691078
le32_to_cpu(rsp->OutputBufferLength),
1070-
&rsp->hdr, sizeof(struct smb2_file_all_info),
1071-
(char *)data);
1079+
&rsp->hdr, min_len, data);
10721080

10731081
qinf_exit:
10741082
free_rsp_buf(resp_buftype, rsp);
10751083
return rc;
10761084
}
10771085

1086+
int
1087+
SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1088+
u64 persistent_fid, u64 volatile_fid,
1089+
struct smb2_file_all_info *data)
1090+
{
1091+
return query_info(xid, tcon, persistent_fid, volatile_fid,
1092+
FILE_ALL_INFORMATION,
1093+
sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
1094+
sizeof(struct smb2_file_all_info), data);
1095+
}
1096+
1097+
int
1098+
SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1099+
u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1100+
{
1101+
return query_info(xid, tcon, persistent_fid, volatile_fid,
1102+
FILE_INTERNAL_INFORMATION,
1103+
sizeof(struct smb2_file_internal_info),
1104+
sizeof(struct smb2_file_internal_info), uniqueid);
1105+
}
1106+
10781107
/*
10791108
* This is a no-op for now. We're not really interested in the reply, but
10801109
* rather in the fact that the server sent one and that server->lstrp

‎fs/cifs/smb2pdu.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ struct smb2_query_info_rsp {
548548
#define FILEID_GLOBAL_TX_DIRECTORY_INFORMATION 50
549549
#define FILE_STANDARD_LINK_INFORMATION 54
550550

551+
struct smb2_file_internal_info {
552+
__le64 IndexNumber;
553+
} __packed; /* level 6 Query */
554+
551555
/*
552556
* This level 18, although with struct with same name is different from cifs
553557
* level 0x107. Level 0x107 has an extra u64 between AccessFlags and

‎fs/cifs/smb2proto.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ extern int smb2_setup_async_request(struct TCP_Server_Info *server,
4848
struct mid_q_entry **ret_mid);
4949
extern void smb2_echo_request(struct work_struct *work);
5050

51+
extern void move_smb2_info_to_cifs(FILE_ALL_INFO *dst,
52+
struct smb2_file_all_info *src);
5153
extern int smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
5254
struct cifs_sb_info *cifs_sb,
5355
const char *full_path, FILE_ALL_INFO *data,
@@ -62,6 +64,12 @@ extern int smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
6264
extern int smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon,
6365
const char *name, struct cifs_sb_info *cifs_sb);
6466

67+
extern int smb2_open_file(const unsigned int xid, struct cifs_tcon *tcon,
68+
const char *full_path, int disposition,
69+
int desired_access, int create_options,
70+
struct cifs_fid *fid, __u32 *oplock,
71+
FILE_ALL_INFO *buf, struct cifs_sb_info *cifs_sb);
72+
6573
/*
6674
* SMB2 Worker functions - most of protocol specific implementation details
6775
* are contained within these calls.
@@ -77,12 +85,16 @@ extern int SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon);
7785
extern int SMB2_open(const unsigned int xid, struct cifs_tcon *tcon,
7886
__le16 *path, u64 *persistent_fid, u64 *volatile_fid,
7987
__u32 desired_access, __u32 create_disposition,
80-
__u32 file_attributes, __u32 create_options);
88+
__u32 file_attributes, __u32 create_options,
89+
struct smb2_file_all_info *buf);
8190
extern int SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
8291
u64 persistent_file_id, u64 volatile_file_id);
8392
extern int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
8493
u64 persistent_file_id, u64 volatile_file_id,
8594
struct smb2_file_all_info *data);
95+
extern int SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
96+
u64 persistent_fid, u64 volatile_fid,
97+
__le64 *uniqueid);
8698
extern int SMB2_echo(struct TCP_Server_Info *server);
8799

88100
#endif /* _SMB2PROTO_H */

0 commit comments

Comments
 (0)