cifs: cache FILE_ALL_INFO for the shared root handle
[linux-2.6-block.git] / fs / cifs / smb2inode.c
CommitLineData
be4cb9e3
PS
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
c5a5f38f
RS
40static int
41smb2_compound_op(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,
c2e0fe3f 44 __u32 create_options, void *ptr, int command)
c5a5f38f
RS
45{
46 int rc;
47 __le16 *utf16_path = NULL;
48 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
49 struct cifs_open_parms oparms;
50 struct cifs_fid fid;
51 struct cifs_ses *ses = tcon->ses;
c5a5f38f
RS
52 int num_rqst = 0;
53 struct smb_rqst rqst[3];
54 int resp_buftype[3];
55 struct kvec rsp_iov[3];
56 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
57 struct kvec qi_iov[1];
14e562ad 58 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
c5a5f38f 59 struct kvec close_iov[1];
c2e0fe3f 60 struct smb2_query_info_rsp *qi_rsp = NULL;
c5a5f38f 61 int flags = 0;
c2e0fe3f 62 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
bb435512
RS
63 unsigned int size[2];
64 void *data[2];
65 struct smb2_file_rename_info rename_info;
66 struct smb2_file_link_info link_info;
67 int len;
c5a5f38f
RS
68
69 if (smb3_encryption_required(tcon))
70 flags |= CIFS_TRANSFORM_REQ;
71
72 memset(rqst, 0, sizeof(rqst));
73 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
74 memset(rsp_iov, 0, sizeof(rsp_iov));
75
76 /* Open */
77 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
78 if (!utf16_path)
79 return -ENOMEM;
80
81 oparms.tcon = tcon;
82 oparms.desired_access = desired_access;
83 oparms.disposition = create_disposition;
84 oparms.create_options = create_options;
4d5bdf28
SF
85 if (backup_cred(cifs_sb))
86 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
c5a5f38f
RS
87 oparms.fid = &fid;
88 oparms.reconnect = false;
89
90 memset(&open_iov, 0, sizeof(open_iov));
91 rqst[num_rqst].rq_iov = open_iov;
92 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
93 rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
94 utf16_path);
95 kfree(utf16_path);
96 if (rc)
97 goto finished;
98
e77fe73c 99 smb2_set_next_command(tcon, &rqst[num_rqst++]);
c5a5f38f
RS
100
101 /* Operation */
102 switch (command) {
103 case SMB2_OP_QUERY_INFO:
104 memset(&qi_iov, 0, sizeof(qi_iov));
105 rqst[num_rqst].rq_iov = qi_iov;
106 rqst[num_rqst].rq_nvec = 1;
107
108 rc = SMB2_query_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
109 COMPOUND_FID, FILE_ALL_INFORMATION,
110 SMB2_O_INFO_FILE, 0,
111 sizeof(struct smb2_file_all_info) +
f5b05d62 112 PATH_MAX * 2, 0, NULL);
e77fe73c 113 smb2_set_next_command(tcon, &rqst[num_rqst]);
c5a5f38f
RS
114 smb2_set_related(&rqst[num_rqst++]);
115 break;
47dd9597
RS
116 case SMB2_OP_DELETE:
117 break;
f733e393
RS
118 case SMB2_OP_MKDIR:
119 /*
120 * Directories are created through parameters in the
121 * SMB2_open() call.
122 */
123 break;
c2e0fe3f
RS
124 case SMB2_OP_RMDIR:
125 memset(&si_iov, 0, sizeof(si_iov));
126 rqst[num_rqst].rq_iov = si_iov;
127 rqst[num_rqst].rq_nvec = 1;
128
271b9c0c 129 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
c2e0fe3f
RS
130 data[0] = &delete_pending[0];
131
132 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
133 COMPOUND_FID, current->tgid,
134 FILE_DISPOSITION_INFORMATION,
135 SMB2_O_INFO_FILE, 0, data, size);
e77fe73c 136 smb2_set_next_command(tcon, &rqst[num_rqst]);
c2e0fe3f
RS
137 smb2_set_related(&rqst[num_rqst++]);
138 break;
f7bfe04b
RS
139 case SMB2_OP_SET_EOF:
140 memset(&si_iov, 0, sizeof(si_iov));
141 rqst[num_rqst].rq_iov = si_iov;
142 rqst[num_rqst].rq_nvec = 1;
143
144 size[0] = 8; /* sizeof __le64 */
145 data[0] = ptr;
146
147 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
148 COMPOUND_FID, current->tgid,
149 FILE_END_OF_FILE_INFORMATION,
150 SMB2_O_INFO_FILE, 0, data, size);
e77fe73c 151 smb2_set_next_command(tcon, &rqst[num_rqst]);
f7bfe04b
RS
152 smb2_set_related(&rqst[num_rqst++]);
153 break;
dcbf9103
RS
154 case SMB2_OP_SET_INFO:
155 memset(&si_iov, 0, sizeof(si_iov));
156 rqst[num_rqst].rq_iov = si_iov;
157 rqst[num_rqst].rq_nvec = 1;
158
bb435512 159
dcbf9103
RS
160 size[0] = sizeof(FILE_BASIC_INFO);
161 data[0] = ptr;
162
163 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
164 COMPOUND_FID, current->tgid,
165 FILE_BASIC_INFORMATION,
166 SMB2_O_INFO_FILE, 0, data, size);
e77fe73c 167 smb2_set_next_command(tcon, &rqst[num_rqst]);
dcbf9103
RS
168 smb2_set_related(&rqst[num_rqst++]);
169 break;
bb435512
RS
170 case SMB2_OP_RENAME:
171 memset(&si_iov, 0, sizeof(si_iov));
172 rqst[num_rqst].rq_iov = si_iov;
173 rqst[num_rqst].rq_nvec = 2;
174
175 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
176
177 rename_info.ReplaceIfExists = 1;
178 rename_info.RootDirectory = 0;
179 rename_info.FileNameLength = cpu_to_le32(len);
180
181 size[0] = sizeof(struct smb2_file_rename_info);
182 data[0] = &rename_info;
183
184 size[1] = len + 2 /* null */;
185 data[1] = (__le16 *)ptr;
186
187 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
188 COMPOUND_FID, current->tgid,
189 FILE_RENAME_INFORMATION,
190 SMB2_O_INFO_FILE, 0, data, size);
e77fe73c 191 smb2_set_next_command(tcon, &rqst[num_rqst]);
bb435512
RS
192 smb2_set_related(&rqst[num_rqst++]);
193 break;
194 case SMB2_OP_HARDLINK:
195 memset(&si_iov, 0, sizeof(si_iov));
196 rqst[num_rqst].rq_iov = si_iov;
197 rqst[num_rqst].rq_nvec = 2;
198
199 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
200
201 link_info.ReplaceIfExists = 0;
202 link_info.RootDirectory = 0;
203 link_info.FileNameLength = cpu_to_le32(len);
204
205 size[0] = sizeof(struct smb2_file_link_info);
206 data[0] = &link_info;
207
208 size[1] = len + 2 /* null */;
209 data[1] = (__le16 *)ptr;
210
211 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
212 COMPOUND_FID, current->tgid,
213 FILE_LINK_INFORMATION,
214 SMB2_O_INFO_FILE, 0, data, size);
e77fe73c 215 smb2_set_next_command(tcon, &rqst[num_rqst]);
bb435512
RS
216 smb2_set_related(&rqst[num_rqst++]);
217 break;
c5a5f38f
RS
218 default:
219 cifs_dbg(VFS, "Invalid command\n");
220 rc = -EINVAL;
221 }
222 if (rc)
223 goto finished;
224
225 /* Close */
226 memset(&close_iov, 0, sizeof(close_iov));
227 rqst[num_rqst].rq_iov = close_iov;
228 rqst[num_rqst].rq_nvec = 1;
229 rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
230 COMPOUND_FID);
231 smb2_set_related(&rqst[num_rqst++]);
232 if (rc)
233 goto finished;
234
235 rc = compound_send_recv(xid, ses, flags, num_rqst, rqst,
236 resp_buftype, rsp_iov);
237
238 finished:
239 SMB2_open_free(&rqst[0]);
240 switch (command) {
241 case SMB2_OP_QUERY_INFO:
242 if (rc == 0) {
c2e0fe3f
RS
243 qi_rsp = (struct smb2_query_info_rsp *)
244 rsp_iov[1].iov_base;
c5a5f38f 245 rc = smb2_validate_and_copy_iov(
c2e0fe3f
RS
246 le16_to_cpu(qi_rsp->OutputBufferOffset),
247 le32_to_cpu(qi_rsp->OutputBufferLength),
c5a5f38f 248 &rsp_iov[1], sizeof(struct smb2_file_all_info),
c2e0fe3f 249 ptr);
c5a5f38f
RS
250 }
251 if (rqst[1].rq_iov)
252 SMB2_query_info_free(&rqst[1]);
253 if (rqst[2].rq_iov)
254 SMB2_close_free(&rqst[2]);
255 break;
47dd9597 256 case SMB2_OP_DELETE:
f733e393
RS
257 case SMB2_OP_MKDIR:
258 if (rqst[1].rq_iov)
259 SMB2_close_free(&rqst[1]);
260 break;
bb435512
RS
261 case SMB2_OP_HARDLINK:
262 case SMB2_OP_RENAME:
c2e0fe3f 263 case SMB2_OP_RMDIR:
dcbf9103
RS
264 case SMB2_OP_SET_EOF:
265 case SMB2_OP_SET_INFO:
c2e0fe3f
RS
266 if (rqst[1].rq_iov)
267 SMB2_set_info_free(&rqst[1]);
268 if (rqst[2].rq_iov)
269 SMB2_close_free(&rqst[2]);
270 break;
c5a5f38f
RS
271 }
272 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
273 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
274 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
275 return rc;
276}
277
f0df737e 278void
be4cb9e3
PS
279move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
280{
281 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
282 dst->CurrentByteOffset = src->CurrentByteOffset;
283 dst->Mode = src->Mode;
284 dst->AlignmentRequirement = src->AlignmentRequirement;
285 dst->IndexNumber1 = 0; /* we don't use it */
286}
287
288int
289smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
290 struct cifs_sb_info *cifs_sb, const char *full_path,
eb85d94b 291 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
be4cb9e3
PS
292{
293 int rc;
294 struct smb2_file_all_info *smb2_data;
61351d6d 295 __u32 create_options = 0;
6a9cbdd1
PS
296 struct cifs_fid fid;
297 bool no_cached_open = tcon->nohandlecache;
be4cb9e3
PS
298
299 *adjust_tz = false;
eb85d94b 300 *symlink = false;
be4cb9e3 301
1bbe4997 302 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
be4cb9e3
PS
303 GFP_KERNEL);
304 if (smb2_data == NULL)
305 return -ENOMEM;
6a9cbdd1
PS
306
307 /* If it is a root and its handle is cached then use it */
308 if (!strlen(full_path) && !no_cached_open) {
309 rc = open_shroot(xid, tcon, &fid);
310 if (rc)
311 goto out;
b0f6df73
RS
312
313 if (tcon->crfid.file_all_info_is_valid) {
314 move_smb2_info_to_cifs(data,
315 &tcon->crfid.file_all_info);
316 } else {
317 rc = SMB2_query_info(xid, tcon, fid.persistent_fid,
318 fid.volatile_fid, smb2_data);
319 if (!rc)
320 move_smb2_info_to_cifs(data, smb2_data);
321 }
6a9cbdd1 322 close_shroot(&tcon->crfid);
6a9cbdd1
PS
323 goto out;
324 }
325
61351d6d
SF
326 if (backup_cred(cifs_sb))
327 create_options |= CREATE_OPEN_BACKUP_INTENT;
be4cb9e3 328
c5a5f38f 329 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
61351d6d 330 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
c5a5f38f 331 smb2_data, SMB2_OP_QUERY_INFO);
eb85d94b
PS
332 if (rc == -EOPNOTSUPP) {
333 *symlink = true;
61351d6d
SF
334 create_options |= OPEN_REPARSE_POINT;
335
eb85d94b 336 /* Failed on a symbolic link - query a reparse point info */
c5a5f38f
RS
337 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
338 FILE_READ_ATTRIBUTES, FILE_OPEN,
61351d6d 339 create_options, smb2_data,
c5a5f38f 340 SMB2_OP_QUERY_INFO);
eb85d94b 341 }
be4cb9e3
PS
342 if (rc)
343 goto out;
344
345 move_smb2_info_to_cifs(data, smb2_data);
346out:
347 kfree(smb2_data);
348 return rc;
349}
a0e73183
PS
350
351int
352smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
353 struct cifs_sb_info *cifs_sb)
354{
f733e393
RS
355 return smb2_compound_op(xid, tcon, cifs_sb, name,
356 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
357 CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
a0e73183
PS
358}
359
360void
361smb2_mkdir_setinfo(struct inode *inode, const char *name,
362 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
363 const unsigned int xid)
364{
365 FILE_BASIC_INFO data;
366 struct cifsInodeInfo *cifs_i;
367 u32 dosattrs;
368 int tmprc;
369
370 memset(&data, 0, sizeof(data));
371 cifs_i = CIFS_I(inode);
372 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
373 data.Attributes = cpu_to_le32(dosattrs);
dcbf9103
RS
374 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
375 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
376 CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
a0e73183
PS
377 if (tmprc == 0)
378 cifs_i->cifsAttrs = dosattrs;
379}
1a500f01
PS
380
381int
382smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
383 struct cifs_sb_info *cifs_sb)
384{
c2e0fe3f
RS
385 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
386 CREATE_NOT_FILE,
387 NULL, SMB2_OP_RMDIR);
1a500f01 388}
cbe6f439
PS
389
390int
391smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
392 struct cifs_sb_info *cifs_sb)
393{
47dd9597
RS
394 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
395 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
396 NULL, SMB2_OP_DELETE);
cbe6f439 397}
35143eb5 398
568798cc
PS
399static int
400smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
401 const char *from_name, const char *to_name,
402 struct cifs_sb_info *cifs_sb, __u32 access, int command)
35143eb5
PS
403{
404 __le16 *smb2_to_name = NULL;
405 int rc;
406
407 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
408 if (smb2_to_name == NULL) {
409 rc = -ENOMEM;
410 goto smb2_rename_path;
411 }
bb435512
RS
412 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
413 FILE_OPEN, 0, smb2_to_name, command);
35143eb5
PS
414smb2_rename_path:
415 kfree(smb2_to_name);
416 return rc;
417}
568798cc
PS
418
419int
420smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
421 const char *from_name, const char *to_name,
422 struct cifs_sb_info *cifs_sb)
423{
424 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
425 DELETE, SMB2_OP_RENAME);
426}
427
428int
429smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
430 const char *from_name, const char *to_name,
431 struct cifs_sb_info *cifs_sb)
432{
433 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
434 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
435}
c839ff24
PS
436
437int
438smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
439 const char *full_path, __u64 size,
440 struct cifs_sb_info *cifs_sb, bool set_alloc)
441{
442 __le64 eof = cpu_to_le64(size);
f7bfe04b
RS
443
444 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
445 FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
446 SMB2_OP_SET_EOF);
c839ff24 447}
1feeaac7
PS
448
449int
450smb2_set_file_info(struct inode *inode, const char *full_path,
451 FILE_BASIC_INFO *buf, const unsigned int xid)
452{
453 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
454 struct tcon_link *tlink;
455 int rc;
456
18dd8e1a 457 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
fd09b7d3 458 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
18dd8e1a
SF
459 (buf->Attributes == 0))
460 return 0; /* would be a no op, no sense sending this */
461
1feeaac7
PS
462 tlink = cifs_sb_tlink(cifs_sb);
463 if (IS_ERR(tlink))
464 return PTR_ERR(tlink);
18dd8e1a 465
dcbf9103
RS
466 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
467 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
468 SMB2_OP_SET_INFO);
1feeaac7
PS
469 cifs_put_tlink(tlink);
470 return rc;
471}