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 */
226 rc = SMB2_set_info_init(tcon, server,
227 &rqst[num_rqst], COMPOUND_FID,
228 COMPOUND_FID, current->tgid,
229 FILE_END_OF_FILE_INFORMATION,
230 SMB2_O_INFO_FILE, 0, data, size);
233 smb2_set_next_command(tcon, &rqst[num_rqst]);
234 smb2_set_related(&rqst[num_rqst++]);
235 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
237 case SMB2_OP_SET_INFO:
238 rqst[num_rqst].rq_iov = &vars->si_iov[0];
239 rqst[num_rqst].rq_nvec = 1;
242 size[0] = sizeof(FILE_BASIC_INFO);
246 rc = SMB2_set_info_init(tcon, server,
248 cfile->fid.persistent_fid,
249 cfile->fid.volatile_fid, current->tgid,
250 FILE_BASIC_INFORMATION,
251 SMB2_O_INFO_FILE, 0, data, size);
253 rc = SMB2_set_info_init(tcon, server,
256 COMPOUND_FID, current->tgid,
257 FILE_BASIC_INFORMATION,
258 SMB2_O_INFO_FILE, 0, data, size);
260 smb2_set_next_command(tcon, &rqst[num_rqst]);
261 smb2_set_related(&rqst[num_rqst]);
268 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
272 rqst[num_rqst].rq_iov = &vars->si_iov[0];
273 rqst[num_rqst].rq_nvec = 2;
275 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
277 vars->rename_info.ReplaceIfExists = 1;
278 vars->rename_info.RootDirectory = 0;
279 vars->rename_info.FileNameLength = cpu_to_le32(len);
281 size[0] = sizeof(struct smb2_file_rename_info);
282 data[0] = &vars->rename_info;
284 size[1] = len + 2 /* null */;
285 data[1] = (__le16 *)ptr;
288 rc = SMB2_set_info_init(tcon, server,
290 cfile->fid.persistent_fid,
291 cfile->fid.volatile_fid,
292 current->tgid, FILE_RENAME_INFORMATION,
293 SMB2_O_INFO_FILE, 0, data, size);
295 rc = SMB2_set_info_init(tcon, server,
297 COMPOUND_FID, COMPOUND_FID,
298 current->tgid, FILE_RENAME_INFORMATION,
299 SMB2_O_INFO_FILE, 0, data, size);
301 smb2_set_next_command(tcon, &rqst[num_rqst]);
302 smb2_set_related(&rqst[num_rqst]);
308 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
310 case SMB2_OP_HARDLINK:
311 rqst[num_rqst].rq_iov = &vars->si_iov[0];
312 rqst[num_rqst].rq_nvec = 2;
314 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
316 vars->link_info.ReplaceIfExists = 0;
317 vars->link_info.RootDirectory = 0;
318 vars->link_info.FileNameLength = cpu_to_le32(len);
320 size[0] = sizeof(struct smb2_file_link_info);
321 data[0] = &vars->link_info;
323 size[1] = len + 2 /* null */;
324 data[1] = (__le16 *)ptr;
326 rc = SMB2_set_info_init(tcon, server,
327 &rqst[num_rqst], COMPOUND_FID,
328 COMPOUND_FID, current->tgid,
329 FILE_LINK_INFORMATION,
330 SMB2_O_INFO_FILE, 0, data, size);
333 smb2_set_next_command(tcon, &rqst[num_rqst]);
334 smb2_set_related(&rqst[num_rqst++]);
335 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
338 cifs_dbg(VFS, "Invalid command\n");
344 /* We already have a handle so we can skip the close */
348 flags |= CIFS_CP_CREATE_CLOSE_OP;
349 rqst[num_rqst].rq_iov = &vars->close_iov[0];
350 rqst[num_rqst].rq_nvec = 1;
351 rc = SMB2_close_init(tcon, server,
352 &rqst[num_rqst], COMPOUND_FID,
353 COMPOUND_FID, false);
354 smb2_set_related(&rqst[num_rqst]);
361 cifsFileInfo_put(cfile);
363 rc = compound_send_recv(xid, ses, server,
365 &rqst[1], &resp_buftype[1],
368 rc = compound_send_recv(xid, ses, server,
375 cifsFileInfo_put(cfile);
377 SMB2_open_free(&rqst[0]);
378 if (rc == -EREMCHG) {
379 pr_warn_once("server share %s deleted\n", tcon->treeName);
380 tcon->need_reconnect = true;
384 case SMB2_OP_QUERY_INFO:
386 qi_rsp = (struct smb2_query_info_rsp *)
388 rc = smb2_validate_and_copy_iov(
389 le16_to_cpu(qi_rsp->OutputBufferOffset),
390 le32_to_cpu(qi_rsp->OutputBufferLength),
391 &rsp_iov[1], sizeof(struct smb2_file_all_info),
395 SMB2_query_info_free(&rqst[1]);
397 SMB2_close_free(&rqst[2]);
399 trace_smb3_query_info_compound_err(xid, ses->Suid,
402 trace_smb3_query_info_compound_done(xid, ses->Suid,
405 case SMB2_OP_POSIX_QUERY_INFO:
407 qi_rsp = (struct smb2_query_info_rsp *)
409 rc = smb2_validate_and_copy_iov(
410 le16_to_cpu(qi_rsp->OutputBufferOffset),
411 le32_to_cpu(qi_rsp->OutputBufferLength),
412 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
415 SMB2_query_info_free(&rqst[1]);
417 SMB2_close_free(&rqst[2]);
419 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
421 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
425 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
427 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
429 SMB2_close_free(&rqst[1]);
433 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
435 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
437 SMB2_close_free(&rqst[1]);
439 case SMB2_OP_HARDLINK:
441 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
443 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
444 free_set_inf_compound(rqst);
448 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
450 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
451 free_set_inf_compound(rqst);
455 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
457 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
458 free_set_inf_compound(rqst);
460 case SMB2_OP_SET_EOF:
462 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
464 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
465 free_set_inf_compound(rqst);
467 case SMB2_OP_SET_INFO:
469 trace_smb3_set_info_compound_err(xid, ses->Suid,
472 trace_smb3_set_info_compound_done(xid, ses->Suid,
474 free_set_inf_compound(rqst);
477 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
478 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
479 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
485 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
487 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
488 dst->CurrentByteOffset = src->CurrentByteOffset;
489 dst->Mode = src->Mode;
490 dst->AlignmentRequirement = src->AlignmentRequirement;
491 dst->IndexNumber1 = 0; /* we don't use it */
495 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
496 struct cifs_sb_info *cifs_sb, const char *full_path,
497 FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
500 struct smb2_file_all_info *smb2_data;
501 __u32 create_options = 0;
502 struct cifsFileInfo *cfile;
503 struct cached_fid *cfid = NULL;
508 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
510 if (smb2_data == NULL)
513 /* If it is a root and its handle is cached then use it */
514 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
516 if (tcon->crfid.file_all_info_is_valid) {
517 move_smb2_info_to_cifs(data,
518 &tcon->crfid.file_all_info);
520 rc = SMB2_query_info(xid, tcon,
521 cfid->fid->persistent_fid,
522 cfid->fid->volatile_fid, smb2_data);
524 move_smb2_info_to_cifs(data, smb2_data);
526 close_cached_dir(cfid);
530 cifs_get_readable_path(tcon, full_path, &cfile);
531 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
532 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
533 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
534 if (rc == -EOPNOTSUPP) {
536 create_options |= OPEN_REPARSE_POINT;
538 /* Failed on a symbolic link - query a reparse point info */
539 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
540 FILE_READ_ATTRIBUTES, FILE_OPEN,
541 create_options, ACL_NO_MODE,
542 smb2_data, SMB2_OP_QUERY_INFO, NULL);
547 move_smb2_info_to_cifs(data, smb2_data);
555 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
556 struct cifs_sb_info *cifs_sb, const char *full_path,
557 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
560 __u32 create_options = 0;
561 struct cifsFileInfo *cfile;
562 struct smb311_posix_qinfo *smb2_data;
567 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
568 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
570 if (smb2_data == NULL)
574 * BB TODO: Add support for using the cached root handle.
575 * Create SMB2_query_posix_info worker function to do non-compounded query
576 * when we already have an open file handle for this. For now this is fast enough
577 * (always using the compounded version).
580 cifs_get_readable_path(tcon, full_path, &cfile);
581 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
582 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
583 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
584 if (rc == -EOPNOTSUPP) {
585 /* BB TODO: When support for special files added to Samba re-verify this path */
587 create_options |= OPEN_REPARSE_POINT;
589 /* Failed on a symbolic link - query a reparse point info */
590 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
591 FILE_READ_ATTRIBUTES, FILE_OPEN,
592 create_options, ACL_NO_MODE,
593 smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
598 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
599 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
607 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
608 struct cifs_tcon *tcon, const char *name,
609 struct cifs_sb_info *cifs_sb)
611 return smb2_compound_op(xid, tcon, cifs_sb, name,
612 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
613 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
618 smb2_mkdir_setinfo(struct inode *inode, const char *name,
619 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
620 const unsigned int xid)
622 FILE_BASIC_INFO data;
623 struct cifsInodeInfo *cifs_i;
624 struct cifsFileInfo *cfile;
628 memset(&data, 0, sizeof(data));
629 cifs_i = CIFS_I(inode);
630 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
631 data.Attributes = cpu_to_le32(dosattrs);
632 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
633 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
634 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
635 CREATE_NOT_FILE, ACL_NO_MODE,
636 &data, SMB2_OP_SET_INFO, cfile);
638 cifs_i->cifsAttrs = dosattrs;
642 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
643 struct cifs_sb_info *cifs_sb)
645 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
646 CREATE_NOT_FILE, ACL_NO_MODE,
647 NULL, SMB2_OP_RMDIR, NULL);
651 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
652 struct cifs_sb_info *cifs_sb)
654 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
655 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
656 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
660 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
661 const char *from_name, const char *to_name,
662 struct cifs_sb_info *cifs_sb, __u32 access, int command,
663 struct cifsFileInfo *cfile)
665 __le16 *smb2_to_name = NULL;
668 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
669 if (smb2_to_name == NULL) {
671 goto smb2_rename_path;
673 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
674 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
682 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
683 const char *from_name, const char *to_name,
684 struct cifs_sb_info *cifs_sb)
686 struct cifsFileInfo *cfile;
688 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
690 return smb2_set_path_attr(xid, tcon, from_name, to_name,
691 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
695 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
696 const char *from_name, const char *to_name,
697 struct cifs_sb_info *cifs_sb)
699 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
700 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
705 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
706 const char *full_path, __u64 size,
707 struct cifs_sb_info *cifs_sb, bool set_alloc)
709 __le64 eof = cpu_to_le64(size);
711 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
712 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
713 &eof, SMB2_OP_SET_EOF, NULL);
717 smb2_set_file_info(struct inode *inode, const char *full_path,
718 FILE_BASIC_INFO *buf, const unsigned int xid)
720 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
721 struct tcon_link *tlink;
724 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
725 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
726 (buf->Attributes == 0))
727 return 0; /* would be a no op, no sense sending this */
729 tlink = cifs_sb_tlink(cifs_sb);
731 return PTR_ERR(tlink);
733 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
734 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
735 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
736 cifs_put_tlink(tlink);