blob: a8c301ae00ed72f2bbcbbf0f3bf0bf6dfce0e29b [file] [log] [blame]
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001/*
2 * fs/cifs/smb2inode.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002, 2011
5 * Etersoft, 2012
6 * Author(s): Pavel Shilovsky (pshilovsky@samba.org),
7 * Steve French (sfrench@us.ibm.com)
8 *
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.
13 *
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.
18 *
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
22 */
23#include <linux/fs.h>
24#include <linux/stat.h>
25#include <linux/slab.h>
26#include <linux/pagemap.h>
27#include <asm/div64.h>
28#include "cifsfs.h"
29#include "cifspdu.h"
30#include "cifsglob.h"
31#include "cifsproto.h"
32#include "cifs_debug.h"
33#include "cifs_fs_sb.h"
34#include "cifs_unicode.h"
35#include "fscache.h"
36#include "smb2glob.h"
37#include "smb2pdu.h"
38#include "smb2proto.h"
39
Steve French81915762019-03-13 00:02:47 -050040static void
41free_set_inf_compound(struct smb_rqst *rqst)
42{
43 if (rqst[1].rq_iov)
44 SMB2_set_info_free(&rqst[1]);
45 if (rqst[2].rq_iov)
46 SMB2_close_free(&rqst[2]);
47}
48
49
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +040050static int
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100051smb2_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,
Steve Frenchc3ca78e2019-09-25 00:32:13 -050054 __u32 create_options, umode_t mode, void *ptr, int command,
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +100055 struct cifsFileInfo *cfile)
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100056{
57 int rc;
58 __le16 *utf16_path = NULL;
59 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
60 struct cifs_open_parms oparms;
61 struct cifs_fid fid;
62 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100063 int num_rqst = 0;
64 struct smb_rqst rqst[3];
65 int resp_buftype[3];
66 struct kvec rsp_iov[3];
67 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
68 struct kvec qi_iov[1];
Ronnie Sahlberg14e562a2018-09-03 13:33:50 +100069 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100070 struct kvec close_iov[1];
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +100071 struct smb2_query_info_rsp *qi_rsp = NULL;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100072 int flags = 0;
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +100073 __u8 delete_pending[8] = {1, 0, 0, 0, 0, 0, 0, 0};
Ronnie Sahlbergbb435512018-09-03 13:33:49 +100074 unsigned int size[2];
75 void *data[2];
76 struct smb2_file_rename_info rename_info;
77 struct smb2_file_link_info link_info;
78 int len;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100079
80 if (smb3_encryption_required(tcon))
81 flags |= CIFS_TRANSFORM_REQ;
82
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));
86
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +100087 /* We already have a handle so we can skip the open */
88 if (cfile)
89 goto after_open;
90
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100091 /* Open */
92 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +100093 if (!utf16_path) {
94 rc = -ENOMEM;
95 goto finished;
96 }
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100097
Steve Frenche0fc5b12019-12-09 22:34:10 -060098 memset(&oparms, 0, sizeof(struct cifs_open_parms));
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +100099 oparms.tcon = tcon;
100 oparms.desired_access = desired_access;
101 oparms.disposition = create_disposition;
Amir Goldstein0f060932020-02-03 21:46:43 +0200102 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000103 oparms.fid = &fid;
104 oparms.reconnect = false;
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500105 oparms.mode = mode;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000106
107 memset(&open_iov, 0, sizeof(open_iov));
108 rqst[num_rqst].rq_iov = open_iov;
109 rqst[num_rqst].rq_nvec = SMB2_CREATE_IOV_SIZE;
110 rc = SMB2_open_init(tcon, &rqst[num_rqst], &oplock, &oparms,
111 utf16_path);
112 kfree(utf16_path);
113 if (rc)
114 goto finished;
115
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000116 smb2_set_next_command(tcon, &rqst[num_rqst]);
117 after_open:
118 num_rqst++;
119 rc = 0;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000120
121 /* Operation */
122 switch (command) {
123 case SMB2_OP_QUERY_INFO:
124 memset(&qi_iov, 0, sizeof(qi_iov));
125 rqst[num_rqst].rq_iov = qi_iov;
126 rqst[num_rqst].rq_nvec = 1;
127
Ronnie Sahlberg496902d2019-09-09 15:30:00 +1000128 if (cfile)
129 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
130 cfile->fid.persistent_fid,
131 cfile->fid.volatile_fid,
132 FILE_ALL_INFORMATION,
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000133 SMB2_O_INFO_FILE, 0,
134 sizeof(struct smb2_file_all_info) +
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -0500135 PATH_MAX * 2, 0, NULL);
Ronnie Sahlberg496902d2019-09-09 15:30:00 +1000136 else {
137 rc = SMB2_query_info_init(tcon, &rqst[num_rqst],
138 COMPOUND_FID,
139 COMPOUND_FID,
140 FILE_ALL_INFORMATION,
141 SMB2_O_INFO_FILE, 0,
142 sizeof(struct smb2_file_all_info) +
143 PATH_MAX * 2, 0, NULL);
144 if (!rc) {
145 smb2_set_next_command(tcon, &rqst[num_rqst]);
146 smb2_set_related(&rqst[num_rqst]);
147 }
148 }
149
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +1000150 if (rc)
151 goto finished;
Ronnie Sahlberg496902d2019-09-09 15:30:00 +1000152 num_rqst++;
Steve French81915762019-03-13 00:02:47 -0500153 trace_smb3_query_info_compound_enter(xid, ses->Suid, tcon->tid,
154 full_path);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000155 break;
Ronnie Sahlberg47dd9592018-09-03 13:33:43 +1000156 case SMB2_OP_DELETE:
Steve French81915762019-03-13 00:02:47 -0500157 trace_smb3_delete_enter(xid, ses->Suid, tcon->tid, full_path);
Ronnie Sahlberg47dd9592018-09-03 13:33:43 +1000158 break;
Ronnie Sahlbergf733e392018-09-03 13:33:42 +1000159 case SMB2_OP_MKDIR:
160 /*
161 * Directories are created through parameters in the
162 * SMB2_open() call.
163 */
Steve French81915762019-03-13 00:02:47 -0500164 trace_smb3_mkdir_enter(xid, ses->Suid, tcon->tid, full_path);
Ronnie Sahlbergf733e392018-09-03 13:33:42 +1000165 break;
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000166 case SMB2_OP_RMDIR:
167 memset(&si_iov, 0, sizeof(si_iov));
168 rqst[num_rqst].rq_iov = si_iov;
169 rqst[num_rqst].rq_nvec = 1;
170
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -0600171 size[0] = 1; /* sizeof __u8 See MS-FSCC section 2.4.11 */
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000172 data[0] = &delete_pending[0];
173
174 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
175 COMPOUND_FID, current->tgid,
176 FILE_DISPOSITION_INFORMATION,
177 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +1000178 if (rc)
179 goto finished;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +1000180 smb2_set_next_command(tcon, &rqst[num_rqst]);
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000181 smb2_set_related(&rqst[num_rqst++]);
Steve French81915762019-03-13 00:02:47 -0500182 trace_smb3_rmdir_enter(xid, ses->Suid, tcon->tid, full_path);
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000183 break;
Ronnie Sahlbergf7bfe042018-09-03 13:33:46 +1000184 case SMB2_OP_SET_EOF:
185 memset(&si_iov, 0, sizeof(si_iov));
186 rqst[num_rqst].rq_iov = si_iov;
187 rqst[num_rqst].rq_nvec = 1;
188
189 size[0] = 8; /* sizeof __le64 */
190 data[0] = ptr;
191
192 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
193 COMPOUND_FID, current->tgid,
194 FILE_END_OF_FILE_INFORMATION,
195 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +1000196 if (rc)
197 goto finished;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +1000198 smb2_set_next_command(tcon, &rqst[num_rqst]);
Ronnie Sahlbergf7bfe042018-09-03 13:33:46 +1000199 smb2_set_related(&rqst[num_rqst++]);
Steve French81915762019-03-13 00:02:47 -0500200 trace_smb3_set_eof_enter(xid, ses->Suid, tcon->tid, full_path);
Ronnie Sahlbergf7bfe042018-09-03 13:33:46 +1000201 break;
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000202 case SMB2_OP_SET_INFO:
203 memset(&si_iov, 0, sizeof(si_iov));
204 rqst[num_rqst].rq_iov = si_iov;
205 rqst[num_rqst].rq_nvec = 1;
206
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000207
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000208 size[0] = sizeof(FILE_BASIC_INFO);
209 data[0] = ptr;
210
Ronnie Sahlbergdc9300a2019-08-30 09:53:56 +1000211 if (cfile)
212 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
213 cfile->fid.persistent_fid,
214 cfile->fid.volatile_fid, current->tgid,
215 FILE_BASIC_INFORMATION,
216 SMB2_O_INFO_FILE, 0, data, size);
217 else {
218 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
219 COMPOUND_FID,
220 COMPOUND_FID, current->tgid,
221 FILE_BASIC_INFORMATION,
222 SMB2_O_INFO_FILE, 0, data, size);
223 if (!rc) {
224 smb2_set_next_command(tcon, &rqst[num_rqst]);
225 smb2_set_related(&rqst[num_rqst]);
226 }
227 }
228
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +1000229 if (rc)
230 goto finished;
Ronnie Sahlbergdc9300a2019-08-30 09:53:56 +1000231 num_rqst++;
Steve French81915762019-03-13 00:02:47 -0500232 trace_smb3_set_info_compound_enter(xid, ses->Suid, tcon->tid,
233 full_path);
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000234 break;
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000235 case SMB2_OP_RENAME:
236 memset(&si_iov, 0, sizeof(si_iov));
237 rqst[num_rqst].rq_iov = si_iov;
238 rqst[num_rqst].rq_nvec = 2;
239
240 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
241
242 rename_info.ReplaceIfExists = 1;
243 rename_info.RootDirectory = 0;
244 rename_info.FileNameLength = cpu_to_le32(len);
245
246 size[0] = sizeof(struct smb2_file_rename_info);
247 data[0] = &rename_info;
248
249 size[1] = len + 2 /* null */;
250 data[1] = (__le16 *)ptr;
251
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000252 if (cfile)
253 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
254 cfile->fid.persistent_fid,
255 cfile->fid.volatile_fid,
256 current->tgid, FILE_RENAME_INFORMATION,
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000257 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000258 else {
259 rc = SMB2_set_info_init(tcon, &rqst[num_rqst],
260 COMPOUND_FID, COMPOUND_FID,
261 current->tgid, FILE_RENAME_INFORMATION,
262 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlbergdc9300a2019-08-30 09:53:56 +1000263 if (!rc) {
264 smb2_set_next_command(tcon, &rqst[num_rqst]);
265 smb2_set_related(&rqst[num_rqst]);
266 }
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000267 }
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +1000268 if (rc)
269 goto finished;
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000270 num_rqst++;
Steve French81915762019-03-13 00:02:47 -0500271 trace_smb3_rename_enter(xid, ses->Suid, tcon->tid, full_path);
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000272 break;
273 case SMB2_OP_HARDLINK:
274 memset(&si_iov, 0, sizeof(si_iov));
275 rqst[num_rqst].rq_iov = si_iov;
276 rqst[num_rqst].rq_nvec = 2;
277
278 len = (2 * UniStrnlen((wchar_t *)ptr, PATH_MAX));
279
280 link_info.ReplaceIfExists = 0;
281 link_info.RootDirectory = 0;
282 link_info.FileNameLength = cpu_to_le32(len);
283
284 size[0] = sizeof(struct smb2_file_link_info);
285 data[0] = &link_info;
286
287 size[1] = len + 2 /* null */;
288 data[1] = (__le16 *)ptr;
289
290 rc = SMB2_set_info_init(tcon, &rqst[num_rqst], COMPOUND_FID,
291 COMPOUND_FID, current->tgid,
292 FILE_LINK_INFORMATION,
293 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +1000294 if (rc)
295 goto finished;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +1000296 smb2_set_next_command(tcon, &rqst[num_rqst]);
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000297 smb2_set_related(&rqst[num_rqst++]);
Steve French81915762019-03-13 00:02:47 -0500298 trace_smb3_hardlink_enter(xid, ses->Suid, tcon->tid, full_path);
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000299 break;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000300 default:
301 cifs_dbg(VFS, "Invalid command\n");
302 rc = -EINVAL;
303 }
304 if (rc)
305 goto finished;
306
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000307 /* We already have a handle so we can skip the close */
308 if (cfile)
309 goto after_close;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000310 /* Close */
311 memset(&close_iov, 0, sizeof(close_iov));
312 rqst[num_rqst].rq_iov = close_iov;
313 rqst[num_rqst].rq_nvec = 1;
314 rc = SMB2_close_init(tcon, &rqst[num_rqst], COMPOUND_FID,
Steve French43f8a6a2019-12-02 21:46:54 -0600315 COMPOUND_FID, false);
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000316 smb2_set_related(&rqst[num_rqst]);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000317 if (rc)
318 goto finished;
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000319 after_close:
320 num_rqst++;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000321
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000322 if (cfile) {
323 cifsFileInfo_put(cfile);
324 cfile = NULL;
325 rc = compound_send_recv(xid, ses, flags, num_rqst - 2,
326 &rqst[1], &resp_buftype[1],
327 &rsp_iov[1]);
328 } else
329 rc = compound_send_recv(xid, ses, flags, num_rqst,
330 rqst, resp_buftype,
331 rsp_iov);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000332
333 finished:
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000334 if (cfile)
335 cifsFileInfo_put(cfile);
336
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000337 SMB2_open_free(&rqst[0]);
Steve French7dcc82c2019-09-11 00:07:36 -0500338 if (rc == -EREMCHG) {
339 printk_once(KERN_WARNING "server share %s deleted\n",
340 tcon->treeName);
341 tcon->need_reconnect = true;
342 }
343
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000344 switch (command) {
345 case SMB2_OP_QUERY_INFO:
346 if (rc == 0) {
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000347 qi_rsp = (struct smb2_query_info_rsp *)
348 rsp_iov[1].iov_base;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000349 rc = smb2_validate_and_copy_iov(
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000350 le16_to_cpu(qi_rsp->OutputBufferOffset),
351 le32_to_cpu(qi_rsp->OutputBufferLength),
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000352 &rsp_iov[1], sizeof(struct smb2_file_all_info),
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000353 ptr);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000354 }
355 if (rqst[1].rq_iov)
356 SMB2_query_info_free(&rqst[1]);
357 if (rqst[2].rq_iov)
358 SMB2_close_free(&rqst[2]);
Steve French81915762019-03-13 00:02:47 -0500359 if (rc)
360 trace_smb3_query_info_compound_err(xid, ses->Suid,
361 tcon->tid, rc);
362 else
363 trace_smb3_query_info_compound_done(xid, ses->Suid,
364 tcon->tid);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000365 break;
Ronnie Sahlberg47dd9592018-09-03 13:33:43 +1000366 case SMB2_OP_DELETE:
Steve French81915762019-03-13 00:02:47 -0500367 if (rc)
368 trace_smb3_delete_err(xid, ses->Suid, tcon->tid, rc);
369 else
370 trace_smb3_delete_done(xid, ses->Suid, tcon->tid);
371 if (rqst[1].rq_iov)
372 SMB2_close_free(&rqst[1]);
373 break;
Ronnie Sahlbergf733e392018-09-03 13:33:42 +1000374 case SMB2_OP_MKDIR:
Steve French81915762019-03-13 00:02:47 -0500375 if (rc)
376 trace_smb3_mkdir_err(xid, ses->Suid, tcon->tid, rc);
377 else
378 trace_smb3_mkdir_done(xid, ses->Suid, tcon->tid);
Ronnie Sahlbergf733e392018-09-03 13:33:42 +1000379 if (rqst[1].rq_iov)
380 SMB2_close_free(&rqst[1]);
381 break;
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000382 case SMB2_OP_HARDLINK:
Steve French81915762019-03-13 00:02:47 -0500383 if (rc)
384 trace_smb3_hardlink_err(xid, ses->Suid, tcon->tid, rc);
385 else
386 trace_smb3_hardlink_done(xid, ses->Suid, tcon->tid);
387 free_set_inf_compound(rqst);
388 break;
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000389 case SMB2_OP_RENAME:
Steve French81915762019-03-13 00:02:47 -0500390 if (rc)
391 trace_smb3_rename_err(xid, ses->Suid, tcon->tid, rc);
392 else
393 trace_smb3_rename_done(xid, ses->Suid, tcon->tid);
394 free_set_inf_compound(rqst);
395 break;
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000396 case SMB2_OP_RMDIR:
Steve French81915762019-03-13 00:02:47 -0500397 if (rc)
398 trace_smb3_rmdir_err(xid, ses->Suid, tcon->tid, rc);
399 else
400 trace_smb3_rmdir_done(xid, ses->Suid, tcon->tid);
401 free_set_inf_compound(rqst);
402 break;
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000403 case SMB2_OP_SET_EOF:
Steve French81915762019-03-13 00:02:47 -0500404 if (rc)
405 trace_smb3_set_eof_err(xid, ses->Suid, tcon->tid, rc);
406 else
407 trace_smb3_set_eof_done(xid, ses->Suid, tcon->tid);
408 free_set_inf_compound(rqst);
409 break;
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000410 case SMB2_OP_SET_INFO:
Steve French81915762019-03-13 00:02:47 -0500411 if (rc)
412 trace_smb3_set_info_compound_err(xid, ses->Suid,
413 tcon->tid, rc);
414 else
415 trace_smb3_set_info_compound_done(xid, ses->Suid,
416 tcon->tid);
417 free_set_inf_compound(rqst);
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000418 break;
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000419 }
420 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
421 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
422 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
423 return rc;
424}
425
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700426void
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400427move_smb2_info_to_cifs(FILE_ALL_INFO *dst, struct smb2_file_all_info *src)
428{
429 memcpy(dst, src, (size_t)(&src->CurrentByteOffset) - (size_t)src);
430 dst->CurrentByteOffset = src->CurrentByteOffset;
431 dst->Mode = src->Mode;
432 dst->AlignmentRequirement = src->AlignmentRequirement;
433 dst->IndexNumber1 = 0; /* we don't use it */
434}
435
436int
437smb2_query_path_info(const unsigned int xid, struct cifs_tcon *tcon,
438 struct cifs_sb_info *cifs_sb, const char *full_path,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400439 FILE_ALL_INFO *data, bool *adjust_tz, bool *symlink)
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400440{
441 int rc;
442 struct smb2_file_all_info *smb2_data;
Steve French61351d62018-10-19 00:32:41 -0500443 __u32 create_options = 0;
Pavel Shilovsky6a9cbdd2019-01-16 11:48:42 -0800444 struct cifs_fid fid;
445 bool no_cached_open = tcon->nohandlecache;
Ronnie Sahlberg496902d2019-09-09 15:30:00 +1000446 struct cifsFileInfo *cfile;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400447
448 *adjust_tz = false;
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400449 *symlink = false;
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400450
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400451 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400452 GFP_KERNEL);
453 if (smb2_data == NULL)
454 return -ENOMEM;
Pavel Shilovsky6a9cbdd2019-01-16 11:48:42 -0800455
456 /* If it is a root and its handle is cached then use it */
457 if (!strlen(full_path) && !no_cached_open) {
Amir Goldstein0f060932020-02-03 21:46:43 +0200458 rc = open_shroot(xid, tcon, cifs_sb, &fid);
Pavel Shilovsky6a9cbdd2019-01-16 11:48:42 -0800459 if (rc)
460 goto out;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000461
462 if (tcon->crfid.file_all_info_is_valid) {
463 move_smb2_info_to_cifs(data,
464 &tcon->crfid.file_all_info);
465 } else {
466 rc = SMB2_query_info(xid, tcon, fid.persistent_fid,
467 fid.volatile_fid, smb2_data);
468 if (!rc)
469 move_smb2_info_to_cifs(data, smb2_data);
470 }
Pavel Shilovsky6a9cbdd2019-01-16 11:48:42 -0800471 close_shroot(&tcon->crfid);
Pavel Shilovsky6a9cbdd2019-01-16 11:48:42 -0800472 goto out;
473 }
474
Ronnie Sahlberg496902d2019-09-09 15:30:00 +1000475 cifs_get_readable_path(tcon, full_path, &cfile);
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000476 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
Steve French61351d62018-10-19 00:32:41 -0500477 FILE_READ_ATTRIBUTES, FILE_OPEN, create_options,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500478 ACL_NO_MODE, smb2_data, SMB2_OP_QUERY_INFO, cfile);
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400479 if (rc == -EOPNOTSUPP) {
480 *symlink = true;
Steve French61351d62018-10-19 00:32:41 -0500481 create_options |= OPEN_REPARSE_POINT;
482
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400483 /* Failed on a symbolic link - query a reparse point info */
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +1000484 rc = smb2_compound_op(xid, tcon, cifs_sb, full_path,
485 FILE_READ_ATTRIBUTES, FILE_OPEN,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500486 create_options, ACL_NO_MODE,
487 smb2_data, SMB2_OP_QUERY_INFO, NULL);
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400488 }
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400489 if (rc)
490 goto out;
491
492 move_smb2_info_to_cifs(data, smb2_data);
493out:
494 kfree(smb2_data);
495 return rc;
496}
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400497
498int
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500499smb2_mkdir(const unsigned int xid, struct inode *parent_inode, umode_t mode,
500 struct cifs_tcon *tcon, const char *name,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400501 struct cifs_sb_info *cifs_sb)
502{
Ronnie Sahlbergf733e392018-09-03 13:33:42 +1000503 return smb2_compound_op(xid, tcon, cifs_sb, name,
504 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500505 CREATE_NOT_FILE, mode, NULL, SMB2_OP_MKDIR,
506 NULL);
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400507}
508
509void
510smb2_mkdir_setinfo(struct inode *inode, const char *name,
511 struct cifs_sb_info *cifs_sb, struct cifs_tcon *tcon,
512 const unsigned int xid)
513{
514 FILE_BASIC_INFO data;
515 struct cifsInodeInfo *cifs_i;
Ronnie Sahlbergdc9300a2019-08-30 09:53:56 +1000516 struct cifsFileInfo *cfile;
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400517 u32 dosattrs;
518 int tmprc;
519
520 memset(&data, 0, sizeof(data));
521 cifs_i = CIFS_I(inode);
522 dosattrs = cifs_i->cifsAttrs | ATTR_READONLY;
523 data.Attributes = cpu_to_le32(dosattrs);
Aurelien Aptel86f740f2020-02-21 11:19:06 +0100524 cifs_get_writable_path(tcon, name, FIND_WR_ANY, &cfile);
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000525 tmprc = smb2_compound_op(xid, tcon, cifs_sb, name,
526 FILE_WRITE_ATTRIBUTES, FILE_CREATE,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500527 CREATE_NOT_FILE, ACL_NO_MODE,
528 &data, SMB2_OP_SET_INFO, cfile);
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400529 if (tmprc == 0)
530 cifs_i->cifsAttrs = dosattrs;
531}
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400532
533int
534smb2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
535 struct cifs_sb_info *cifs_sb)
536{
Ronnie Sahlbergc2e0fe32018-09-03 13:33:45 +1000537 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500538 CREATE_NOT_FILE, ACL_NO_MODE,
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000539 NULL, SMB2_OP_RMDIR, NULL);
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400540}
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700541
542int
543smb2_unlink(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
544 struct cifs_sb_info *cifs_sb)
545{
Ronnie Sahlberg47dd9592018-09-03 13:33:43 +1000546 return smb2_compound_op(xid, tcon, cifs_sb, name, DELETE, FILE_OPEN,
547 CREATE_DELETE_ON_CLOSE | OPEN_REPARSE_POINT,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500548 ACL_NO_MODE, NULL, SMB2_OP_DELETE, NULL);
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700549}
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700550
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700551static int
552smb2_set_path_attr(const unsigned int xid, struct cifs_tcon *tcon,
553 const char *from_name, const char *to_name,
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000554 struct cifs_sb_info *cifs_sb, __u32 access, int command,
555 struct cifsFileInfo *cfile)
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700556{
557 __le16 *smb2_to_name = NULL;
558 int rc;
559
560 smb2_to_name = cifs_convert_path_to_utf16(to_name, cifs_sb);
561 if (smb2_to_name == NULL) {
562 rc = -ENOMEM;
563 goto smb2_rename_path;
564 }
Ronnie Sahlbergbb435512018-09-03 13:33:49 +1000565 rc = smb2_compound_op(xid, tcon, cifs_sb, from_name, access,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500566 FILE_OPEN, 0, ACL_NO_MODE, smb2_to_name,
567 command, cfile);
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700568smb2_rename_path:
569 kfree(smb2_to_name);
570 return rc;
571}
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700572
573int
574smb2_rename_path(const unsigned int xid, struct cifs_tcon *tcon,
575 const char *from_name, const char *to_name,
576 struct cifs_sb_info *cifs_sb)
577{
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000578 struct cifsFileInfo *cfile;
579
Aurelien Aptel86f740f2020-02-21 11:19:06 +0100580 cifs_get_writable_path(tcon, from_name, FIND_WR_WITH_DELETE, &cfile);
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000581
582 return smb2_set_path_attr(xid, tcon, from_name, to_name,
583 cifs_sb, DELETE, SMB2_OP_RENAME, cfile);
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700584}
585
586int
587smb2_create_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
588 const char *from_name, const char *to_name,
589 struct cifs_sb_info *cifs_sb)
590{
591 return smb2_set_path_attr(xid, tcon, from_name, to_name, cifs_sb,
Ronnie Sahlberg8de9e862019-08-30 08:25:46 +1000592 FILE_READ_ATTRIBUTES, SMB2_OP_HARDLINK,
593 NULL);
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700594}
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700595
596int
597smb2_set_path_size(const unsigned int xid, struct cifs_tcon *tcon,
598 const char *full_path, __u64 size,
599 struct cifs_sb_info *cifs_sb, bool set_alloc)
600{
601 __le64 eof = cpu_to_le64(size);
Ronnie Sahlbergf7bfe042018-09-03 13:33:46 +1000602
603 return smb2_compound_op(xid, tcon, cifs_sb, full_path,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500604 FILE_WRITE_DATA, FILE_OPEN, 0, ACL_NO_MODE,
605 &eof, SMB2_OP_SET_EOF, NULL);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700606}
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700607
608int
609smb2_set_file_info(struct inode *inode, const char *full_path,
610 FILE_BASIC_INFO *buf, const unsigned int xid)
611{
612 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
613 struct tcon_link *tlink;
614 int rc;
615
Steve French18dd8e12016-09-26 14:23:08 -0500616 if ((buf->CreationTime == 0) && (buf->LastAccessTime == 0) &&
Steve Frenchfd09b7d2018-08-02 20:28:18 -0500617 (buf->LastWriteTime == 0) && (buf->ChangeTime == 0) &&
Steve French18dd8e12016-09-26 14:23:08 -0500618 (buf->Attributes == 0))
619 return 0; /* would be a no op, no sense sending this */
620
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700621 tlink = cifs_sb_tlink(cifs_sb);
622 if (IS_ERR(tlink))
623 return PTR_ERR(tlink);
Steve French18dd8e12016-09-26 14:23:08 -0500624
Ronnie Sahlbergdcbf9102018-09-03 13:33:48 +1000625 rc = smb2_compound_op(xid, tlink_tcon(tlink), cifs_sb, full_path,
Steve Frenchc3ca78e2019-09-25 00:32:13 -0500626 FILE_WRITE_ATTRIBUTES, FILE_OPEN,
627 0, ACL_NO_MODE, buf, SMB2_OP_SET_INFO, NULL);
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700628 cifs_put_tlink(tlink);
629 return rc;
630}