Merge tag 'overflow-v5.8-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[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
8191576a
SF
40static void
41free_set_inf_compound(struct smb_rqst *rqst)
42{
43 if (rqst[1].rq_iov)
44 SMB2_set_info_free(&rqst[1]);
45 if (rqst[2].rq_iov)
46 SMB2_close_free(&rqst[2]);
47}
48
49
a7d5c294
RS
50struct cop_vars {
51 struct cifs_open_parms oparms;
52 struct kvec rsp_iov[3];
53 struct smb_rqst rqst[3];
54 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
55 struct kvec qi_iov[1];
56 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
57 struct kvec close_iov[1];
58 struct smb2_file_rename_info rename_info;
59 struct smb2_file_link_info link_info;
60};
61
c5a5f38f
RS
62static int
63smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
64 struct cifs_sb_info *cifs_sb, const char *full_path,
65 __u32 desired_access, __u32 create_disposition,
c3ca78e2 66 __u32 create_options, umode_t mode, void *ptr, int command,
8de9e86c 67 struct cifsFileInfo *cfile)
c5a5f38f 68{
a7d5c294
RS
69 struct cop_vars *vars = NULL;
70 struct kvec *rsp_iov;
71 struct smb_rqst *rqst;
c5a5f38f
RS
72 int rc;
73 __le16 *utf16_path = NULL;
74 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
c5a5f38f
RS
75 struct cifs_fid fid;
76 struct cifs_ses *ses = tcon->ses;
352d96f3 77 struct TCP_Server_Info *server;
c5a5f38f 78 int num_rqst = 0;
c5a5f38f 79 int resp_buftype[3];
c2e0fe3f 80 struct smb2_query_info_rsp *qi_rsp = NULL;
c5a5f38f 81 int flags = 0;
c2e0fe3f 82 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
bb435512
RS
83 unsigned int size[2];
84 void *data[2];
bb435512 85 int len;
c5a5f38f 86
a7d5c294
RS
87 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
88 if (vars == NULL)
89 return -ENOMEM;
90 rqst = &vars->rqst[0];
91 rsp_iov = &vars->rsp_iov[0];
92
352d96f3
AA
93 server = cifs_pick_channel(ses);
94
c5a5f38f
RS
95 if (smb3_encryption_required(tcon))
96 flags |= CIFS_TRANSFORM_REQ;
97
c5a5f38f 98 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
c5a5f38f 99
8de9e86c
RS
100 /* We already have a handle so we can skip the open */
101 if (cfile)
102 goto after_open;
103
c5a5f38f
RS
104 /* Open */
105 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
8de9e86c
RS
106 if (!utf16_path) {
107 rc = -ENOMEM;
108 goto finished;
109 }
c5a5f38f 110
a7d5c294
RS
111 vars->oparms.tcon = tcon;
112 vars->oparms.desired_access = desired_access;
113 vars->oparms.disposition = create_disposition;
114 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
115 vars->oparms.fid = &fid;
116 vars->oparms.reconnect = false;
117 vars->oparms.mode = mode;
118
119 rqst[num_rqst].rq_iov = &vars->open_iov[0];
c5a5f38f 120 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
352d96f3
AA
121 rc = SMB2_open_init(tcon, server,
122 &rqst[num_rqst], &oplock, &vars->oparms,
c5a5f38f
RS
123 utf16_path);
124 kfree(utf16_path);
125 if (rc)
126 goto finished;
127
8de9e86c
RS
128 smb2_set_next_command(tcon, &rqst[num_rqst]);
129 after_open:
130 num_rqst++;
131 rc = 0;
c5a5f38f
RS
132
133 /* Operation */
134 switch (command) {
135 case SMB2_OP_QUERY_INFO:
a7d5c294 136 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
c5a5f38f
RS
137 rqst[num_rqst].rq_nvec = 1;
138
496902dc 139 if (cfile)
352d96f3
AA
140 rc = SMB2_query_info_init(tcon, server,
141 &rqst[num_rqst],
496902dc
RS
142 cfile->fid.persistent_fid,
143 cfile->fid.volatile_fid,
144 FILE_ALL_INFORMATION,
c5a5f38f
RS
145 SMB2_O_INFO_FILE, 0,
146 sizeof(struct smb2_file_all_info) +
f5b05d62 147 PATH_MAX * 2, 0, NULL);
496902dc 148 else {
352d96f3
AA
149 rc = SMB2_query_info_init(tcon, server,
150 &rqst[num_rqst],
496902dc
RS
151 COMPOUND_FID,
152 COMPOUND_FID,
352d96f3 153 FILE_ALL_INFORMATION,
496902dc
RS
154 SMB2_O_INFO_FILE, 0,
155 sizeof(struct smb2_file_all_info) +
156 PATH_MAX * 2, 0, NULL);
157 if (!rc) {
158 smb2_set_next_command(tcon, &rqst[num_rqst]);
159 smb2_set_related(&rqst[num_rqst]);
160 }
161 }
162
6a5f6592
SF
163 if (rc)
164 goto finished;
165 num_rqst++;
166 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
167 full_path);
168 break;
169 case SMB2_OP_POSIX_QUERY_INFO:
170 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
171 rqst[num_rqst].rq_nvec = 1;
172
173 if (cfile)
174 rc = SMB2_query_info_init(tcon, server,
175 &rqst[num_rqst],
176 cfile->fid.persistent_fid,
177 cfile->fid.volatile_fid,
178 SMB_FIND_FILE_POSIX_INFO,
179 SMB2_O_INFO_FILE, 0,
180 /* TBD: fix following to allow for longer SIDs */
181 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
182 (sizeof(struct cifs_sid) * 2), 0, NULL);
183 else {
184 rc = SMB2_query_info_init(tcon, server,
185 &rqst[num_rqst],
186 COMPOUND_FID,
187 COMPOUND_FID,
188 SMB_FIND_FILE_POSIX_INFO,
189 SMB2_O_INFO_FILE, 0,
190 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
191 (sizeof(struct cifs_sid) * 2), 0, NULL);
192 if (!rc) {
193 smb2_set_next_command(tcon, &rqst[num_rqst]);
194 smb2_set_related(&rqst[num_rqst]);
195 }
196 }
197
88a92c91
RS
198 if (rc)
199 goto finished;
496902dc 200 num_rqst++;
e4bd7c4a 201 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
c5a5f38f 202 break;
47dd9597 203 case SMB2_OP_DELETE:
8191576a 204 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
47dd9597 205 break;
f733e393
RS
206 case SMB2_OP_MKDIR:
207 /*
208 * Directories are created through parameters in the
209 * SMB2_open() call.
210 */
8191576a 211 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
f733e393 212 break;
c2e0fe3f 213 case SMB2_OP_RMDIR:
a7d5c294 214 rqst[num_rqst].rq_iov = &vars->si_iov[0];
c2e0fe3f
RS
215 rqst[num_rqst].rq_nvec = 1;
216
271b9c0c 217 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
c2e0fe3f
RS
218 data[0] = &delete_pending[0];
219
352d96f3
AA
220 rc = SMB2_set_info_init(tcon, server,
221 &rqst[num_rqst], COMPOUND_FID,
c2e0fe3f
RS
222 COMPOUND_FID, current->tgid,
223 FILE_DISPOSITION_INFORMATION,
224 SMB2_O_INFO_FILE, 0, data, size);
88a92c91
RS
225 if (rc)
226 goto finished;
e77fe73c 227 smb2_set_next_command(tcon, &rqst[num_rqst]);
c2e0fe3f 228 smb2_set_related(&rqst[num_rqst++]);
8191576a 229 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
c2e0fe3f 230 break;
f7bfe04b 231 case SMB2_OP_SET_EOF:
a7d5c294 232 rqst[num_rqst].rq_iov = &vars->si_iov[0];
f7bfe04b
RS
233 rqst[num_rqst].rq_nvec = 1;
234
235 size[0] = 8; /* sizeof __le64 */
236 data[0] = ptr;
237
352d96f3
AA
238 rc = SMB2_set_info_init(tcon, server,
239 &rqst[num_rqst], COMPOUND_FID,
f7bfe04b
RS
240 COMPOUND_FID, current->tgid,
241 FILE_END_OF_FILE_INFORMATION,
242 SMB2_O_INFO_FILE, 0, data, size);
88a92c91
RS
243 if (rc)
244 goto finished;
e77fe73c 245 smb2_set_next_command(tcon, &rqst[num_rqst]);
f7bfe04b 246 smb2_set_related(&rqst[num_rqst++]);
8191576a 247 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
f7bfe04b 248 break;
dcbf9103 249 case SMB2_OP_SET_INFO:
a7d5c294 250 rqst[num_rqst].rq_iov = &vars->si_iov[0];
dcbf9103
RS
251 rqst[num_rqst].rq_nvec = 1;
252
bb435512 253
dcbf9103
RS
254 size[0] = sizeof(FILE_BASIC_INFO);
255 data[0] = ptr;
256
dc9300a6 257 if (cfile)
352d96f3
AA
258 rc = SMB2_set_info_init(tcon, server,
259 &rqst[num_rqst],
dc9300a6
RS
260 cfile->fid.persistent_fid,
261 cfile->fid.volatile_fid, current->tgid,
262 FILE_BASIC_INFORMATION,
263 SMB2_O_INFO_FILE, 0, data, size);
264 else {
352d96f3
AA
265 rc = SMB2_set_info_init(tcon, server,
266 &rqst[num_rqst],
dc9300a6
RS
267 COMPOUND_FID,
268 COMPOUND_FID, current->tgid,
269 FILE_BASIC_INFORMATION,
270 SMB2_O_INFO_FILE, 0, data, size);
271 if (!rc) {
272 smb2_set_next_command(tcon, &rqst[num_rqst]);
273 smb2_set_related(&rqst[num_rqst]);
274 }
275 }
276
88a92c91
RS
277 if (rc)
278 goto finished;
dc9300a6 279 num_rqst++;
8191576a
SF
280 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
281 full_path);
dcbf9103 282 break;
bb435512 283 case SMB2_OP_RENAME:
a7d5c294 284 rqst[num_rqst].rq_iov = &vars->si_iov[0];
bb435512
RS
285 rqst[num_rqst].rq_nvec = 2;
286
287 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
288
a7d5c294
RS
289 vars->rename_info.ReplaceIfExists = 1;
290 vars->rename_info.RootDirectory = 0;
291 vars->rename_info.FileNameLength = cpu_to_le32(len);
bb435512
RS
292
293 size[0] = sizeof(struct smb2_file_rename_info);
a7d5c294 294 data[0] = &vars->rename_info;
bb435512
RS
295
296 size[1] = len + 2 /* null */;
297 data[1] = (__le16 *)ptr;
298
8de9e86c 299 if (cfile)
352d96f3
AA
300 rc = SMB2_set_info_init(tcon, server,
301 &rqst[num_rqst],
8de9e86c
RS
302 cfile->fid.persistent_fid,
303 cfile->fid.volatile_fid,
304 current->tgid, FILE_RENAME_INFORMATION,
305 SMB2_O_INFO_FILE, 0, data, size);
306 else {
352d96f3
AA
307 rc = SMB2_set_info_init(tcon, server,
308 &rqst[num_rqst],
8de9e86c
RS
309 COMPOUND_FID, COMPOUND_FID,
310 current->tgid, FILE_RENAME_INFORMATION,
bb435512 311 SMB2_O_INFO_FILE, 0, data, size);
dc9300a6
RS
312 if (!rc) {
313 smb2_set_next_command(tcon, &rqst[num_rqst]);
314 smb2_set_related(&rqst[num_rqst]);
315 }
8de9e86c 316 }
88a92c91
RS
317 if (rc)
318 goto finished;
8de9e86c 319 num_rqst++;
8191576a 320 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
bb435512
RS
321 break;
322 case SMB2_OP_HARDLINK:
a7d5c294 323 rqst[num_rqst].rq_iov = &vars->si_iov[0];
bb435512
RS
324 rqst[num_rqst].rq_nvec = 2;
325
326 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
327
a7d5c294
RS
328 vars->link_info.ReplaceIfExists = 0;
329 vars->link_info.RootDirectory = 0;
330 vars->link_info.FileNameLength = cpu_to_le32(len);
bb435512
RS
331
332 size[0] = sizeof(struct smb2_file_link_info);
a7d5c294 333 data[0] = &vars->link_info;
bb435512
RS
334
335 size[1] = len + 2 /* null */;
336 data[1] = (__le16 *)ptr;
337
352d96f3
AA
338 rc = SMB2_set_info_init(tcon, server,
339 &rqst[num_rqst], COMPOUND_FID,
bb435512
RS
340 COMPOUND_FID, current->tgid,
341 FILE_LINK_INFORMATION,
342 SMB2_O_INFO_FILE, 0, data, size);
88a92c91
RS
343 if (rc)
344 goto finished;
e77fe73c 345 smb2_set_next_command(tcon, &rqst[num_rqst]);
bb435512 346 smb2_set_related(&rqst[num_rqst++]);
8191576a 347 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
bb435512 348 break;
c5a5f38f
RS
349 default:
350 cifs_dbg(VFS, "Invalid command\n");
351 rc = -EINVAL;
352 }
353 if (rc)
354 goto finished;
355
8de9e86c
RS
356 /* We already have a handle so we can skip the close */
357 if (cfile)
358 goto after_close;
c5a5f38f 359 /* Close */
a7d5c294 360 rqst[num_rqst].rq_iov = &vars->close_iov[0];
c5a5f38f 361 rqst[num_rqst].rq_nvec = 1;
352d96f3
AA
362 rc = SMB2_close_init(tcon, server,
363 &rqst[num_rqst], COMPOUND_FID,
43f8a6a7 364 COMPOUND_FID, false);
8de9e86c 365 smb2_set_related(&rqst[num_rqst]);
c5a5f38f
RS
366 if (rc)
367 goto finished;
8de9e86c
RS
368 after_close:
369 num_rqst++;
370
371 if (cfile) {
372 cifsFileInfo_put(cfile);
373 cfile = NULL;
352d96f3
AA
374 rc = compound_send_recv(xid, ses, server,
375 flags, num_rqst - 2,
8de9e86c
RS
376 &rqst[1], &resp_buftype[1],
377 &rsp_iov[1]);
378 } else
352d96f3
AA
379 rc = compound_send_recv(xid, ses, server,
380 flags, num_rqst,
8de9e86c
RS
381 rqst, resp_buftype,
382 rsp_iov);
c5a5f38f
RS
383
384 finished:
8de9e86c
RS
385 if (cfile)
386 cifsFileInfo_put(cfile);
387
c5a5f38f 388 SMB2_open_free(&rqst[0]);
7dcc82c2 389 if (rc == -EREMCHG) {
a0a3036b 390 pr_warn_once("server share %s deleted\n", tcon->treeName);
7dcc82c2
SF
391 tcon->need_reconnect = true;
392 }
393
c5a5f38f
RS
394 switch (command) {
395 case SMB2_OP_QUERY_INFO:
396 if (rc == 0) {
c2e0fe3f
RS
397 qi_rsp = (struct smb2_query_info_rsp *)
398 rsp_iov[1].iov_base;
c5a5f38f 399 rc = smb2_validate_and_copy_iov(
c2e0fe3f
RS
400 le16_to_cpu(qi_rsp->OutputBufferOffset),
401 le32_to_cpu(qi_rsp->OutputBufferLength),
c5a5f38f 402 &rsp_iov[1], sizeof(struct smb2_file_all_info),
c2e0fe3f 403 ptr);
c5a5f38f
RS
404 }
405 if (rqst[1].rq_iov)
406 SMB2_query_info_free(&rqst[1]);
407 if (rqst[2].rq_iov)
408 SMB2_close_free(&rqst[2]);
8191576a
SF
409 if (rc)
410 trace_smb3_query_info_compound_err(xid, ses->Suid,
411 tcon->tid, rc);
412 else
413 trace_smb3_query_info_compound_done(xid, ses->Suid,
414 tcon->tid);
c5a5f38f 415 break;
6a5f6592
SF
416 case SMB2_OP_POSIX_QUERY_INFO:
417 if (rc == 0) {
418 qi_rsp = (struct smb2_query_info_rsp *)
419 rsp_iov[1].iov_base;
420 rc = smb2_validate_and_copy_iov(
421 le16_to_cpu(qi_rsp->OutputBufferOffset),
422 le32_to_cpu(qi_rsp->OutputBufferLength),
423 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
424 }
425 if (rqst[1].rq_iov)
426 SMB2_query_info_free(&rqst[1]);
427 if (rqst[2].rq_iov)
428 SMB2_close_free(&rqst[2]);
429 if (rc)
e4bd7c4a 430 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
6a5f6592 431 else
e4bd7c4a 432 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
6a5f6592 433 break;
47dd9597 434 case SMB2_OP_DELETE:
8191576a
SF
435 if (rc)
436 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
437 else
438 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
439 if (rqst[1].rq_iov)
440 SMB2_close_free(&rqst[1]);
441 break;
f733e393 442 case SMB2_OP_MKDIR:
8191576a
SF
443 if (rc)
444 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
445 else
446 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
f733e393
RS
447 if (rqst[1].rq_iov)
448 SMB2_close_free(&rqst[1]);
449 break;
bb435512 450 case SMB2_OP_HARDLINK:
8191576a
SF
451 if (rc)
452 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
453 else
454 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
455 free_set_inf_compound(rqst);
456 break;
bb435512 457 case SMB2_OP_RENAME:
8191576a
SF
458 if (rc)
459 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
460 else
461 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
462 free_set_inf_compound(rqst);
463 break;
c2e0fe3f 464 case SMB2_OP_RMDIR:
8191576a
SF
465 if (rc)
466 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
467 else
468 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
469 free_set_inf_compound(rqst);
470 break;
dcbf9103 471 case SMB2_OP_SET_EOF:
8191576a
SF
472 if (rc)
473 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
474 else
475 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
476 free_set_inf_compound(rqst);
477 break;
dcbf9103 478 case SMB2_OP_SET_INFO:
8191576a
SF
479 if (rc)
480 trace_smb3_set_info_compound_err(xid, ses->Suid,
481 tcon->tid, rc);
482 else
483 trace_smb3_set_info_compound_done(xid, ses->Suid,
484 tcon->tid);
485 free_set_inf_compound(rqst);
c2e0fe3f 486 break;
c5a5f38f
RS
487 }
488 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
489 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
490 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
a7d5c294 491 kfree(vars);
c5a5f38f
RS
492 return rc;
493}
494
f0df737e 495void
be4cb9e3
PS
496move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
497{
498 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
499 dst->CurrentByteOffset = src->CurrentByteOffset;
500 dst->Mode = src->Mode;
501 dst->AlignmentRequirement = src->AlignmentRequirement;
502 dst->IndexNumber1 = 0; /* we don't use it */
503}
504
505int
506smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
507 struct cifs_sb_info *cifs_sb, const char *full_path,
eb85d94b 508 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
be4cb9e3
PS
509{
510 int rc;
511 struct smb2_file_all_info *smb2_data;
61351d6d 512 __u32 create_options = 0;
6a9cbdd1
PS
513 struct cifs_fid fid;
514 bool no_cached_open = tcon->nohandlecache;
496902dc 515 struct cifsFileInfo *cfile;
be4cb9e3
PS
516
517 *adjust_tz = false;
eb85d94b 518 *symlink = false;
be4cb9e3 519
1bbe4997 520 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
be4cb9e3
PS
521 GFP_KERNEL);
522 if (smb2_data == NULL)
523 return -ENOMEM;
6a9cbdd1
PS
524
525 /* If it is a root and its handle is cached then use it */
526 if (!strlen(full_path) && !no_cached_open) {
0f060936 527 rc = open_shroot(xid, tcon, cifs_sb, &fid);
6a9cbdd1
PS
528 if (rc)
529 goto out;
b0f6df73
RS
530
531 if (tcon->crfid.file_all_info_is_valid) {
532 move_smb2_info_to_cifs(data,
533 &tcon->crfid.file_all_info);
534 } else {
535 rc = SMB2_query_info(xid, tcon, fid.persistent_fid,
536 fid.volatile_fid, smb2_data);
537 if (!rc)
538 move_smb2_info_to_cifs(data, smb2_data);
539 }
6a9cbdd1 540 close_shroot(&tcon->crfid);
6a9cbdd1
PS
541 goto out;
542 }
543
496902dc 544 cifs_get_readable_path(tcon, full_path, &cfile);
c5a5f38f 545 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
61351d6d 546 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
c3ca78e2 547 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
eb85d94b
PS
548 if (rc == -EOPNOTSUPP) {
549 *symlink = true;
61351d6d
SF
550 create_options |= OPEN_REPARSE_POINT;
551
eb85d94b 552 /* Failed on a symbolic link - query a reparse point info */
c5a5f38f
RS
553 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
554 FILE_READ_ATTRIBUTES, FILE_OPEN,
c3ca78e2
SF
555 create_options, ACL_NO_MODE,
556 smb2_data, SMB2_OP_QUERY_INFO, NULL);
eb85d94b 557 }
be4cb9e3
PS
558 if (rc)
559 goto out;
560
561 move_smb2_info_to_cifs(data, smb2_data);
562out:
563 kfree(smb2_data);
564 return rc;
565}
a0e73183 566
6a5f6592
SF
567
568int
569smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
570 struct cifs_sb_info *cifs_sb, const char *full_path,
571 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *symlink)
572{
573 int rc;
574 __u32 create_options = 0;
575 struct cifsFileInfo *cfile;
576 struct smb311_posix_qinfo *smb2_data;
577
578 *adjust_tz = false;
579 *symlink = false;
580
581 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
582 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
583 GFP_KERNEL);
584 if (smb2_data == NULL)
585 return -ENOMEM;
586
587 /*
588 * BB TODO: Add support for using the cached root handle.
589 * Create SMB2_query_posix_info worker function to do non-compounded query
590 * when we already have an open file handle for this. For now this is fast enough
591 * (always using the compounded version).
592 */
593
594 cifs_get_readable_path(tcon, full_path, &cfile);
595 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
596 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
597 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
598 if (rc == -EOPNOTSUPP) {
599 /* BB TODO: When support for special files added to Samba re-verify this path */
600 *symlink = true;
601 create_options |= OPEN_REPARSE_POINT;
602
603 /* Failed on a symbolic link - query a reparse point info */
604 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
605 FILE_READ_ATTRIBUTES, FILE_OPEN,
606 create_options, ACL_NO_MODE,
607 smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
608 }
609 if (rc)
610 goto out;
611
612 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
613 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
614
615out:
616 kfree(smb2_data);
617 return rc;
618}
619
a0e73183 620int
c3ca78e2
SF
621smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
622 struct cifs_tcon *tcon, const char *name,
a0e73183
PS
623 struct cifs_sb_info *cifs_sb)
624{
f733e393
RS
625 return smb2_compound_op(xid, tcon, cifs_sb, name,
626 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
c3ca78e2
SF
627 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
628 NULL);
a0e73183
PS
629}
630
631void
632smb2_mkdir_setinfo(struct inode *inode, const char *name,
633 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
634 const unsigned int xid)
635{
636 FILE_BASIC_INFO data;
637 struct cifsInodeInfo *cifs_i;
dc9300a6 638 struct cifsFileInfo *cfile;
a0e73183
PS
639 u32 dosattrs;
640 int tmprc;
641
642 memset(&data, 0, sizeof(data));
643 cifs_i = CIFS_I(inode);
644 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
645 data.Attributes = cpu_to_le32(dosattrs);
86f740f2 646 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
dcbf9103
RS
647 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
648 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
c3ca78e2
SF
649 CREATE_NOT_FILE, ACL_NO_MODE,
650 &data, SMB2_OP_SET_INFO, cfile);
a0e73183
PS
651 if (tmprc == 0)
652 cifs_i->cifsAttrs = dosattrs;
653}
1a500f01
PS
654
655int
656smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
657 struct cifs_sb_info *cifs_sb)
658{
c2e0fe3f 659 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
c3ca78e2 660 CREATE_NOT_FILE, ACL_NO_MODE,
8de9e86c 661 NULL, SMB2_OP_RMDIR, NULL);
1a500f01 662}
cbe6f439
PS
663
664int
665smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
666 struct cifs_sb_info *cifs_sb)
667{
47dd9597
RS
668 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
669 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
c3ca78e2 670 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
cbe6f439 671}
35143eb5 672
568798cc
PS
673static int
674smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
675 const char *from_name, const char *to_name,
8de9e86c
RS
676 struct cifs_sb_info *cifs_sb, __u32 access, int command,
677 struct cifsFileInfo *cfile)
35143eb5
PS
678{
679 __le16 *smb2_to_name = NULL;
680 int rc;
681
682 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
683 if (smb2_to_name == NULL) {
684 rc = -ENOMEM;
685 goto smb2_rename_path;
686 }
bb435512 687 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
c3ca78e2
SF
688 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
689 command, cfile);
35143eb5
PS
690smb2_rename_path:
691 kfree(smb2_to_name);
692 return rc;
693}
568798cc
PS
694
695int
696smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
697 const char *from_name, const char *to_name,
698 struct cifs_sb_info *cifs_sb)
699{
8de9e86c
RS
700 struct cifsFileInfo *cfile;
701
86f740f2 702 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
8de9e86c
RS
703
704 return smb2_set_path_attr(xid, tcon, from_name, to_name,
705 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
568798cc
PS
706}
707
708int
709smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
710 const char *from_name, const char *to_name,
711 struct cifs_sb_info *cifs_sb)
712{
713 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
8de9e86c
RS
714 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
715 NULL);
568798cc 716}
c839ff24
PS
717
718int
719smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
720 const char *full_path, __u64 size,
721 struct cifs_sb_info *cifs_sb, bool set_alloc)
722{
723 __le64 eof = cpu_to_le64(size);
f7bfe04b
RS
724
725 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
c3ca78e2
SF
726 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
727 &eof, SMB2_OP_SET_EOF, NULL);
c839ff24 728}
1feeaac7
PS
729
730int
731smb2_set_file_info(struct inode *inode, const char *full_path,
732 FILE_BASIC_INFO *buf, const unsigned int xid)
733{
734 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
735 struct tcon_link *tlink;
736 int rc;
737
18dd8e1a 738 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
fd09b7d3 739 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
18dd8e1a
SF
740 (buf->Attributes == 0))
741 return 0; /* would be a no op, no sense sending this */
742
1feeaac7
PS
743 tlink = cifs_sb_tlink(cifs_sb);
744 if (IS_ERR(tlink))
745 return PTR_ERR(tlink);
18dd8e1a 746
dcbf9103 747 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
c3ca78e2
SF
748 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
749 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
1feeaac7
PS
750 cifs_put_tlink(tlink);
751 return rc;
752}