1 // SPDX-License-Identifier: LGPL-2.1
5 * Copyright (C) International Business Machines Corp., 2002, 2011
7 * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
8 * Steve French (sfrench@us.ibm.com)
12 #include <linux/stat.h>
13 #include <linux/slab.h>
14 #include <linux/pagemap.h>
15 #include <asm/div64.h>
19 #include "cifsproto.h"
20 #include "cifs_debug.h"
21 #include "cifs_fs_sb.h"
22 #include "cifs_unicode.h"
26 #include "smb2proto.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 smb2_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)
57 struct cop_vars *vars = NULL;
59 struct smb_rqst *rqst;
61 __le16 *utf16_path = NULL;
62 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
64 struct cifs_ses *ses = tcon->ses;
65 struct TCP_Server_Info *server;
68 struct smb2_query_info_rsp *qi_rsp = NULL;
70 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
75 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
78 rqst = &vars->rqst[0];
79 rsp_iov = &vars->rsp_iov[0];
81 server = cifs_pick_channel(ses);
83 if (smb3_encryption_required(tcon))
84 flags |= CIFS_TRANSFORM_REQ;
86 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
88 /* We already have a handle so we can skip the open */
93 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
99 vars->oparms.tcon = tcon;
100 vars->oparms.desired_access = desired_access;
101 vars->oparms.disposition = create_disposition;
102 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
103 vars->oparms.fid = &fid;
104 vars->oparms.reconnect = false;
105 vars->oparms.mode = mode;
106 vars->oparms.cifs_sb = cifs_sb;
108 rqst[num_rqst].rq_iov = &vars->open_iov[0];
109 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
110 rc = SMB2_open_init(tcon, server,
111 &rqst[num_rqst], &oplock, &vars->oparms,
117 smb2_set_next_command(tcon, &rqst[num_rqst]);
124 case SMB2_OP_QUERY_INFO:
125 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
126 rqst[num_rqst].rq_nvec = 1;
129 rc = SMB2_query_info_init(tcon, server,
131 cfile->fid.persistent_fid,
132 cfile->fid.volatile_fid,
133 FILE_ALL_INFORMATION,
135 sizeof(struct smb2_file_all_info) +
136 PATH_MAX * 2, 0, NULL);
138 rc = SMB2_query_info_init(tcon, server,
142 FILE_ALL_INFORMATION,
144 sizeof(struct smb2_file_all_info) +
145 PATH_MAX * 2, 0, NULL);
147 smb2_set_next_command(tcon, &rqst[num_rqst]);
148 smb2_set_related(&rqst[num_rqst]);
155 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
158 case SMB2_OP_POSIX_QUERY_INFO:
159 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
160 rqst[num_rqst].rq_nvec = 1;
163 rc = SMB2_query_info_init(tcon, server,
165 cfile->fid.persistent_fid,
166 cfile->fid.volatile_fid,
167 SMB_FIND_FILE_POSIX_INFO,
169 /* TBD: fix following to allow for longer SIDs */
170 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
171 (sizeof(struct cifs_sid) * 2), 0, NULL);
173 rc = SMB2_query_info_init(tcon, server,
177 SMB_FIND_FILE_POSIX_INFO,
179 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
180 (sizeof(struct cifs_sid) * 2), 0, NULL);
182 smb2_set_next_command(tcon, &rqst[num_rqst]);
183 smb2_set_related(&rqst[num_rqst]);
190 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
193 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
197 * Directories are created through parameters in the
200 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
203 rqst[num_rqst].rq_iov = &vars->si_iov[0];
204 rqst[num_rqst].rq_nvec = 1;
206 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
207 data[0] = &delete_pending[0];
209 rc = SMB2_set_info_init(tcon, server,
210 &rqst[num_rqst], COMPOUND_FID,
211 COMPOUND_FID, current->tgid,
212 FILE_DISPOSITION_INFORMATION,
213 SMB2_O_INFO_FILE, 0, data, size);
216 smb2_set_next_command(tcon, &rqst[num_rqst]);
217 smb2_set_related(&rqst[num_rqst++]);
218 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
220 case SMB2_OP_SET_EOF:
221 rqst[num_rqst].rq_iov = &vars->si_iov[0];
222 rqst[num_rqst].rq_nvec = 1;
224 size[0] = 8; /* sizeof __le64 */
227 rc = SMB2_set_info_init(tcon, server,
228 &rqst[num_rqst], COMPOUND_FID,
229 COMPOUND_FID, current->tgid,
230 FILE_END_OF_FILE_INFORMATION,
231 SMB2_O_INFO_FILE, 0, data, size);
234 smb2_set_next_command(tcon, &rqst[num_rqst]);
235 smb2_set_related(&rqst[num_rqst++]);
236 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
238 case SMB2_OP_SET_INFO:
239 rqst[num_rqst].rq_iov = &vars->si_iov[0];
240 rqst[num_rqst].rq_nvec = 1;
243 size[0] = sizeof(FILE_BASIC_INFO);
247 rc = SMB2_set_info_init(tcon, server,
249 cfile->fid.persistent_fid,
250 cfile->fid.volatile_fid, current->tgid,
251 FILE_BASIC_INFORMATION,
252 SMB2_O_INFO_FILE, 0, data, size);
254 rc = SMB2_set_info_init(tcon, server,
257 COMPOUND_FID, current->tgid,
258 FILE_BASIC_INFORMATION,
259 SMB2_O_INFO_FILE, 0, data, size);
261 smb2_set_next_command(tcon, &rqst[num_rqst]);
262 smb2_set_related(&rqst[num_rqst]);
269 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
273 rqst[num_rqst].rq_iov = &vars->si_iov[0];
274 rqst[num_rqst].rq_nvec = 2;
276 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
278 vars->rename_info.ReplaceIfExists = 1;
279 vars->rename_info.RootDirectory = 0;
280 vars->rename_info.FileNameLength = cpu_to_le32(len);
282 size[0] = sizeof(struct smb2_file_rename_info);
283 data[0] = &vars->rename_info;
285 size[1] = len + 2 /* null */;
286 data[1] = (__le16 *)ptr;
289 rc = SMB2_set_info_init(tcon, server,
291 cfile->fid.persistent_fid,
292 cfile->fid.volatile_fid,
293 current->tgid, FILE_RENAME_INFORMATION,
294 SMB2_O_INFO_FILE, 0, data, size);
296 rc = SMB2_set_info_init(tcon, server,
298 COMPOUND_FID, COMPOUND_FID,
299 current->tgid, FILE_RENAME_INFORMATION,
300 SMB2_O_INFO_FILE, 0, data, size);
302 smb2_set_next_command(tcon, &rqst[num_rqst]);
303 smb2_set_related(&rqst[num_rqst]);
309 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
311 case SMB2_OP_HARDLINK:
312 rqst[num_rqst].rq_iov = &vars->si_iov[0];
313 rqst[num_rqst].rq_nvec = 2;
315 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
317 vars->link_info.ReplaceIfExists = 0;
318 vars->link_info.RootDirectory = 0;
319 vars->link_info.FileNameLength = cpu_to_le32(len);
321 size[0] = sizeof(struct smb2_file_link_info);
322 data[0] = &vars->link_info;
324 size[1] = len + 2 /* null */;
325 data[1] = (__le16 *)ptr;
327 rc = SMB2_set_info_init(tcon, server,
328 &rqst[num_rqst], COMPOUND_FID,
329 COMPOUND_FID, current->tgid,
330 FILE_LINK_INFORMATION,
331 SMB2_O_INFO_FILE, 0, data, size);
334 smb2_set_next_command(tcon, &rqst[num_rqst]);
335 smb2_set_related(&rqst[num_rqst++]);
336 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
339 cifs_dbg(VFS, "Invalid command\n");
345 /* We already have a handle so we can skip the close */
349 flags |= CIFS_CP_CREATE_CLOSE_OP;
350 rqst[num_rqst].rq_iov = &vars->close_iov[0];
351 rqst[num_rqst].rq_nvec = 1;
352 rc = SMB2_close_init(tcon, server,
353 &rqst[num_rqst], COMPOUND_FID,
354 COMPOUND_FID, false);
355 smb2_set_related(&rqst[num_rqst]);
362 cifsFileInfo_put(cfile);
364 rc = compound_send_recv(xid, ses, server,
366 &rqst[1], &resp_buftype[1],
369 rc = compound_send_recv(xid, ses, server,
376 cifsFileInfo_put(cfile);
378 SMB2_open_free(&rqst[0]);
379 if (rc == -EREMCHG) {
380 pr_warn_once("server share %s deleted\n", tcon->treeName);
381 tcon->need_reconnect = true;
385 case SMB2_OP_QUERY_INFO:
387 qi_rsp = (struct smb2_query_info_rsp *)
389 rc = smb2_validate_and_copy_iov(
390 le16_to_cpu(qi_rsp->OutputBufferOffset),
391 le32_to_cpu(qi_rsp->OutputBufferLength),
392 &rsp_iov[1], sizeof(struct smb2_file_all_info),
396 SMB2_query_info_free(&rqst[1]);
398 SMB2_close_free(&rqst[2]);
400 trace_smb3_query_info_compound_err(xid, ses->Suid,
403 trace_smb3_query_info_compound_done(xid, ses->Suid,
406 case SMB2_OP_POSIX_QUERY_INFO:
408 qi_rsp = (struct smb2_query_info_rsp *)
410 rc = smb2_validate_and_copy_iov(
411 le16_to_cpu(qi_rsp->OutputBufferOffset),
412 le32_to_cpu(qi_rsp->OutputBufferLength),
413 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
416 SMB2_query_info_free(&rqst[1]);
418 SMB2_close_free(&rqst[2]);
420 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
422 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
426 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
428 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
430 SMB2_close_free(&rqst[1]);
434 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
436 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
438 SMB2_close_free(&rqst[1]);
440 case SMB2_OP_HARDLINK:
442 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
444 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
445 free_set_inf_compound(rqst);
449 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
451 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
452 free_set_inf_compound(rqst);
456 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
458 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
459 free_set_inf_compound(rqst);
461 case SMB2_OP_SET_EOF:
463 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
465 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
466 free_set_inf_compound(rqst);
468 case SMB2_OP_SET_INFO:
470 trace_smb3_set_info_compound_err(xid, ses->Suid,
473 trace_smb3_set_info_compound_done(xid, ses->Suid,
475 free_set_inf_compound(rqst);
478 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
479 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
480 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
486 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
488 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
489 dst->CurrentByteOffset = src->CurrentByteOffset;
490 dst->Mode = src->Mode;
491 dst->AlignmentRequirement = src->AlignmentRequirement;
492 dst->IndexNumber1 = 0; /* we don't use it */
496 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
497 struct cifs_sb_info *cifs_sb, const char *full_path,
498 FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
501 struct smb2_file_all_info *smb2_data;
502 __u32 create_options = 0;
503 struct cifsFileInfo *cfile;
504 struct cached_fid *cfid = NULL;
509 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
511 if (smb2_data == NULL)
514 /* If it is a root and its handle is cached then use it */
515 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
517 if (tcon->crfid.file_all_info_is_valid) {
518 move_smb2_info_to_cifs(data,
519 &tcon->crfid.file_all_info);
521 rc = SMB2_query_info(xid, tcon,
522 cfid->fid->persistent_fid,
523 cfid->fid->volatile_fid, smb2_data);
525 move_smb2_info_to_cifs(data, smb2_data);
527 close_cached_dir(cfid);
531 cifs_get_readable_path(tcon, full_path, &cfile);
532 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
533 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
534 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
535 if (rc == -EOPNOTSUPP) {
537 create_options |= OPEN_REPARSE_POINT;
539 /* Failed on a symbolic link - query a reparse point info */
540 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
541 FILE_READ_ATTRIBUTES, FILE_OPEN,
542 create_options, ACL_NO_MODE,
543 smb2_data, SMB2_OP_QUERY_INFO, NULL);
548 move_smb2_info_to_cifs(data, smb2_data);
556 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
557 struct cifs_sb_info *cifs_sb, const char *full_path,
558 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
561 __u32 create_options = 0;
562 struct cifsFileInfo *cfile;
563 struct smb311_posix_qinfo *smb2_data;
568 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
569 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
571 if (smb2_data == NULL)
575 * BB TODO: Add support for using the cached root handle.
576 * Create SMB2_query_posix_info worker function to do non-compounded query
577 * when we already have an open file handle for this. For now this is fast enough
578 * (always using the compounded version).
581 cifs_get_readable_path(tcon, full_path, &cfile);
582 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
583 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
584 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
585 if (rc == -EOPNOTSUPP) {
586 /* BB TODO: When support for special files added to Samba re-verify this path */
588 create_options |= OPEN_REPARSE_POINT;
590 /* Failed on a symbolic link - query a reparse point info */
591 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
592 FILE_READ_ATTRIBUTES, FILE_OPEN,
593 create_options, ACL_NO_MODE,
594 smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
599 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
600 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
608 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
609 struct cifs_tcon *tcon, const char *name,
610 struct cifs_sb_info *cifs_sb)
612 return smb2_compound_op(xid, tcon, cifs_sb, name,
613 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
614 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
619 smb2_mkdir_setinfo(struct inode *inode, const char *name,
620 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
621 const unsigned int xid)
623 FILE_BASIC_INFO data;
624 struct cifsInodeInfo *cifs_i;
625 struct cifsFileInfo *cfile;
629 memset(&data, 0, sizeof(data));
630 cifs_i = CIFS_I(inode);
631 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
632 data.Attributes = cpu_to_le32(dosattrs);
633 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
634 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
635 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
636 CREATE_NOT_FILE, ACL_NO_MODE,
637 &data, SMB2_OP_SET_INFO, cfile);
639 cifs_i->cifsAttrs = dosattrs;
643 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
644 struct cifs_sb_info *cifs_sb)
646 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
647 CREATE_NOT_FILE, ACL_NO_MODE,
648 NULL, SMB2_OP_RMDIR, NULL);
652 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
653 struct cifs_sb_info *cifs_sb)
655 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
656 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
657 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
661 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
662 const char *from_name, const char *to_name,
663 struct cifs_sb_info *cifs_sb, __u32 access, int command,
664 struct cifsFileInfo *cfile)
666 __le16 *smb2_to_name = NULL;
669 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
670 if (smb2_to_name == NULL) {
672 goto smb2_rename_path;
674 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
675 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
683 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
684 const char *from_name, const char *to_name,
685 struct cifs_sb_info *cifs_sb)
687 struct cifsFileInfo *cfile;
689 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
691 return smb2_set_path_attr(xid, tcon, from_name, to_name,
692 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
696 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
697 const char *from_name, const char *to_name,
698 struct cifs_sb_info *cifs_sb)
700 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
701 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
706 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
707 const char *full_path, __u64 size,
708 struct cifs_sb_info *cifs_sb, bool set_alloc)
710 __le64 eof = cpu_to_le64(size);
712 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
713 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
714 &eof, SMB2_OP_SET_EOF, NULL);
718 smb2_set_file_info(struct inode *inode, const char *full_path,
719 FILE_BASIC_INFO *buf, const unsigned int xid)
721 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
722 struct tcon_link *tlink;
725 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
726 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
727 (buf->Attributes == 0))
728 return 0; /* would be a no op, no sense sending this */
730 tlink = cifs_sb_tlink(cifs_sb);
732 return PTR_ERR(tlink);
734 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
735 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
736 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
737 cifs_put_tlink(tlink);