4 * Copyright (C) International Business Machines Corp., 2002, 2011
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/pagemap.h>
27 #include <asm/div64.h>
31 #include "cifsproto.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
34 #include "cifs_unicode.h"
38 #include "smb2proto.h"
41 free_set_inf_compound(struct smb_rqst *rqst)
44 SMB2_set_info_free(&rqst[1]);
46 SMB2_close_free(&rqst[2]);
51 struct cifs_open_parms oparms;
52 struct kvec rsp_iov[3];
53 struct smb_rqst rqst[3];
54 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
55 struct kvec qi_iov[1];
56 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
57 struct kvec close_iov[1];
58 struct smb2_file_rename_info rename_info;
59 struct smb2_file_link_info link_info;
63 smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
64 struct cifs_sb_info *cifs_sb, const char *full_path,
65 __u32 desired_access, __u32 create_disposition,
66 __u32 create_options, umode_t mode, void *ptr, int command,
67 struct cifsFileInfo *cfile)
69 struct cop_vars *vars = NULL;
71 struct smb_rqst *rqst;
73 __le16 *utf16_path = NULL;
74 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
76 struct cifs_ses *ses = tcon->ses;
77 struct TCP_Server_Info *server;
80 struct smb2_query_info_rsp *qi_rsp = NULL;
82 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
87 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
90 rqst = &vars->rqst[0];
91 rsp_iov = &vars->rsp_iov[0];
93 server = cifs_pick_channel(ses);
95 if (smb3_encryption_required(tcon))
96 flags |= CIFS_TRANSFORM_REQ;
98 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
100 /* We already have a handle so we can skip the open */
105 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
111 vars->oparms.tcon = tcon;
112 vars->oparms.desired_access = desired_access;
113 vars->oparms.disposition = create_disposition;
114 vars->oparms.create_options = cifs_create_options(cifs_sb, create_options);
115 vars->oparms.fid = &fid;
116 vars->oparms.reconnect = false;
117 vars->oparms.mode = mode;
118 vars->oparms.cifs_sb = cifs_sb;
120 rqst[num_rqst].rq_iov = &vars->open_iov[0];
121 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
122 rc = SMB2_open_init(tcon, server,
123 &rqst[num_rqst], &oplock, &vars->oparms,
129 smb2_set_next_command(tcon, &rqst[num_rqst]);
136 case SMB2_OP_QUERY_INFO:
137 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
138 rqst[num_rqst].rq_nvec = 1;
141 rc = SMB2_query_info_init(tcon, server,
143 cfile->fid.persistent_fid,
144 cfile->fid.volatile_fid,
145 FILE_ALL_INFORMATION,
147 sizeof(struct smb2_file_all_info) +
148 PATH_MAX * 2, 0, NULL);
150 rc = SMB2_query_info_init(tcon, server,
154 FILE_ALL_INFORMATION,
156 sizeof(struct smb2_file_all_info) +
157 PATH_MAX * 2, 0, NULL);
159 smb2_set_next_command(tcon, &rqst[num_rqst]);
160 smb2_set_related(&rqst[num_rqst]);
167 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
170 case SMB2_OP_POSIX_QUERY_INFO:
171 rqst[num_rqst].rq_iov = &vars->qi_iov[0];
172 rqst[num_rqst].rq_nvec = 1;
175 rc = SMB2_query_info_init(tcon, server,
177 cfile->fid.persistent_fid,
178 cfile->fid.volatile_fid,
179 SMB_FIND_FILE_POSIX_INFO,
181 /* TBD: fix following to allow for longer SIDs */
182 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
183 (sizeof(struct cifs_sid) * 2), 0, NULL);
185 rc = SMB2_query_info_init(tcon, server,
189 SMB_FIND_FILE_POSIX_INFO,
191 sizeof(struct smb311_posix_qinfo *) + (PATH_MAX * 2) +
192 (sizeof(struct cifs_sid) * 2), 0, NULL);
194 smb2_set_next_command(tcon, &rqst[num_rqst]);
195 smb2_set_related(&rqst[num_rqst]);
202 trace_smb3_posix_query_info_compound_enter(xid, ses->Suid, tcon->tid, full_path);
205 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
209 * Directories are created through parameters in the
212 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
215 rqst[num_rqst].rq_iov = &vars->si_iov[0];
216 rqst[num_rqst].rq_nvec = 1;
218 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
219 data[0] = &delete_pending[0];
221 rc = SMB2_set_info_init(tcon, server,
222 &rqst[num_rqst], COMPOUND_FID,
223 COMPOUND_FID, current->tgid,
224 FILE_DISPOSITION_INFORMATION,
225 SMB2_O_INFO_FILE, 0, data, size);
228 smb2_set_next_command(tcon, &rqst[num_rqst]);
229 smb2_set_related(&rqst[num_rqst++]);
230 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
232 case SMB2_OP_SET_EOF:
233 rqst[num_rqst].rq_iov = &vars->si_iov[0];
234 rqst[num_rqst].rq_nvec = 1;
236 size[0] = 8; /* sizeof __le64 */
239 rc = SMB2_set_info_init(tcon, server,
240 &rqst[num_rqst], COMPOUND_FID,
241 COMPOUND_FID, current->tgid,
242 FILE_END_OF_FILE_INFORMATION,
243 SMB2_O_INFO_FILE, 0, data, size);
246 smb2_set_next_command(tcon, &rqst[num_rqst]);
247 smb2_set_related(&rqst[num_rqst++]);
248 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
250 case SMB2_OP_SET_INFO:
251 rqst[num_rqst].rq_iov = &vars->si_iov[0];
252 rqst[num_rqst].rq_nvec = 1;
255 size[0] = sizeof(FILE_BASIC_INFO);
259 rc = SMB2_set_info_init(tcon, server,
261 cfile->fid.persistent_fid,
262 cfile->fid.volatile_fid, current->tgid,
263 FILE_BASIC_INFORMATION,
264 SMB2_O_INFO_FILE, 0, data, size);
266 rc = SMB2_set_info_init(tcon, server,
269 COMPOUND_FID, current->tgid,
270 FILE_BASIC_INFORMATION,
271 SMB2_O_INFO_FILE, 0, data, size);
273 smb2_set_next_command(tcon, &rqst[num_rqst]);
274 smb2_set_related(&rqst[num_rqst]);
281 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
285 rqst[num_rqst].rq_iov = &vars->si_iov[0];
286 rqst[num_rqst].rq_nvec = 2;
288 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
290 vars->rename_info.ReplaceIfExists = 1;
291 vars->rename_info.RootDirectory = 0;
292 vars->rename_info.FileNameLength = cpu_to_le32(len);
294 size[0] = sizeof(struct smb2_file_rename_info);
295 data[0] = &vars->rename_info;
297 size[1] = len + 2 /* null */;
298 data[1] = (__le16 *)ptr;
301 rc = SMB2_set_info_init(tcon, server,
303 cfile->fid.persistent_fid,
304 cfile->fid.volatile_fid,
305 current->tgid, FILE_RENAME_INFORMATION,
306 SMB2_O_INFO_FILE, 0, data, size);
308 rc = SMB2_set_info_init(tcon, server,
310 COMPOUND_FID, COMPOUND_FID,
311 current->tgid, FILE_RENAME_INFORMATION,
312 SMB2_O_INFO_FILE, 0, data, size);
314 smb2_set_next_command(tcon, &rqst[num_rqst]);
315 smb2_set_related(&rqst[num_rqst]);
321 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
323 case SMB2_OP_HARDLINK:
324 rqst[num_rqst].rq_iov = &vars->si_iov[0];
325 rqst[num_rqst].rq_nvec = 2;
327 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
329 vars->link_info.ReplaceIfExists = 0;
330 vars->link_info.RootDirectory = 0;
331 vars->link_info.FileNameLength = cpu_to_le32(len);
333 size[0] = sizeof(struct smb2_file_link_info);
334 data[0] = &vars->link_info;
336 size[1] = len + 2 /* null */;
337 data[1] = (__le16 *)ptr;
339 rc = SMB2_set_info_init(tcon, server,
340 &rqst[num_rqst], COMPOUND_FID,
341 COMPOUND_FID, current->tgid,
342 FILE_LINK_INFORMATION,
343 SMB2_O_INFO_FILE, 0, data, size);
346 smb2_set_next_command(tcon, &rqst[num_rqst]);
347 smb2_set_related(&rqst[num_rqst++]);
348 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
351 cifs_dbg(VFS, "Invalid command\n");
357 /* We already have a handle so we can skip the close */
361 flags |= CIFS_CP_CREATE_CLOSE_OP;
362 rqst[num_rqst].rq_iov = &vars->close_iov[0];
363 rqst[num_rqst].rq_nvec = 1;
364 rc = SMB2_close_init(tcon, server,
365 &rqst[num_rqst], COMPOUND_FID,
366 COMPOUND_FID, false);
367 smb2_set_related(&rqst[num_rqst]);
374 cifsFileInfo_put(cfile);
376 rc = compound_send_recv(xid, ses, server,
378 &rqst[1], &resp_buftype[1],
381 rc = compound_send_recv(xid, ses, server,
388 cifsFileInfo_put(cfile);
390 SMB2_open_free(&rqst[0]);
391 if (rc == -EREMCHG) {
392 pr_warn_once("server share %s deleted\n", tcon->treeName);
393 tcon->need_reconnect = true;
397 case SMB2_OP_QUERY_INFO:
399 qi_rsp = (struct smb2_query_info_rsp *)
401 rc = smb2_validate_and_copy_iov(
402 le16_to_cpu(qi_rsp->OutputBufferOffset),
403 le32_to_cpu(qi_rsp->OutputBufferLength),
404 &rsp_iov[1], sizeof(struct smb2_file_all_info),
408 SMB2_query_info_free(&rqst[1]);
410 SMB2_close_free(&rqst[2]);
412 trace_smb3_query_info_compound_err(xid, ses->Suid,
415 trace_smb3_query_info_compound_done(xid, ses->Suid,
418 case SMB2_OP_POSIX_QUERY_INFO:
420 qi_rsp = (struct smb2_query_info_rsp *)
422 rc = smb2_validate_and_copy_iov(
423 le16_to_cpu(qi_rsp->OutputBufferOffset),
424 le32_to_cpu(qi_rsp->OutputBufferLength),
425 &rsp_iov[1], sizeof(struct smb311_posix_qinfo) /* add SIDs */, ptr);
428 SMB2_query_info_free(&rqst[1]);
430 SMB2_close_free(&rqst[2]);
432 trace_smb3_posix_query_info_compound_err(xid, ses->Suid, tcon->tid, rc);
434 trace_smb3_posix_query_info_compound_done(xid, ses->Suid, tcon->tid);
438 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
440 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
442 SMB2_close_free(&rqst[1]);
446 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
448 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
450 SMB2_close_free(&rqst[1]);
452 case SMB2_OP_HARDLINK:
454 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
456 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
457 free_set_inf_compound(rqst);
461 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
463 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
464 free_set_inf_compound(rqst);
468 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
470 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
471 free_set_inf_compound(rqst);
473 case SMB2_OP_SET_EOF:
475 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
477 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
478 free_set_inf_compound(rqst);
480 case SMB2_OP_SET_INFO:
482 trace_smb3_set_info_compound_err(xid, ses->Suid,
485 trace_smb3_set_info_compound_done(xid, ses->Suid,
487 free_set_inf_compound(rqst);
490 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
491 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
492 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
498 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
500 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
501 dst->CurrentByteOffset = src->CurrentByteOffset;
502 dst->Mode = src->Mode;
503 dst->AlignmentRequirement = src->AlignmentRequirement;
504 dst->IndexNumber1 = 0; /* we don't use it */
508 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
509 struct cifs_sb_info *cifs_sb, const char *full_path,
510 FILE_ALL_INFO *data, bool *adjust_tz, bool *reparse)
513 struct smb2_file_all_info *smb2_data;
514 __u32 create_options = 0;
515 bool no_cached_open = tcon->nohandlecache;
516 struct cifsFileInfo *cfile;
517 struct cached_fid *cfid = NULL;
522 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
524 if (smb2_data == NULL)
527 /* If it is a root and its handle is cached then use it */
528 if (!strlen(full_path) && !no_cached_open) {
529 rc = open_shroot(xid, tcon, cifs_sb, &cfid);
533 if (tcon->crfid.file_all_info_is_valid) {
534 move_smb2_info_to_cifs(data,
535 &tcon->crfid.file_all_info);
537 rc = SMB2_query_info(xid, tcon,
538 cfid->fid->persistent_fid,
539 cfid->fid->volatile_fid, smb2_data);
541 move_smb2_info_to_cifs(data, smb2_data);
547 cifs_get_readable_path(tcon, full_path, &cfile);
548 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
549 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
550 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
551 if (rc == -EOPNOTSUPP) {
553 create_options |= OPEN_REPARSE_POINT;
555 /* Failed on a symbolic link - query a reparse point info */
556 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
557 FILE_READ_ATTRIBUTES, FILE_OPEN,
558 create_options, ACL_NO_MODE,
559 smb2_data, SMB2_OP_QUERY_INFO, NULL);
564 move_smb2_info_to_cifs(data, smb2_data);
572 smb311_posix_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
573 struct cifs_sb_info *cifs_sb, const char *full_path,
574 struct smb311_posix_qinfo *data, bool *adjust_tz, bool *reparse)
577 __u32 create_options = 0;
578 struct cifsFileInfo *cfile;
579 struct smb311_posix_qinfo *smb2_data;
584 /* BB TODO: Make struct larger when add support for parsing owner SIDs */
585 smb2_data = kzalloc(sizeof(struct smb311_posix_qinfo),
587 if (smb2_data == NULL)
591 * BB TODO: Add support for using the cached root handle.
592 * Create SMB2_query_posix_info worker function to do non-compounded query
593 * when we already have an open file handle for this. For now this is fast enough
594 * (always using the compounded version).
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, create_options,
600 ACL_NO_MODE, smb2_data, SMB2_OP_POSIX_QUERY_INFO, cfile);
601 if (rc == -EOPNOTSUPP) {
602 /* BB TODO: When support for special files added to Samba re-verify this path */
604 create_options |= OPEN_REPARSE_POINT;
606 /* Failed on a symbolic link - query a reparse point info */
607 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
608 FILE_READ_ATTRIBUTES, FILE_OPEN,
609 create_options, ACL_NO_MODE,
610 smb2_data, SMB2_OP_POSIX_QUERY_INFO, NULL);
615 /* TODO: will need to allow for the 2 SIDs when add support for getting owner UID/GID */
616 memcpy(data, smb2_data, sizeof(struct smb311_posix_qinfo));
624 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
625 struct cifs_tcon *tcon, const char *name,
626 struct cifs_sb_info *cifs_sb)
628 return smb2_compound_op(xid, tcon, cifs_sb, name,
629 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
630 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
635 smb2_mkdir_setinfo(struct inode *inode, const char *name,
636 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
637 const unsigned int xid)
639 FILE_BASIC_INFO data;
640 struct cifsInodeInfo *cifs_i;
641 struct cifsFileInfo *cfile;
645 memset(&data, 0, sizeof(data));
646 cifs_i = CIFS_I(inode);
647 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
648 data.Attributes = cpu_to_le32(dosattrs);
649 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
650 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
651 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
652 CREATE_NOT_FILE, ACL_NO_MODE,
653 &data, SMB2_OP_SET_INFO, cfile);
655 cifs_i->cifsAttrs = dosattrs;
659 smb2_rmdir(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_NOT_FILE, ACL_NO_MODE,
664 NULL, SMB2_OP_RMDIR, NULL);
668 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
669 struct cifs_sb_info *cifs_sb)
671 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
672 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
673 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
677 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
678 const char *from_name, const char *to_name,
679 struct cifs_sb_info *cifs_sb, __u32 access, int command,
680 struct cifsFileInfo *cfile)
682 __le16 *smb2_to_name = NULL;
685 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
686 if (smb2_to_name == NULL) {
688 goto smb2_rename_path;
690 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
691 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
699 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
700 const char *from_name, const char *to_name,
701 struct cifs_sb_info *cifs_sb)
703 struct cifsFileInfo *cfile;
705 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
707 return smb2_set_path_attr(xid, tcon, from_name, to_name,
708 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
712 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
713 const char *from_name, const char *to_name,
714 struct cifs_sb_info *cifs_sb)
716 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
717 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
722 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
723 const char *full_path, __u64 size,
724 struct cifs_sb_info *cifs_sb, bool set_alloc)
726 __le64 eof = cpu_to_le64(size);
728 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
729 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
730 &eof, SMB2_OP_SET_EOF, NULL);
734 smb2_set_file_info(struct inode *inode, const char *full_path,
735 FILE_BASIC_INFO *buf, const unsigned int xid)
737 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
738 struct tcon_link *tlink;
741 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
742 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
743 (buf->Attributes == 0))
744 return 0; /* would be a no op, no sense sending this */
746 tlink = cifs_sb_tlink(cifs_sb);
748 return PTR_ERR(tlink);
750 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
751 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
752 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
753 cifs_put_tlink(tlink);