1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002, 2011
11 #include <linux/stat.h>
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <asm/div64.h>
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
21 #include "cifs_unicode.h"
25 #include "smb2proto.h"
26 #include "cached_dir.h"
29 free_set_inf_compound(struct smb_rqst *rqst)
32 SMB2_set_info_free(&rqst[1]);
34 SMB2_close_free(&rqst[2]);
39 struct cifs_open_parms oparms;
40 struct kvec rsp_iov[3];
41 struct smb_rqst rqst[3];
42 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
43 struct kvec qi_iov[1];
44 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
45 struct kvec close_iov[1];
46 struct smb2_file_rename_info rename_info;
47 struct smb2_file_link_info link_info;
51 * note: If cfile is passed, the reference to it is dropped here.
52 * So make sure that you do not reuse cfile after return from this func.
55 smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
56 struct cifs_sb_info *cifs_sb, const char *full_path,
57 __u32 desired_access, __u32 create_disposition,
58 __u32 create_options, umode_t mode, void *ptr, int command,
59 struct cifsFileInfo *cfile)
61 struct cop_vars *vars = NULL;
63 struct smb_rqst *rqst;
65 __le16 *utf16_path = NULL;
66 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
68 struct cifs_ses *ses = tcon->ses;
69 struct TCP_Server_Info *server;
72 struct smb2_query_info_rsp *qi_rsp = NULL;
74 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
79 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
82 rqst = &vars->rqst[0];
83 rsp_iov = &vars->rsp_iov[0];
85 server = cifs_pick_channel(ses);
87 if (smb3_encryption_required(tcon))
88 flags |= CIFS_TRANSFORM_REQ;
90 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
92 /* We already have a handle so we can skip the open */
97 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
103 vars->oparms.tcon = tcon;
104 vars->oparms.desired_access = desired_access;
105 vars->oparms.disposition = create_disposition;
106 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
107 vars->oparms.fid = &fid;
108 vars->oparms.reconnect = false;
109 vars->oparms.mode = mode;
110 vars->oparms.cifs_sb = cifs_sb;
112 rqst[num_rqst].rq_iov = &vars->open_iov[0];
113 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
114 rc = SMB2_open_init(tcon, server,
115 &rqst[num_rqst], &oplock, &vars->oparms,
121 smb2_set_next_command(tcon, &rqst[num_rqst]);
128 case SMB2_OP_QUERY_INFO:
129 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
130 rqst[num_rqst].rq_nvec = 1;
133 rc = SMB2_query_info_init(tcon, server,
135 cfile->fid.persistent_fid,
136 cfile->fid.volatile_fid,
137 FILE_ALL_INFORMATION,
139 sizeof(struct smb2_file_all_info) +
140 PATH_MAX * 2, 0, NULL);
142 rc = SMB2_query_info_init(tcon, server,
146 FILE_ALL_INFORMATION,
148 sizeof(struct smb2_file_all_info) +
149 PATH_MAX * 2, 0, NULL);
151 smb2_set_next_command(tcon, &rqst[num_rqst]);
152 smb2_set_related(&rqst[num_rqst]);
159 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
162 case SMB2_OP_POSIX_QUERY_INFO:
163 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
164 rqst[num_rqst].rq_nvec = 1;
167 rc = SMB2_query_info_init(tcon, server,
169 cfile->fid.persistent_fid,
170 cfile->fid.volatile_fid,
171 SMB_FIND_FILE_POSIX_INFO,
173 /* TBD: fix following to allow for longer SIDs */
174 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
175 (sizeof(struct cifs_sid) * 2), 0, NULL);
177 rc = SMB2_query_info_init(tcon, server,
181 SMB_FIND_FILE_POSIX_INFO,
183 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
184 (sizeof(struct cifs_sid) * 2), 0, NULL);
186 smb2_set_next_command(tcon, &rqst[num_rqst]);
187 smb2_set_related(&rqst[num_rqst]);
194 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
197 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
201 * Directories are created through parameters in the
204 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
207 rqst[num_rqst].rq_iov = &vars->si_iov[0];
208 rqst[num_rqst].rq_nvec = 1;
210 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
211 data[0] = &delete_pending[0];
213 rc = SMB2_set_info_init(tcon, server,
214 &rqst[num_rqst], COMPOUND_FID,
215 COMPOUND_FID, current->tgid,
216 FILE_DISPOSITION_INFORMATION,
217 SMB2_O_INFO_FILE, 0, data, size);
220 smb2_set_next_command(tcon, &rqst[num_rqst]);
221 smb2_set_related(&rqst[num_rqst++]);
222 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
224 case SMB2_OP_SET_EOF:
225 rqst[num_rqst].rq_iov = &vars->si_iov[0];
226 rqst[num_rqst].rq_nvec = 1;
228 size[0] = 8; /* sizeof __le64 */
231 rc = SMB2_set_info_init(tcon, server,
232 &rqst[num_rqst], COMPOUND_FID,
233 COMPOUND_FID, current->tgid,
234 FILE_END_OF_FILE_INFORMATION,
235 SMB2_O_INFO_FILE, 0, data, size);
238 smb2_set_next_command(tcon, &rqst[num_rqst]);
239 smb2_set_related(&rqst[num_rqst++]);
240 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
242 case SMB2_OP_SET_INFO:
243 rqst[num_rqst].rq_iov = &vars->si_iov[0];
244 rqst[num_rqst].rq_nvec = 1;
247 size[0] = sizeof(FILE_BASIC_INFO);
251 rc = SMB2_set_info_init(tcon, server,
253 cfile->fid.persistent_fid,
254 cfile->fid.volatile_fid, current->tgid,
255 FILE_BASIC_INFORMATION,
256 SMB2_O_INFO_FILE, 0, data, size);
258 rc = SMB2_set_info_init(tcon, server,
261 COMPOUND_FID, current->tgid,
262 FILE_BASIC_INFORMATION,
263 SMB2_O_INFO_FILE, 0, data, size);
265 smb2_set_next_command(tcon, &rqst[num_rqst]);
266 smb2_set_related(&rqst[num_rqst]);
273 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
277 rqst[num_rqst].rq_iov = &vars->si_iov[0];
278 rqst[num_rqst].rq_nvec = 2;
280 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
282 vars->rename_info.ReplaceIfExists = 1;
283 vars->rename_info.RootDirectory = 0;
284 vars->rename_info.FileNameLength = cpu_to_le32(len);
286 size[0] = sizeof(struct smb2_file_rename_info);
287 data[0] = &vars->rename_info;
289 size[1] = len + 2 /* null */;
290 data[1] = (__le16 *)ptr;
293 rc = SMB2_set_info_init(tcon, server,
295 cfile->fid.persistent_fid,
296 cfile->fid.volatile_fid,
297 current->tgid, FILE_RENAME_INFORMATION,
298 SMB2_O_INFO_FILE, 0, data, size);
300 rc = SMB2_set_info_init(tcon, server,
302 COMPOUND_FID, COMPOUND_FID,
303 current->tgid, FILE_RENAME_INFORMATION,
304 SMB2_O_INFO_FILE, 0, data, size);
306 smb2_set_next_command(tcon, &rqst[num_rqst]);
307 smb2_set_related(&rqst[num_rqst]);
313 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
315 case SMB2_OP_HARDLINK:
316 rqst[num_rqst].rq_iov = &vars->si_iov[0];
317 rqst[num_rqst].rq_nvec = 2;
319 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
321 vars->link_info.ReplaceIfExists = 0;
322 vars->link_info.RootDirectory = 0;
323 vars->link_info.FileNameLength = cpu_to_le32(len);
325 size[0] = sizeof(struct smb2_file_link_info);
326 data[0] = &vars->link_info;
328 size[1] = len + 2 /* null */;
329 data[1] = (__le16 *)ptr;
331 rc = SMB2_set_info_init(tcon, server,
332 &rqst[num_rqst], COMPOUND_FID,
333 COMPOUND_FID, current->tgid,
334 FILE_LINK_INFORMATION,
335 SMB2_O_INFO_FILE, 0, data, size);
338 smb2_set_next_command(tcon, &rqst[num_rqst]);
339 smb2_set_related(&rqst[num_rqst++]);
340 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
343 cifs_dbg(VFS, "Invalid command\n");
349 /* We already have a handle so we can skip the close */
353 flags |= CIFS_CP_CREATE_CLOSE_OP;
354 rqst[num_rqst].rq_iov = &vars->close_iov[0];
355 rqst[num_rqst].rq_nvec = 1;
356 rc = SMB2_close_init(tcon, server,
357 &rqst[num_rqst], COMPOUND_FID,
358 COMPOUND_FID, false);
359 smb2_set_related(&rqst[num_rqst]);
366 rc = compound_send_recv(xid, ses, server,
368 &rqst[1], &resp_buftype[1],
371 rc = compound_send_recv(xid, ses, server,
378 cifsFileInfo_put(cfile);
380 SMB2_open_free(&rqst[0]);
381 if (rc == -EREMCHG) {
382 pr_warn_once("server share %s deleted\n", tcon->treeName);
383 tcon->need_reconnect = true;
387 case SMB2_OP_QUERY_INFO:
389 qi_rsp = (struct smb2_query_info_rsp *)
391 rc = smb2_validate_and_copy_iov(
392 le16_to_cpu(qi_rsp->OutputBufferOffset),
393 le32_to_cpu(qi_rsp->OutputBufferLength),
394 &rsp_iov[1], sizeof(struct smb2_file_all_info),
398 SMB2_query_info_free(&rqst[1]);
400 SMB2_close_free(&rqst[2]);
402 trace_smb3_query_info_compound_err(xid, ses->Suid,
405 trace_smb3_query_info_compound_done(xid, ses->Suid,
408 case SMB2_OP_POSIX_QUERY_INFO:
410 qi_rsp = (struct smb2_query_info_rsp *)
412 rc = smb2_validate_and_copy_iov(
413 le16_to_cpu(qi_rsp->OutputBufferOffset),
414 le32_to_cpu(qi_rsp->OutputBufferLength),
415 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
418 SMB2_query_info_free(&rqst[1]);
420 SMB2_close_free(&rqst[2]);
422 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
424 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
428 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
430 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
432 SMB2_close_free(&rqst[1]);
436 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
438 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
440 SMB2_close_free(&rqst[1]);
442 case SMB2_OP_HARDLINK:
444 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
446 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
447 free_set_inf_compound(rqst);
451 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
453 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
454 free_set_inf_compound(rqst);
458 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
460 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
461 free_set_inf_compound(rqst);
463 case SMB2_OP_SET_EOF:
465 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
467 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
468 free_set_inf_compound(rqst);
470 case SMB2_OP_SET_INFO:
472 trace_smb3_set_info_compound_err(xid, ses->Suid,
475 trace_smb3_set_info_compound_done(xid, ses->Suid,
477 free_set_inf_compound(rqst);
480 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
481 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
482 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
488 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
490 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
491 dst->CurrentByteOffset = src->CurrentByteOffset;
492 dst->Mode = src->Mode;
493 dst->AlignmentRequirement = src->AlignmentRequirement;
494 dst->IndexNumber1 = 0; /* we don't use it */
498 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
499 struct cifs_sb_info *cifs_sb, const char *full_path,
500 FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
503 struct smb2_file_all_info *smb2_data;
504 __u32 create_options = 0;
505 struct cifsFileInfo *cfile;
506 struct cached_fid *cfid = NULL;
511 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
513 if (smb2_data == NULL)
516 if (strcmp(full_path, ""))
519 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, false, &cfid);
520 /* If it is a root and its handle is cached then use it */
522 if (cfid->file_all_info_is_valid) {
523 move_smb2_info_to_cifs(data,
524 &cfid->file_all_info);
526 rc = SMB2_query_info(xid, tcon,
527 cfid->fid.persistent_fid,
528 cfid->fid.volatile_fid, smb2_data);
530 move_smb2_info_to_cifs(data, smb2_data);
532 close_cached_dir(cfid);
536 cifs_get_readable_path(tcon, full_path, &cfile);
537 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
538 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
539 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
540 if (rc == -EOPNOTSUPP) {
542 create_options |= OPEN_REPARSE_POINT;
544 /* Failed on a symbolic link - query a reparse point info */
545 cifs_get_readable_path(tcon, full_path, &cfile);
546 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
547 FILE_READ_ATTRIBUTES, FILE_OPEN,
548 create_options, ACL_NO_MODE,
549 smb2_data, SMB2_OP_QUERY_INFO, cfile);
554 move_smb2_info_to_cifs(data, smb2_data);
562 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
563 struct cifs_sb_info *cifs_sb, const char *full_path,
564 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
567 __u32 create_options = 0;
568 struct cifsFileInfo *cfile;
569 struct smb311_posix_qinfo *smb2_data;
574 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
575 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
577 if (smb2_data == NULL)
581 * BB TODO: Add support for using the cached root handle.
582 * Create SMB2_query_posix_info worker function to do non-compounded query
583 * when we already have an open file handle for this. For now this is fast enough
584 * (always using the compounded version).
587 cifs_get_readable_path(tcon, full_path, &cfile);
588 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
589 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
590 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
591 if (rc == -EOPNOTSUPP) {
592 /* BB TODO: When support for special files added to Samba re-verify this path */
594 create_options |= OPEN_REPARSE_POINT;
596 /* Failed on a symbolic link - query a reparse point info */
597 cifs_get_readable_path(tcon, full_path, &cfile);
598 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
599 FILE_READ_ATTRIBUTES, FILE_OPEN,
600 create_options, ACL_NO_MODE,
601 smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
606 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
607 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
615 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
616 struct cifs_tcon *tcon, const char *name,
617 struct cifs_sb_info *cifs_sb)
619 return smb2_compound_op(xid, tcon, cifs_sb, name,
620 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
621 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
626 smb2_mkdir_setinfo(struct inode *inode, const char *name,
627 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
628 const unsigned int xid)
630 FILE_BASIC_INFO data;
631 struct cifsInodeInfo *cifs_i;
632 struct cifsFileInfo *cfile;
636 memset(&data, 0, sizeof(data));
637 cifs_i = CIFS_I(inode);
638 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
639 data.Attributes = cpu_to_le32(dosattrs);
640 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
641 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
642 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
643 CREATE_NOT_FILE, ACL_NO_MODE,
644 &data, SMB2_OP_SET_INFO, cfile);
646 cifs_i->cifsAttrs = dosattrs;
650 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
651 struct cifs_sb_info *cifs_sb)
653 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
654 CREATE_NOT_FILE, ACL_NO_MODE,
655 NULL, SMB2_OP_RMDIR, NULL);
659 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
660 struct cifs_sb_info *cifs_sb)
662 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
663 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
664 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
668 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
669 const char *from_name, const char *to_name,
670 struct cifs_sb_info *cifs_sb, __u32 access, int command,
671 struct cifsFileInfo *cfile)
673 __le16 *smb2_to_name = NULL;
676 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
677 if (smb2_to_name == NULL) {
679 goto smb2_rename_path;
681 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
682 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
690 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
691 const char *from_name, const char *to_name,
692 struct cifs_sb_info *cifs_sb)
694 struct cifsFileInfo *cfile;
696 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
698 return smb2_set_path_attr(xid, tcon, from_name, to_name,
699 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
703 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
704 const char *from_name, const char *to_name,
705 struct cifs_sb_info *cifs_sb)
707 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
708 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
713 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
714 const char *full_path, __u64 size,
715 struct cifs_sb_info *cifs_sb, bool set_alloc)
717 __le64 eof = cpu_to_le64(size);
718 struct cifsFileInfo *cfile;
720 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
721 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
722 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
723 &eof, SMB2_OP_SET_EOF, cfile);
727 smb2_set_file_info(struct inode *inode, const char *full_path,
728 FILE_BASIC_INFO *buf, const unsigned int xid)
730 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
731 struct tcon_link *tlink;
732 struct cifs_tcon *tcon;
733 struct cifsFileInfo *cfile;
736 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
737 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
738 (buf->Attributes == 0))
739 return 0; /* would be a no op, no sense sending this */
741 tlink = cifs_sb_tlink(cifs_sb);
743 return PTR_ERR(tlink);
744 tcon = tlink_tcon(tlink);
746 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
747 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
748 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
749 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile);
750 cifs_put_tlink(tlink);