4 * Copyright (C) International Business Machines Corp., 2002, 2011
6 * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7 * Steve French (sfrench@us.ibm.com)
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 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)
58 __le16 *utf16_path = NULL;
59 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
60 struct cifs_open_parms oparms;
62 struct cifs_ses *ses = tcon->ses;
64 struct smb_rqst rqst[3];
66 struct kvec rsp_iov[3];
67 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
68 struct kvec qi_iov[1];
69 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
70 struct kvec close_iov[1];
71 struct smb2_query_info_rsp *qi_rsp = NULL;
73 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
76 struct smb2_file_rename_info rename_info;
77 struct smb2_file_link_info link_info;
80 if (smb3_encryption_required(tcon))
81 flags |= CIFS_TRANSFORM_REQ;
83 memset(rqst, 0, sizeof(rqst));
84 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
85 memset(rsp_iov, 0, sizeof(rsp_iov));
87 /* We already have a handle so we can skip the open */
92 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
99 oparms.desired_access = desired_access;
100 oparms.disposition = create_disposition;
101 oparms.create_options = create_options;
102 if (backup_cred(cifs_sb))
103 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
105 oparms.reconnect = false;
108 memset(&open_iov, 0, sizeof(open_iov));
109 rqst[num_rqst].rq_iov = open_iov;
110 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
111 rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
117 smb2_set_next_command(tcon, &rqst[num_rqst]);
124 case SMB2_OP_QUERY_INFO:
125 memset(&qi_iov, 0, sizeof(qi_iov));
126 rqst[num_rqst].rq_iov = qi_iov;
127 rqst[num_rqst].rq_nvec = 1;
130 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
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, &rqst[num_rqst],
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,
158 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
162 * Directories are created through parameters in the
165 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
168 memset(&si_iov, 0, sizeof(si_iov));
169 rqst[num_rqst].rq_iov = si_iov;
170 rqst[num_rqst].rq_nvec = 1;
172 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
173 data[0] = &delete_pending[0];
175 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
176 COMPOUND_FID, current->tgid,
177 FILE_DISPOSITION_INFORMATION,
178 SMB2_O_INFO_FILE, 0, data, size);
181 smb2_set_next_command(tcon, &rqst[num_rqst]);
182 smb2_set_related(&rqst[num_rqst++]);
183 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
185 case SMB2_OP_SET_EOF:
186 memset(&si_iov, 0, sizeof(si_iov));
187 rqst[num_rqst].rq_iov = si_iov;
188 rqst[num_rqst].rq_nvec = 1;
190 size[0] = 8; /* sizeof __le64 */
193 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
194 COMPOUND_FID, current->tgid,
195 FILE_END_OF_FILE_INFORMATION,
196 SMB2_O_INFO_FILE, 0, data, size);
199 smb2_set_next_command(tcon, &rqst[num_rqst]);
200 smb2_set_related(&rqst[num_rqst++]);
201 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
203 case SMB2_OP_SET_INFO:
204 memset(&si_iov, 0, sizeof(si_iov));
205 rqst[num_rqst].rq_iov = si_iov;
206 rqst[num_rqst].rq_nvec = 1;
209 size[0] = sizeof(FILE_BASIC_INFO);
213 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
214 cfile->fid.persistent_fid,
215 cfile->fid.volatile_fid, current->tgid,
216 FILE_BASIC_INFORMATION,
217 SMB2_O_INFO_FILE, 0, data, size);
219 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
221 COMPOUND_FID, current->tgid,
222 FILE_BASIC_INFORMATION,
223 SMB2_O_INFO_FILE, 0, data, size);
225 smb2_set_next_command(tcon, &rqst[num_rqst]);
226 smb2_set_related(&rqst[num_rqst]);
233 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
237 memset(&si_iov, 0, sizeof(si_iov));
238 rqst[num_rqst].rq_iov = si_iov;
239 rqst[num_rqst].rq_nvec = 2;
241 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
243 rename_info.ReplaceIfExists = 1;
244 rename_info.RootDirectory = 0;
245 rename_info.FileNameLength = cpu_to_le32(len);
247 size[0] = sizeof(struct smb2_file_rename_info);
248 data[0] = &rename_info;
250 size[1] = len + 2 /* null */;
251 data[1] = (__le16 *)ptr;
254 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
255 cfile->fid.persistent_fid,
256 cfile->fid.volatile_fid,
257 current->tgid, FILE_RENAME_INFORMATION,
258 SMB2_O_INFO_FILE, 0, data, size);
260 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
261 COMPOUND_FID, COMPOUND_FID,
262 current->tgid, FILE_RENAME_INFORMATION,
263 SMB2_O_INFO_FILE, 0, data, size);
265 smb2_set_next_command(tcon, &rqst[num_rqst]);
266 smb2_set_related(&rqst[num_rqst]);
272 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
274 case SMB2_OP_HARDLINK:
275 memset(&si_iov, 0, sizeof(si_iov));
276 rqst[num_rqst].rq_iov = si_iov;
277 rqst[num_rqst].rq_nvec = 2;
279 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
281 link_info.ReplaceIfExists = 0;
282 link_info.RootDirectory = 0;
283 link_info.FileNameLength = cpu_to_le32(len);
285 size[0] = sizeof(struct smb2_file_link_info);
286 data[0] = &link_info;
288 size[1] = len + 2 /* null */;
289 data[1] = (__le16 *)ptr;
291 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
292 COMPOUND_FID, current->tgid,
293 FILE_LINK_INFORMATION,
294 SMB2_O_INFO_FILE, 0, data, size);
297 smb2_set_next_command(tcon, &rqst[num_rqst]);
298 smb2_set_related(&rqst[num_rqst++]);
299 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
302 cifs_dbg(VFS, "Invalid command\n");
308 /* We already have a handle so we can skip the close */
312 memset(&close_iov, 0, sizeof(close_iov));
313 rqst[num_rqst].rq_iov = close_iov;
314 rqst[num_rqst].rq_nvec = 1;
315 rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
317 smb2_set_related(&rqst[num_rqst]);
324 cifsFileInfo_put(cfile);
326 rc = compound_send_recv(xid, ses, flags, num_rqst - 2,
327 &rqst[1], &resp_buftype[1],
330 rc = compound_send_recv(xid, ses, flags, num_rqst,
336 cifsFileInfo_put(cfile);
338 SMB2_open_free(&rqst[0]);
339 if (rc == -EREMCHG) {
340 printk_once(KERN_WARNING "server share %s deleted\n",
342 tcon->need_reconnect = true;
346 case SMB2_OP_QUERY_INFO:
348 qi_rsp = (struct smb2_query_info_rsp *)
350 rc = smb2_validate_and_copy_iov(
351 le16_to_cpu(qi_rsp->OutputBufferOffset),
352 le32_to_cpu(qi_rsp->OutputBufferLength),
353 &rsp_iov[1], sizeof(struct smb2_file_all_info),
357 SMB2_query_info_free(&rqst[1]);
359 SMB2_close_free(&rqst[2]);
361 trace_smb3_query_info_compound_err(xid, ses->Suid,
364 trace_smb3_query_info_compound_done(xid, ses->Suid,
369 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
371 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
373 SMB2_close_free(&rqst[1]);
377 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
379 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
381 SMB2_close_free(&rqst[1]);
383 case SMB2_OP_HARDLINK:
385 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
387 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
388 free_set_inf_compound(rqst);
392 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
394 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
395 free_set_inf_compound(rqst);
399 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
401 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
402 free_set_inf_compound(rqst);
404 case SMB2_OP_SET_EOF:
406 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
408 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
409 free_set_inf_compound(rqst);
411 case SMB2_OP_SET_INFO:
413 trace_smb3_set_info_compound_err(xid, ses->Suid,
416 trace_smb3_set_info_compound_done(xid, ses->Suid,
418 free_set_inf_compound(rqst);
421 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
422 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
423 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
428 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
430 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
431 dst->CurrentByteOffset = src->CurrentByteOffset;
432 dst->Mode = src->Mode;
433 dst->AlignmentRequirement = src->AlignmentRequirement;
434 dst->IndexNumber1 = 0; /* we don't use it */
438 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
439 struct cifs_sb_info *cifs_sb, const char *full_path,
440 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
443 struct smb2_file_all_info *smb2_data;
444 __u32 create_options = 0;
446 bool no_cached_open = tcon->nohandlecache;
447 struct cifsFileInfo *cfile;
452 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
454 if (smb2_data == NULL)
457 /* If it is a root and its handle is cached then use it */
458 if (!strlen(full_path) && !no_cached_open) {
459 rc = open_shroot(xid, tcon, &fid);
463 if (tcon->crfid.file_all_info_is_valid) {
464 move_smb2_info_to_cifs(data,
465 &tcon->crfid.file_all_info);
467 rc = SMB2_query_info(xid, tcon, fid.persistent_fid,
468 fid.volatile_fid, smb2_data);
470 move_smb2_info_to_cifs(data, smb2_data);
472 close_shroot(&tcon->crfid);
476 if (backup_cred(cifs_sb))
477 create_options |= CREATE_OPEN_BACKUP_INTENT;
479 cifs_get_readable_path(tcon, full_path, &cfile);
480 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
481 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
482 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
483 if (rc == -EOPNOTSUPP) {
485 create_options |= OPEN_REPARSE_POINT;
487 /* Failed on a symbolic link - query a reparse point info */
488 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
489 FILE_READ_ATTRIBUTES, FILE_OPEN,
490 create_options, ACL_NO_MODE,
491 smb2_data, SMB2_OP_QUERY_INFO, NULL);
496 move_smb2_info_to_cifs(data, smb2_data);
503 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
504 struct cifs_tcon *tcon, const char *name,
505 struct cifs_sb_info *cifs_sb)
507 return smb2_compound_op(xid, tcon, cifs_sb, name,
508 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
509 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
514 smb2_mkdir_setinfo(struct inode *inode, const char *name,
515 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
516 const unsigned int xid)
518 FILE_BASIC_INFO data;
519 struct cifsInodeInfo *cifs_i;
520 struct cifsFileInfo *cfile;
524 memset(&data, 0, sizeof(data));
525 cifs_i = CIFS_I(inode);
526 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
527 data.Attributes = cpu_to_le32(dosattrs);
528 cifs_get_writable_path(tcon, name, &cfile);
529 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
530 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
531 CREATE_NOT_FILE, ACL_NO_MODE,
532 &data, SMB2_OP_SET_INFO, cfile);
534 cifs_i->cifsAttrs = dosattrs;
538 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
539 struct cifs_sb_info *cifs_sb)
541 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
542 CREATE_NOT_FILE, ACL_NO_MODE,
543 NULL, SMB2_OP_RMDIR, NULL);
547 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
548 struct cifs_sb_info *cifs_sb)
550 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
551 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
552 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
556 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
557 const char *from_name, const char *to_name,
558 struct cifs_sb_info *cifs_sb, __u32 access, int command,
559 struct cifsFileInfo *cfile)
561 __le16 *smb2_to_name = NULL;
564 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
565 if (smb2_to_name == NULL) {
567 goto smb2_rename_path;
569 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
570 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
578 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
579 const char *from_name, const char *to_name,
580 struct cifs_sb_info *cifs_sb)
582 struct cifsFileInfo *cfile;
584 cifs_get_writable_path(tcon, from_name, &cfile);
586 return smb2_set_path_attr(xid, tcon, from_name, to_name,
587 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
591 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
592 const char *from_name, const char *to_name,
593 struct cifs_sb_info *cifs_sb)
595 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
596 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
601 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
602 const char *full_path, __u64 size,
603 struct cifs_sb_info *cifs_sb, bool set_alloc)
605 __le64 eof = cpu_to_le64(size);
607 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
608 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
609 &eof, SMB2_OP_SET_EOF, NULL);
613 smb2_set_file_info(struct inode *inode, const char *full_path,
614 FILE_BASIC_INFO *buf, const unsigned int xid)
616 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
617 struct tcon_link *tlink;
620 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
621 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
622 (buf->Attributes == 0))
623 return 0; /* would be a no op, no sense sending this */
625 tlink = cifs_sb_tlink(cifs_sb);
627 return PTR_ERR(tlink);
629 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
630 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
631 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
632 cifs_put_tlink(tlink);