CIFS: Process oplocks for SMB2
[linux-3.10.git] / fs / cifs / smb2inode.c
1 /*
2  *   fs/cifs/smb2inode.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002, 2011
5  *                 Etersoft, 2012
6  *   Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7  *              Steve French (sfrench@us.ibm.com)
8  *
9  *   This library is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU Lesser General Public License as published
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
19  *   You should have received a copy of the GNU Lesser General Public License
20  *   along with this library; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/pagemap.h>
27 #include <asm/div64.h>
28 #include "cifsfs.h"
29 #include "cifspdu.h"
30 #include "cifsglob.h"
31 #include "cifsproto.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "cifs_unicode.h"
35 #include "fscache.h"
36 #include "smb2glob.h"
37 #include "smb2pdu.h"
38 #include "smb2proto.h"
39
40 static int
41 smb2_open_op_close(const unsigned int xid, struct cifs_tcon *tcon,
42                    struct cifs_sb_info *cifs_sb, const char *full_path,
43                    __u32 desired_access, __u32 create_disposition,
44                    __u32 file_attributes, __u32 create_options,
45                    void *data, int command)
46 {
47         int rc, tmprc = 0;
48         u64 persistent_fid, volatile_fid;
49         __le16 *utf16_path;
50         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
51
52         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
53         if (!utf16_path)
54                 return -ENOMEM;
55
56         rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
57                        desired_access, create_disposition, file_attributes,
58                        create_options, &oplock, NULL);
59         if (rc) {
60                 kfree(utf16_path);
61                 return rc;
62         }
63
64         switch (command) {
65         case SMB2_OP_DELETE:
66                 break;
67         case SMB2_OP_QUERY_INFO:
68                 tmprc = SMB2_query_info(xid, tcon, persistent_fid,
69                                         volatile_fid,
70                                         (struct smb2_file_all_info *)data);
71                 break;
72         case SMB2_OP_MKDIR:
73                 /*
74                  * Directories are created through parameters in the
75                  * SMB2_open() call.
76                  */
77                 break;
78         case SMB2_OP_RENAME:
79                 tmprc = SMB2_rename(xid, tcon, persistent_fid, volatile_fid,
80                                     (__le16 *)data);
81                 break;
82         case SMB2_OP_HARDLINK:
83                 tmprc = SMB2_set_hardlink(xid, tcon, persistent_fid,
84                                           volatile_fid, (__le16 *)data);
85                 break;
86         case SMB2_OP_SET_EOF:
87                 tmprc = SMB2_set_eof(xid, tcon, persistent_fid, volatile_fid,
88                                      current->tgid, (__le64 *)data);
89                 break;
90         case SMB2_OP_SET_INFO:
91                 tmprc = SMB2_set_info(xid, tcon, persistent_fid, volatile_fid,
92                                       (FILE_BASIC_INFO *)data);
93                 break;
94         default:
95                 cERROR(1, "Invalid command");
96                 break;
97         }
98
99         rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
100         if (tmprc)
101                 rc = tmprc;
102         kfree(utf16_path);
103         return rc;
104 }
105
106 void
107 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
108 {
109         memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
110         dst->CurrentByteOffset = src->CurrentByteOffset;
111         dst->Mode = src->Mode;
112         dst->AlignmentRequirement = src->AlignmentRequirement;
113         dst->IndexNumber1 = 0; /* we don't use it */
114 }
115
116 int
117 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
118                      struct cifs_sb_info *cifs_sb, const char *full_path,
119                      FILE_ALL_INFO *data, bool *adjust_tz)
120 {
121         int rc;
122         struct smb2_file_all_info *smb2_data;
123
124         *adjust_tz = false;
125
126         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
127                             GFP_KERNEL);
128         if (smb2_data == NULL)
129                 return -ENOMEM;
130
131         rc = smb2_open_op_close(xid, tcon, cifs_sb, full_path,
132                                 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0,
133                                 smb2_data, SMB2_OP_QUERY_INFO);
134         if (rc)
135                 goto out;
136
137         move_smb2_info_to_cifs(data, smb2_data);
138 out:
139         kfree(smb2_data);
140         return rc;
141 }
142
143 int
144 smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
145            struct cifs_sb_info *cifs_sb)
146 {
147         return smb2_open_op_close(xid, tcon, cifs_sb, name,
148                                   FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
149                                   CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
150 }
151
152 void
153 smb2_mkdir_setinfo(struct inode *inode, const char *name,
154                    struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
155                    const unsigned int xid)
156 {
157         FILE_BASIC_INFO data;
158         struct cifsInodeInfo *cifs_i;
159         u32 dosattrs;
160         int tmprc;
161
162         memset(&data, 0, sizeof(data));
163         cifs_i = CIFS_I(inode);
164         dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
165         data.Attributes = cpu_to_le32(dosattrs);
166         tmprc = smb2_open_op_close(xid, tcon, cifs_sb, name,
167                                    FILE_WRITE_ATTRIBUTES, FILE_CREATE, 0,
168                                    CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
169         if (tmprc == 0)
170                 cifs_i->cifsAttrs = dosattrs;
171 }
172
173 int
174 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
175            struct cifs_sb_info *cifs_sb)
176 {
177         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
178                                   0, CREATE_NOT_FILE | CREATE_DELETE_ON_CLOSE,
179                                   NULL, SMB2_OP_DELETE);
180 }
181
182 int
183 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
184             struct cifs_sb_info *cifs_sb)
185 {
186         return smb2_open_op_close(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
187                                   0, CREATE_DELETE_ON_CLOSE, NULL,
188                                   SMB2_OP_DELETE);
189 }
190
191 static int
192 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
193                    const char *from_name, const char *to_name,
194                    struct cifs_sb_info *cifs_sb, __u32 access, int command)
195 {
196         __le16 *smb2_to_name = NULL;
197         int rc;
198
199         smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
200         if (smb2_to_name == NULL) {
201                 rc = -ENOMEM;
202                 goto smb2_rename_path;
203         }
204
205         rc = smb2_open_op_close(xid, tcon, cifs_sb, from_name, access,
206                                 FILE_OPEN, 0, 0, smb2_to_name, command);
207 smb2_rename_path:
208         kfree(smb2_to_name);
209         return rc;
210 }
211
212 int
213 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
214                  const char *from_name, const char *to_name,
215                  struct cifs_sb_info *cifs_sb)
216 {
217         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
218                                   DELETE, SMB2_OP_RENAME);
219 }
220
221 int
222 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
223                      const char *from_name, const char *to_name,
224                      struct cifs_sb_info *cifs_sb)
225 {
226         return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
227                                   FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
228 }
229
230 int
231 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
232                    const char *full_path, __u64 size,
233                    struct cifs_sb_info *cifs_sb, bool set_alloc)
234 {
235         __le64 eof = cpu_to_le64(size);
236         return smb2_open_op_close(xid, tcon, cifs_sb, full_path,
237                                   FILE_WRITE_DATA, FILE_OPEN, 0, 0, &eof,
238                                   SMB2_OP_SET_EOF);
239 }
240
241 int
242 smb2_set_file_info(struct inode *inode, const char *full_path,
243                    FILE_BASIC_INFO *buf, const unsigned int xid)
244 {
245         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
246         struct tcon_link *tlink;
247         int rc;
248
249         tlink = cifs_sb_tlink(cifs_sb);
250         if (IS_ERR(tlink))
251                 return PTR_ERR(tlink);
252         rc = smb2_open_op_close(xid, tlink_tcon(tlink), cifs_sb, full_path,
253                                 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, 0, buf,
254                                 SMB2_OP_SET_INFO);
255         cifs_put_tlink(tlink);
256         return rc;
257 }