1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002, 2011
6 * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7 * Steve French (sfrench@us.ibm.com)
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"
28 free_set_inf_compound(struct smb_rqst *rqst)
31 SMB2_set_info_free(&rqst[1]);
33 SMB2_close_free(&rqst[2]);
38 struct cifs_open_parms oparms;
39 struct kvec rsp_iov[3];
40 struct smb_rqst rqst[3];
41 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
42 struct kvec qi_iov[1];
43 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
44 struct kvec close_iov[1];
45 struct smb2_file_rename_info rename_info;
46 struct smb2_file_link_info link_info;
50 smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
51 struct cifs_sb_info *cifs_sb, const char *full_path,
52 __u32 desired_access, __u32 create_disposition,
53 __u32 create_options, umode_t mode, void *ptr, int command,
54 struct cifsFileInfo *cfile)
56 struct cop_vars *vars = NULL;
58 struct smb_rqst *rqst;
60 __le16 *utf16_path = NULL;
61 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
63 struct cifs_ses *ses = tcon->ses;
64 struct TCP_Server_Info *server;
67 struct smb2_query_info_rsp *qi_rsp = NULL;
69 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
74 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
77 rqst = &vars->rqst[0];
78 rsp_iov = &vars->rsp_iov[0];
80 server = cifs_pick_channel(ses);
82 if (smb3_encryption_required(tcon))
83 flags |= CIFS_TRANSFORM_REQ;
85 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
87 /* We already have a handle so we can skip the open */
92 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
98 vars->oparms.tcon = tcon;
99 vars->oparms.desired_access = desired_access;
100 vars->oparms.disposition = create_disposition;
101 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
102 vars->oparms.fid = &fid;
103 vars->oparms.reconnect = false;
104 vars->oparms.mode = mode;
105 vars->oparms.cifs_sb = cifs_sb;
107 rqst[num_rqst].rq_iov = &vars->open_iov[0];
108 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
109 rc = SMB2_open_init(tcon, server,
110 &rqst[num_rqst], &oplock, &vars->oparms,
116 smb2_set_next_command(tcon, &rqst[num_rqst]);
123 case SMB2_OP_QUERY_INFO:
124 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
125 rqst[num_rqst].rq_nvec = 1;
128 rc = SMB2_query_info_init(tcon, server,
130 cfile->fid.persistent_fid,
131 cfile->fid.volatile_fid,
132 FILE_ALL_INFORMATION,
134 sizeof(struct smb2_file_all_info) +
135 PATH_MAX * 2, 0, NULL);
137 rc = SMB2_query_info_init(tcon, server,
141 FILE_ALL_INFORMATION,
143 sizeof(struct smb2_file_all_info) +
144 PATH_MAX * 2, 0, NULL);
146 smb2_set_next_command(tcon, &rqst[num_rqst]);
147 smb2_set_related(&rqst[num_rqst]);
154 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
157 case SMB2_OP_POSIX_QUERY_INFO:
158 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
159 rqst[num_rqst].rq_nvec = 1;
162 rc = SMB2_query_info_init(tcon, server,
164 cfile->fid.persistent_fid,
165 cfile->fid.volatile_fid,
166 SMB_FIND_FILE_POSIX_INFO,
168 /* TBD: fix following to allow for longer SIDs */
169 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
170 (sizeof(struct cifs_sid) * 2), 0, NULL);
172 rc = SMB2_query_info_init(tcon, server,
176 SMB_FIND_FILE_POSIX_INFO,
178 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
179 (sizeof(struct cifs_sid) * 2), 0, NULL);
181 smb2_set_next_command(tcon, &rqst[num_rqst]);
182 smb2_set_related(&rqst[num_rqst]);
189 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
192 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
196 * Directories are created through parameters in the
199 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
202 rqst[num_rqst].rq_iov = &vars->si_iov[0];
203 rqst[num_rqst].rq_nvec = 1;
205 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
206 data[0] = &delete_pending[0];
208 rc = SMB2_set_info_init(tcon, server,
209 &rqst[num_rqst], COMPOUND_FID,
210 COMPOUND_FID, current->tgid,
211 FILE_DISPOSITION_INFORMATION,
212 SMB2_O_INFO_FILE, 0, data, size);
215 smb2_set_next_command(tcon, &rqst[num_rqst]);
216 smb2_set_related(&rqst[num_rqst++]);
217 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
219 case SMB2_OP_SET_EOF:
220 rqst[num_rqst].rq_iov = &vars->si_iov[0];
221 rqst[num_rqst].rq_nvec = 1;
223 size[0] = 8; /* sizeof __le64 */
227 rc = SMB2_set_info_init(tcon, server,
229 cfile->fid.persistent_fid,
230 cfile->fid.volatile_fid,
232 FILE_END_OF_FILE_INFORMATION,
236 rc = SMB2_set_info_init(tcon, server,
241 FILE_END_OF_FILE_INFORMATION,
245 smb2_set_next_command(tcon, &rqst[num_rqst]);
246 smb2_set_related(&rqst[num_rqst]);
252 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
254 case SMB2_OP_SET_INFO:
255 rqst[num_rqst].rq_iov = &vars->si_iov[0];
256 rqst[num_rqst].rq_nvec = 1;
259 size[0] = sizeof(FILE_BASIC_INFO);
263 rc = SMB2_set_info_init(tcon, server,
265 cfile->fid.persistent_fid,
266 cfile->fid.volatile_fid, current->tgid,
267 FILE_BASIC_INFORMATION,
268 SMB2_O_INFO_FILE, 0, data, size);
270 rc = SMB2_set_info_init(tcon, server,
273 COMPOUND_FID, current->tgid,
274 FILE_BASIC_INFORMATION,
275 SMB2_O_INFO_FILE, 0, data, size);
277 smb2_set_next_command(tcon, &rqst[num_rqst]);
278 smb2_set_related(&rqst[num_rqst]);
285 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
289 rqst[num_rqst].rq_iov = &vars->si_iov[0];
290 rqst[num_rqst].rq_nvec = 2;
292 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
294 vars->rename_info.ReplaceIfExists = 1;
295 vars->rename_info.RootDirectory = 0;
296 vars->rename_info.FileNameLength = cpu_to_le32(len);
298 size[0] = sizeof(struct smb2_file_rename_info);
299 data[0] = &vars->rename_info;
301 size[1] = len + 2 /* null */;
302 data[1] = (__le16 *)ptr;
305 rc = SMB2_set_info_init(tcon, server,
307 cfile->fid.persistent_fid,
308 cfile->fid.volatile_fid,
309 current->tgid, FILE_RENAME_INFORMATION,
310 SMB2_O_INFO_FILE, 0, data, size);
312 rc = SMB2_set_info_init(tcon, server,
314 COMPOUND_FID, COMPOUND_FID,
315 current->tgid, FILE_RENAME_INFORMATION,
316 SMB2_O_INFO_FILE, 0, data, size);
318 smb2_set_next_command(tcon, &rqst[num_rqst]);
319 smb2_set_related(&rqst[num_rqst]);
325 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
327 case SMB2_OP_HARDLINK:
328 rqst[num_rqst].rq_iov = &vars->si_iov[0];
329 rqst[num_rqst].rq_nvec = 2;
331 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
333 vars->link_info.ReplaceIfExists = 0;
334 vars->link_info.RootDirectory = 0;
335 vars->link_info.FileNameLength = cpu_to_le32(len);
337 size[0] = sizeof(struct smb2_file_link_info);
338 data[0] = &vars->link_info;
340 size[1] = len + 2 /* null */;
341 data[1] = (__le16 *)ptr;
343 rc = SMB2_set_info_init(tcon, server,
344 &rqst[num_rqst], COMPOUND_FID,
345 COMPOUND_FID, current->tgid,
346 FILE_LINK_INFORMATION,
347 SMB2_O_INFO_FILE, 0, data, size);
350 smb2_set_next_command(tcon, &rqst[num_rqst]);
351 smb2_set_related(&rqst[num_rqst++]);
352 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
355 cifs_dbg(VFS, "Invalid command\n");
361 /* We already have a handle so we can skip the close */
365 flags |= CIFS_CP_CREATE_CLOSE_OP;
366 rqst[num_rqst].rq_iov = &vars->close_iov[0];
367 rqst[num_rqst].rq_nvec = 1;
368 rc = SMB2_close_init(tcon, server,
369 &rqst[num_rqst], COMPOUND_FID,
370 COMPOUND_FID, false);
371 smb2_set_related(&rqst[num_rqst]);
378 rc = compound_send_recv(xid, ses, server,
380 &rqst[1], &resp_buftype[1],
383 rc = compound_send_recv(xid, ses, server,
390 cifsFileInfo_put(cfile);
392 SMB2_open_free(&rqst[0]);
393 if (rc == -EREMCHG) {
394 pr_warn_once("server share %s deleted\n", tcon->treeName);
395 tcon->need_reconnect = true;
399 case SMB2_OP_QUERY_INFO:
401 qi_rsp = (struct smb2_query_info_rsp *)
403 rc = smb2_validate_and_copy_iov(
404 le16_to_cpu(qi_rsp->OutputBufferOffset),
405 le32_to_cpu(qi_rsp->OutputBufferLength),
406 &rsp_iov[1], sizeof(struct smb2_file_all_info),
410 SMB2_query_info_free(&rqst[1]);
412 SMB2_close_free(&rqst[2]);
414 trace_smb3_query_info_compound_err(xid, ses->Suid,
417 trace_smb3_query_info_compound_done(xid, ses->Suid,
420 case SMB2_OP_POSIX_QUERY_INFO:
422 qi_rsp = (struct smb2_query_info_rsp *)
424 rc = smb2_validate_and_copy_iov(
425 le16_to_cpu(qi_rsp->OutputBufferOffset),
426 le32_to_cpu(qi_rsp->OutputBufferLength),
427 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
430 SMB2_query_info_free(&rqst[1]);
432 SMB2_close_free(&rqst[2]);
434 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
436 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
440 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
442 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
444 SMB2_close_free(&rqst[1]);
448 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
450 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
452 SMB2_close_free(&rqst[1]);
454 case SMB2_OP_HARDLINK:
456 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
458 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
459 free_set_inf_compound(rqst);
463 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
465 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
466 free_set_inf_compound(rqst);
470 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
472 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
473 free_set_inf_compound(rqst);
475 case SMB2_OP_SET_EOF:
477 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
479 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
480 free_set_inf_compound(rqst);
482 case SMB2_OP_SET_INFO:
484 trace_smb3_set_info_compound_err(xid, ses->Suid,
487 trace_smb3_set_info_compound_done(xid, ses->Suid,
489 free_set_inf_compound(rqst);
492 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
493 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
494 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
500 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
502 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
503 dst->CurrentByteOffset = src->CurrentByteOffset;
504 dst->Mode = src->Mode;
505 dst->AlignmentRequirement = src->AlignmentRequirement;
506 dst->IndexNumber1 = 0; /* we don't use it */
510 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
511 struct cifs_sb_info *cifs_sb, const char *full_path,
512 FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
515 struct smb2_file_all_info *smb2_data;
516 __u32 create_options = 0;
517 struct cifsFileInfo *cfile;
518 struct cached_fid *cfid = NULL;
523 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
525 if (smb2_data == NULL)
528 /* If it is a root and its handle is cached then use it */
529 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
531 if (tcon->crfid.file_all_info_is_valid) {
532 move_smb2_info_to_cifs(data,
533 &tcon->crfid.file_all_info);
535 rc = SMB2_query_info(xid, tcon,
536 cfid->fid->persistent_fid,
537 cfid->fid->volatile_fid, smb2_data);
539 move_smb2_info_to_cifs(data, smb2_data);
541 close_cached_dir(cfid);
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, create_options,
548 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
549 if (rc == -EOPNOTSUPP) {
551 create_options |= OPEN_REPARSE_POINT;
553 /* Failed on a symbolic link - query a reparse point info */
554 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
555 FILE_READ_ATTRIBUTES, FILE_OPEN,
556 create_options, ACL_NO_MODE,
557 smb2_data, SMB2_OP_QUERY_INFO, NULL);
562 move_smb2_info_to_cifs(data, smb2_data);
570 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
571 struct cifs_sb_info *cifs_sb, const char *full_path,
572 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
575 __u32 create_options = 0;
576 struct cifsFileInfo *cfile;
577 struct smb311_posix_qinfo *smb2_data;
582 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
583 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
585 if (smb2_data == NULL)
589 * BB TODO: Add support for using the cached root handle.
590 * Create SMB2_query_posix_info worker function to do non-compounded query
591 * when we already have an open file handle for this. For now this is fast enough
592 * (always using the compounded version).
595 cifs_get_readable_path(tcon, full_path, &cfile);
596 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
597 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
598 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
599 if (rc == -EOPNOTSUPP) {
600 /* BB TODO: When support for special files added to Samba re-verify this path */
602 create_options |= OPEN_REPARSE_POINT;
604 /* Failed on a symbolic link - query a reparse point info */
605 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
606 FILE_READ_ATTRIBUTES, FILE_OPEN,
607 create_options, ACL_NO_MODE,
608 smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
613 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
614 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
622 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
623 struct cifs_tcon *tcon, const char *name,
624 struct cifs_sb_info *cifs_sb)
626 return smb2_compound_op(xid, tcon, cifs_sb, name,
627 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
628 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
633 smb2_mkdir_setinfo(struct inode *inode, const char *name,
634 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
635 const unsigned int xid)
637 FILE_BASIC_INFO data;
638 struct cifsInodeInfo *cifs_i;
639 struct cifsFileInfo *cfile;
643 memset(&data, 0, sizeof(data));
644 cifs_i = CIFS_I(inode);
645 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
646 data.Attributes = cpu_to_le32(dosattrs);
647 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
648 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
649 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
650 CREATE_NOT_FILE, ACL_NO_MODE,
651 &data, SMB2_OP_SET_INFO, cfile);
653 cifs_i->cifsAttrs = dosattrs;
657 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
658 struct cifs_sb_info *cifs_sb)
660 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
661 CREATE_NOT_FILE, ACL_NO_MODE,
662 NULL, SMB2_OP_RMDIR, NULL);
666 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
667 struct cifs_sb_info *cifs_sb)
669 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
670 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
671 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
675 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
676 const char *from_name, const char *to_name,
677 struct cifs_sb_info *cifs_sb, __u32 access, int command,
678 struct cifsFileInfo *cfile)
680 __le16 *smb2_to_name = NULL;
683 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
684 if (smb2_to_name == NULL) {
686 goto smb2_rename_path;
688 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
689 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
697 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
698 const char *from_name, const char *to_name,
699 struct cifs_sb_info *cifs_sb)
701 struct cifsFileInfo *cfile;
703 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
705 return smb2_set_path_attr(xid, tcon, from_name, to_name,
706 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
710 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
711 const char *from_name, const char *to_name,
712 struct cifs_sb_info *cifs_sb)
714 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
715 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
720 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
721 const char *full_path, __u64 size,
722 struct cifs_sb_info *cifs_sb, bool set_alloc)
724 __le64 eof = cpu_to_le64(size);
726 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
727 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
728 &eof, SMB2_OP_SET_EOF, NULL);
732 smb2_set_file_info(struct inode *inode, const char *full_path,
733 FILE_BASIC_INFO *buf, const unsigned int xid)
735 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
736 struct tcon_link *tlink;
739 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
740 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
741 (buf->Attributes == 0))
742 return 0; /* would be a no op, no sense sending this */
744 tlink = cifs_sb_tlink(cifs_sb);
746 return PTR_ERR(tlink);
748 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
749 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
750 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
751 cifs_put_tlink(tlink);