Loading...
1// SPDX-License-Identifier: LGPL-2.1
2/*
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 */
10#include <linux/fs.h>
11#include <linux/stat.h>
12#include <linux/slab.h>
13#include <linux/pagemap.h>
14#include <asm/div64.h>
15#include "cifsfs.h"
16#include "cifspdu.h"
17#include "cifsglob.h"
18#include "cifsproto.h"
19#include "cifs_debug.h"
20#include "cifs_fs_sb.h"
21#include "cifs_unicode.h"
22#include "fscache.h"
23#include "smb2glob.h"
24#include "smb2pdu.h"
25#include "smb2proto.h"
26#include "cached_dir.h"
27#include "smb2status.h"
28
29static void
30free_set_inf_compound(struct smb_rqst *rqst)
31{
32 if (rqst[1].rq_iov)
33 SMB2_set_info_free(&rqst[1]);
34 if (rqst[2].rq_iov)
35 SMB2_close_free(&rqst[2]);
36}
37
38
39struct cop_vars {
40 struct cifs_open_parms oparms;
41 struct kvec rsp_iov[3];
42 struct smb_rqst rqst[3];
43 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
44 struct kvec qi_iov[1];
45 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
46 struct kvec close_iov[1];
47 struct smb2_file_rename_info rename_info;
48 struct smb2_file_link_info link_info;
49};
50
51/*
52 * note: If cfile is passed, the reference to it is dropped here.
53 * So make sure that you do not reuse cfile after return from this func.
54 *
55 * If passing @err_iov and @err_buftype, ensure to make them both large enough (>= 3) to hold all
56 * error responses. Caller is also responsible for freeing them up.
57 */
58static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
59 struct cifs_sb_info *cifs_sb, const char *full_path,
60 __u32 desired_access, __u32 create_disposition, __u32 create_options,
61 umode_t mode, void *ptr, int command, struct cifsFileInfo *cfile,
62 __u8 **extbuf, size_t *extbuflen,
63 struct kvec *err_iov, int *err_buftype)
64{
65 struct cop_vars *vars = NULL;
66 struct kvec *rsp_iov;
67 struct smb_rqst *rqst;
68 int rc;
69 __le16 *utf16_path = NULL;
70 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
71 struct cifs_fid fid;
72 struct cifs_ses *ses = tcon->ses;
73 struct TCP_Server_Info *server;
74 int num_rqst = 0;
75 int resp_buftype[3];
76 struct smb2_query_info_rsp *qi_rsp = NULL;
77 struct cifs_open_info_data *idata;
78 int flags = 0;
79 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
80 unsigned int size[2];
81 void *data[2];
82 int len;
83
84 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
85 if (vars == NULL)
86 return -ENOMEM;
87 rqst = &vars->rqst[0];
88 rsp_iov = &vars->rsp_iov[0];
89
90 server = cifs_pick_channel(ses);
91
92 if (smb3_encryption_required(tcon))
93 flags |= CIFS_TRANSFORM_REQ;
94
95 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
96
97 /* We already have a handle so we can skip the open */
98 if (cfile)
99 goto after_open;
100
101 /* Open */
102 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
103 if (!utf16_path) {
104 rc = -ENOMEM;
105 goto finished;
106 }
107
108 vars->oparms.tcon = tcon;
109 vars->oparms.desired_access = desired_access;
110 vars->oparms.disposition = create_disposition;
111 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
112 vars->oparms.fid = &fid;
113 vars->oparms.reconnect = false;
114 vars->oparms.mode = mode;
115 vars->oparms.cifs_sb = cifs_sb;
116
117 rqst[num_rqst].rq_iov = &vars->open_iov[0];
118 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
119 rc = SMB2_open_init(tcon, server,
120 &rqst[num_rqst], &oplock, &vars->oparms,
121 utf16_path);
122 kfree(utf16_path);
123 if (rc)
124 goto finished;
125
126 smb2_set_next_command(tcon, &rqst[num_rqst]);
127 after_open:
128 num_rqst++;
129 rc = 0;
130
131 /* Operation */
132 switch (command) {
133 case SMB2_OP_QUERY_INFO:
134 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
135 rqst[num_rqst].rq_nvec = 1;
136
137 if (cfile)
138 rc = SMB2_query_info_init(tcon, server,
139 &rqst[num_rqst],
140 cfile->fid.persistent_fid,
141 cfile->fid.volatile_fid,
142 FILE_ALL_INFORMATION,
143 SMB2_O_INFO_FILE, 0,
144 sizeof(struct smb2_file_all_info) +
145 PATH_MAX * 2, 0, NULL);
146 else {
147 rc = SMB2_query_info_init(tcon, server,
148 &rqst[num_rqst],
149 COMPOUND_FID,
150 COMPOUND_FID,
151 FILE_ALL_INFORMATION,
152 SMB2_O_INFO_FILE, 0,
153 sizeof(struct smb2_file_all_info) +
154 PATH_MAX * 2, 0, NULL);
155 if (!rc) {
156 smb2_set_next_command(tcon, &rqst[num_rqst]);
157 smb2_set_related(&rqst[num_rqst]);
158 }
159 }
160
161 if (rc)
162 goto finished;
163 num_rqst++;
164 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
165 full_path);
166 break;
167 case SMB2_OP_POSIX_QUERY_INFO:
168 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
169 rqst[num_rqst].rq_nvec = 1;
170
171 if (cfile)
172 rc = SMB2_query_info_init(tcon, server,
173 &rqst[num_rqst],
174 cfile->fid.persistent_fid,
175 cfile->fid.volatile_fid,
176 SMB_FIND_FILE_POSIX_INFO,
177 SMB2_O_INFO_FILE, 0,
178 /* TBD: fix following to allow for longer SIDs */
179 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
180 (sizeof(struct cifs_sid) * 2), 0, NULL);
181 else {
182 rc = SMB2_query_info_init(tcon, server,
183 &rqst[num_rqst],
184 COMPOUND_FID,
185 COMPOUND_FID,
186 SMB_FIND_FILE_POSIX_INFO,
187 SMB2_O_INFO_FILE, 0,
188 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
189 (sizeof(struct cifs_sid) * 2), 0, NULL);
190 if (!rc) {
191 smb2_set_next_command(tcon, &rqst[num_rqst]);
192 smb2_set_related(&rqst[num_rqst]);
193 }
194 }
195
196 if (rc)
197 goto finished;
198 num_rqst++;
199 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
200 break;
201 case SMB2_OP_DELETE:
202 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
203 break;
204 case SMB2_OP_MKDIR:
205 /*
206 * Directories are created through parameters in the
207 * SMB2_open() call.
208 */
209 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
210 break;
211 case SMB2_OP_RMDIR:
212 rqst[num_rqst].rq_iov = &vars->si_iov[0];
213 rqst[num_rqst].rq_nvec = 1;
214
215 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
216 data[0] = &delete_pending[0];
217
218 rc = SMB2_set_info_init(tcon, server,
219 &rqst[num_rqst], COMPOUND_FID,
220 COMPOUND_FID, current->tgid,
221 FILE_DISPOSITION_INFORMATION,
222 SMB2_O_INFO_FILE, 0, data, size);
223 if (rc)
224 goto finished;
225 smb2_set_next_command(tcon, &rqst[num_rqst]);
226 smb2_set_related(&rqst[num_rqst++]);
227 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
228 break;
229 case SMB2_OP_SET_EOF:
230 rqst[num_rqst].rq_iov = &vars->si_iov[0];
231 rqst[num_rqst].rq_nvec = 1;
232
233 size[0] = 8; /* sizeof __le64 */
234 data[0] = ptr;
235
236 rc = SMB2_set_info_init(tcon, server,
237 &rqst[num_rqst], COMPOUND_FID,
238 COMPOUND_FID, current->tgid,
239 FILE_END_OF_FILE_INFORMATION,
240 SMB2_O_INFO_FILE, 0, data, size);
241 if (rc)
242 goto finished;
243 smb2_set_next_command(tcon, &rqst[num_rqst]);
244 smb2_set_related(&rqst[num_rqst++]);
245 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
246 break;
247 case SMB2_OP_SET_INFO:
248 rqst[num_rqst].rq_iov = &vars->si_iov[0];
249 rqst[num_rqst].rq_nvec = 1;
250
251
252 size[0] = sizeof(FILE_BASIC_INFO);
253 data[0] = ptr;
254
255 if (cfile)
256 rc = SMB2_set_info_init(tcon, server,
257 &rqst[num_rqst],
258 cfile->fid.persistent_fid,
259 cfile->fid.volatile_fid, current->tgid,
260 FILE_BASIC_INFORMATION,
261 SMB2_O_INFO_FILE, 0, data, size);
262 else {
263 rc = SMB2_set_info_init(tcon, server,
264 &rqst[num_rqst],
265 COMPOUND_FID,
266 COMPOUND_FID, current->tgid,
267 FILE_BASIC_INFORMATION,
268 SMB2_O_INFO_FILE, 0, data, size);
269 if (!rc) {
270 smb2_set_next_command(tcon, &rqst[num_rqst]);
271 smb2_set_related(&rqst[num_rqst]);
272 }
273 }
274
275 if (rc)
276 goto finished;
277 num_rqst++;
278 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
279 full_path);
280 break;
281 case SMB2_OP_RENAME:
282 rqst[num_rqst].rq_iov = &vars->si_iov[0];
283 rqst[num_rqst].rq_nvec = 2;
284
285 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
286
287 vars->rename_info.ReplaceIfExists = 1;
288 vars->rename_info.RootDirectory = 0;
289 vars->rename_info.FileNameLength = cpu_to_le32(len);
290
291 size[0] = sizeof(struct smb2_file_rename_info);
292 data[0] = &vars->rename_info;
293
294 size[1] = len + 2 /* null */;
295 data[1] = (__le16 *)ptr;
296
297 if (cfile)
298 rc = SMB2_set_info_init(tcon, server,
299 &rqst[num_rqst],
300 cfile->fid.persistent_fid,
301 cfile->fid.volatile_fid,
302 current->tgid, FILE_RENAME_INFORMATION,
303 SMB2_O_INFO_FILE, 0, data, size);
304 else {
305 rc = SMB2_set_info_init(tcon, server,
306 &rqst[num_rqst],
307 COMPOUND_FID, COMPOUND_FID,
308 current->tgid, FILE_RENAME_INFORMATION,
309 SMB2_O_INFO_FILE, 0, data, size);
310 if (!rc) {
311 smb2_set_next_command(tcon, &rqst[num_rqst]);
312 smb2_set_related(&rqst[num_rqst]);
313 }
314 }
315 if (rc)
316 goto finished;
317 num_rqst++;
318 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
319 break;
320 case SMB2_OP_HARDLINK:
321 rqst[num_rqst].rq_iov = &vars->si_iov[0];
322 rqst[num_rqst].rq_nvec = 2;
323
324 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
325
326 vars->link_info.ReplaceIfExists = 0;
327 vars->link_info.RootDirectory = 0;
328 vars->link_info.FileNameLength = cpu_to_le32(len);
329
330 size[0] = sizeof(struct smb2_file_link_info);
331 data[0] = &vars->link_info;
332
333 size[1] = len + 2 /* null */;
334 data[1] = (__le16 *)ptr;
335
336 rc = SMB2_set_info_init(tcon, server,
337 &rqst[num_rqst], COMPOUND_FID,
338 COMPOUND_FID, current->tgid,
339 FILE_LINK_INFORMATION,
340 SMB2_O_INFO_FILE, 0, data, size);
341 if (rc)
342 goto finished;
343 smb2_set_next_command(tcon, &rqst[num_rqst]);
344 smb2_set_related(&rqst[num_rqst++]);
345 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
346 break;
347 default:
348 cifs_dbg(VFS, "Invalid command\n");
349 rc = -EINVAL;
350 }
351 if (rc)
352 goto finished;
353
354 /* We already have a handle so we can skip the close */
355 if (cfile)
356 goto after_close;
357 /* Close */
358 flags |= CIFS_CP_CREATE_CLOSE_OP;
359 rqst[num_rqst].rq_iov = &vars->close_iov[0];
360 rqst[num_rqst].rq_nvec = 1;
361 rc = SMB2_close_init(tcon, server,
362 &rqst[num_rqst], COMPOUND_FID,
363 COMPOUND_FID, false);
364 smb2_set_related(&rqst[num_rqst]);
365 if (rc)
366 goto finished;
367 after_close:
368 num_rqst++;
369
370 if (cfile) {
371 rc = compound_send_recv(xid, ses, server,
372 flags, num_rqst - 2,
373 &rqst[1], &resp_buftype[1],
374 &rsp_iov[1]);
375 } else
376 rc = compound_send_recv(xid, ses, server,
377 flags, num_rqst,
378 rqst, resp_buftype,
379 rsp_iov);
380
381 finished:
382 if (cfile)
383 cifsFileInfo_put(cfile);
384
385 SMB2_open_free(&rqst[0]);
386 if (rc == -EREMCHG) {
387 pr_warn_once("server share %s deleted\n", tcon->tree_name);
388 tcon->need_reconnect = true;
389 }
390
391 switch (command) {
392 case SMB2_OP_QUERY_INFO:
393 idata = ptr;
394 if (rc == 0 && cfile && cfile->symlink_target) {
395 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
396 if (!idata->symlink_target)
397 rc = -ENOMEM;
398 }
399 if (rc == 0) {
400 qi_rsp = (struct smb2_query_info_rsp *)
401 rsp_iov[1].iov_base;
402 rc = smb2_validate_and_copy_iov(
403 le16_to_cpu(qi_rsp->OutputBufferOffset),
404 le32_to_cpu(qi_rsp->OutputBufferLength),
405 &rsp_iov[1], sizeof(idata->fi), (char *)&idata->fi);
406 }
407 if (rqst[1].rq_iov)
408 SMB2_query_info_free(&rqst[1]);
409 if (rqst[2].rq_iov)
410 SMB2_close_free(&rqst[2]);
411 if (rc)
412 trace_smb3_query_info_compound_err(xid, ses->Suid,
413 tcon->tid, rc);
414 else
415 trace_smb3_query_info_compound_done(xid, ses->Suid,
416 tcon->tid);
417 break;
418 case SMB2_OP_POSIX_QUERY_INFO:
419 idata = ptr;
420 if (rc == 0 && cfile && cfile->symlink_target) {
421 idata->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
422 if (!idata->symlink_target)
423 rc = -ENOMEM;
424 }
425 if (rc == 0) {
426 qi_rsp = (struct smb2_query_info_rsp *)
427 rsp_iov[1].iov_base;
428 rc = smb2_validate_and_copy_iov(
429 le16_to_cpu(qi_rsp->OutputBufferOffset),
430 le32_to_cpu(qi_rsp->OutputBufferLength),
431 &rsp_iov[1], sizeof(idata->posix_fi) /* add SIDs */,
432 (char *)&idata->posix_fi);
433 }
434 if (rc == 0) {
435 unsigned int length = le32_to_cpu(qi_rsp->OutputBufferLength);
436
437 if (length > sizeof(idata->posix_fi)) {
438 char *base = (char *)rsp_iov[1].iov_base +
439 le16_to_cpu(qi_rsp->OutputBufferOffset) +
440 sizeof(idata->posix_fi);
441 *extbuflen = length - sizeof(idata->posix_fi);
442 *extbuf = kmemdup(base, *extbuflen, GFP_KERNEL);
443 if (!*extbuf)
444 rc = -ENOMEM;
445 } else {
446 rc = -EINVAL;
447 }
448 }
449 if (rqst[1].rq_iov)
450 SMB2_query_info_free(&rqst[1]);
451 if (rqst[2].rq_iov)
452 SMB2_close_free(&rqst[2]);
453 if (rc)
454 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
455 else
456 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
457 break;
458 case SMB2_OP_DELETE:
459 if (rc)
460 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
461 else
462 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
463 if (rqst[1].rq_iov)
464 SMB2_close_free(&rqst[1]);
465 break;
466 case SMB2_OP_MKDIR:
467 if (rc)
468 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
469 else
470 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
471 if (rqst[1].rq_iov)
472 SMB2_close_free(&rqst[1]);
473 break;
474 case SMB2_OP_HARDLINK:
475 if (rc)
476 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
477 else
478 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
479 free_set_inf_compound(rqst);
480 break;
481 case SMB2_OP_RENAME:
482 if (rc)
483 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
484 else
485 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
486 free_set_inf_compound(rqst);
487 break;
488 case SMB2_OP_RMDIR:
489 if (rc)
490 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
491 else
492 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
493 free_set_inf_compound(rqst);
494 break;
495 case SMB2_OP_SET_EOF:
496 if (rc)
497 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
498 else
499 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
500 free_set_inf_compound(rqst);
501 break;
502 case SMB2_OP_SET_INFO:
503 if (rc)
504 trace_smb3_set_info_compound_err(xid, ses->Suid,
505 tcon->tid, rc);
506 else
507 trace_smb3_set_info_compound_done(xid, ses->Suid,
508 tcon->tid);
509 free_set_inf_compound(rqst);
510 break;
511 }
512
513 if (rc && err_iov && err_buftype) {
514 memcpy(err_iov, rsp_iov, 3 * sizeof(*err_iov));
515 memcpy(err_buftype, resp_buftype, 3 * sizeof(*err_buftype));
516 } else {
517 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
518 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
519 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
520 }
521 kfree(vars);
522 return rc;
523}
524
525int smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
526 struct cifs_sb_info *cifs_sb, const char *full_path,
527 struct cifs_open_info_data *data, bool *adjust_tz, bool *reparse)
528{
529 int rc;
530 __u32 create_options = 0;
531 struct cifsFileInfo *cfile;
532 struct cached_fid *cfid = NULL;
533 struct kvec err_iov[3] = {};
534 int err_buftype[3] = {};
535
536 *adjust_tz = false;
537 *reparse = false;
538
539 if (strcmp(full_path, ""))
540 rc = -ENOENT;
541 else
542 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
543 /* If it is a root and its handle is cached then use it */
544 if (!rc) {
545 if (cfid->file_all_info_is_valid) {
546 memcpy(&data->fi, &cfid->file_all_info, sizeof(data->fi));
547 } else {
548 rc = SMB2_query_info(xid, tcon, cfid->fid.persistent_fid,
549 cfid->fid.volatile_fid, &data->fi);
550 }
551 close_cached_dir(cfid);
552 return rc;
553 }
554
555 cifs_get_readable_path(tcon, full_path, &cfile);
556 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
557 create_options, ACL_NO_MODE, data, SMB2_OP_QUERY_INFO, cfile,
558 NULL, NULL, err_iov, err_buftype);
559 if (rc) {
560 struct smb2_hdr *hdr = err_iov[0].iov_base;
561
562 if (unlikely(!hdr || err_buftype[0] == CIFS_NO_BUFFER))
563 goto out;
564 if (rc == -EOPNOTSUPP && hdr->Command == SMB2_CREATE &&
565 hdr->Status == STATUS_STOPPED_ON_SYMLINK) {
566 rc = smb2_parse_symlink_response(cifs_sb, err_iov,
567 &data->symlink_target);
568 if (rc)
569 goto out;
570
571 *reparse = true;
572 create_options |= OPEN_REPARSE_POINT;
573
574 /* Failed on a symbolic link - query a reparse point info */
575 cifs_get_readable_path(tcon, full_path, &cfile);
576 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
577 FILE_READ_ATTRIBUTES, FILE_OPEN,
578 create_options, ACL_NO_MODE, data,
579 SMB2_OP_QUERY_INFO, cfile, NULL, NULL,
580 NULL, NULL);
581 goto out;
582 } else if (rc != -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) &&
583 hdr->Status == STATUS_OBJECT_NAME_INVALID) {
584 /*
585 * Handle weird Windows SMB server behaviour. It responds with
586 * STATUS_OBJECT_NAME_INVALID code to SMB2 QUERY_INFO request
587 * for "\<server>\<dfsname>\<linkpath>" DFS reference,
588 * where <dfsname> contains non-ASCII unicode symbols.
589 */
590 rc = -EREMOTE;
591 }
592 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb &&
593 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
594 rc = -EOPNOTSUPP;
595 }
596
597out:
598 free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
599 free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
600 free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
601 return rc;
602}
603
604
605int smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
606 struct cifs_sb_info *cifs_sb, const char *full_path,
607 struct cifs_open_info_data *data,
608 struct cifs_sid *owner,
609 struct cifs_sid *group,
610 bool *adjust_tz, bool *reparse)
611{
612 int rc;
613 __u32 create_options = 0;
614 struct cifsFileInfo *cfile;
615 struct kvec err_iov[3] = {};
616 int err_buftype[3] = {};
617 __u8 *sidsbuf = NULL;
618 __u8 *sidsbuf_end = NULL;
619 size_t sidsbuflen = 0;
620 size_t owner_len, group_len;
621
622 *adjust_tz = false;
623 *reparse = false;
624
625 /*
626 * BB TODO: Add support for using the cached root handle.
627 * Create SMB2_query_posix_info worker function to do non-compounded query
628 * when we already have an open file handle for this. For now this is fast enough
629 * (always using the compounded version).
630 */
631
632 cifs_get_readable_path(tcon, full_path, &cfile);
633 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES, FILE_OPEN,
634 create_options, ACL_NO_MODE, data, SMB2_OP_POSIX_QUERY_INFO, cfile,
635 &sidsbuf, &sidsbuflen, err_iov, err_buftype);
636 if (rc == -EOPNOTSUPP) {
637 /* BB TODO: When support for special files added to Samba re-verify this path */
638 if (err_iov[0].iov_base && err_buftype[0] != CIFS_NO_BUFFER &&
639 ((struct smb2_hdr *)err_iov[0].iov_base)->Command == SMB2_CREATE &&
640 ((struct smb2_hdr *)err_iov[0].iov_base)->Status == STATUS_STOPPED_ON_SYMLINK) {
641 rc = smb2_parse_symlink_response(cifs_sb, err_iov, &data->symlink_target);
642 if (rc)
643 goto out;
644 }
645 *reparse = true;
646 create_options |= OPEN_REPARSE_POINT;
647
648 /* Failed on a symbolic link - query a reparse point info */
649 cifs_get_readable_path(tcon, full_path, &cfile);
650 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path, FILE_READ_ATTRIBUTES,
651 FILE_OPEN, create_options, ACL_NO_MODE, data,
652 SMB2_OP_POSIX_QUERY_INFO, cfile,
653 &sidsbuf, &sidsbuflen, NULL, NULL);
654 }
655
656 if (rc == 0) {
657 sidsbuf_end = sidsbuf + sidsbuflen;
658
659 owner_len = posix_info_sid_size(sidsbuf, sidsbuf_end);
660 if (owner_len == -1) {
661 rc = -EINVAL;
662 goto out;
663 }
664 memcpy(owner, sidsbuf, owner_len);
665
666 group_len = posix_info_sid_size(
667 sidsbuf + owner_len, sidsbuf_end);
668 if (group_len == -1) {
669 rc = -EINVAL;
670 goto out;
671 }
672 memcpy(group, sidsbuf + owner_len, group_len);
673 }
674
675out:
676 kfree(sidsbuf);
677 free_rsp_buf(err_buftype[0], err_iov[0].iov_base);
678 free_rsp_buf(err_buftype[1], err_iov[1].iov_base);
679 free_rsp_buf(err_buftype[2], err_iov[2].iov_base);
680 return rc;
681}
682
683int
684smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
685 struct cifs_tcon *tcon, const char *name,
686 struct cifs_sb_info *cifs_sb)
687{
688 return smb2_compound_op(xid, tcon, cifs_sb, name,
689 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
690 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
691 NULL, NULL, NULL, NULL, NULL);
692}
693
694void
695smb2_mkdir_setinfo(struct inode *inode, const char *name,
696 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
697 const unsigned int xid)
698{
699 FILE_BASIC_INFO data;
700 struct cifsInodeInfo *cifs_i;
701 struct cifsFileInfo *cfile;
702 u32 dosattrs;
703 int tmprc;
704
705 memset(&data, 0, sizeof(data));
706 cifs_i = CIFS_I(inode);
707 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
708 data.Attributes = cpu_to_le32(dosattrs);
709 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
710 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
711 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
712 CREATE_NOT_FILE, ACL_NO_MODE,
713 &data, SMB2_OP_SET_INFO, cfile, NULL, NULL, NULL, NULL);
714 if (tmprc == 0)
715 cifs_i->cifsAttrs = dosattrs;
716}
717
718int
719smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
720 struct cifs_sb_info *cifs_sb)
721{
722 drop_cached_dir_by_name(xid, tcon, name, cifs_sb);
723 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
724 CREATE_NOT_FILE, ACL_NO_MODE,
725 NULL, SMB2_OP_RMDIR, NULL, NULL, NULL, NULL, NULL);
726}
727
728int
729smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
730 struct cifs_sb_info *cifs_sb)
731{
732 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
733 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
734 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL, NULL, NULL, NULL, NULL);
735}
736
737static int
738smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
739 const char *from_name, const char *to_name,
740 struct cifs_sb_info *cifs_sb, __u32 access, int command,
741 struct cifsFileInfo *cfile)
742{
743 __le16 *smb2_to_name = NULL;
744 int rc;
745
746 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
747 if (smb2_to_name == NULL) {
748 rc = -ENOMEM;
749 goto smb2_rename_path;
750 }
751 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
752 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
753 command, cfile, NULL, NULL, NULL, NULL);
754smb2_rename_path:
755 kfree(smb2_to_name);
756 return rc;
757}
758
759int
760smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
761 const char *from_name, const char *to_name,
762 struct cifs_sb_info *cifs_sb)
763{
764 struct cifsFileInfo *cfile;
765
766 drop_cached_dir_by_name(xid, tcon, from_name, cifs_sb);
767 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
768
769 return smb2_set_path_attr(xid, tcon, from_name, to_name,
770 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
771}
772
773int
774smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
775 const char *from_name, const char *to_name,
776 struct cifs_sb_info *cifs_sb)
777{
778 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
779 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
780 NULL);
781}
782
783int
784smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
785 const char *full_path, __u64 size,
786 struct cifs_sb_info *cifs_sb, bool set_alloc)
787{
788 __le64 eof = cpu_to_le64(size);
789 struct cifsFileInfo *cfile;
790
791 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
792 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
793 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
794 &eof, SMB2_OP_SET_EOF, cfile, NULL, NULL, NULL, NULL);
795}
796
797int
798smb2_set_file_info(struct inode *inode, const char *full_path,
799 FILE_BASIC_INFO *buf, const unsigned int xid)
800{
801 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
802 struct tcon_link *tlink;
803 struct cifs_tcon *tcon;
804 struct cifsFileInfo *cfile;
805 int rc;
806
807 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
808 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
809 (buf->Attributes == 0))
810 return 0; /* would be a no op, no sense sending this */
811
812 tlink = cifs_sb_tlink(cifs_sb);
813 if (IS_ERR(tlink))
814 return PTR_ERR(tlink);
815 tcon = tlink_tcon(tlink);
816
817 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
818 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
819 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
820 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile,
821 NULL, NULL, NULL, NULL);
822 cifs_put_tlink(tlink);
823 return rc;
824}
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
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
50static int
51smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
52 struct cifs_sb_info *cifs_sb, const char *full_path,
53 __u32 desired_access, __u32 create_disposition,
54 __u32 create_options, umode_t mode, void *ptr, int command,
55 struct cifsFileInfo *cfile)
56{
57 int rc;
58 __le16 *utf16_path = NULL;
59 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
60 struct cifs_open_parms oparms;
61 struct cifs_fid fid;
62 struct cifs_ses *ses = tcon->ses;
63 int num_rqst = 0;
64 struct smb_rqst rqst[3];
65 int resp_buftype[3];
66 struct kvec rsp_iov[3];
67 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
68 struct kvec qi_iov[1];
69 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
70 struct kvec close_iov[1];
71 struct smb2_query_info_rsp *qi_rsp = NULL;
72 int flags = 0;
73 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
74 unsigned int size[2];
75 void *data[2];
76 struct smb2_file_rename_info rename_info;
77 struct smb2_file_link_info link_info;
78 int len;
79
80 if (smb3_encryption_required(tcon))
81 flags |= CIFS_TRANSFORM_REQ;
82
83 memset(rqst, 0, sizeof(rqst));
84 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
85 memset(rsp_iov, 0, sizeof(rsp_iov));
86
87 /* We already have a handle so we can skip the open */
88 if (cfile)
89 goto after_open;
90
91 /* Open */
92 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
93 if (!utf16_path) {
94 rc = -ENOMEM;
95 goto finished;
96 }
97
98 oparms.tcon = tcon;
99 oparms.desired_access = desired_access;
100 oparms.disposition = create_disposition;
101 oparms.create_options = create_options;
102 if (backup_cred(cifs_sb))
103 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
104 oparms.fid = &fid;
105 oparms.reconnect = false;
106 oparms.mode = mode;
107
108 memset(&open_iov, 0, sizeof(open_iov));
109 rqst[num_rqst].rq_iov = open_iov;
110 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
111 rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
112 utf16_path);
113 kfree(utf16_path);
114 if (rc)
115 goto finished;
116
117 smb2_set_next_command(tcon, &rqst[num_rqst]);
118 after_open:
119 num_rqst++;
120 rc = 0;
121
122 /* Operation */
123 switch (command) {
124 case SMB2_OP_QUERY_INFO:
125 memset(&qi_iov, 0, sizeof(qi_iov));
126 rqst[num_rqst].rq_iov = qi_iov;
127 rqst[num_rqst].rq_nvec = 1;
128
129 if (cfile)
130 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
131 cfile->fid.persistent_fid,
132 cfile->fid.volatile_fid,
133 FILE_ALL_INFORMATION,
134 SMB2_O_INFO_FILE, 0,
135 sizeof(struct smb2_file_all_info) +
136 PATH_MAX * 2, 0, NULL);
137 else {
138 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
139 COMPOUND_FID,
140 COMPOUND_FID,
141 FILE_ALL_INFORMATION,
142 SMB2_O_INFO_FILE, 0,
143 sizeof(struct smb2_file_all_info) +
144 PATH_MAX * 2, 0, NULL);
145 if (!rc) {
146 smb2_set_next_command(tcon, &rqst[num_rqst]);
147 smb2_set_related(&rqst[num_rqst]);
148 }
149 }
150
151 if (rc)
152 goto finished;
153 num_rqst++;
154 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
155 full_path);
156 break;
157 case SMB2_OP_DELETE:
158 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
159 break;
160 case SMB2_OP_MKDIR:
161 /*
162 * Directories are created through parameters in the
163 * SMB2_open() call.
164 */
165 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
166 break;
167 case SMB2_OP_RMDIR:
168 memset(&si_iov, 0, sizeof(si_iov));
169 rqst[num_rqst].rq_iov = si_iov;
170 rqst[num_rqst].rq_nvec = 1;
171
172 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
173 data[0] = &delete_pending[0];
174
175 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
176 COMPOUND_FID, current->tgid,
177 FILE_DISPOSITION_INFORMATION,
178 SMB2_O_INFO_FILE, 0, data, size);
179 if (rc)
180 goto finished;
181 smb2_set_next_command(tcon, &rqst[num_rqst]);
182 smb2_set_related(&rqst[num_rqst++]);
183 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
184 break;
185 case SMB2_OP_SET_EOF:
186 memset(&si_iov, 0, sizeof(si_iov));
187 rqst[num_rqst].rq_iov = si_iov;
188 rqst[num_rqst].rq_nvec = 1;
189
190 size[0] = 8; /* sizeof __le64 */
191 data[0] = ptr;
192
193 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
194 COMPOUND_FID, current->tgid,
195 FILE_END_OF_FILE_INFORMATION,
196 SMB2_O_INFO_FILE, 0, data, size);
197 if (rc)
198 goto finished;
199 smb2_set_next_command(tcon, &rqst[num_rqst]);
200 smb2_set_related(&rqst[num_rqst++]);
201 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
202 break;
203 case SMB2_OP_SET_INFO:
204 memset(&si_iov, 0, sizeof(si_iov));
205 rqst[num_rqst].rq_iov = si_iov;
206 rqst[num_rqst].rq_nvec = 1;
207
208
209 size[0] = sizeof(FILE_BASIC_INFO);
210 data[0] = ptr;
211
212 if (cfile)
213 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
214 cfile->fid.persistent_fid,
215 cfile->fid.volatile_fid, current->tgid,
216 FILE_BASIC_INFORMATION,
217 SMB2_O_INFO_FILE, 0, data, size);
218 else {
219 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
220 COMPOUND_FID,
221 COMPOUND_FID, current->tgid,
222 FILE_BASIC_INFORMATION,
223 SMB2_O_INFO_FILE, 0, data, size);
224 if (!rc) {
225 smb2_set_next_command(tcon, &rqst[num_rqst]);
226 smb2_set_related(&rqst[num_rqst]);
227 }
228 }
229
230 if (rc)
231 goto finished;
232 num_rqst++;
233 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
234 full_path);
235 break;
236 case SMB2_OP_RENAME:
237 memset(&si_iov, 0, sizeof(si_iov));
238 rqst[num_rqst].rq_iov = si_iov;
239 rqst[num_rqst].rq_nvec = 2;
240
241 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
242
243 rename_info.ReplaceIfExists = 1;
244 rename_info.RootDirectory = 0;
245 rename_info.FileNameLength = cpu_to_le32(len);
246
247 size[0] = sizeof(struct smb2_file_rename_info);
248 data[0] = &rename_info;
249
250 size[1] = len + 2 /* null */;
251 data[1] = (__le16 *)ptr;
252
253 if (cfile)
254 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
255 cfile->fid.persistent_fid,
256 cfile->fid.volatile_fid,
257 current->tgid, FILE_RENAME_INFORMATION,
258 SMB2_O_INFO_FILE, 0, data, size);
259 else {
260 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
261 COMPOUND_FID, COMPOUND_FID,
262 current->tgid, FILE_RENAME_INFORMATION,
263 SMB2_O_INFO_FILE, 0, data, size);
264 if (!rc) {
265 smb2_set_next_command(tcon, &rqst[num_rqst]);
266 smb2_set_related(&rqst[num_rqst]);
267 }
268 }
269 if (rc)
270 goto finished;
271 num_rqst++;
272 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
273 break;
274 case SMB2_OP_HARDLINK:
275 memset(&si_iov, 0, sizeof(si_iov));
276 rqst[num_rqst].rq_iov = si_iov;
277 rqst[num_rqst].rq_nvec = 2;
278
279 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
280
281 link_info.ReplaceIfExists = 0;
282 link_info.RootDirectory = 0;
283 link_info.FileNameLength = cpu_to_le32(len);
284
285 size[0] = sizeof(struct smb2_file_link_info);
286 data[0] = &link_info;
287
288 size[1] = len + 2 /* null */;
289 data[1] = (__le16 *)ptr;
290
291 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
292 COMPOUND_FID, current->tgid,
293 FILE_LINK_INFORMATION,
294 SMB2_O_INFO_FILE, 0, data, size);
295 if (rc)
296 goto finished;
297 smb2_set_next_command(tcon, &rqst[num_rqst]);
298 smb2_set_related(&rqst[num_rqst++]);
299 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
300 break;
301 default:
302 cifs_dbg(VFS, "Invalid command\n");
303 rc = -EINVAL;
304 }
305 if (rc)
306 goto finished;
307
308 /* We already have a handle so we can skip the close */
309 if (cfile)
310 goto after_close;
311 /* Close */
312 memset(&close_iov, 0, sizeof(close_iov));
313 rqst[num_rqst].rq_iov = close_iov;
314 rqst[num_rqst].rq_nvec = 1;
315 rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
316 COMPOUND_FID);
317 smb2_set_related(&rqst[num_rqst]);
318 if (rc)
319 goto finished;
320 after_close:
321 num_rqst++;
322
323 if (cfile) {
324 cifsFileInfo_put(cfile);
325 cfile = NULL;
326 rc = compound_send_recv(xid, ses, flags, num_rqst - 2,
327 &rqst[1], &resp_buftype[1],
328 &rsp_iov[1]);
329 } else
330 rc = compound_send_recv(xid, ses, flags, num_rqst,
331 rqst, resp_buftype,
332 rsp_iov);
333
334 finished:
335 if (cfile)
336 cifsFileInfo_put(cfile);
337
338 SMB2_open_free(&rqst[0]);
339 if (rc == -EREMCHG) {
340 printk_once(KERN_WARNING "server share %s deleted\n",
341 tcon->treeName);
342 tcon->need_reconnect = true;
343 }
344
345 switch (command) {
346 case SMB2_OP_QUERY_INFO:
347 if (rc == 0) {
348 qi_rsp = (struct smb2_query_info_rsp *)
349 rsp_iov[1].iov_base;
350 rc = smb2_validate_and_copy_iov(
351 le16_to_cpu(qi_rsp->OutputBufferOffset),
352 le32_to_cpu(qi_rsp->OutputBufferLength),
353 &rsp_iov[1], sizeof(struct smb2_file_all_info),
354 ptr);
355 }
356 if (rqst[1].rq_iov)
357 SMB2_query_info_free(&rqst[1]);
358 if (rqst[2].rq_iov)
359 SMB2_close_free(&rqst[2]);
360 if (rc)
361 trace_smb3_query_info_compound_err(xid, ses->Suid,
362 tcon->tid, rc);
363 else
364 trace_smb3_query_info_compound_done(xid, ses->Suid,
365 tcon->tid);
366 break;
367 case SMB2_OP_DELETE:
368 if (rc)
369 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
370 else
371 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
372 if (rqst[1].rq_iov)
373 SMB2_close_free(&rqst[1]);
374 break;
375 case SMB2_OP_MKDIR:
376 if (rc)
377 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
378 else
379 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
380 if (rqst[1].rq_iov)
381 SMB2_close_free(&rqst[1]);
382 break;
383 case SMB2_OP_HARDLINK:
384 if (rc)
385 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
386 else
387 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
388 free_set_inf_compound(rqst);
389 break;
390 case SMB2_OP_RENAME:
391 if (rc)
392 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
393 else
394 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
395 free_set_inf_compound(rqst);
396 break;
397 case SMB2_OP_RMDIR:
398 if (rc)
399 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
400 else
401 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
402 free_set_inf_compound(rqst);
403 break;
404 case SMB2_OP_SET_EOF:
405 if (rc)
406 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
407 else
408 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
409 free_set_inf_compound(rqst);
410 break;
411 case SMB2_OP_SET_INFO:
412 if (rc)
413 trace_smb3_set_info_compound_err(xid, ses->Suid,
414 tcon->tid, rc);
415 else
416 trace_smb3_set_info_compound_done(xid, ses->Suid,
417 tcon->tid);
418 free_set_inf_compound(rqst);
419 break;
420 }
421 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
422 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
423 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
424 return rc;
425}
426
427void
428move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
429{
430 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
431 dst->CurrentByteOffset = src->CurrentByteOffset;
432 dst->Mode = src->Mode;
433 dst->AlignmentRequirement = src->AlignmentRequirement;
434 dst->IndexNumber1 = 0; /* we don't use it */
435}
436
437int
438smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
439 struct cifs_sb_info *cifs_sb, const char *full_path,
440 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
441{
442 int rc;
443 struct smb2_file_all_info *smb2_data;
444 __u32 create_options = 0;
445 struct cifs_fid fid;
446 bool no_cached_open = tcon->nohandlecache;
447 struct cifsFileInfo *cfile;
448
449 *adjust_tz = false;
450 *symlink = false;
451
452 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
453 GFP_KERNEL);
454 if (smb2_data == NULL)
455 return -ENOMEM;
456
457 /* If it is a root and its handle is cached then use it */
458 if (!strlen(full_path) && !no_cached_open) {
459 rc = open_shroot(xid, tcon, &fid);
460 if (rc)
461 goto out;
462
463 if (tcon->crfid.file_all_info_is_valid) {
464 move_smb2_info_to_cifs(data,
465 &tcon->crfid.file_all_info);
466 } else {
467 rc = SMB2_query_info(xid, tcon, fid.persistent_fid,
468 fid.volatile_fid, smb2_data);
469 if (!rc)
470 move_smb2_info_to_cifs(data, smb2_data);
471 }
472 close_shroot(&tcon->crfid);
473 goto out;
474 }
475
476 if (backup_cred(cifs_sb))
477 create_options |= CREATE_OPEN_BACKUP_INTENT;
478
479 cifs_get_readable_path(tcon, full_path, &cfile);
480 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
481 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
482 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
483 if (rc == -EOPNOTSUPP) {
484 *symlink = true;
485 create_options |= OPEN_REPARSE_POINT;
486
487 /* Failed on a symbolic link - query a reparse point info */
488 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
489 FILE_READ_ATTRIBUTES, FILE_OPEN,
490 create_options, ACL_NO_MODE,
491 smb2_data, SMB2_OP_QUERY_INFO, NULL);
492 }
493 if (rc)
494 goto out;
495
496 move_smb2_info_to_cifs(data, smb2_data);
497out:
498 kfree(smb2_data);
499 return rc;
500}
501
502int
503smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
504 struct cifs_tcon *tcon, const char *name,
505 struct cifs_sb_info *cifs_sb)
506{
507 return smb2_compound_op(xid, tcon, cifs_sb, name,
508 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
509 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
510 NULL);
511}
512
513void
514smb2_mkdir_setinfo(struct inode *inode, const char *name,
515 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
516 const unsigned int xid)
517{
518 FILE_BASIC_INFO data;
519 struct cifsInodeInfo *cifs_i;
520 struct cifsFileInfo *cfile;
521 u32 dosattrs;
522 int tmprc;
523
524 memset(&data, 0, sizeof(data));
525 cifs_i = CIFS_I(inode);
526 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
527 data.Attributes = cpu_to_le32(dosattrs);
528 cifs_get_writable_path(tcon, name, &cfile);
529 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
530 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
531 CREATE_NOT_FILE, ACL_NO_MODE,
532 &data, SMB2_OP_SET_INFO, cfile);
533 if (tmprc == 0)
534 cifs_i->cifsAttrs = dosattrs;
535}
536
537int
538smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
539 struct cifs_sb_info *cifs_sb)
540{
541 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
542 CREATE_NOT_FILE, ACL_NO_MODE,
543 NULL, SMB2_OP_RMDIR, NULL);
544}
545
546int
547smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
548 struct cifs_sb_info *cifs_sb)
549{
550 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
551 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
552 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
553}
554
555static int
556smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
557 const char *from_name, const char *to_name,
558 struct cifs_sb_info *cifs_sb, __u32 access, int command,
559 struct cifsFileInfo *cfile)
560{
561 __le16 *smb2_to_name = NULL;
562 int rc;
563
564 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
565 if (smb2_to_name == NULL) {
566 rc = -ENOMEM;
567 goto smb2_rename_path;
568 }
569 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
570 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
571 command, cfile);
572smb2_rename_path:
573 kfree(smb2_to_name);
574 return rc;
575}
576
577int
578smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
579 const char *from_name, const char *to_name,
580 struct cifs_sb_info *cifs_sb)
581{
582 struct cifsFileInfo *cfile;
583
584 cifs_get_writable_path(tcon, from_name, &cfile);
585
586 return smb2_set_path_attr(xid, tcon, from_name, to_name,
587 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
588}
589
590int
591smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
592 const char *from_name, const char *to_name,
593 struct cifs_sb_info *cifs_sb)
594{
595 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
596 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
597 NULL);
598}
599
600int
601smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
602 const char *full_path, __u64 size,
603 struct cifs_sb_info *cifs_sb, bool set_alloc)
604{
605 __le64 eof = cpu_to_le64(size);
606
607 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
608 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
609 &eof, SMB2_OP_SET_EOF, NULL);
610}
611
612int
613smb2_set_file_info(struct inode *inode, const char *full_path,
614 FILE_BASIC_INFO *buf, const unsigned int xid)
615{
616 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
617 struct tcon_link *tlink;
618 int rc;
619
620 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
621 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
622 (buf->Attributes == 0))
623 return 0; /* would be a no op, no sense sending this */
624
625 tlink = cifs_sb_tlink(cifs_sb);
626 if (IS_ERR(tlink))
627 return PTR_ERR(tlink);
628
629 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
630 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
631 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
632 cifs_put_tlink(tlink);
633 return rc;
634}