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 = cifs_create_options(cifs_sb, create_options);
103 oparms.reconnect = false;
106 memset(&open_iov, 0, sizeof(open_iov));
107 rqst[num_rqst].rq_iov = open_iov;
108 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
109 rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
115 smb2_set_next_command(tcon, &rqst[num_rqst]);
122 case SMB2_OP_QUERY_INFO:
123 memset(&qi_iov, 0, sizeof(qi_iov));
124 rqst[num_rqst].rq_iov = qi_iov;
125 rqst[num_rqst].rq_nvec = 1;
128 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
129 cfile->fid.persistent_fid,
130 cfile->fid.volatile_fid,
131 FILE_ALL_INFORMATION,
133 sizeof(struct smb2_file_all_info) +
134 PATH_MAX * 2, 0, NULL);
136 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
139 FILE_ALL_INFORMATION,
141 sizeof(struct smb2_file_all_info) +
142 PATH_MAX * 2, 0, NULL);
144 smb2_set_next_command(tcon, &rqst[num_rqst]);
145 smb2_set_related(&rqst[num_rqst]);
152 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
156 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
160 * Directories are created through parameters in the
163 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
166 memset(&si_iov, 0, sizeof(si_iov));
167 rqst[num_rqst].rq_iov = si_iov;
168 rqst[num_rqst].rq_nvec = 1;
170 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
171 data[0] = &delete_pending[0];
173 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
174 COMPOUND_FID, current->tgid,
175 FILE_DISPOSITION_INFORMATION,
176 SMB2_O_INFO_FILE, 0, data, size);
179 smb2_set_next_command(tcon, &rqst[num_rqst]);
180 smb2_set_related(&rqst[num_rqst++]);
181 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
183 case SMB2_OP_SET_EOF:
184 memset(&si_iov, 0, sizeof(si_iov));
185 rqst[num_rqst].rq_iov = si_iov;
186 rqst[num_rqst].rq_nvec = 1;
188 size[0] = 8; /* sizeof __le64 */
191 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
192 COMPOUND_FID, current->tgid,
193 FILE_END_OF_FILE_INFORMATION,
194 SMB2_O_INFO_FILE, 0, data, size);
197 smb2_set_next_command(tcon, &rqst[num_rqst]);
198 smb2_set_related(&rqst[num_rqst++]);
199 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
201 case SMB2_OP_SET_INFO:
202 memset(&si_iov, 0, sizeof(si_iov));
203 rqst[num_rqst].rq_iov = si_iov;
204 rqst[num_rqst].rq_nvec = 1;
207 size[0] = sizeof(FILE_BASIC_INFO);
211 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
212 cfile->fid.persistent_fid,
213 cfile->fid.volatile_fid, current->tgid,
214 FILE_BASIC_INFORMATION,
215 SMB2_O_INFO_FILE, 0, data, size);
217 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
219 COMPOUND_FID, current->tgid,
220 FILE_BASIC_INFORMATION,
221 SMB2_O_INFO_FILE, 0, data, size);
223 smb2_set_next_command(tcon, &rqst[num_rqst]);
224 smb2_set_related(&rqst[num_rqst]);
231 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
235 memset(&si_iov, 0, sizeof(si_iov));
236 rqst[num_rqst].rq_iov = si_iov;
237 rqst[num_rqst].rq_nvec = 2;
239 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
241 rename_info.ReplaceIfExists = 1;
242 rename_info.RootDirectory = 0;
243 rename_info.FileNameLength = cpu_to_le32(len);
245 size[0] = sizeof(struct smb2_file_rename_info);
246 data[0] = &rename_info;
248 size[1] = len + 2 /* null */;
249 data[1] = (__le16 *)ptr;
252 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
253 cfile->fid.persistent_fid,
254 cfile->fid.volatile_fid,
255 current->tgid, FILE_RENAME_INFORMATION,
256 SMB2_O_INFO_FILE, 0, data, size);
258 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
259 COMPOUND_FID, COMPOUND_FID,
260 current->tgid, FILE_RENAME_INFORMATION,
261 SMB2_O_INFO_FILE, 0, data, size);
263 smb2_set_next_command(tcon, &rqst[num_rqst]);
264 smb2_set_related(&rqst[num_rqst]);
270 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
272 case SMB2_OP_HARDLINK:
273 memset(&si_iov, 0, sizeof(si_iov));
274 rqst[num_rqst].rq_iov = si_iov;
275 rqst[num_rqst].rq_nvec = 2;
277 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
279 link_info.ReplaceIfExists = 0;
280 link_info.RootDirectory = 0;
281 link_info.FileNameLength = cpu_to_le32(len);
283 size[0] = sizeof(struct smb2_file_link_info);
284 data[0] = &link_info;
286 size[1] = len + 2 /* null */;
287 data[1] = (__le16 *)ptr;
289 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
290 COMPOUND_FID, current->tgid,
291 FILE_LINK_INFORMATION,
292 SMB2_O_INFO_FILE, 0, data, size);
295 smb2_set_next_command(tcon, &rqst[num_rqst]);
296 smb2_set_related(&rqst[num_rqst++]);
297 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
300 cifs_dbg(VFS, "Invalid command\n");
306 /* We already have a handle so we can skip the close */
310 memset(&close_iov, 0, sizeof(close_iov));
311 rqst[num_rqst].rq_iov = close_iov;
312 rqst[num_rqst].rq_nvec = 1;
313 rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
315 smb2_set_related(&rqst[num_rqst]);
322 cifsFileInfo_put(cfile);
324 rc = compound_send_recv(xid, ses, flags, num_rqst - 2,
325 &rqst[1], &resp_buftype[1],
328 rc = compound_send_recv(xid, ses, flags, num_rqst,
334 cifsFileInfo_put(cfile);
336 SMB2_open_free(&rqst[0]);
337 if (rc == -EREMCHG) {
338 printk_once(KERN_WARNING "server share %s deleted\n",
340 tcon->need_reconnect = true;
344 case SMB2_OP_QUERY_INFO:
346 qi_rsp = (struct smb2_query_info_rsp *)
348 rc = smb2_validate_and_copy_iov(
349 le16_to_cpu(qi_rsp->OutputBufferOffset),
350 le32_to_cpu(qi_rsp->OutputBufferLength),
351 &rsp_iov[1], sizeof(struct smb2_file_all_info),
355 SMB2_query_info_free(&rqst[1]);
357 SMB2_close_free(&rqst[2]);
359 trace_smb3_query_info_compound_err(xid, ses->Suid,
362 trace_smb3_query_info_compound_done(xid, ses->Suid,
367 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
369 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
371 SMB2_close_free(&rqst[1]);
375 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
377 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
379 SMB2_close_free(&rqst[1]);
381 case SMB2_OP_HARDLINK:
383 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
385 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
386 free_set_inf_compound(rqst);
390 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
392 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
393 free_set_inf_compound(rqst);
397 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
399 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
400 free_set_inf_compound(rqst);
402 case SMB2_OP_SET_EOF:
404 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
406 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
407 free_set_inf_compound(rqst);
409 case SMB2_OP_SET_INFO:
411 trace_smb3_set_info_compound_err(xid, ses->Suid,
414 trace_smb3_set_info_compound_done(xid, ses->Suid,
416 free_set_inf_compound(rqst);
419 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
420 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
421 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
426 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
428 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
429 dst->CurrentByteOffset = src->CurrentByteOffset;
430 dst->Mode = src->Mode;
431 dst->AlignmentRequirement = src->AlignmentRequirement;
432 dst->IndexNumber1 = 0; /* we don't use it */
436 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
437 struct cifs_sb_info *cifs_sb, const char *full_path,
438 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
441 struct smb2_file_all_info *smb2_data;
442 __u32 create_options = 0;
444 bool no_cached_open = tcon->nohandlecache;
445 struct cifsFileInfo *cfile;
450 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
452 if (smb2_data == NULL)
455 /* If it is a root and its handle is cached then use it */
456 if (!strlen(full_path) && !no_cached_open) {
457 rc = open_shroot(xid, tcon, cifs_sb, &fid);
461 if (tcon->crfid.file_all_info_is_valid) {
462 move_smb2_info_to_cifs(data,
463 &tcon->crfid.file_all_info);
465 rc = SMB2_query_info(xid, tcon, fid.persistent_fid,
466 fid.volatile_fid, smb2_data);
468 move_smb2_info_to_cifs(data, smb2_data);
470 close_shroot(&tcon->crfid);
474 cifs_get_readable_path(tcon, full_path, &cfile);
475 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
476 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
477 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
478 if (rc == -EOPNOTSUPP) {
480 create_options |= OPEN_REPARSE_POINT;
482 /* Failed on a symbolic link - query a reparse point info */
483 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
484 FILE_READ_ATTRIBUTES, FILE_OPEN,
485 create_options, ACL_NO_MODE,
486 smb2_data, SMB2_OP_QUERY_INFO, NULL);
491 move_smb2_info_to_cifs(data, smb2_data);
498 smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
499 struct cifs_tcon *tcon, const char *name,
500 struct cifs_sb_info *cifs_sb)
502 return smb2_compound_op(xid, tcon, cifs_sb, name,
503 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
504 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
509 smb2_mkdir_setinfo(struct inode *inode, const char *name,
510 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
511 const unsigned int xid)
513 FILE_BASIC_INFO data;
514 struct cifsInodeInfo *cifs_i;
515 struct cifsFileInfo *cfile;
519 memset(&data, 0, sizeof(data));
520 cifs_i = CIFS_I(inode);
521 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
522 data.Attributes = cpu_to_le32(dosattrs);
523 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
524 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
525 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
526 CREATE_NOT_FILE, ACL_NO_MODE,
527 &data, SMB2_OP_SET_INFO, cfile);
529 cifs_i->cifsAttrs = dosattrs;
533 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
534 struct cifs_sb_info *cifs_sb)
536 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
537 CREATE_NOT_FILE, ACL_NO_MODE,
538 NULL, SMB2_OP_RMDIR, NULL);
542 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
543 struct cifs_sb_info *cifs_sb)
545 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
546 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
547 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
551 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
552 const char *from_name, const char *to_name,
553 struct cifs_sb_info *cifs_sb, __u32 access, int command,
554 struct cifsFileInfo *cfile)
556 __le16 *smb2_to_name = NULL;
559 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
560 if (smb2_to_name == NULL) {
562 goto smb2_rename_path;
564 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
565 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
573 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
574 const char *from_name, const char *to_name,
575 struct cifs_sb_info *cifs_sb)
577 struct cifsFileInfo *cfile;
579 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
581 return smb2_set_path_attr(xid, tcon, from_name, to_name,
582 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
586 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
587 const char *from_name, const char *to_name,
588 struct cifs_sb_info *cifs_sb)
590 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
591 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
596 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
597 const char *full_path, __u64 size,
598 struct cifs_sb_info *cifs_sb, bool set_alloc)
600 __le64 eof = cpu_to_le64(size);
602 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
603 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
604 &eof, SMB2_OP_SET_EOF, NULL);
608 smb2_set_file_info(struct inode *inode, const char *full_path,
609 FILE_BASIC_INFO *buf, const unsigned int xid)
611 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
612 struct tcon_link *tlink;
615 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
616 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
617 (buf->Attributes == 0))
618 return 0; /* would be a no op, no sense sending this */
620 tlink = cifs_sb_tlink(cifs_sb);
622 return PTR_ERR(tlink);
624 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
625 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
626 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
627 cifs_put_tlink(tlink);