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 smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon,
42 struct cifs_sb_info *cifs_sb, const char *full_path,
43 __u32 desired_access, __u32 create_disposition,
44 __u32 create_options, void *ptr, int command)
47 __le16 *utf16_path = NULL;
48 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
49 struct cifs_open_parms oparms;
51 struct cifs_ses *ses = tcon->ses;
52 struct TCP_Server_Info *server = ses->server;
54 struct smb_rqst rqst[3];
56 struct kvec rsp_iov[3];
57 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
58 struct kvec qi_iov[1];
59 struct kvec si_iov[3]; /* 2 + potential padding. */
60 struct kvec close_iov[1];
61 struct smb2_query_info_rsp *qi_rsp = NULL;
63 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
66 struct smb2_file_rename_info rename_info;
67 struct smb2_file_link_info link_info;
70 if (smb3_encryption_required(tcon))
71 flags |= CIFS_TRANSFORM_REQ;
73 memset(rqst, 0, sizeof(rqst));
74 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
75 memset(rsp_iov, 0, sizeof(rsp_iov));
78 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
83 oparms.desired_access = desired_access;
84 oparms.disposition = create_disposition;
85 oparms.create_options = create_options;
87 oparms.reconnect = false;
89 memset(&open_iov, 0, sizeof(open_iov));
90 rqst[num_rqst].rq_iov = open_iov;
91 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
92 rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
98 smb2_set_next_command(server, &rqst[num_rqst++]);
102 case SMB2_OP_QUERY_INFO:
103 memset(&qi_iov, 0, sizeof(qi_iov));
104 rqst[num_rqst].rq_iov = qi_iov;
105 rqst[num_rqst].rq_nvec = 1;
107 rc = SMB2_query_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
108 COMPOUND_FID, FILE_ALL_INFORMATION,
110 sizeof(struct smb2_file_all_info) +
112 smb2_set_next_command(server, &rqst[num_rqst]);
113 smb2_set_related(&rqst[num_rqst++]);
119 * Directories are created through parameters in the
124 memset(&si_iov, 0, sizeof(si_iov));
125 rqst[num_rqst].rq_iov = si_iov;
126 rqst[num_rqst].rq_nvec = 1;
129 data[0] = &delete_pending[0];
131 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
132 COMPOUND_FID, current->tgid,
133 FILE_DISPOSITION_INFORMATION,
134 SMB2_O_INFO_FILE, 0, data, size);
135 smb2_set_next_command(server, &rqst[num_rqst]);
136 smb2_set_related(&rqst[num_rqst++]);
138 case SMB2_OP_SET_EOF:
139 memset(&si_iov, 0, sizeof(si_iov));
140 rqst[num_rqst].rq_iov = si_iov;
141 rqst[num_rqst].rq_nvec = 1;
143 size[0] = 8; /* sizeof __le64 */
146 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
147 COMPOUND_FID, current->tgid,
148 FILE_END_OF_FILE_INFORMATION,
149 SMB2_O_INFO_FILE, 0, data, size);
150 smb2_set_next_command(server, &rqst[num_rqst]);
151 smb2_set_related(&rqst[num_rqst++]);
153 case SMB2_OP_SET_INFO:
154 memset(&si_iov, 0, sizeof(si_iov));
155 rqst[num_rqst].rq_iov = si_iov;
156 rqst[num_rqst].rq_nvec = 1;
159 size[0] = sizeof(FILE_BASIC_INFO);
162 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
163 COMPOUND_FID, current->tgid,
164 FILE_BASIC_INFORMATION,
165 SMB2_O_INFO_FILE, 0, data, size);
166 smb2_set_next_command(server, &rqst[num_rqst]);
167 smb2_set_related(&rqst[num_rqst++]);
170 memset(&si_iov, 0, sizeof(si_iov));
171 rqst[num_rqst].rq_iov = si_iov;
172 rqst[num_rqst].rq_nvec = 2;
174 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
176 rename_info.ReplaceIfExists = 1;
177 rename_info.RootDirectory = 0;
178 rename_info.FileNameLength = cpu_to_le32(len);
180 size[0] = sizeof(struct smb2_file_rename_info);
181 data[0] = &rename_info;
183 size[1] = len + 2 /* null */;
184 data[1] = (__le16 *)ptr;
186 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
187 COMPOUND_FID, current->tgid,
188 FILE_RENAME_INFORMATION,
189 SMB2_O_INFO_FILE, 0, data, size);
190 smb2_set_next_command(server, &rqst[num_rqst]);
191 smb2_set_related(&rqst[num_rqst++]);
193 case SMB2_OP_HARDLINK:
194 memset(&si_iov, 0, sizeof(si_iov));
195 rqst[num_rqst].rq_iov = si_iov;
196 rqst[num_rqst].rq_nvec = 2;
198 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
200 link_info.ReplaceIfExists = 0;
201 link_info.RootDirectory = 0;
202 link_info.FileNameLength = cpu_to_le32(len);
204 size[0] = sizeof(struct smb2_file_link_info);
205 data[0] = &link_info;
207 size[1] = len + 2 /* null */;
208 data[1] = (__le16 *)ptr;
210 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
211 COMPOUND_FID, current->tgid,
212 FILE_LINK_INFORMATION,
213 SMB2_O_INFO_FILE, 0, data, size);
214 smb2_set_next_command(server, &rqst[num_rqst]);
215 smb2_set_related(&rqst[num_rqst++]);
218 cifs_dbg(VFS, "Invalid command\n");
225 memset(&close_iov, 0, sizeof(close_iov));
226 rqst[num_rqst].rq_iov = close_iov;
227 rqst[num_rqst].rq_nvec = 1;
228 rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
230 smb2_set_related(&rqst[num_rqst++]);
234 rc = compound_send_recv(xid, ses, flags, num_rqst, rqst,
235 resp_buftype, rsp_iov);
238 SMB2_open_free(&rqst[0]);
240 case SMB2_OP_QUERY_INFO:
242 qi_rsp = (struct smb2_query_info_rsp *)
244 rc = smb2_validate_and_copy_iov(
245 le16_to_cpu(qi_rsp->OutputBufferOffset),
246 le32_to_cpu(qi_rsp->OutputBufferLength),
247 &rsp_iov[1], sizeof(struct smb2_file_all_info),
251 SMB2_query_info_free(&rqst[1]);
253 SMB2_close_free(&rqst[2]);
258 SMB2_close_free(&rqst[1]);
260 case SMB2_OP_HARDLINK:
263 case SMB2_OP_SET_EOF:
264 case SMB2_OP_SET_INFO:
266 SMB2_set_info_free(&rqst[1]);
268 SMB2_close_free(&rqst[2]);
271 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
272 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
273 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
278 move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
280 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
281 dst->CurrentByteOffset = src->CurrentByteOffset;
282 dst->Mode = src->Mode;
283 dst->AlignmentRequirement = src->AlignmentRequirement;
284 dst->IndexNumber1 = 0; /* we don't use it */
288 smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
289 struct cifs_sb_info *cifs_sb, const char *full_path,
290 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
293 struct smb2_file_all_info *smb2_data;
298 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
300 if (smb2_data == NULL)
303 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
304 FILE_READ_ATTRIBUTES, FILE_OPEN, 0,
305 smb2_data, SMB2_OP_QUERY_INFO);
306 if (rc == -EOPNOTSUPP) {
308 /* Failed on a symbolic link - query a reparse point info */
309 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
310 FILE_READ_ATTRIBUTES, FILE_OPEN,
311 OPEN_REPARSE_POINT, smb2_data,
317 move_smb2_info_to_cifs(data, smb2_data);
324 smb2_mkdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
325 struct cifs_sb_info *cifs_sb)
327 return smb2_compound_op(xid, tcon, cifs_sb, name,
328 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
329 CREATE_NOT_FILE, NULL, SMB2_OP_MKDIR);
333 smb2_mkdir_setinfo(struct inode *inode, const char *name,
334 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
335 const unsigned int xid)
337 FILE_BASIC_INFO data;
338 struct cifsInodeInfo *cifs_i;
342 memset(&data, 0, sizeof(data));
343 cifs_i = CIFS_I(inode);
344 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
345 data.Attributes = cpu_to_le32(dosattrs);
346 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
347 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
348 CREATE_NOT_FILE, &data, SMB2_OP_SET_INFO);
350 cifs_i->cifsAttrs = dosattrs;
354 smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
355 struct cifs_sb_info *cifs_sb)
357 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
359 NULL, SMB2_OP_RMDIR);
363 smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
364 struct cifs_sb_info *cifs_sb)
366 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
367 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
368 NULL, SMB2_OP_DELETE);
372 smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
373 const char *from_name, const char *to_name,
374 struct cifs_sb_info *cifs_sb, __u32 access, int command)
376 __le16 *smb2_to_name = NULL;
379 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
380 if (smb2_to_name == NULL) {
382 goto smb2_rename_path;
385 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
386 FILE_OPEN, 0, smb2_to_name, command);
393 smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
394 const char *from_name, const char *to_name,
395 struct cifs_sb_info *cifs_sb)
397 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
398 DELETE, SMB2_OP_RENAME);
402 smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
403 const char *from_name, const char *to_name,
404 struct cifs_sb_info *cifs_sb)
406 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
407 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK);
411 smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
412 const char *full_path, __u64 size,
413 struct cifs_sb_info *cifs_sb, bool set_alloc)
415 __le64 eof = cpu_to_le64(size);
417 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
418 FILE_WRITE_DATA, FILE_OPEN, 0, &eof,
423 smb2_set_file_info(struct inode *inode, const char *full_path,
424 FILE_BASIC_INFO *buf, const unsigned int xid)
426 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
427 struct tcon_link *tlink;
430 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
431 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
432 (buf->Attributes == 0))
433 return 0; /* would be a no op, no sense sending this */
435 tlink = cifs_sb_tlink(cifs_sb);
437 return PTR_ERR(tlink);
439 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
440 FILE_WRITE_ATTRIBUTES, FILE_OPEN, 0, buf,
442 cifs_put_tlink(tlink);