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 * note: If cfile is passed, the reference to it is dropped here.
51 * So make sure that you do not reuse cfile after return from this func.
54 smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
55 struct cifs_sb_info *cifs_sb, const char *full_path,
56 __u32 desired_access, __u32 create_disposition,
57 __u32 create_options, umode_t mode, void *ptr, int command,
58 struct cifsFileInfo *cfile)
60 struct cop_vars *vars = NULL;
62 struct smb_rqst *rqst;
64 __le16 *utf16_path = NULL;
65 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
67 struct cifs_ses *ses = tcon->ses;
68 struct TCP_Server_Info *server;
71 struct smb2_query_info_rsp *qi_rsp = NULL;
73 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
78 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
81 rqst = &vars->rqst[0];
82 rsp_iov = &vars->rsp_iov[0];
84 server = cifs_pick_channel(ses);
86 if (smb3_encryption_required(tcon))
87 flags |= CIFS_TRANSFORM_REQ;
89 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
91 /* We already have a handle so we can skip the open */
96 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
102 vars->oparms.tcon = tcon;
103 vars->oparms.desired_access = desired_access;
104 vars->oparms.disposition = create_disposition;
105 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
106 vars->oparms.fid = &fid;
107 vars->oparms.reconnect = false;
108 vars->oparms.mode = mode;
109 vars->oparms.cifs_sb = cifs_sb;
111 rqst[num_rqst].rq_iov = &vars->open_iov[0];
112 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
113 rc = SMB2_open_init(tcon, server,
114 &rqst[num_rqst], &oplock, &vars->oparms,
120 smb2_set_next_command(tcon, &rqst[num_rqst]);
127 case SMB2_OP_QUERY_INFO:
128 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
129 rqst[num_rqst].rq_nvec = 1;
132 rc = SMB2_query_info_init(tcon, server,
134 cfile->fid.persistent_fid,
135 cfile->fid.volatile_fid,
136 FILE_ALL_INFORMATION,
138 sizeof(struct smb2_file_all_info) +
139 PATH_MAX * 2, 0, NULL);
141 rc = SMB2_query_info_init(tcon, server,
145 FILE_ALL_INFORMATION,
147 sizeof(struct smb2_file_all_info) +
148 PATH_MAX * 2, 0, NULL);
150 smb2_set_next_command(tcon, &rqst[num_rqst]);
151 smb2_set_related(&rqst[num_rqst]);
158 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
161 case SMB2_OP_POSIX_QUERY_INFO:
162 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
163 rqst[num_rqst].rq_nvec = 1;
166 rc = SMB2_query_info_init(tcon, server,
168 cfile->fid.persistent_fid,
169 cfile->fid.volatile_fid,
170 SMB_FIND_FILE_POSIX_INFO,
172 /* TBD: fix following to allow for longer SIDs */
173 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
174 (sizeof(struct cifs_sid) * 2), 0, NULL);
176 rc = SMB2_query_info_init(tcon, server,
180 SMB_FIND_FILE_POSIX_INFO,
182 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
183 (sizeof(struct cifs_sid) * 2), 0, NULL);
185 smb2_set_next_command(tcon, &rqst[num_rqst]);
186 smb2_set_related(&rqst[num_rqst]);
193 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
196 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
200 * Directories are created through parameters in the
203 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
206 rqst[num_rqst].rq_iov = &vars->si_iov[0];
207 rqst[num_rqst].rq_nvec = 1;
209 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
210 data[0] = &delete_pending[0];
212 rc = SMB2_set_info_init(tcon, server,
213 &rqst[num_rqst], COMPOUND_FID,
214 COMPOUND_FID, current->tgid,
215 FILE_DISPOSITION_INFORMATION,
216 SMB2_O_INFO_FILE, 0, data, size);
219 smb2_set_next_command(tcon, &rqst[num_rqst]);
220 smb2_set_related(&rqst[num_rqst++]);
221 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
223 case SMB2_OP_SET_EOF:
224 rqst[num_rqst].rq_iov = &vars->si_iov[0];
225 rqst[num_rqst].rq_nvec = 1;
227 size[0] = 8; /* sizeof __le64 */
230 rc = SMB2_set_info_init(tcon, server,
231 &rqst[num_rqst], COMPOUND_FID,
232 COMPOUND_FID, current->tgid,
233 FILE_END_OF_FILE_INFORMATION,
234 SMB2_O_INFO_FILE, 0, data, size);
237 smb2_set_next_command(tcon, &rqst[num_rqst]);
238 smb2_set_related(&rqst[num_rqst++]);
239 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
241 case SMB2_OP_SET_INFO:
242 rqst[num_rqst].rq_iov = &vars->si_iov[0];
243 rqst[num_rqst].rq_nvec = 1;
246 size[0] = sizeof(FILE_BASIC_INFO);
250 rc = SMB2_set_info_init(tcon, server,
252 cfile->fid.persistent_fid,
253 cfile->fid.volatile_fid, current->tgid,
254 FILE_BASIC_INFORMATION,
255 SMB2_O_INFO_FILE, 0, data, size);
257 rc = SMB2_set_info_init(tcon, server,
260 COMPOUND_FID, current->tgid,
261 FILE_BASIC_INFORMATION,
262 SMB2_O_INFO_FILE, 0, data, size);
264 smb2_set_next_command(tcon, &rqst[num_rqst]);
265 smb2_set_related(&rqst[num_rqst]);
272 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
276 rqst[num_rqst].rq_iov = &vars->si_iov[0];
277 rqst[num_rqst].rq_nvec = 2;
279 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
281 vars->rename_info.ReplaceIfExists = 1;
282 vars->rename_info.RootDirectory = 0;
283 vars->rename_info.FileNameLength = cpu_to_le32(len);
285 size[0] = sizeof(struct smb2_file_rename_info);
286 data[0] = &vars->rename_info;
288 size[1] = len + 2 /* null */;
289 data[1] = (__le16 *)ptr;
292 rc = SMB2_set_info_init(tcon, server,
294 cfile->fid.persistent_fid,
295 cfile->fid.volatile_fid,
296 current->tgid, FILE_RENAME_INFORMATION,
297 SMB2_O_INFO_FILE, 0, data, size);
299 rc = SMB2_set_info_init(tcon, server,
301 COMPOUND_FID, COMPOUND_FID,
302 current->tgid, FILE_RENAME_INFORMATION,
303 SMB2_O_INFO_FILE, 0, data, size);
305 smb2_set_next_command(tcon, &rqst[num_rqst]);
306 smb2_set_related(&rqst[num_rqst]);
312 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
314 case SMB2_OP_HARDLINK:
315 rqst[num_rqst].rq_iov = &vars->si_iov[0];
316 rqst[num_rqst].rq_nvec = 2;
318 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
320 vars->link_info.ReplaceIfExists = 0;
321 vars->link_info.RootDirectory = 0;
322 vars->link_info.FileNameLength = cpu_to_le32(len);
324 size[0] = sizeof(struct smb2_file_link_info);
325 data[0] = &vars->link_info;
327 size[1] = len + 2 /* null */;
328 data[1] = (__le16 *)ptr;
330 rc = SMB2_set_info_init(tcon, server,
331 &rqst[num_rqst], COMPOUND_FID,
332 COMPOUND_FID, current->tgid,
333 FILE_LINK_INFORMATION,
334 SMB2_O_INFO_FILE, 0, data, size);
337 smb2_set_next_command(tcon, &rqst[num_rqst]);
338 smb2_set_related(&rqst[num_rqst++]);
339 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
342 cifs_dbg(VFS, "Invalid command\n");
348 /* We already have a handle so we can skip the close */
352 flags |= CIFS_CP_CREATE_CLOSE_OP;
353 rqst[num_rqst].rq_iov = &vars->close_iov[0];
354 rqst[num_rqst].rq_nvec = 1;
355 rc = SMB2_close_init(tcon, server,
356 &rqst[num_rqst], COMPOUND_FID,
357 COMPOUND_FID, false);
358 smb2_set_related(&rqst[num_rqst]);
365 cifsFileInfo_put(cfile);
367 rc = compound_send_recv(xid, ses, server,
369 &rqst[1], &resp_buftype[1],
372 rc = compound_send_recv(xid, ses, server,
379 cifsFileInfo_put(cfile);
381 SMB2_open_free(&rqst[0]);
382 if (rc == -EREMCHG) {
383 pr_warn_once("server share %s deleted\n", tcon->treeName);
384 tcon->need_reconnect = true;
388 case SMB2_OP_QUERY_INFO:
390 qi_rsp = (struct smb2_query_info_rsp *)
392 rc = smb2_validate_and_copy_iov(
393 le16_to_cpu(qi_rsp->OutputBufferOffset),
394 le32_to_cpu(qi_rsp->OutputBufferLength),
395 &rsp_iov[1], sizeof(struct smb2_file_all_info),
399 SMB2_query_info_free(&rqst[1]);
401 SMB2_close_free(&rqst[2]);
403 trace_smb3_query_info_compound_err(xid, ses->Suid,
406 trace_smb3_query_info_compound_done(xid, ses->Suid,
409 case SMB2_OP_POSIX_QUERY_INFO:
411 qi_rsp = (struct smb2_query_info_rsp *)
413 rc = smb2_validate_and_copy_iov(
414 le16_to_cpu(qi_rsp->OutputBufferOffset),
415 le32_to_cpu(qi_rsp->OutputBufferLength),
416 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
419 SMB2_query_info_free(&rqst[1]);
421 SMB2_close_free(&rqst[2]);
423 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
425 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
429 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
431 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
433 SMB2_close_free(&rqst[1]);
437 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
439 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
441 SMB2_close_free(&rqst[1]);
443 case SMB2_OP_HARDLINK:
445 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
447 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
448 free_set_inf_compound(rqst);
452 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
454 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
455 free_set_inf_compound(rqst);
459 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
461 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
462 free_set_inf_compound(rqst);
464 case SMB2_OP_SET_EOF:
466 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
468 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
469 free_set_inf_compound(rqst);
471 case SMB2_OP_SET_INFO:
473 trace_smb3_set_info_compound_err(xid, ses->Suid,
476 trace_smb3_set_info_compound_done(xid, ses->Suid,
478 free_set_inf_compound(rqst);
481 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
482 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
483 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
489 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
491 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
492 dst->CurrentByteOffset = src->CurrentByteOffset;
493 dst->Mode = src->Mode;
494 dst->AlignmentRequirement = src->AlignmentRequirement;
495 dst->IndexNumber1 = 0; /* we don't use it */
499 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
500 struct cifs_sb_info *cifs_sb, const char *full_path,
501 FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
504 struct smb2_file_all_info *smb2_data;
505 __u32 create_options = 0;
506 struct cifsFileInfo *cfile;
507 struct cached_fid *cfid = NULL;
512 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
514 if (smb2_data == NULL)
517 /* If it is a root and its handle is cached then use it */
518 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, &cfid);
520 if (tcon->crfid.file_all_info_is_valid) {
521 move_smb2_info_to_cifs(data,
522 &tcon->crfid.file_all_info);
524 rc = SMB2_query_info(xid, tcon,
525 cfid->fid->persistent_fid,
526 cfid->fid->volatile_fid, smb2_data);
528 move_smb2_info_to_cifs(data, smb2_data);
530 close_cached_dir(cfid);
534 cifs_get_readable_path(tcon, full_path, &cfile);
535 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
536 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
537 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
538 if (rc == -EOPNOTSUPP) {
540 create_options |= OPEN_REPARSE_POINT;
542 /* Failed on a symbolic link - query a reparse point info */
543 cifs_get_readable_path(tcon, full_path, &cfile);
544 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
545 FILE_READ_ATTRIBUTES, FILE_OPEN,
546 create_options, ACL_NO_MODE,
547 smb2_data, SMB2_OP_QUERY_INFO, cfile);
552 move_smb2_info_to_cifs(data, smb2_data);
560 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
561 struct cifs_sb_info *cifs_sb, const char *full_path,
562 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
565 __u32 create_options = 0;
566 struct cifsFileInfo *cfile;
567 struct smb311_posix_qinfo *smb2_data;
572 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
573 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
575 if (smb2_data == NULL)
579 * BB TODO: Add support for using the cached root handle.
580 * Create SMB2_query_posix_info worker function to do non-compounded query
581 * when we already have an open file handle for this. For now this is fast enough
582 * (always using the compounded version).
585 cifs_get_readable_path(tcon, full_path, &cfile);
586 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
587 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
588 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
589 if (rc == -EOPNOTSUPP) {
590 /* BB TODO: When support for special files added to Samba re-verify this path */
592 create_options |= OPEN_REPARSE_POINT;
594 /* Failed on a symbolic link - query a reparse point info */
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,
598 create_options, ACL_NO_MODE,
599 smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
604 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
605 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
613 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
614 struct cifs_tcon *tcon, const char *name,
615 struct cifs_sb_info *cifs_sb)
617 return smb2_compound_op(xid, tcon, cifs_sb, name,
618 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
619 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
624 smb2_mkdir_setinfo(struct inode *inode, const char *name,
625 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
626 const unsigned int xid)
628 FILE_BASIC_INFO data;
629 struct cifsInodeInfo *cifs_i;
630 struct cifsFileInfo *cfile;
634 memset(&data, 0, sizeof(data));
635 cifs_i = CIFS_I(inode);
636 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
637 data.Attributes = cpu_to_le32(dosattrs);
638 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
639 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
640 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
641 CREATE_NOT_FILE, ACL_NO_MODE,
642 &data, SMB2_OP_SET_INFO, cfile);
644 cifs_i->cifsAttrs = dosattrs;
648 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
649 struct cifs_sb_info *cifs_sb)
651 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
652 CREATE_NOT_FILE, ACL_NO_MODE,
653 NULL, SMB2_OP_RMDIR, NULL);
657 smb2_unlink(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_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
662 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
666 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
667 const char *from_name, const char *to_name,
668 struct cifs_sb_info *cifs_sb, __u32 access, int command,
669 struct cifsFileInfo *cfile)
671 __le16 *smb2_to_name = NULL;
674 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
675 if (smb2_to_name == NULL) {
677 goto smb2_rename_path;
679 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
680 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
688 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
689 const char *from_name, const char *to_name,
690 struct cifs_sb_info *cifs_sb)
692 struct cifsFileInfo *cfile;
694 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
696 return smb2_set_path_attr(xid, tcon, from_name, to_name,
697 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
701 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
702 const char *from_name, const char *to_name,
703 struct cifs_sb_info *cifs_sb)
705 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
706 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
711 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
712 const char *full_path, __u64 size,
713 struct cifs_sb_info *cifs_sb, bool set_alloc)
715 __le64 eof = cpu_to_le64(size);
716 struct cifsFileInfo *cfile;
718 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
719 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
720 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
721 &eof, SMB2_OP_SET_EOF, cfile);
725 smb2_set_file_info(struct inode *inode, const char *full_path,
726 FILE_BASIC_INFO *buf, const unsigned int xid)
728 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
729 struct tcon_link *tlink;
730 struct cifs_tcon *tcon;
731 struct cifsFileInfo *cfile;
734 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
735 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
736 (buf->Attributes == 0))
737 return 0; /* would be a no op, no sense sending this */
739 tlink = cifs_sb_tlink(cifs_sb);
741 return PTR_ERR(tlink);
742 tcon = tlink_tcon(tlink);
744 cifs_get_writable_path(tcon, full_path, FIND_WR_ANY, &cfile);
745 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
746 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
747 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, cfile);
748 cifs_put_tlink(tlink);