blob: d62f9175c546f0456628b8560d52ae192ec45ae2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifssmb.c
3 *
Steve Frenchf19159d2010-04-21 04:12:10 +00004 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Contains the routines for constructing the SMB PDUs themselves
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
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
Steve French2dd29d32007-04-23 22:07:35 +000027 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/fs.h>
31#include <linux/kernel.h>
32#include <linux/vfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/posix_acl_xattr.h>
Jeff Laytonc28c89f2011-05-19 16:22:56 -040035#include <linux/pagemap.h>
Jeff Laytone28bc5b2011-10-19 15:30:07 -040036#include <linux/swap.h>
37#include <linux/task_io_accounting_ops.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "cifspdu.h"
40#include "cifsglob.h"
Shirish Pargaonkard0d66c42007-10-03 18:22:19 +000041#include "cifsacl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "cifsproto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
Pavel Shilovskyd9191312019-12-10 11:44:52 -080045#include "smb2proto.h"
Jeff Laytone28bc5b2011-10-19 15:30:07 -040046#include "fscache.h"
Long Lidb223a52017-11-22 17:38:45 -070047#include "smbdirect.h"
Paulo Alcantara08744012018-11-14 17:24:29 -020048#ifdef CONFIG_CIFS_DFS_UPCALL
49#include "dfs_cache.h"
50#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#ifdef CONFIG_CIFS_POSIX
53static struct {
54 int index;
55 char *name;
56} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000057#ifdef CONFIG_CIFS_WEAK_PW_HASH
58 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000059 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000060#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000061 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000062 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 {BAD_PROT, "\2"}
64};
65#else
66static struct {
67 int index;
68 char *name;
69} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000070#ifdef CONFIG_CIFS_WEAK_PW_HASH
71 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000072 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000073#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000074 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 {BAD_PROT, "\2"}
76};
77#endif
78
Steve French39798772006-05-31 22:40:51 +000079/* define the number of elements in the cifs dialect array */
80#ifdef CONFIG_CIFS_POSIX
81#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000082#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000083#else
84#define CIFS_NUM_PROT 2
85#endif /* CIFS_WEAK_PW_HASH */
86#else /* not posix */
87#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000088#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000089#else
90#define CIFS_NUM_PROT 1
91#endif /* CONFIG_CIFS_WEAK_PW_HASH */
92#endif /* CIFS_POSIX */
93
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +040094/*
95 * Mark as invalid, all open files on tree connections since they
96 * were closed when session to server was lost.
97 */
98void
99cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +0000102 struct list_head *tmp;
103 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400105 /* list all files open on tree connection and mark them invalid */
Steve French3afca262016-09-22 18:58:16 -0500106 spin_lock(&tcon->open_file_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400107 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000108 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000109 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400110 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Steve French3afca262016-09-22 18:58:16 -0500112 spin_unlock(&tcon->open_file_lock);
Steve French3d4ef9a2018-04-25 22:19:09 -0500113
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000114 mutex_lock(&tcon->crfid.fid_mutex);
115 tcon->crfid.is_valid = false;
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800116 /* cached handle is not valid, so SMB2_CLOSE won't be sent below */
117 close_shroot_lease_locked(&tcon->crfid);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000118 memset(tcon->crfid.fid, 0, sizeof(struct cifs_fid));
119 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500120
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400121 /*
122 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
123 * to this tcon.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Paulo Alcantara08744012018-11-14 17:24:29 -0200127#ifdef CONFIG_CIFS_DFS_UPCALL
128static int __cifs_reconnect_tcon(const struct nls_table *nlsc,
129 struct cifs_tcon *tcon)
130{
131 int rc;
132 struct dfs_cache_tgt_list tl;
133 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100134 char *tree;
Paulo Alcantara08744012018-11-14 17:24:29 -0200135 const char *tcp_host;
136 size_t tcp_host_len;
137 const char *dfs_host;
138 size_t dfs_host_len;
139
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100140 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
141 if (!tree)
142 return -ENOMEM;
143
Paulo Alcantara08744012018-11-14 17:24:29 -0200144 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000145 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
146 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100147 rc = CIFSTCon(0, tcon->ses, tree, tcon, nlsc);
148 goto out;
Paulo Alcantara08744012018-11-14 17:24:29 -0200149 }
150
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100151 if (!tcon->dfs_path) {
152 rc = CIFSTCon(0, tcon->ses, tcon->treeName, tcon, nlsc);
153 goto out;
154 }
Paulo Alcantara08744012018-11-14 17:24:29 -0200155
156 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
157 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100158 goto out;
Paulo Alcantara08744012018-11-14 17:24:29 -0200159
160 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
161 &tcp_host_len);
162
163 for (it = dfs_cache_get_tgt_iterator(&tl); it;
164 it = dfs_cache_get_next_tgt(&tl, it)) {
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300165 const char *share, *prefix;
166 size_t share_len, prefix_len;
Paulo Alcantara08744012018-11-14 17:24:29 -0200167
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300168 rc = dfs_cache_get_tgt_share(it, &share, &share_len, &prefix,
169 &prefix_len);
170 if (rc) {
171 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
172 __func__, rc);
173 continue;
174 }
175
176 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
Paulo Alcantara08744012018-11-14 17:24:29 -0200177
178 if (dfs_host_len != tcp_host_len
179 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
180 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
181 __func__,
182 (int)dfs_host_len, dfs_host,
183 (int)tcp_host_len, tcp_host);
184 continue;
185 }
186
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300187 scnprintf(tree, MAX_TREE_SIZE, "\\%.*s", (int)share_len, share);
Paulo Alcantara08744012018-11-14 17:24:29 -0200188
189 rc = CIFSTCon(0, tcon->ses, tree, tcon, nlsc);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300190 if (!rc) {
191 rc = update_super_prepath(tcon, prefix, prefix_len);
Paulo Alcantara08744012018-11-14 17:24:29 -0200192 break;
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300193 }
Paulo Alcantara08744012018-11-14 17:24:29 -0200194 if (rc == -EREMOTE)
195 break;
196 }
197
198 if (!rc) {
199 if (it)
200 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
201 it);
202 else
203 rc = -ENOENT;
204 }
205 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100206out:
207 kfree(tree);
Paulo Alcantara08744012018-11-14 17:24:29 -0200208 return rc;
209}
210#else
211static inline int __cifs_reconnect_tcon(const struct nls_table *nlsc,
212 struct cifs_tcon *tcon)
213{
214 return CIFSTCon(0, tcon->ses, tcon->treeName, tcon, nlsc);
215}
216#endif
217
Jeff Layton9162ab22009-09-03 12:07:17 -0400218/* reconnect the socket, tcon, and smb session if needed */
219static int
Steve French96daf2b2011-05-27 04:34:02 +0000220cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400221{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400222 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000223 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400224 struct TCP_Server_Info *server;
225 struct nls_table *nls_codepage;
Paulo Alcantara08744012018-11-14 17:24:29 -0200226 int retries;
Jeff Layton9162ab22009-09-03 12:07:17 -0400227
228 /*
229 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
230 * tcp and smb session status done differently for those three - in the
231 * calling routine
232 */
233 if (!tcon)
234 return 0;
235
236 ses = tcon->ses;
237 server = ses->server;
238
239 /*
240 * only tree disconnect, open, and write, (and ulogoff which does not
241 * have tcon) are allowed as we start force umount
242 */
243 if (tcon->tidStatus == CifsExiting) {
244 if (smb_command != SMB_COM_WRITE_ANDX &&
245 smb_command != SMB_COM_OPEN_ANDX &&
246 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500247 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
248 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400249 return -ENODEV;
250 }
251 }
252
Paulo Alcantara08744012018-11-14 17:24:29 -0200253 retries = server->nr_targets;
254
Jeff Layton9162ab22009-09-03 12:07:17 -0400255 /*
Paulo Alcantara08744012018-11-14 17:24:29 -0200256 * Give demultiplex thread up to 10 seconds to each target available for
257 * reconnect -- should be greater than cifs socket timeout which is 7
258 * seconds.
Jeff Layton9162ab22009-09-03 12:07:17 -0400259 */
260 while (server->tcpStatus == CifsNeedReconnect) {
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300261 rc = wait_event_interruptible_timeout(server->response_q,
262 (server->tcpStatus != CifsNeedReconnect),
263 10 * HZ);
264 if (rc < 0) {
265 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
266 " signal by the process\n", __func__);
267 return -ERESTARTSYS;
268 }
Jeff Layton9162ab22009-09-03 12:07:17 -0400269
Steve Frenchfd88ce92011-04-12 01:01:14 +0000270 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400271 if (server->tcpStatus != CifsNeedReconnect)
272 break;
273
Ronnie Sahlberg09c40b12020-02-06 13:55:19 +1000274 if (retries && --retries)
Paulo Alcantara08744012018-11-14 17:24:29 -0200275 continue;
276
Jeff Layton9162ab22009-09-03 12:07:17 -0400277 /*
278 * on "soft" mounts we wait once. Hard mounts keep
279 * retrying until process is killed or server comes
280 * back on-line
281 */
Jeff Laytond4025392011-02-07 08:54:35 -0500282 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500283 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400284 return -EHOSTDOWN;
285 }
Paulo Alcantara08744012018-11-14 17:24:29 -0200286 retries = server->nr_targets;
Jeff Layton9162ab22009-09-03 12:07:17 -0400287 }
288
289 if (!ses->need_reconnect && !tcon->need_reconnect)
290 return 0;
291
292 nls_codepage = load_nls_default();
293
294 /*
295 * need to prevent multiple threads trying to simultaneously
296 * reconnect the same SMB session
297 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000298 mutex_lock(&ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200299
300 /*
301 * Recheck after acquire mutex. If another thread is negotiating
302 * and the server never sends an answer the socket will be closed
303 * and tcpStatus set to reconnect.
304 */
305 if (server->tcpStatus == CifsNeedReconnect) {
306 rc = -EHOSTDOWN;
307 mutex_unlock(&ses->session_mutex);
308 goto out;
309 }
310
Jeff Layton198b5682010-04-24 07:57:48 -0400311 rc = cifs_negotiate_protocol(0, ses);
312 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400313 rc = cifs_setup_session(0, ses, nls_codepage);
314
315 /* do we need to reconnect tcon? */
316 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000317 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400318 goto out;
319 }
320
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400321 cifs_mark_open_files_invalid(tcon);
Paulo Alcantara08744012018-11-14 17:24:29 -0200322 rc = __cifs_reconnect_tcon(nls_codepage, tcon);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000323 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500324 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400325
Steve Frenchc318e6c2018-04-04 14:08:52 -0500326 if (rc) {
327 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400328 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500329 }
Jeff Layton9162ab22009-09-03 12:07:17 -0400330
Jeff Layton9162ab22009-09-03 12:07:17 -0400331 atomic_inc(&tconInfoReconnectCount);
332
333 /* tell server Unix caps we support */
Stefan Metzmacher864138c2020-02-24 14:15:00 +0100334 if (cap_unix(ses))
Jeff Layton9162ab22009-09-03 12:07:17 -0400335 reset_cifs_unix_caps(0, tcon, NULL, NULL);
336
337 /*
338 * Removed call to reopen open files here. It is safer (and faster) to
339 * reopen files one at a time as needed in read and write.
340 *
341 * FIXME: what about file locks? don't we need to reclaim them ASAP?
342 */
343
344out:
345 /*
346 * Check if handle based operation so we know whether we can continue
347 * or not without returning to caller to reset file handle
348 */
349 switch (smb_command) {
350 case SMB_COM_READ_ANDX:
351 case SMB_COM_WRITE_ANDX:
352 case SMB_COM_CLOSE:
353 case SMB_COM_FIND_CLOSE2:
354 case SMB_COM_LOCKING_ANDX:
355 rc = -EAGAIN;
356 }
357
358 unload_nls(nls_codepage);
359 return rc;
360}
361
Steve Frenchad7a2922008-02-07 23:25:02 +0000362/* Allocate and return pointer to an SMB request buffer, and set basic
363 SMB information in the SMB header. If the return code is zero, this
364 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int
Steve French96daf2b2011-05-27 04:34:02 +0000366small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000367 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Jeff Laytonf5695992010-09-29 15:27:08 -0400369 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jeff Layton9162ab22009-09-03 12:07:17 -0400371 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000372 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return rc;
374
375 *request_buf = cifs_small_buf_get();
376 if (*request_buf == NULL) {
377 /* BB should we add a retry in here if not a writepage? */
378 return -ENOMEM;
379 }
380
Steve French63135e02007-07-17 17:34:02 +0000381 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000382 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Steve French790fe572007-07-07 19:25:05 +0000384 if (tcon != NULL)
385 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700386
Jeff Laytonf5695992010-09-29 15:27:08 -0400387 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000388}
389
Steve French12b3b8f2006-02-09 21:12:47 +0000390int
Steve French50c2f752007-07-13 00:33:32 +0000391small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000392 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000393{
394 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000395 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000396
Steve French5815449d2006-02-14 01:36:20 +0000397 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000398 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000399 return rc;
400
Steve French04fdabe2006-02-10 05:52:50 +0000401 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400402 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000403 if (ses->capabilities & CAP_UNICODE)
404 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000405 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000406 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
407
408 /* uid, tid can stay at zero as set in header assemble */
409
Steve French50c2f752007-07-13 00:33:32 +0000410 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000411 this function is used after 1st of session setup requests */
412
413 return rc;
414}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416/* If the return code is zero, this function must fill in request_buf pointer */
417static int
Steve French96daf2b2011-05-27 04:34:02 +0000418__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400419 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *request_buf = cifs_buf_get();
422 if (*request_buf == NULL) {
423 /* BB should we add a retry in here if not a writepage? */
424 return -ENOMEM;
425 }
426 /* Although the original thought was we needed the response buf for */
427 /* potential retries of smb operations it turns out we can determine */
428 /* from the mid flags when the request buffer can be resent without */
429 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000430 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000431 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000434 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Steve French790fe572007-07-07 19:25:05 +0000436 if (tcon != NULL)
437 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700438
Jeff Laytonf5695992010-09-29 15:27:08 -0400439 return 0;
440}
441
442/* If the return code is zero, this function must fill in request_buf pointer */
443static int
Steve French96daf2b2011-05-27 04:34:02 +0000444smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400445 void **request_buf, void **response_buf)
446{
447 int rc;
448
449 rc = cifs_reconnect_tcon(tcon, smb_command);
450 if (rc)
451 return rc;
452
453 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
454}
455
456static int
Steve French96daf2b2011-05-27 04:34:02 +0000457smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400458 void **request_buf, void **response_buf)
459{
460 if (tcon->ses->need_reconnect || tcon->need_reconnect)
461 return -EHOSTDOWN;
462
463 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Steve French50c2f752007-07-13 00:33:32 +0000466static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jeff Layton12df83c2011-01-20 13:36:51 -0500468 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Jeff Layton12df83c2011-01-20 13:36:51 -0500470 /* check for plausible wct */
471 if (pSMB->hdr.WordCount < 10)
472 goto vt2_err;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500475 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
476 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
477 goto vt2_err;
478
Jeff Layton12df83c2011-01-20 13:36:51 -0500479 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
480 if (total_size >= 512)
481 goto vt2_err;
482
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400483 /* check that bcc is at least as big as parms + data, and that it is
484 * less than negotiated smb buffer
485 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500486 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
487 if (total_size > get_bcc(&pSMB->hdr) ||
488 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
489 goto vt2_err;
490
491 return 0;
492vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000493 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Jeff Layton690c5222011-01-20 13:36:51 -0500497
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400498static int
Jeff Layton3f618222013-06-12 19:52:14 -0500499decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400500{
501 int rc = 0;
502 u16 count;
503 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500504 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400505
506 count = get_bcc(&pSMBr->hdr);
507 if (count < SMB1_CLIENT_GUID_SIZE)
508 return -EIO;
509
510 spin_lock(&cifs_tcp_ses_lock);
511 if (server->srv_count > 1) {
512 spin_unlock(&cifs_tcp_ses_lock);
513 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
514 cifs_dbg(FYI, "server UID changed\n");
515 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
516 }
517 } else {
518 spin_unlock(&cifs_tcp_ses_lock);
519 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
520 }
521
522 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500523 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400524 } else {
525 count -= SMB1_CLIENT_GUID_SIZE;
526 rc = decode_negTokenInit(
527 pSMBr->u.extended_response.SecurityBlob, count, server);
528 if (rc != 1)
529 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400530 }
531
532 return 0;
533}
534
Jeff Layton9ddec562013-05-26 07:00:58 -0400535int
Jeff Layton38d77c52013-05-26 07:01:00 -0400536cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400537{
Jeff Layton502858822013-06-27 12:45:00 -0400538 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
539 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400540 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
541
542 /*
543 * Is signing required by mnt options? If not then check
544 * global_secflags to see if it is there.
545 */
546 if (!mnt_sign_required)
547 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
548 CIFSSEC_MUST_SIGN);
549
550 /*
551 * If signing is required then it's automatically enabled too,
552 * otherwise, check to see if the secflags allow it.
553 */
554 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
555 (global_secflags & CIFSSEC_MAY_SIGN);
556
557 /* If server requires signing, does client allow it? */
558 if (srv_sign_required) {
559 if (!mnt_sign_enabled) {
560 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
561 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400562 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400563 server->sign = true;
564 }
565
566 /* If client requires signing, does server allow it? */
567 if (mnt_sign_required) {
568 if (!srv_sign_enabled) {
569 cifs_dbg(VFS, "Server does not support signing!");
570 return -ENOTSUPP;
571 }
572 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400573 }
574
Long Libb4c0412018-04-17 12:17:08 -0700575 if (cifs_rdma_enabled(server) && server->sign)
576 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled");
577
Jeff Layton9ddec562013-05-26 07:00:58 -0400578 return 0;
579}
580
Jeff Layton2190eca2013-05-26 07:00:57 -0400581#ifdef CONFIG_CIFS_WEAK_PW_HASH
582static int
Jeff Layton3f618222013-06-12 19:52:14 -0500583decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400584{
585 __s16 tmp;
586 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
587
588 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
589 return -EOPNOTSUPP;
590
Jeff Layton2190eca2013-05-26 07:00:57 -0400591 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
592 server->maxReq = min_t(unsigned int,
593 le16_to_cpu(rsp->MaxMpxCount),
594 cifs_max_pending);
595 set_credits(server, server->maxReq);
596 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Jones Syue1f641d92020-04-13 09:37:23 +0800597 /* set up max_read for readpages check */
598 server->max_read = server->maxBuf;
Jeff Layton2190eca2013-05-26 07:00:57 -0400599 /* even though we do not use raw we might as well set this
600 accurately, in case we ever find a need for it */
601 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
602 server->max_rw = 0xFF00;
603 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
604 } else {
605 server->max_rw = 0;/* do not need to use raw anyway */
606 server->capabilities = CAP_MPX_MODE;
607 }
608 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
609 if (tmp == -1) {
610 /* OS/2 often does not set timezone therefore
611 * we must use server time to calc time zone.
612 * Could deviate slightly from the right zone.
613 * Smallest defined timezone difference is 15 minutes
614 * (i.e. Nepal). Rounding up/down is done to match
615 * this requirement.
616 */
617 int val, seconds, remain, result;
Arnd Bergmann95390202018-06-19 17:27:58 +0200618 struct timespec64 ts;
619 time64_t utc = ktime_get_real_seconds();
Jeff Layton2190eca2013-05-26 07:00:57 -0400620 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
621 rsp->SrvTime.Time, 0);
Arnd Bergmann95390202018-06-19 17:27:58 +0200622 cifs_dbg(FYI, "SrvTime %lld sec since 1970 (utc: %lld) diff: %lld\n",
623 ts.tv_sec, utc,
624 utc - ts.tv_sec);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700625 val = (int)(utc - ts.tv_sec);
Jeff Layton2190eca2013-05-26 07:00:57 -0400626 seconds = abs(val);
627 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
628 remain = seconds % MIN_TZ_ADJ;
629 if (remain >= (MIN_TZ_ADJ / 2))
630 result += MIN_TZ_ADJ;
631 if (val < 0)
632 result = -result;
633 server->timeAdj = result;
634 } else {
635 server->timeAdj = (int)tmp;
636 server->timeAdj *= 60; /* also in seconds */
637 }
638 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
639
640
641 /* BB get server time for time conversions and add
642 code to use it and timezone since this is not UTC */
643
644 if (rsp->EncryptionKeyLength ==
645 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
646 memcpy(server->cryptkey, rsp->EncryptionKey,
647 CIFS_CRYPTO_KEY_SIZE);
648 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
649 return -EIO; /* need cryptkey unless plain text */
650 }
651
652 cifs_dbg(FYI, "LANMAN negotiated\n");
653 return 0;
654}
655#else
656static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500657decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400658{
659 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
660 return -EOPNOTSUPP;
661}
662#endif
663
Jeff Layton91934002013-05-26 07:00:58 -0400664static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500665should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400666{
Jeff Layton3f618222013-06-12 19:52:14 -0500667 switch (sectype) {
668 case RawNTLMSSP:
669 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400670 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500671 case Unspecified:
672 if (global_secflags &
673 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
674 return true;
675 /* Fallthrough */
676 default:
677 return false;
678 }
Jeff Layton91934002013-05-26 07:00:58 -0400679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400682CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 NEGOTIATE_REQ *pSMB;
685 NEGOTIATE_RSP *pSMBr;
686 int rc = 0;
687 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000688 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400689 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 u16 count;
691
Jeff Layton3534b852013-05-24 07:41:01 -0400692 if (!server) {
693 WARN(1, "%s: server is NULL!\n", __func__);
694 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Jeff Layton3534b852013-05-24 07:41:01 -0400696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
698 (void **) &pSMB, (void **) &pSMBr);
699 if (rc)
700 return rc;
Steve French750d1152006-06-27 06:28:30 +0000701
Pavel Shilovsky88257362012-05-23 14:01:59 +0400702 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000703 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000704
Jeff Layton3f618222013-06-12 19:52:14 -0500705 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400706 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000707 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
708 }
Steve French50c2f752007-07-13 00:33:32 +0000709
Steve French39798772006-05-31 22:40:51 +0000710 count = 0;
Stephen Rothwellbcfb84a2018-09-03 13:15:58 +1000711 /*
712 * We know that all the name entries in the protocols array
713 * are short (< 16 bytes anyway) and are NUL terminated.
714 */
Steve French50c2f752007-07-13 00:33:32 +0000715 for (i = 0; i < CIFS_NUM_PROT; i++) {
Stephen Rothwellbcfb84a2018-09-03 13:15:58 +1000716 size_t len = strlen(protocols[i].name) + 1;
717
718 memcpy(pSMB->DialectsArray+count, protocols[i].name, len);
719 count += len;
Steve French39798772006-05-31 22:40:51 +0000720 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000721 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 pSMB->ByteCount = cpu_to_le16(count);
723
724 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
725 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000726 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000727 goto neg_err_exit;
728
Jeff Layton9bf67e52010-04-24 07:57:46 -0400729 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500730 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000731 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400732 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000733 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000734 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000735 could not negotiate a common dialect */
736 rc = -EOPNOTSUPP;
737 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000738 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400739 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500740 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400741 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000742 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000743 /* unknown wct */
744 rc = -EOPNOTSUPP;
745 goto neg_err_exit;
746 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400747 /* else wct == 17, NTLM or better */
748
Steve French96daf2b2011-05-27 04:34:02 +0000749 server->sec_mode = pSMBr->SecurityMode;
750 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500751 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000752
Steve French254e55e2006-06-04 05:53:15 +0000753 /* one byte, so no need to convert this or EncryptionKeyLen from
754 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300755 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
756 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400757 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000758 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400759 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Jones Syue1f641d92020-04-13 09:37:23 +0800760 /* set up max_read for readpages check */
761 server->max_read = server->maxBuf;
Steve Frencheca6acf2009-02-20 05:43:09 +0000762 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500763 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000764 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000765 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
766 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400767
Jeff Laytone598d1d82013-05-26 07:00:59 -0400768 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
769 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500770 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000771 CIFS_CRYPTO_KEY_SIZE);
Noel Powerf2910952015-05-27 09:22:10 +0100772 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
773 server->capabilities & CAP_EXTENDED_SECURITY) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400774 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500775 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400776 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000777 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400778 } else {
779 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000780 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400781 }
Steve French254e55e2006-06-04 05:53:15 +0000782
783signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400784 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400785 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000786neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700787 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000788
Joe Perchesf96637b2013-05-04 22:12:25 -0500789 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return rc;
791}
792
793int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400794CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500800
801 /* BB: do we need to check this? These should never be NULL. */
802 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
803 return -EIO;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500806 * No need to return error on this operation if tid invalidated and
807 * closed on server already e.g. due to tcp session crashing. Also,
808 * the tcon is no longer on the list, so no need to take lock before
809 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Steve French268875b2009-06-25 00:29:21 +0000811 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Steve French50c2f752007-07-13 00:33:32 +0000814 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700815 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500816 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return rc;
Steve French133672e2007-11-13 22:41:37 +0000818
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400819 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700820 cifs_small_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500822 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Steve French50c2f752007-07-13 00:33:32 +0000824 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500825 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (rc == -EAGAIN)
827 rc = 0;
828
829 return rc;
830}
831
Jeff Layton766fdbb2011-01-11 07:24:21 -0500832/*
833 * This is a no-op for now. We're not really interested in the reply, but
834 * rather in the fact that the server sent one and that server->lstrp
835 * gets updated.
836 *
837 * FIXME: maybe we should consider checking that the reply matches request?
838 */
839static void
840cifs_echo_callback(struct mid_q_entry *mid)
841{
842 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800843 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500844
845 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800846 add_credits(server, &credits, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500847}
848
849int
850CIFSSMBEcho(struct TCP_Server_Info *server)
851{
852 ECHO_REQ *smb;
853 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800854 struct kvec iov[2];
855 struct smb_rqst rqst = { .rq_iov = iov,
856 .rq_nvec = 2 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500857
Joe Perchesf96637b2013-05-04 22:12:25 -0500858 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500859
860 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
861 if (rc)
862 return rc;
863
Steve French26c9cb62017-05-02 13:35:20 -0500864 if (server->capabilities & CAP_UNICODE)
865 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
866
Jeff Layton766fdbb2011-01-11 07:24:21 -0500867 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000868 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500869 smb->hdr.WordCount = 1;
870 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400871 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500872 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000873 inc_rfc1001_len(smb, 3);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800874
875 iov[0].iov_len = 4;
876 iov[0].iov_base = smb;
877 iov[1].iov_len = get_rfc1002_length(smb);
878 iov[1].iov_base = (char *)smb + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500879
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800880 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +1000881 server, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500882 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500883 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500884
885 cifs_small_buf_release(smb);
886
887 return rc;
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400891CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 LOGOFF_ANDX_REQ *pSMB;
894 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Joe Perchesf96637b2013-05-04 22:12:25 -0500896 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500897
898 /*
899 * BB: do we need to check validity of ses and server? They should
900 * always be valid since we have an active reference. If not, that
901 * should probably be a BUG()
902 */
903 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -EIO;
905
Steve Frenchd7b619c2010-02-25 05:36:46 +0000906 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000907 if (ses->need_reconnect)
908 goto session_already_dead; /* no need to send SMBlogoff if uid
909 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
911 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000912 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return rc;
914 }
915
Pavel Shilovsky88257362012-05-23 14:01:59 +0400916 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700917
Jeff Layton38d77c52013-05-26 07:01:00 -0400918 if (ses->server->sign)
919 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 pSMB->hdr.Uid = ses->Suid;
922
923 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400924 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700925 cifs_small_buf_release(pSMB);
Steve French3b795212008-11-13 19:45:32 +0000926session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000927 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000930 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 error */
932 if (rc == -EAGAIN)
933 rc = 0;
934 return rc;
935}
936
937int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400938CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
939 const char *fileName, __u16 type,
940 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000941{
942 TRANSACTION2_SPI_REQ *pSMB = NULL;
943 TRANSACTION2_SPI_RSP *pSMBr = NULL;
944 struct unlink_psx_rq *pRqD;
945 int name_len;
946 int rc = 0;
947 int bytes_returned = 0;
948 __u16 params, param_offset, offset, byte_count;
949
Joe Perchesf96637b2013-05-04 22:12:25 -0500950 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000951PsxDelete:
952 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
953 (void **) &pSMBr);
954 if (rc)
955 return rc;
956
957 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
958 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600959 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
960 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000961 name_len++; /* trailing null */
962 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000963 } else {
964 name_len = copy_path_name(pSMB->FileName, fileName);
Steve French2d785a52007-07-15 01:48:57 +0000965 }
966
967 params = 6 + name_len;
968 pSMB->MaxParameterCount = cpu_to_le16(2);
969 pSMB->MaxDataCount = 0; /* BB double check this with jra */
970 pSMB->MaxSetupCount = 0;
971 pSMB->Reserved = 0;
972 pSMB->Flags = 0;
973 pSMB->Timeout = 0;
974 pSMB->Reserved2 = 0;
975 param_offset = offsetof(struct smb_com_transaction2_spi_req,
976 InformationLevel) - 4;
977 offset = param_offset + params;
978
979 /* Setup pointer to Request Data (inode type) */
980 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
981 pRqD->type = cpu_to_le16(type);
982 pSMB->ParameterOffset = cpu_to_le16(param_offset);
983 pSMB->DataOffset = cpu_to_le16(offset);
984 pSMB->SetupCount = 1;
985 pSMB->Reserved3 = 0;
986 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
987 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
988
989 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
990 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
991 pSMB->ParameterCount = cpu_to_le16(params);
992 pSMB->TotalParameterCount = pSMB->ParameterCount;
993 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
994 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000995 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000996 pSMB->ByteCount = cpu_to_le16(byte_count);
997 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
998 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000999 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001000 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +00001001 cifs_buf_release(pSMB);
1002
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001003 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +00001004
1005 if (rc == -EAGAIN)
1006 goto PsxDelete;
1007
1008 return rc;
1009}
1010
1011int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001012CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1013 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 DELETE_FILE_REQ *pSMB = NULL;
1016 DELETE_FILE_RSP *pSMBr = NULL;
1017 int rc = 0;
1018 int bytes_returned;
1019 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001020 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022DelFileRetry:
1023 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
1024 (void **) &pSMBr);
1025 if (rc)
1026 return rc;
1027
1028 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001029 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
1030 PATH_MAX, cifs_sb->local_nls,
1031 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 name_len++; /* trailing null */
1033 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001034 } else {
1035 name_len = copy_path_name(pSMB->fileName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 pSMB->SearchAttributes =
1038 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
1039 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001040 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001044 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +00001045 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001046 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 cifs_buf_release(pSMB);
1049 if (rc == -EAGAIN)
1050 goto DelFileRetry;
1051
1052 return rc;
1053}
1054
1055int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001056CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1057 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 DELETE_DIRECTORY_REQ *pSMB = NULL;
1060 DELETE_DIRECTORY_RSP *pSMBr = NULL;
1061 int rc = 0;
1062 int bytes_returned;
1063 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001064 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Joe Perchesf96637b2013-05-04 22:12:25 -05001066 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067RmDirRetry:
1068 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
1069 (void **) &pSMBr);
1070 if (rc)
1071 return rc;
1072
1073 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001074 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1075 PATH_MAX, cifs_sb->local_nls,
1076 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 name_len++; /* trailing null */
1078 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001079 } else {
1080 name_len = copy_path_name(pSMB->DirName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001084 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1086 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1087 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001088 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001089 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001090 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 cifs_buf_release(pSMB);
1093 if (rc == -EAGAIN)
1094 goto RmDirRetry;
1095 return rc;
1096}
1097
1098int
Steve Frenchc3ca78e2019-09-25 00:32:13 -05001099CIFSSMBMkDir(const unsigned int xid, struct inode *inode, umode_t mode,
1100 struct cifs_tcon *tcon, const char *name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001101 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 int rc = 0;
1104 CREATE_DIRECTORY_REQ *pSMB = NULL;
1105 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1106 int bytes_returned;
1107 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001108 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Joe Perchesf96637b2013-05-04 22:12:25 -05001110 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111MkDirRetry:
1112 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1113 (void **) &pSMBr);
1114 if (rc)
1115 return rc;
1116
1117 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06001118 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001119 PATH_MAX, cifs_sb->local_nls,
1120 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 name_len++; /* trailing null */
1122 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001123 } else {
1124 name_len = copy_path_name(pSMB->DirName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001128 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1130 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1131 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001132 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001133 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001134 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 cifs_buf_release(pSMB);
1137 if (rc == -EAGAIN)
1138 goto MkDirRetry;
1139 return rc;
1140}
1141
Steve French2dd29d32007-04-23 22:07:35 +00001142int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001143CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1144 __u32 posix_flags, __u64 mode, __u16 *netfid,
1145 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1146 const char *name, const struct nls_table *nls_codepage,
1147 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001148{
1149 TRANSACTION2_SPI_REQ *pSMB = NULL;
1150 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1151 int name_len;
1152 int rc = 0;
1153 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001154 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001155 OPEN_PSX_REQ *pdata;
1156 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001157
Joe Perchesf96637b2013-05-04 22:12:25 -05001158 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001159PsxCreat:
1160 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1161 (void **) &pSMBr);
1162 if (rc)
1163 return rc;
1164
1165 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1166 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001167 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1168 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001169 name_len++; /* trailing null */
1170 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001171 } else {
1172 name_len = copy_path_name(pSMB->FileName, name);
Steve French2dd29d32007-04-23 22:07:35 +00001173 }
1174
1175 params = 6 + name_len;
1176 count = sizeof(OPEN_PSX_REQ);
1177 pSMB->MaxParameterCount = cpu_to_le16(2);
1178 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1179 pSMB->MaxSetupCount = 0;
1180 pSMB->Reserved = 0;
1181 pSMB->Flags = 0;
1182 pSMB->Timeout = 0;
1183 pSMB->Reserved2 = 0;
1184 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001185 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001186 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001187 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001188 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001189 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001190 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001191 pdata->OpenFlags = cpu_to_le32(*pOplock);
1192 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1193 pSMB->DataOffset = cpu_to_le16(offset);
1194 pSMB->SetupCount = 1;
1195 pSMB->Reserved3 = 0;
1196 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1197 byte_count = 3 /* pad */ + params + count;
1198
1199 pSMB->DataCount = cpu_to_le16(count);
1200 pSMB->ParameterCount = cpu_to_le16(params);
1201 pSMB->TotalDataCount = pSMB->DataCount;
1202 pSMB->TotalParameterCount = pSMB->ParameterCount;
1203 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1204 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001205 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001206 pSMB->ByteCount = cpu_to_le16(byte_count);
1207 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1208 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1209 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001210 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001211 goto psx_create_err;
1212 }
1213
Joe Perchesf96637b2013-05-04 22:12:25 -05001214 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001215 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1216
Jeff Layton820a8032011-05-04 08:05:26 -04001217 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001218 rc = -EIO; /* bad smb */
1219 goto psx_create_err;
1220 }
1221
1222 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001223 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001224 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001225
Steve French2dd29d32007-04-23 22:07:35 +00001226 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001227 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001228 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1229 /* Let caller know file was created so we can set the mode. */
1230 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001231 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001232 *pOplock |= CIFS_CREATE_ACTION;
1233 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001234 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1235 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001236 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001237 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001238 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001239 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001240 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001241 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001242 goto psx_create_err;
1243 }
Steve French50c2f752007-07-13 00:33:32 +00001244 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001245 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001246 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001247 }
Steve French2dd29d32007-04-23 22:07:35 +00001248
1249psx_create_err:
1250 cifs_buf_release(pSMB);
1251
Steve French65bc98b2009-07-10 15:27:25 +00001252 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001253 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001254 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001255 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001256
1257 if (rc == -EAGAIN)
1258 goto PsxCreat;
1259
Steve French50c2f752007-07-13 00:33:32 +00001260 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001261}
1262
Steve Frencha9d02ad2005-08-24 23:06:05 -07001263static __u16 convert_disposition(int disposition)
1264{
1265 __u16 ofun = 0;
1266
1267 switch (disposition) {
1268 case FILE_SUPERSEDE:
1269 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1270 break;
1271 case FILE_OPEN:
1272 ofun = SMBOPEN_OAPPEND;
1273 break;
1274 case FILE_CREATE:
1275 ofun = SMBOPEN_OCREATE;
1276 break;
1277 case FILE_OPEN_IF:
1278 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1279 break;
1280 case FILE_OVERWRITE:
1281 ofun = SMBOPEN_OTRUNC;
1282 break;
1283 case FILE_OVERWRITE_IF:
1284 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1285 break;
1286 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001287 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001288 ofun = SMBOPEN_OAPPEND; /* regular open */
1289 }
1290 return ofun;
1291}
1292
Jeff Layton35fc37d2008-05-14 10:22:03 -07001293static int
1294access_flags_to_smbopen_mode(const int access_flags)
1295{
1296 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1297
1298 if (masked_flags == GENERIC_READ)
1299 return SMBOPEN_READ;
1300 else if (masked_flags == GENERIC_WRITE)
1301 return SMBOPEN_WRITE;
1302
1303 /* just go for read/write */
1304 return SMBOPEN_READWRITE;
1305}
1306
Steve Frencha9d02ad2005-08-24 23:06:05 -07001307int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001308SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001309 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001310 const int access_flags, const int create_options, __u16 *netfid,
1311 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001312 const struct nls_table *nls_codepage, int remap)
1313{
1314 int rc = -EACCES;
1315 OPENX_REQ *pSMB = NULL;
1316 OPENX_RSP *pSMBr = NULL;
1317 int bytes_returned;
1318 int name_len;
1319 __u16 count;
1320
1321OldOpenRetry:
1322 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1323 (void **) &pSMBr);
1324 if (rc)
1325 return rc;
1326
1327 pSMB->AndXCommand = 0xFF; /* none */
1328
1329 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1330 count = 1; /* account for one byte pad to word boundary */
1331 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001332 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1333 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001334 name_len++; /* trailing null */
1335 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001336 } else {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001337 count = 0; /* no pad */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001338 name_len = copy_path_name(pSMB->fileName, fileName);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001339 }
1340 if (*pOplock & REQ_OPLOCK)
1341 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001342 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001343 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001344
Steve Frencha9d02ad2005-08-24 23:06:05 -07001345 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001346 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001347 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1348 /* set file as system file if special file such
1349 as fifo and server expecting SFU style and
1350 no Unix extensions */
1351
Steve French790fe572007-07-07 19:25:05 +00001352 if (create_options & CREATE_OPTION_SPECIAL)
1353 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001354 else /* BB FIXME BB */
1355 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001356
Jeff Layton67750fb2008-05-09 22:28:02 +00001357 if (create_options & CREATE_OPTION_READONLY)
1358 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001359
1360 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001361/* pSMB->CreateOptions = cpu_to_le32(create_options &
1362 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001363 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001364
1365 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001366 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001367 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001368 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001369
1370 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001371 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001372 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001373 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001374 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001375 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001376 } else {
1377 /* BB verify if wct == 15 */
1378
Steve French582d21e2008-05-13 04:54:12 +00001379/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001380
1381 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1382 /* Let caller know file was created so we can set the mode. */
1383 /* Do we care about the CreateAction in any other cases? */
1384 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001385/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001386 *pOplock |= CIFS_CREATE_ACTION; */
1387 /* BB FIXME END */
1388
Steve French790fe572007-07-07 19:25:05 +00001389 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001390 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1391 pfile_info->LastAccessTime = 0; /* BB fixme */
1392 pfile_info->LastWriteTime = 0; /* BB fixme */
1393 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001394 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001395 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001396 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001397 pfile_info->AllocationSize =
1398 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1399 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001400 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001401 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001402 }
1403 }
1404
1405 cifs_buf_release(pSMB);
1406 if (rc == -EAGAIN)
1407 goto OldOpenRetry;
1408 return rc;
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001412CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1413 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Colin Ian King1afdea42019-07-23 16:09:19 +01001415 int rc;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001416 OPEN_REQ *req = NULL;
1417 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int bytes_returned;
1419 int name_len;
1420 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001421 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1422 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001423 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001424 const struct nls_table *nls = cifs_sb->local_nls;
1425 int create_options = oparms->create_options;
1426 int desired_access = oparms->desired_access;
1427 int disposition = oparms->disposition;
1428 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001431 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1432 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (rc)
1434 return rc;
1435
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001436 /* no commands go after this */
1437 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001439 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1440 /* account for one byte pad to word boundary */
1441 count = 1;
1442 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1443 path, PATH_MAX, nls, remap);
1444 /* trailing null */
1445 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001447 req->NameLength = cpu_to_le16(name_len);
1448 } else {
1449 /* BB improve check for buffer overruns BB */
1450 /* no pad */
1451 count = 0;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001452 name_len = copy_path_name(req->fileName, path);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001453 req->NameLength = cpu_to_le16(name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001455
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001456 if (*oplock & REQ_OPLOCK)
1457 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1458 else if (*oplock & REQ_BATCHOPLOCK)
1459 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1460
1461 req->DesiredAccess = cpu_to_le32(desired_access);
1462 req->AllocationSize = 0;
1463
1464 /*
1465 * Set file as system file if special file such as fifo and server
1466 * expecting SFU style and no Unix extensions.
1467 */
1468 if (create_options & CREATE_OPTION_SPECIAL)
1469 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1470 else
1471 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1472
1473 /*
1474 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1475 * sensitive checks for other servers such as Samba.
1476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001478 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jeff Layton67750fb2008-05-09 22:28:02 +00001480 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001481 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001482
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001483 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1484 req->CreateDisposition = cpu_to_le32(disposition);
1485 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1486
Steve French09d1db52005-04-28 22:41:08 -07001487 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001488 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1489 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001492 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001494 req->ByteCount = cpu_to_le16(count);
1495 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1496 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001497 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001499 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001500 cifs_buf_release(req);
1501 if (rc == -EAGAIN)
1502 goto openRetry;
1503 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001505
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001506 /* 1 byte no need to le_to_cpu */
1507 *oplock = rsp->OplockLevel;
1508 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001509 oparms->fid->netfid = rsp->Fid;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01001510 oparms->fid->access = desired_access;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001511
1512 /* Let caller know file was created so we can set the mode. */
1513 /* Do we care about the CreateAction in any other cases? */
1514 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1515 *oplock |= CIFS_CREATE_ACTION;
1516
1517 if (buf) {
1518 /* copy from CreationTime to Attributes */
1519 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1520 /* the file_info buf is endian converted by caller */
1521 buf->AllocationSize = rsp->AllocationSize;
1522 buf->EndOfFile = rsp->EndOfFile;
1523 buf->NumberOfLinks = cpu_to_le32(1);
1524 buf->DeletePending = 0;
1525 }
1526
1527 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return rc;
1529}
1530
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001531/*
1532 * Discard any remaining data in the current SMB. To do this, we borrow the
1533 * current bigbuf.
1534 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001535int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001536cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001537{
Ronnie Sahlberg05432e22018-04-09 18:06:31 +10001538 unsigned int rfclen = server->pdu_size;
1539 int remaining = rfclen + server->vals->header_preamble_size -
1540 server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001541
1542 while (remaining > 0) {
1543 int length;
1544
1545 length = cifs_read_from_socket(server, server->bigbuf,
1546 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001547 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001548 if (length < 0)
1549 return length;
1550 server->total_read += length;
1551 remaining -= length;
1552 }
1553
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001554 return 0;
1555}
1556
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001557static int
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001558__cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1559 bool malformed)
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001560{
1561 int length;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001562
Pavel Shilovsky350be252017-04-10 10:31:33 -07001563 length = cifs_discard_remaining_data(server);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001564 dequeue_mid(mid, malformed);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001565 mid->resp_buf = server->smallbuf;
1566 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001567 return length;
1568}
1569
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001570static int
1571cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1572{
1573 struct cifs_readdata *rdata = mid->callback_data;
1574
1575 return __cifs_readv_discard(server, mid, rdata->result);
1576}
1577
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001578int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001579cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1580{
1581 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001582 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001583 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001584 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10001585 unsigned int buflen = server->pdu_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001586 server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07001587 bool use_rdma_mr = false;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001588
Joe Perchesf96637b2013-05-04 22:12:25 -05001589 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1590 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001591
1592 /*
1593 * read the rest of READ_RSP header (sans Data array), or whatever we
1594 * can if there's not enough data. At this point, we've read down to
1595 * the Mid.
1596 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001597 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001598 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001599
Al Viroa6137302016-01-09 19:37:16 -05001600 length = cifs_read_from_socket(server,
1601 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001602 if (length < 0)
1603 return length;
1604 server->total_read += length;
1605
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001606 if (server->ops->is_session_expired &&
1607 server->ops->is_session_expired(buf)) {
1608 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001609 return -1;
1610 }
1611
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001612 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001613 server->ops->is_status_pending(buf, server)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001614 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001615 return -1;
1616 }
1617
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001618 /* set up first two iov for signature check and to get credits */
1619 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08001620 rdata->iov[0].iov_len = server->vals->header_preamble_size;
1621 rdata->iov[1].iov_base = buf + server->vals->header_preamble_size;
1622 rdata->iov[1].iov_len =
1623 server->total_read - server->vals->header_preamble_size;
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001624 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1625 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1626 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
1627 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
1628
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001629 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001630 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001631 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001632 cifs_dbg(FYI, "%s: server returned error %d\n",
1633 __func__, rdata->result);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001634 /* normal error on read response */
1635 return __cifs_readv_discard(server, mid, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001636 }
1637
1638 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001639 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001640 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1641 __func__, server->total_read,
1642 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001643 rdata->result = -EIO;
1644 return cifs_readv_discard(server, mid);
1645 }
1646
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001647 data_offset = server->ops->read_data_offset(buf) +
1648 server->vals->header_preamble_size;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001649 if (data_offset < server->total_read) {
1650 /*
1651 * win2k8 sometimes sends an offset of 0 when the read
1652 * is beyond the EOF. Treat it as if the data starts just after
1653 * the header.
1654 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001655 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1656 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001657 data_offset = server->total_read;
1658 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1659 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001660 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1661 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001662 rdata->result = -EIO;
1663 return cifs_readv_discard(server, mid);
1664 }
1665
Joe Perchesf96637b2013-05-04 22:12:25 -05001666 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1667 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001668
1669 len = data_offset - server->total_read;
1670 if (len > 0) {
1671 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001672 length = cifs_read_from_socket(server,
1673 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001674 if (length < 0)
1675 return length;
1676 server->total_read += length;
1677 }
1678
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001679 /* how much data is in the response? */
Long Li74dcf412017-11-22 17:38:46 -07001680#ifdef CONFIG_CIFS_SMB_DIRECT
1681 use_rdma_mr = rdata->mr;
1682#endif
1683 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1684 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001685 /* data_len is corrupt -- discard frame */
1686 rdata->result = -EIO;
1687 return cifs_readv_discard(server, mid);
1688 }
1689
Jeff Layton8321fec2012-09-19 06:22:32 -07001690 length = rdata->read_into_pages(server, rdata, data_len);
1691 if (length < 0)
1692 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001693
Jeff Layton8321fec2012-09-19 06:22:32 -07001694 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001695
Joe Perchesf96637b2013-05-04 22:12:25 -05001696 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1697 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001698
1699 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001700 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001701 return cifs_readv_discard(server, mid);
1702
1703 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001704 mid->resp_buf = server->smallbuf;
1705 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001706 return length;
1707}
1708
1709static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001710cifs_readv_callback(struct mid_q_entry *mid)
1711{
1712 struct cifs_readdata *rdata = mid->callback_data;
1713 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1714 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001715 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1716 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001717 .rq_pages = rdata->pages,
Long Li6d3adb22018-09-20 21:18:38 +00001718 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07001719 .rq_npages = rdata->nr_pages,
1720 .rq_pagesz = rdata->pagesz,
1721 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001722 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001723
Joe Perchesf96637b2013-05-04 22:12:25 -05001724 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1725 __func__, mid->mid, mid->mid_state, rdata->result,
1726 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001727
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001728 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001729 case MID_RESPONSE_RECEIVED:
1730 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001731 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001732 int rc = 0;
1733
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001734 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001735 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001736 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001737 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1738 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001739 }
1740 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001741 task_io_account_read(rdata->got_bytes);
1742 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001743 break;
1744 case MID_REQUEST_SUBMITTED:
1745 case MID_RETRY_NEEDED:
1746 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001747 if (server->sign && rdata->got_bytes)
1748 /* reset bytes number since we can not check a sign */
1749 rdata->got_bytes = 0;
1750 /* FIXME: should this be counted toward the initiating task? */
1751 task_io_account_read(rdata->got_bytes);
1752 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001753 break;
1754 default:
1755 rdata->result = -EIO;
1756 }
1757
Jeff Laytonda472fc2012-03-23 14:40:53 -04001758 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001759 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001760 add_credits(server, &credits, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001761}
1762
1763/* cifs_async_readv - send an async write, and set up mid to handle result */
1764int
1765cifs_async_readv(struct cifs_readdata *rdata)
1766{
1767 int rc;
1768 READ_REQ *smb = NULL;
1769 int wct;
1770 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001771 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1772 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001773
Joe Perchesf96637b2013-05-04 22:12:25 -05001774 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1775 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001776
1777 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1778 wct = 12;
1779 else {
1780 wct = 10; /* old style read */
1781 if ((rdata->offset >> 32) > 0) {
1782 /* can not handle this big offset for old */
1783 return -EIO;
1784 }
1785 }
1786
1787 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1788 if (rc)
1789 return rc;
1790
1791 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1792 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1793
1794 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001795 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001796 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1797 if (wct == 12)
1798 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1799 smb->Remaining = 0;
1800 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1801 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1802 if (wct == 12)
1803 smb->ByteCount = 0;
1804 else {
1805 /* old style read */
1806 struct smb_com_readx_req *smbr =
1807 (struct smb_com_readx_req *)smb;
1808 smbr->ByteCount = 0;
1809 }
1810
1811 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001812 rdata->iov[0].iov_base = smb;
1813 rdata->iov[0].iov_len = 4;
1814 rdata->iov[1].iov_base = (char *)smb + 4;
1815 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001816
Jeff Layton6993f742012-05-16 07:13:17 -04001817 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001818 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08001819 cifs_readv_callback, NULL, rdata, 0, NULL);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001820
1821 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001822 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001823 else
1824 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001825
1826 cifs_small_buf_release(smb);
1827 return rc;
1828}
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001831CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1832 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 int rc = -EACCES;
1835 READ_REQ *pSMB = NULL;
1836 READ_RSP *pSMBr = NULL;
1837 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001838 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001839 int resp_buf_type = 0;
1840 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001841 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001842 __u32 pid = io_parms->pid;
1843 __u16 netfid = io_parms->netfid;
1844 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001845 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001846 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Joe Perchesf96637b2013-05-04 22:12:25 -05001848 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001849 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001850 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001851 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001852 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001853 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001854 /* can not handle this big offset for old */
1855 return -EIO;
1856 }
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001860 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (rc)
1862 return rc;
1863
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001864 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1865 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /* tcon and ses pointer are checked in smb_init */
1868 if (tcon->ses->server == NULL)
1869 return -ECONNABORTED;
1870
Steve Frenchec637e32005-12-12 20:53:18 -08001871 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001873 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001874 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001875 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 pSMB->Remaining = 0;
1878 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1879 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001880 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001881 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1882 else {
1883 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001884 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001885 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001886 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001887 }
Steve Frenchec637e32005-12-12 20:53:18 -08001888
1889 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001890 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001891 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1892 CIFS_LOG_ERROR, &rsp_iov);
1893 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001894 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001895 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001897 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 } else {
1899 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1900 data_length = data_length << 16;
1901 data_length += le16_to_cpu(pSMBr->DataLength);
1902 *nbytes = data_length;
1903
1904 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001905 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001907 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001908 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 rc = -EIO;
1910 *nbytes = 0;
1911 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001912 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001913 le16_to_cpu(pSMBr->DataOffset);
1914/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001915 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001916 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001917 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001918 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001919 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Steve French790fe572007-07-07 19:25:05 +00001923 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001924 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001925 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001926 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001927 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001928 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001929 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001930 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001931 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001932 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001933
1934 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 since file handle passed in no longer valid */
1936 return rc;
1937}
1938
Steve Frenchec637e32005-12-12 20:53:18 -08001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001941CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001942 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 int rc = -EACCES;
1945 WRITE_REQ *pSMB = NULL;
1946 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001947 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 __u32 bytes_sent;
1949 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001950 __u32 pid = io_parms->pid;
1951 __u16 netfid = io_parms->netfid;
1952 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001953 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001954 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Steve Frencha24e2d72010-04-03 17:20:21 +00001956 *nbytes = 0;
1957
Joe Perchesf96637b2013-05-04 22:12:25 -05001958 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001959 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001960 return -ECONNABORTED;
1961
Steve French790fe572007-07-07 19:25:05 +00001962 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001963 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001964 else {
Steve French1c955182005-08-30 20:58:07 -07001965 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001966 if ((offset >> 32) > 0) {
1967 /* can not handle big offset for old srv */
1968 return -EIO;
1969 }
1970 }
Steve French1c955182005-08-30 20:58:07 -07001971
1972 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 (void **) &pSMBr);
1974 if (rc)
1975 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001976
1977 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1978 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /* tcon and ses pointer are checked in smb_init */
1981 if (tcon->ses->server == NULL)
1982 return -ECONNABORTED;
1983
1984 pSMB->AndXCommand = 0xFF; /* none */
1985 pSMB->Fid = netfid;
1986 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001987 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001988 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 pSMB->Reserved = 0xFFFFFFFF;
1991 pSMB->WriteMode = 0;
1992 pSMB->Remaining = 0;
1993
Steve French50c2f752007-07-13 00:33:32 +00001994 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 can send more if LARGE_WRITE_X capability returned by the server and if
1996 our buffer is big enough or if we convert to iovecs on socket writes
1997 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001998 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
2000 } else {
2001 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
2002 & ~0xFF;
2003 }
2004
2005 if (bytes_sent > count)
2006 bytes_sent = count;
2007 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002008 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00002009 if (buf)
Steve French61e74802008-12-03 00:57:54 +00002010 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04002011 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* No buffer */
2013 cifs_buf_release(pSMB);
2014 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07002015 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00002016 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07002017 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00002018 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07002019 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
2022 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002023 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07002024
Steve French790fe572007-07-07 19:25:05 +00002025 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07002026 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00002027 else { /* old style write has byte count 4 bytes earlier
2028 so 4 bytes pad */
2029 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07002030 (struct smb_com_writex_req *)pSMB;
2031 pSMBW->ByteCount = cpu_to_le16(byte_count);
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04002035 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002036 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002038 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 } else {
2040 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2041 *nbytes = (*nbytes) << 16;
2042 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302043
2044 /*
2045 * Mask off high 16 bits when bytes written as returned by the
2046 * server is greater than bytes requested by the client. Some
2047 * OS/2 servers are known to set incorrect CountHigh values.
2048 */
2049 if (*nbytes > count)
2050 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
2053 cifs_buf_release(pSMB);
2054
Steve French50c2f752007-07-13 00:33:32 +00002055 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 since file handle passed in no longer valid */
2057
2058 return rc;
2059}
2060
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002061void
2062cifs_writedata_release(struct kref *refcount)
2063{
2064 struct cifs_writedata *wdata = container_of(refcount,
2065 struct cifs_writedata, refcount);
Long Lidb223a52017-11-22 17:38:45 -07002066#ifdef CONFIG_CIFS_SMB_DIRECT
2067 if (wdata->mr) {
2068 smbd_deregister_mr(wdata->mr);
2069 wdata->mr = NULL;
2070 }
2071#endif
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002072
2073 if (wdata->cfile)
2074 cifsFileInfo_put(wdata->cfile);
2075
Long Li8e7360f2018-05-30 12:47:56 -07002076 kvfree(wdata->pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002077 kfree(wdata);
2078}
2079
2080/*
2081 * Write failed with a retryable error. Resend the write request. It's also
2082 * possible that the page was redirtied so re-clean the page.
2083 */
2084static void
2085cifs_writev_requeue(struct cifs_writedata *wdata)
2086{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002087 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00002088 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07002089 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002090 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002091
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002092 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2093 i = 0;
2094 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002095 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002096 struct cifs_writedata *wdata2;
2097 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002098
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002099 wsize = server->ops->wp_retry_size(inode);
2100 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002101 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002102 if (!nr_pages) {
2103 rc = -ENOTSUPP;
2104 break;
2105 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 cur_len = nr_pages * PAGE_SIZE;
2107 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002108 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002109 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002110 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06002112 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002113
2114 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2115 if (!wdata2) {
2116 rc = -ENOMEM;
2117 break;
2118 }
2119
2120 for (j = 0; j < nr_pages; j++) {
2121 wdata2->pages[j] = wdata->pages[i + j];
2122 lock_page(wdata2->pages[j]);
2123 clear_page_dirty_for_io(wdata2->pages[j]);
2124 }
2125
2126 wdata2->sync_mode = wdata->sync_mode;
2127 wdata2->nr_pages = nr_pages;
2128 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002129 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002130 wdata2->tailsz = tailsz;
2131 wdata2->bytes = cur_len;
2132
Aurelien Aptel86f740f2020-02-21 11:19:06 +01002133 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
Pavel Shilovskyfe768d52019-01-29 12:15:11 -08002134 &wdata2->cfile);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002135 if (!wdata2->cfile) {
Pavel Shilovskyfe768d52019-01-29 12:15:11 -08002136 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2137 rc);
2138 if (!is_retryable_error(rc))
2139 rc = -EBADF;
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002140 } else {
2141 wdata2->pid = wdata2->cfile->pid;
2142 rc = server->ops->async_writev(wdata2,
2143 cifs_writedata_release);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002144 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002145
2146 for (j = 0; j < nr_pages; j++) {
2147 unlock_page(wdata2->pages[j]);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002148 if (rc != 0 && !is_retryable_error(rc)) {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002149 SetPageError(wdata2->pages[j]);
2150 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002151 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002152 }
2153 }
2154
Adam McCoya4813792020-05-13 11:53:30 +00002155 kref_put(&wdata2->refcount, cifs_writedata_release);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002156 if (rc) {
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002157 if (is_retryable_error(rc))
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002158 continue;
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002159 i += nr_pages;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002160 break;
2161 }
2162
2163 rest_len -= cur_len;
2164 i += nr_pages;
2165 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002166
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002167 /* cleanup remaining pages from the original wdata */
2168 for (; i < wdata->nr_pages; i++) {
2169 SetPageError(wdata->pages[i]);
2170 end_page_writeback(wdata->pages[i]);
2171 put_page(wdata->pages[i]);
2172 }
2173
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002174 if (rc != 0 && !is_retryable_error(rc))
2175 mapping_set_error(inode->i_mapping, rc);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002176 kref_put(&wdata->refcount, cifs_writedata_release);
2177}
2178
Jeff Laytonc2e87642012-03-23 14:40:55 -04002179void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002180cifs_writev_complete(struct work_struct *work)
2181{
2182 struct cifs_writedata *wdata = container_of(work,
2183 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002184 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002185 int i = 0;
2186
2187 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002188 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002189 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002190 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002191 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2192 wdata->bytes);
2193 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2194 return cifs_writev_requeue(wdata);
2195
2196 for (i = 0; i < wdata->nr_pages; i++) {
2197 struct page *page = wdata->pages[i];
2198 if (wdata->result == -EAGAIN)
2199 __set_page_dirty_nobuffers(page);
2200 else if (wdata->result < 0)
2201 SetPageError(page);
2202 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002203 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002204 }
2205 if (wdata->result != -EAGAIN)
2206 mapping_set_error(inode->i_mapping, wdata->result);
2207 kref_put(&wdata->refcount, cifs_writedata_release);
2208}
2209
2210struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002211cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002212{
Long Li8e7360f2018-05-30 12:47:56 -07002213 struct page **pages =
Kees Cook6396bb22018-06-12 14:03:40 -07002214 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Long Li8e7360f2018-05-30 12:47:56 -07002215 if (pages)
2216 return cifs_writedata_direct_alloc(pages, complete);
2217
2218 return NULL;
2219}
2220
2221struct cifs_writedata *
2222cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2223{
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002224 struct cifs_writedata *wdata;
2225
Long Li8e7360f2018-05-30 12:47:56 -07002226 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002227 if (wdata != NULL) {
Long Li8e7360f2018-05-30 12:47:56 -07002228 wdata->pages = pages;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002229 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002230 INIT_LIST_HEAD(&wdata->list);
2231 init_completion(&wdata->done);
2232 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002233 }
2234 return wdata;
2235}
2236
2237/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002238 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002239 * workqueue completion task.
2240 */
2241static void
2242cifs_writev_callback(struct mid_q_entry *mid)
2243{
2244 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002245 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002246 unsigned int written;
2247 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08002248 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002249
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002250 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002251 case MID_RESPONSE_RECEIVED:
2252 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2253 if (wdata->result != 0)
2254 break;
2255
2256 written = le16_to_cpu(smb->CountHigh);
2257 written <<= 16;
2258 written += le16_to_cpu(smb->Count);
2259 /*
2260 * Mask off high 16 bits when bytes written as returned
2261 * by the server is greater than bytes requested by the
2262 * client. OS/2 servers are known to set incorrect
2263 * CountHigh values.
2264 */
2265 if (written > wdata->bytes)
2266 written &= 0xFFFF;
2267
2268 if (written < wdata->bytes)
2269 wdata->result = -ENOSPC;
2270 else
2271 wdata->bytes = written;
2272 break;
2273 case MID_REQUEST_SUBMITTED:
2274 case MID_RETRY_NEEDED:
2275 wdata->result = -EAGAIN;
2276 break;
2277 default:
2278 wdata->result = -EIO;
2279 break;
2280 }
2281
Jeff Laytonda472fc2012-03-23 14:40:53 -04002282 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002283 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08002284 add_credits(tcon->ses->server, &credits, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002285}
2286
2287/* cifs_async_writev - send an async write, and set up mid to handle result */
2288int
Steve French4a5c80d2014-02-07 20:45:12 -06002289cifs_async_writev(struct cifs_writedata *wdata,
2290 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002291{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002292 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002293 WRITE_REQ *smb = NULL;
2294 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002295 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002296 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002297 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002298
2299 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2300 wct = 14;
2301 } else {
2302 wct = 12;
2303 if (wdata->offset >> 32 > 0) {
2304 /* can not handle big offset for old srv */
2305 return -EIO;
2306 }
2307 }
2308
2309 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2310 if (rc)
2311 goto async_writev_out;
2312
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002313 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2314 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002315
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002316 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002317 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002318 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2319 if (wct == 14)
2320 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2321 smb->Reserved = 0xFFFFFFFF;
2322 smb->WriteMode = 0;
2323 smb->Remaining = 0;
2324
2325 smb->DataOffset =
2326 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2327
2328 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002329 iov[0].iov_len = 4;
2330 iov[0].iov_base = smb;
2331 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2332 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002333
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002334 rqst.rq_iov = iov;
2335 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002336 rqst.rq_pages = wdata->pages;
Long Li6d3adb22018-09-20 21:18:38 +00002337 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002338 rqst.rq_npages = wdata->nr_pages;
2339 rqst.rq_pagesz = wdata->pagesz;
2340 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002341
Joe Perchesf96637b2013-05-04 22:12:25 -05002342 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2343 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002344
2345 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2346 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2347
2348 if (wct == 14) {
2349 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2350 put_bcc(wdata->bytes + 1, &smb->hdr);
2351 } else {
2352 /* wct == 12 */
2353 struct smb_com_writex_req *smbw =
2354 (struct smb_com_writex_req *)smb;
2355 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2356 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002357 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002358 }
2359
2360 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002361 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08002362 cifs_writev_callback, NULL, wdata, 0, NULL);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002363
2364 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002365 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002366 else
Steve French4a5c80d2014-02-07 20:45:12 -06002367 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002368
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002369async_writev_out:
2370 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002371 return rc;
2372}
2373
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002374int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002375CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002376 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Colin Ian King136a5dc2020-05-27 13:50:31 +01002378 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002380 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002381 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002382 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002383 __u32 pid = io_parms->pid;
2384 __u16 netfid = io_parms->netfid;
2385 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002386 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002387 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002388 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002390 *nbytes = 0;
2391
Joe Perchesf96637b2013-05-04 22:12:25 -05002392 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002393
Steve French4c3130e2008-12-09 00:28:16 +00002394 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002395 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002396 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002397 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002398 if ((offset >> 32) > 0) {
2399 /* can not handle big offset for old srv */
2400 return -EIO;
2401 }
2402 }
Steve French8cc64c62005-10-03 13:49:43 -07002403 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (rc)
2405 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002406
2407 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2408 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 /* tcon and ses pointer are checked in smb_init */
2411 if (tcon->ses->server == NULL)
2412 return -ECONNABORTED;
2413
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002414 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 pSMB->Fid = netfid;
2416 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002417 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002418 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 pSMB->Reserved = 0xFFFFFFFF;
2420 pSMB->WriteMode = 0;
2421 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002424 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Steve French3e844692005-10-03 13:37:24 -07002426 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2427 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002428 /* header + 1 byte pad */
2429 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002430 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002431 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002432 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002433 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002434 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002435 pSMB->ByteCount = cpu_to_le16(count + 1);
2436 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002437 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002438 (struct smb_com_writex_req *)pSMB;
2439 pSMBW->ByteCount = cpu_to_le16(count + 5);
2440 }
Steve French3e844692005-10-03 13:37:24 -07002441 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002442 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002443 iov[0].iov_len = smb_hdr_len + 4;
2444 else /* wct == 12 pad bigger by four bytes */
2445 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002446
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002447 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2448 &rsp_iov);
2449 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002450 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002452 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002453 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002454 /* presumably this can not happen, but best to be safe */
2455 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002456 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002457 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002458 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2459 *nbytes = (*nbytes) << 16;
2460 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302461
2462 /*
2463 * Mask off high 16 bits when bytes written as returned by the
2464 * server is greater than bytes requested by the client. OS/2
2465 * servers are known to set incorrect CountHigh values.
2466 */
2467 if (*nbytes > count)
2468 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002471 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Steve French50c2f752007-07-13 00:33:32 +00002473 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 since file handle passed in no longer valid */
2475
2476 return rc;
2477}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002478
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002479int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2480 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002481 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2482{
2483 int rc = 0;
2484 LOCK_REQ *pSMB = NULL;
2485 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002486 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002487 int resp_buf_type;
2488 __u16 count;
2489
Joe Perchesf96637b2013-05-04 22:12:25 -05002490 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2491 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002492
2493 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2494 if (rc)
2495 return rc;
2496
2497 pSMB->Timeout = 0;
2498 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2499 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2500 pSMB->LockType = lock_type;
2501 pSMB->AndXCommand = 0xFF; /* none */
2502 pSMB->Fid = netfid; /* netfid stays le */
2503
2504 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2505 inc_rfc1001_len(pSMB, count);
2506 pSMB->ByteCount = cpu_to_le16(count);
2507
2508 iov[0].iov_base = (char *)pSMB;
2509 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2510 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2511 iov[1].iov_base = (char *)buf;
2512 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2513
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002514 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10002515 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type,
2516 CIFS_NO_RSP_BUF, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002517 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002518 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002519 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002520
2521 return rc;
2522}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002525CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002526 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002528 const __u32 numLock, const __u8 lockType,
2529 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 int rc = 0;
2532 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002533/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002535 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 __u16 count;
2537
Joe Perchesf96637b2013-05-04 22:12:25 -05002538 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2539 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002540 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (rc)
2543 return rc;
2544
Steve French790fe572007-07-07 19:25:05 +00002545 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002546 /* no response expected */
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10002547 flags = CIFS_NO_SRV_RSP | CIFS_NON_BLOCKING | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002549 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002550 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2552 } else {
2553 pSMB->Timeout = 0;
2554 }
2555
2556 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2557 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2558 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002559 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 pSMB->AndXCommand = 0xFF; /* none */
2561 pSMB->Fid = smb_file_id; /* netfid stays le */
2562
Steve French790fe572007-07-07 19:25:05 +00002563 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002564 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /* BB where to store pid high? */
2566 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2567 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2568 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2569 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2570 count = sizeof(LOCKING_ANDX_RANGE);
2571 } else {
2572 /* oplock break */
2573 count = 0;
2574 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002575 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 pSMB->ByteCount = cpu_to_le16(count);
2577
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002578 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002579 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002580 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002581 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002582 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002583 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002584 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002585 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002586 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Steve French50c2f752007-07-13 00:33:32 +00002588 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 since file handle passed in no longer valid */
2590 return rc;
2591}
2592
2593int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002594CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002595 const __u16 smb_file_id, const __u32 netpid,
2596 const loff_t start_offset, const __u64 len,
2597 struct file_lock *pLockData, const __u16 lock_type,
2598 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002599{
2600 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2601 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002602 struct cifs_posix_lock *parm_data;
2603 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002604 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002605 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002606 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002607 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002608 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002609 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002610
Joe Perchesf96637b2013-05-04 22:12:25 -05002611 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002612
Steve French08547b02006-02-28 22:39:25 +00002613 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2614
2615 if (rc)
2616 return rc;
2617
2618 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2619
Steve French50c2f752007-07-13 00:33:32 +00002620 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002621 pSMB->MaxSetupCount = 0;
2622 pSMB->Reserved = 0;
2623 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002624 pSMB->Reserved2 = 0;
2625 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2626 offset = param_offset + params;
2627
Steve French08547b02006-02-28 22:39:25 +00002628 count = sizeof(struct cifs_posix_lock);
2629 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002630 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002631 pSMB->SetupCount = 1;
2632 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002633 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002634 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2635 else
2636 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2637 byte_count = 3 /* pad */ + params + count;
2638 pSMB->DataCount = cpu_to_le16(count);
2639 pSMB->ParameterCount = cpu_to_le16(params);
2640 pSMB->TotalDataCount = pSMB->DataCount;
2641 pSMB->TotalParameterCount = pSMB->ParameterCount;
2642 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002643 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002644 (((char *) &pSMB->hdr.Protocol) + offset);
2645
2646 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002647 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002648 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002649 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002650 pSMB->Timeout = cpu_to_le32(-1);
2651 } else
2652 pSMB->Timeout = 0;
2653
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002654 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002655 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002656 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002657
2658 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002659 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002660 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2661 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002662 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002663 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002664 if (waitFlag) {
2665 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2666 (struct smb_hdr *) pSMBr, &bytes_returned);
2667 } else {
Steve French133672e2007-11-13 22:41:37 +00002668 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002669 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002670 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002671 &resp_buf_type, timeout, &rsp_iov);
2672 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002673 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002674 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002675
Steve French08547b02006-02-28 22:39:25 +00002676 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002677 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002678 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002679 /* lock structure can be returned on get */
2680 __u16 data_offset;
2681 __u16 data_count;
2682 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002683
Jeff Layton820a8032011-05-04 08:05:26 -04002684 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002685 rc = -EIO; /* bad smb */
2686 goto plk_err_exit;
2687 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002688 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2689 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002690 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002691 rc = -EIO;
2692 goto plk_err_exit;
2693 }
2694 parm_data = (struct cifs_posix_lock *)
2695 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002696 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002697 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002698 else {
2699 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002700 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002701 pLockData->fl_type = F_RDLCK;
2702 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002703 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002704 pLockData->fl_type = F_WRLCK;
2705
Steve French5443d132011-03-13 05:08:25 +00002706 pLockData->fl_start = le64_to_cpu(parm_data->start);
2707 pLockData->fl_end = pLockData->fl_start +
2708 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002709 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002710 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002711 }
Steve French50c2f752007-07-13 00:33:32 +00002712
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002713plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002714 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002715
Steve French08547b02006-02-28 22:39:25 +00002716 /* Note: On -EAGAIN error only caller can retry on handle based calls
2717 since file handle passed in no longer valid */
2718
2719 return rc;
2720}
2721
2722
2723int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002724CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 int rc = 0;
2727 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002728 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730/* do not retry on dead session on close */
2731 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002732 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return 0;
2734 if (rc)
2735 return rc;
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002738 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002740 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002741 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002742 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002744 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002746 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748 }
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002751 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 rc = 0;
2753
2754 return rc;
2755}
2756
2757int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002758CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002759{
2760 int rc = 0;
2761 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002762 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002763
2764 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2765 if (rc)
2766 return rc;
2767
2768 pSMB->FileID = (__u16) smb_file_id;
2769 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002770 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002771 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002772 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002773 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002774 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002775
2776 return rc;
2777}
2778
2779int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002780CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002781 const char *from_name, const char *to_name,
2782 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 int rc = 0;
2785 RENAME_REQ *pSMB = NULL;
2786 RENAME_RSP *pSMBr = NULL;
2787 int bytes_returned;
2788 int name_len, name_len2;
2789 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002790 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Joe Perchesf96637b2013-05-04 22:12:25 -05002792 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793renameRetry:
2794 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2795 (void **) &pSMBr);
2796 if (rc)
2797 return rc;
2798
2799 pSMB->BufferFormat = 0x04;
2800 pSMB->SearchAttributes =
2801 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2802 ATTR_DIRECTORY);
2803
2804 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002805 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2806 from_name, PATH_MAX,
2807 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 name_len++; /* trailing null */
2809 name_len *= 2;
2810 pSMB->OldFileName[name_len] = 0x04; /* pad */
2811 /* protocol requires ASCII signature byte on Unicode string */
2812 pSMB->OldFileName[name_len + 1] = 0x00;
2813 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002814 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002815 to_name, PATH_MAX, cifs_sb->local_nls,
2816 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2818 name_len2 *= 2; /* convert to bytes */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002819 } else {
2820 name_len = copy_path_name(pSMB->OldFileName, from_name);
2821 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 name_len2++; /* signature byte */
2824 }
2825
2826 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002827 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 pSMB->ByteCount = cpu_to_le16(count);
2829
2830 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2831 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002832 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002833 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002834 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 cifs_buf_release(pSMB);
2837
2838 if (rc == -EAGAIN)
2839 goto renameRetry;
2840
2841 return rc;
2842}
2843
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002844int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002845 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002846 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2849 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002850 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 char *data_offset;
2852 char dummy_string[30];
2853 int rc = 0;
2854 int bytes_returned = 0;
2855 int len_of_str;
2856 __u16 params, param_offset, offset, count, byte_count;
2857
Joe Perchesf96637b2013-05-04 22:12:25 -05002858 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2860 (void **) &pSMBr);
2861 if (rc)
2862 return rc;
2863
2864 params = 6;
2865 pSMB->MaxSetupCount = 0;
2866 pSMB->Reserved = 0;
2867 pSMB->Flags = 0;
2868 pSMB->Timeout = 0;
2869 pSMB->Reserved2 = 0;
2870 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2871 offset = param_offset + params;
2872
2873 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2874 rename_info = (struct set_file_rename *) data_offset;
2875 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002876 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 pSMB->SetupCount = 1;
2878 pSMB->Reserved3 = 0;
2879 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2880 byte_count = 3 /* pad */ + params;
2881 pSMB->ParameterCount = cpu_to_le16(params);
2882 pSMB->TotalParameterCount = pSMB->ParameterCount;
2883 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2884 pSMB->DataOffset = cpu_to_le16(offset);
2885 /* construct random name ".cifs_tmp<inodenum><mid>" */
2886 rename_info->overwrite = cpu_to_le32(1);
2887 rename_info->root_fid = 0;
2888 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002889 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002890 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002891 len_of_str =
2892 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002893 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002895 len_of_str =
2896 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002897 target_name, PATH_MAX, nls_codepage,
2898 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002901 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 byte_count += count;
2903 pSMB->DataCount = cpu_to_le16(count);
2904 pSMB->TotalDataCount = pSMB->DataCount;
2905 pSMB->Fid = netfid;
2906 pSMB->InformationLevel =
2907 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2908 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002909 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 pSMB->ByteCount = cpu_to_le16(byte_count);
2911 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002912 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002913 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002914 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002915 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2916 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 cifs_buf_release(pSMB);
2919
2920 /* Note: On -EAGAIN error only caller can retry on handle based calls
2921 since file handle passed in no longer valid */
2922
2923 return rc;
2924}
2925
2926int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002927CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2928 const char *fromName, const __u16 target_tid, const char *toName,
2929 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
2931 int rc = 0;
2932 COPY_REQ *pSMB = NULL;
2933 COPY_RSP *pSMBr = NULL;
2934 int bytes_returned;
2935 int name_len, name_len2;
2936 __u16 count;
2937
Joe Perchesf96637b2013-05-04 22:12:25 -05002938 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939copyRetry:
2940 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2941 (void **) &pSMBr);
2942 if (rc)
2943 return rc;
2944
2945 pSMB->BufferFormat = 0x04;
2946 pSMB->Tid2 = target_tid;
2947
2948 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2949
2950 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002951 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2952 fromName, PATH_MAX, nls_codepage,
2953 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 name_len++; /* trailing null */
2955 name_len *= 2;
2956 pSMB->OldFileName[name_len] = 0x04; /* pad */
2957 /* protocol requires ASCII signature byte on Unicode string */
2958 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002959 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002960 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2961 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2963 name_len2 *= 2; /* convert to bytes */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002964 } else {
2965 name_len = copy_path_name(pSMB->OldFileName, fromName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002967 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 name_len2++; /* signature byte */
2969 }
2970
2971 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002972 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 pSMB->ByteCount = cpu_to_le16(count);
2974
2975 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2976 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2977 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002978 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2979 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
Steve French0d817bc2008-05-22 02:02:03 +00002981 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 if (rc == -EAGAIN)
2984 goto copyRetry;
2985
2986 return rc;
2987}
2988
2989int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002990CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002992 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
2994 TRANSACTION2_SPI_REQ *pSMB = NULL;
2995 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2996 char *data_offset;
2997 int name_len;
2998 int name_len_target;
2999 int rc = 0;
3000 int bytes_returned = 0;
3001 __u16 params, param_offset, offset, byte_count;
3002
Joe Perchesf96637b2013-05-04 22:12:25 -05003003 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004createSymLinkRetry:
3005 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3006 (void **) &pSMBr);
3007 if (rc)
3008 return rc;
3009
3010 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3011 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003012 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
3013 /* find define for this maxpathcomponent */
3014 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 name_len++; /* trailing null */
3016 name_len *= 2;
3017
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003018 } else {
3019 name_len = copy_path_name(pSMB->FileName, fromName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021 params = 6 + name_len;
3022 pSMB->MaxSetupCount = 0;
3023 pSMB->Reserved = 0;
3024 pSMB->Flags = 0;
3025 pSMB->Timeout = 0;
3026 pSMB->Reserved2 = 0;
3027 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003028 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 offset = param_offset + params;
3030
3031 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3032 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3033 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003034 cifsConvertToUTF16((__le16 *) data_offset, toName,
3035 /* find define for this maxpathcomponent */
3036 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 name_len_target++; /* trailing null */
3038 name_len_target *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003039 } else {
3040 name_len_target = copy_path_name(data_offset, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042
3043 pSMB->MaxParameterCount = cpu_to_le16(2);
3044 /* BB find exact max on data count below from sess */
3045 pSMB->MaxDataCount = cpu_to_le16(1000);
3046 pSMB->SetupCount = 1;
3047 pSMB->Reserved3 = 0;
3048 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3049 byte_count = 3 /* pad */ + params + name_len_target;
3050 pSMB->DataCount = cpu_to_le16(name_len_target);
3051 pSMB->ParameterCount = cpu_to_le16(params);
3052 pSMB->TotalDataCount = pSMB->DataCount;
3053 pSMB->TotalParameterCount = pSMB->ParameterCount;
3054 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3055 pSMB->DataOffset = cpu_to_le16(offset);
3056 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
3057 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003058 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 pSMB->ByteCount = cpu_to_le16(byte_count);
3060 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3061 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003062 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003063 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003064 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
3065 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Steve French0d817bc2008-05-22 02:02:03 +00003067 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 if (rc == -EAGAIN)
3070 goto createSymLinkRetry;
3071
3072 return rc;
3073}
3074
3075int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003076CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003078 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 TRANSACTION2_SPI_REQ *pSMB = NULL;
3081 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3082 char *data_offset;
3083 int name_len;
3084 int name_len_target;
3085 int rc = 0;
3086 int bytes_returned = 0;
3087 __u16 params, param_offset, offset, byte_count;
3088
Joe Perchesf96637b2013-05-04 22:12:25 -05003089 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090createHardLinkRetry:
3091 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3092 (void **) &pSMBr);
3093 if (rc)
3094 return rc;
3095
3096 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06003097 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
3098 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 name_len++; /* trailing null */
3100 name_len *= 2;
3101
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003102 } else {
3103 name_len = copy_path_name(pSMB->FileName, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105 params = 6 + name_len;
3106 pSMB->MaxSetupCount = 0;
3107 pSMB->Reserved = 0;
3108 pSMB->Flags = 0;
3109 pSMB->Timeout = 0;
3110 pSMB->Reserved2 = 0;
3111 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003112 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 offset = param_offset + params;
3114
3115 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3116 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3117 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06003118 cifsConvertToUTF16((__le16 *) data_offset, fromName,
3119 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 name_len_target++; /* trailing null */
3121 name_len_target *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003122 } else {
3123 name_len_target = copy_path_name(data_offset, fromName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
3125
3126 pSMB->MaxParameterCount = cpu_to_le16(2);
3127 /* BB find exact max on data count below from sess*/
3128 pSMB->MaxDataCount = cpu_to_le16(1000);
3129 pSMB->SetupCount = 1;
3130 pSMB->Reserved3 = 0;
3131 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3132 byte_count = 3 /* pad */ + params + name_len_target;
3133 pSMB->ParameterCount = cpu_to_le16(params);
3134 pSMB->TotalParameterCount = pSMB->ParameterCount;
3135 pSMB->DataCount = cpu_to_le16(name_len_target);
3136 pSMB->TotalDataCount = pSMB->DataCount;
3137 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3138 pSMB->DataOffset = cpu_to_le16(offset);
3139 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3140 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003141 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 pSMB->ByteCount = cpu_to_le16(byte_count);
3143 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3144 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003145 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003146 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003147 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3148 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150 cifs_buf_release(pSMB);
3151 if (rc == -EAGAIN)
3152 goto createHardLinkRetry;
3153
3154 return rc;
3155}
3156
3157int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003158CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07003159 const char *from_name, const char *to_name,
3160 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 int rc = 0;
3163 NT_RENAME_REQ *pSMB = NULL;
3164 RENAME_RSP *pSMBr = NULL;
3165 int bytes_returned;
3166 int name_len, name_len2;
3167 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003168 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Joe Perchesf96637b2013-05-04 22:12:25 -05003170 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171winCreateHardLinkRetry:
3172
3173 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3174 (void **) &pSMBr);
3175 if (rc)
3176 return rc;
3177
3178 pSMB->SearchAttributes =
3179 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3180 ATTR_DIRECTORY);
3181 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3182 pSMB->ClusterCount = 0;
3183
3184 pSMB->BufferFormat = 0x04;
3185
3186 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3187 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003188 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3189 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 name_len++; /* trailing null */
3191 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003192
3193 /* protocol specifies ASCII buffer format (0x04) for unicode */
3194 pSMB->OldFileName[name_len] = 0x04;
3195 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003197 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003198 to_name, PATH_MAX, cifs_sb->local_nls,
3199 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3201 name_len2 *= 2; /* convert to bytes */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003202 } else {
3203 name_len = copy_path_name(pSMB->OldFileName, from_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003205 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 name_len2++; /* signature byte */
3207 }
3208
3209 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003210 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 pSMB->ByteCount = cpu_to_le16(count);
3212
3213 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3214 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003215 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003216 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003217 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 cifs_buf_release(pSMB);
3220 if (rc == -EAGAIN)
3221 goto winCreateHardLinkRetry;
3222
3223 return rc;
3224}
3225
3226int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003227CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003228 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003229 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231/* SMB_QUERY_FILE_UNIX_LINK */
3232 TRANSACTION2_QPI_REQ *pSMB = NULL;
3233 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3234 int rc = 0;
3235 int bytes_returned;
3236 int name_len;
3237 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003238 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Joe Perchesf96637b2013-05-04 22:12:25 -05003240 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242querySymLinkRetry:
3243 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3244 (void **) &pSMBr);
3245 if (rc)
3246 return rc;
3247
3248 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3249 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003250 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3251 searchName, PATH_MAX, nls_codepage,
3252 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 name_len++; /* trailing null */
3254 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003255 } else {
3256 name_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258
3259 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3260 pSMB->TotalDataCount = 0;
3261 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003262 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 pSMB->MaxSetupCount = 0;
3264 pSMB->Reserved = 0;
3265 pSMB->Flags = 0;
3266 pSMB->Timeout = 0;
3267 pSMB->Reserved2 = 0;
3268 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003269 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 pSMB->DataCount = 0;
3271 pSMB->DataOffset = 0;
3272 pSMB->SetupCount = 1;
3273 pSMB->Reserved3 = 0;
3274 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3275 byte_count = params + 1 /* pad */ ;
3276 pSMB->TotalParameterCount = cpu_to_le16(params);
3277 pSMB->ParameterCount = pSMB->TotalParameterCount;
3278 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3279 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003280 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 pSMB->ByteCount = cpu_to_le16(byte_count);
3282
3283 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3284 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3285 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003286 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 } else {
3288 /* decode response */
3289
3290 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003292 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003293 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003295 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003296 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Jeff Layton460b9692009-04-30 07:17:56 -04003298 data_start = ((char *) &pSMBr->hdr.Protocol) +
3299 le16_to_cpu(pSMBr->t2.DataOffset);
3300
Steve French0e0d2cf2009-05-01 05:27:32 +00003301 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3302 is_unicode = true;
3303 else
3304 is_unicode = false;
3305
Steve French737b7582005-04-28 22:41:06 -07003306 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003307 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3308 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003309 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003310 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312 }
3313 cifs_buf_release(pSMB);
3314 if (rc == -EAGAIN)
3315 goto querySymLinkRetry;
3316 return rc;
3317}
3318
Steve Frenchc52a95542011-02-24 06:16:22 +00003319/*
3320 * Recent Windows versions now create symlinks more frequently
3321 * and they use the "reparse point" mechanism below. We can of course
3322 * do symlinks nicely to Samba and other servers which support the
3323 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3324 * "MF" symlinks optionally, but for recent Windows we really need to
3325 * reenable the code below and fix the cifs_symlink callers to handle this.
3326 * In the interim this code has been moved to its own config option so
3327 * it is not compiled in by default until callers fixed up and more tested.
3328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003330CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3331 __u16 fid, char **symlinkinfo,
3332 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
3334 int rc = 0;
3335 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003336 struct smb_com_transaction_ioctl_req *pSMB;
3337 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003338 bool is_unicode;
3339 unsigned int sub_len;
3340 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003341 struct reparse_symlink_data *reparse_buf;
3342 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003343 __u32 data_offset, data_count;
3344 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003346 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3348 (void **) &pSMBr);
3349 if (rc)
3350 return rc;
3351
3352 pSMB->TotalParameterCount = 0 ;
3353 pSMB->TotalDataCount = 0;
3354 pSMB->MaxParameterCount = cpu_to_le32(2);
3355 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003356 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 pSMB->MaxSetupCount = 4;
3358 pSMB->Reserved = 0;
3359 pSMB->ParameterOffset = 0;
3360 pSMB->DataCount = 0;
3361 pSMB->DataOffset = 0;
3362 pSMB->SetupCount = 4;
3363 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3364 pSMB->ParameterCount = pSMB->TotalParameterCount;
3365 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3366 pSMB->IsFsctl = 1; /* FSCTL */
3367 pSMB->IsRootFlag = 0;
3368 pSMB->Fid = fid; /* file handle always le */
3369 pSMB->ByteCount = 0;
3370
3371 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3372 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3373 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003374 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003375 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
Steve French989c7e52009-05-02 05:32:20 +00003377
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003378 data_offset = le32_to_cpu(pSMBr->DataOffset);
3379 data_count = le32_to_cpu(pSMBr->DataCount);
3380 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3381 /* BB also check enough total bytes returned */
3382 rc = -EIO; /* bad smb */
3383 goto qreparse_out;
3384 }
3385 if (!data_count || (data_count > 2048)) {
3386 rc = -EIO;
3387 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3388 goto qreparse_out;
3389 }
3390 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003391 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003392 ((char *)&pSMBr->hdr.Protocol + data_offset);
3393 if ((char *)reparse_buf >= end_of_smb) {
3394 rc = -EIO;
3395 goto qreparse_out;
3396 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003397 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3398 cifs_dbg(FYI, "NFS style reparse tag\n");
3399 posix_buf = (struct reparse_posix_data *)reparse_buf;
3400
3401 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3402 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3403 le64_to_cpu(posix_buf->InodeType));
3404 rc = -EOPNOTSUPP;
3405 goto qreparse_out;
3406 }
3407 is_unicode = true;
3408 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3409 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3410 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3411 rc = -EIO;
3412 goto qreparse_out;
3413 }
3414 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3415 sub_len, is_unicode, nls_codepage);
3416 goto qreparse_out;
3417 } else if (reparse_buf->ReparseTag !=
3418 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3419 rc = -EOPNOTSUPP;
3420 goto qreparse_out;
3421 }
3422
3423 /* Reparse tag is NTFS symlink */
3424 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3425 reparse_buf->PathBuffer;
3426 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3427 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003428 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3429 rc = -EIO;
3430 goto qreparse_out;
3431 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003432 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3433 is_unicode = true;
3434 else
3435 is_unicode = false;
3436
3437 /* BB FIXME investigate remapping reserved chars here */
3438 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3439 nls_codepage);
3440 if (!*symlinkinfo)
3441 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003443 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003445 /*
3446 * Note: On -EAGAIN error only caller can retry on handle based calls
3447 * since file handle passed in no longer valid.
3448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 return rc;
3450}
3451
Steve Frenchc7f508a2013-10-14 15:27:32 -05003452int
3453CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3454 __u16 fid)
3455{
3456 int rc = 0;
3457 int bytes_returned;
3458 struct smb_com_transaction_compr_ioctl_req *pSMB;
3459 struct smb_com_transaction_ioctl_rsp *pSMBr;
3460
3461 cifs_dbg(FYI, "Set compression for %u\n", fid);
3462 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3463 (void **) &pSMBr);
3464 if (rc)
3465 return rc;
3466
3467 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3468
3469 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003470 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003471 pSMB->MaxParameterCount = 0;
3472 pSMB->MaxDataCount = 0;
3473 pSMB->MaxSetupCount = 4;
3474 pSMB->Reserved = 0;
3475 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003476 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003477 pSMB->DataOffset =
3478 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3479 compression_state) - 4); /* 84 */
3480 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003481 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003482 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003483 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003484 pSMB->IsFsctl = 1; /* FSCTL */
3485 pSMB->IsRootFlag = 0;
3486 pSMB->Fid = fid; /* file handle always le */
3487 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003488 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003489 inc_rfc1001_len(pSMB, 5);
3490
3491 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3492 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3493 if (rc)
3494 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3495
3496 cifs_buf_release(pSMB);
3497
3498 /*
3499 * Note: On -EAGAIN error only caller can retry on handle based calls
3500 * since file handle passed in no longer valid.
3501 */
3502 return rc;
3503}
3504
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506#ifdef CONFIG_CIFS_POSIX
3507
3508/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003509static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003510 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
3512 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003513 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3514 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3515 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003516/*
3517 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3518 ace->e_perm, ace->e_tag, ace->e_id);
3519*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 return;
3522}
3523
3524/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003525static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3526 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
3528 int size = 0;
3529 int i;
3530 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003531 struct cifs_posix_ace *pACE;
3532 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003533 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
3535 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3536 return -EOPNOTSUPP;
3537
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003538 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 count = le16_to_cpu(cifs_acl->access_entry_count);
3540 pACE = &cifs_acl->ace_array[0];
3541 size = sizeof(struct cifs_posix_acl);
3542 size += sizeof(struct cifs_posix_ace) * count;
3543 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003544 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003545 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3546 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 return -EINVAL;
3548 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003549 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 count = le16_to_cpu(cifs_acl->access_entry_count);
3551 size = sizeof(struct cifs_posix_acl);
3552 size += sizeof(struct cifs_posix_ace) * count;
3553/* skip past access ACEs to get to default ACEs */
3554 pACE = &cifs_acl->ace_array[count];
3555 count = le16_to_cpu(cifs_acl->default_entry_count);
3556 size += sizeof(struct cifs_posix_ace) * count;
3557 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003558 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return -EINVAL;
3560 } else {
3561 /* illegal type */
3562 return -EINVAL;
3563 }
3564
3565 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003566 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003567 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003568 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return -ERANGE;
3570 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003571 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3572
Steve Frenchff7feac2005-11-15 16:45:16 -08003573 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003574 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003575 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003576 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578 }
3579 return size;
3580}
3581
Hariprasad Kelam0aa3a242019-07-02 23:50:02 +05303582static void convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003583 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Steve Frenchff7feac2005-11-15 16:45:16 -08003585 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3586 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003588 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 /* Probably no need to le convert -1 on any arch but can not hurt */
3590 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003591 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003592 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003593/*
3594 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3595 ace->e_perm, ace->e_tag, ace->e_id);
3596*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597}
3598
3599/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003600static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3601 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003604 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003605 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003606 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 int count;
3608 int i;
3609
Steve French790fe572007-07-07 19:25:05 +00003610 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 return 0;
3612
3613 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003614 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3615 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003616 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003617 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3618 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return 0;
3620 }
3621 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003622 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003623 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003624 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003625 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003626 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003627 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003628 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003629 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return 0;
3631 }
Hariprasad Kelam0aa3a242019-07-02 23:50:02 +05303632 for (i = 0; i < count; i++)
3633 convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003634 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3636 rc += sizeof(struct cifs_posix_acl);
3637 /* BB add check to make sure ACL does not overflow SMB */
3638 }
3639 return rc;
3640}
3641
3642int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003643CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003644 const unsigned char *searchName,
3645 char *acl_inf, const int buflen, const int acl_type,
3646 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648/* SMB_QUERY_POSIX_ACL */
3649 TRANSACTION2_QPI_REQ *pSMB = NULL;
3650 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3651 int rc = 0;
3652 int bytes_returned;
3653 int name_len;
3654 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003655
Joe Perchesf96637b2013-05-04 22:12:25 -05003656 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658queryAclRetry:
3659 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3660 (void **) &pSMBr);
3661 if (rc)
3662 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3665 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003666 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3667 searchName, PATH_MAX, nls_codepage,
3668 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 name_len++; /* trailing null */
3670 name_len *= 2;
3671 pSMB->FileName[name_len] = 0;
3672 pSMB->FileName[name_len+1] = 0;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003673 } else {
3674 name_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
3676
3677 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3678 pSMB->TotalDataCount = 0;
3679 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003680 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 pSMB->MaxDataCount = cpu_to_le16(4000);
3682 pSMB->MaxSetupCount = 0;
3683 pSMB->Reserved = 0;
3684 pSMB->Flags = 0;
3685 pSMB->Timeout = 0;
3686 pSMB->Reserved2 = 0;
3687 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003688 offsetof(struct smb_com_transaction2_qpi_req,
3689 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 pSMB->DataCount = 0;
3691 pSMB->DataOffset = 0;
3692 pSMB->SetupCount = 1;
3693 pSMB->Reserved3 = 0;
3694 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3695 byte_count = params + 1 /* pad */ ;
3696 pSMB->TotalParameterCount = cpu_to_le16(params);
3697 pSMB->ParameterCount = pSMB->TotalParameterCount;
3698 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3699 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003700 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 pSMB->ByteCount = cpu_to_le16(byte_count);
3702
3703 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3704 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003705 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003707 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 } else {
3709 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003713 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 rc = -EIO; /* bad smb */
3715 else {
3716 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3717 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3718 rc = cifs_copy_posix_acl(acl_inf,
3719 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003720 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722 }
3723 cifs_buf_release(pSMB);
3724 if (rc == -EAGAIN)
3725 goto queryAclRetry;
3726 return rc;
3727}
3728
3729int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003730CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003731 const unsigned char *fileName,
3732 const char *local_acl, const int buflen,
3733 const int acl_type,
3734 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 struct smb_com_transaction2_spi_req *pSMB = NULL;
3737 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3738 char *parm_data;
3739 int name_len;
3740 int rc = 0;
3741 int bytes_returned = 0;
3742 __u16 params, byte_count, data_count, param_offset, offset;
3743
Joe Perchesf96637b2013-05-04 22:12:25 -05003744 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745setAclRetry:
3746 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003747 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 if (rc)
3749 return rc;
3750 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3751 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003752 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3753 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 name_len++; /* trailing null */
3755 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003756 } else {
3757 name_len = copy_path_name(pSMB->FileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759 params = 6 + name_len;
3760 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003761 /* BB find max SMB size from sess */
3762 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 pSMB->MaxSetupCount = 0;
3764 pSMB->Reserved = 0;
3765 pSMB->Flags = 0;
3766 pSMB->Timeout = 0;
3767 pSMB->Reserved2 = 0;
3768 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003769 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 offset = param_offset + params;
3771 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3772 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3773
3774 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003775 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Steve French790fe572007-07-07 19:25:05 +00003777 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 rc = -EOPNOTSUPP;
3779 goto setACLerrorExit;
3780 }
3781 pSMB->DataOffset = cpu_to_le16(offset);
3782 pSMB->SetupCount = 1;
3783 pSMB->Reserved3 = 0;
3784 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3785 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3786 byte_count = 3 /* pad */ + params + data_count;
3787 pSMB->DataCount = cpu_to_le16(data_count);
3788 pSMB->TotalDataCount = pSMB->DataCount;
3789 pSMB->ParameterCount = cpu_to_le16(params);
3790 pSMB->TotalParameterCount = pSMB->ParameterCount;
3791 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003792 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 pSMB->ByteCount = cpu_to_le16(byte_count);
3794 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003795 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003796 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003797 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799setACLerrorExit:
3800 cifs_buf_release(pSMB);
3801 if (rc == -EAGAIN)
3802 goto setAclRetry;
3803 return rc;
3804}
3805
Steve Frenchf654bac2005-04-28 22:41:04 -07003806/* BB fix tabs in this function FIXME BB */
3807int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003808CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003809 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003810{
Steve French50c2f752007-07-13 00:33:32 +00003811 int rc = 0;
3812 struct smb_t2_qfi_req *pSMB = NULL;
3813 struct smb_t2_qfi_rsp *pSMBr = NULL;
3814 int bytes_returned;
3815 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003816
Joe Perchesf96637b2013-05-04 22:12:25 -05003817 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003818 if (tcon == NULL)
3819 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003820
3821GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003822 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3823 (void **) &pSMBr);
3824 if (rc)
3825 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003826
Steve Frenchad7a2922008-02-07 23:25:02 +00003827 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003828 pSMB->t2.TotalDataCount = 0;
3829 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3830 /* BB find exact max data count below from sess structure BB */
3831 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3832 pSMB->t2.MaxSetupCount = 0;
3833 pSMB->t2.Reserved = 0;
3834 pSMB->t2.Flags = 0;
3835 pSMB->t2.Timeout = 0;
3836 pSMB->t2.Reserved2 = 0;
3837 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3838 Fid) - 4);
3839 pSMB->t2.DataCount = 0;
3840 pSMB->t2.DataOffset = 0;
3841 pSMB->t2.SetupCount = 1;
3842 pSMB->t2.Reserved3 = 0;
3843 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3844 byte_count = params + 1 /* pad */ ;
3845 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3846 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3847 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3848 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003849 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003850 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003851 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003852
Steve French790fe572007-07-07 19:25:05 +00003853 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3854 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3855 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003856 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003857 } else {
3858 /* decode response */
3859 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003860 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003861 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003862 /* If rc should we check for EOPNOSUPP and
3863 disable the srvino flag? or in caller? */
3864 rc = -EIO; /* bad smb */
3865 else {
3866 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3867 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3868 struct file_chattr_info *pfinfo;
3869 /* BB Do we need a cast or hash here ? */
3870 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003871 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003872 rc = -EIO;
3873 goto GetExtAttrOut;
3874 }
3875 pfinfo = (struct file_chattr_info *)
3876 (data_offset + (char *) &pSMBr->hdr.Protocol);
3877 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003878 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003879 }
3880 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003881GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003882 cifs_buf_release(pSMB);
3883 if (rc == -EAGAIN)
3884 goto GetExtAttrRetry;
3885 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003886}
3887
Steve Frenchf654bac2005-04-28 22:41:04 -07003888#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Jeff Layton79df1ba2010-12-06 12:52:08 -05003890/*
3891 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3892 * all NT TRANSACTS that we init here have total parm and data under about 400
3893 * bytes (to fit in small cifs buffer size), which is the case so far, it
3894 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3895 * returned setup area) and MaxParameterCount (returned parms size) must be set
3896 * by caller
3897 */
3898static int
3899smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003900 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003901 void **ret_buf)
3902{
3903 int rc;
3904 __u32 temp_offset;
3905 struct smb_com_ntransact_req *pSMB;
3906
3907 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3908 (void **)&pSMB);
3909 if (rc)
3910 return rc;
3911 *ret_buf = (void *)pSMB;
3912 pSMB->Reserved = 0;
3913 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3914 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003915 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003916 pSMB->ParameterCount = pSMB->TotalParameterCount;
3917 pSMB->DataCount = pSMB->TotalDataCount;
3918 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3919 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3920 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3921 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3922 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3923 pSMB->SubCommand = cpu_to_le16(sub_command);
3924 return 0;
3925}
3926
3927static int
3928validate_ntransact(char *buf, char **ppparm, char **ppdata,
3929 __u32 *pparmlen, __u32 *pdatalen)
3930{
3931 char *end_of_smb;
3932 __u32 data_count, data_offset, parm_count, parm_offset;
3933 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003934 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003935
3936 *pdatalen = 0;
3937 *pparmlen = 0;
3938
3939 if (buf == NULL)
3940 return -EINVAL;
3941
3942 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3943
Jeff Layton820a8032011-05-04 08:05:26 -04003944 bcc = get_bcc(&pSMBr->hdr);
3945 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003946 (char *)&pSMBr->ByteCount;
3947
3948 data_offset = le32_to_cpu(pSMBr->DataOffset);
3949 data_count = le32_to_cpu(pSMBr->DataCount);
3950 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3951 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3952
3953 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3954 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3955
3956 /* should we also check that parm and data areas do not overlap? */
3957 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003958 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003959 return -EINVAL;
3960 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003961 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003962 return -EINVAL;
3963 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003964 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003965 return -EINVAL;
3966 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003967 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3968 *ppdata, data_count, (data_count + *ppdata),
3969 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003970 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003971 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003972 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003973 return -EINVAL;
3974 }
3975 *pdatalen = data_count;
3976 *pparmlen = parm_count;
3977 return 0;
3978}
3979
Steve French0a4b92c2006-01-12 15:44:21 -08003980/* Get Security Descriptor (by handle) from remote server for a file or dir */
3981int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003982CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003983 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003984{
3985 int rc = 0;
3986 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003987 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003988 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003989 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003990
Joe Perchesf96637b2013-05-04 22:12:25 -05003991 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003992
Steve French630f3f0c2007-10-25 21:17:17 +00003993 *pbuflen = 0;
3994 *acl_inf = NULL;
3995
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003996 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003997 8 /* parm len */, tcon, (void **) &pSMB);
3998 if (rc)
3999 return rc;
4000
4001 pSMB->MaxParameterCount = cpu_to_le32(4);
4002 /* BB TEST with big acls that might need to be e.g. larger than 16K */
4003 pSMB->MaxSetupCount = 0;
4004 pSMB->Fid = fid; /* file handle always le */
4005 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
4006 CIFS_ACL_DACL);
4007 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004008 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08004009 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004010 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08004011
Steve Frencha761ac52007-10-18 21:45:27 +00004012 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004013 0, &rsp_iov);
4014 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004015 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08004016 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004017 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08004018 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00004019 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00004020 __u32 parm_len;
4021 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00004022 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00004023 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08004024
4025/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004026 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00004027 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00004028 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08004029 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004030 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08004031
Joe Perchesf96637b2013-05-04 22:12:25 -05004032 cifs_dbg(FYI, "smb %p parm %p data %p\n",
4033 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08004034
4035 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
4036 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00004037 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08004038 goto qsec_out;
4039 }
4040
4041/* BB check that data area is minimum length and as big as acl_len */
4042
Steve Frenchaf6f4612007-10-16 18:40:37 +00004043 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00004044 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004045 cifs_dbg(VFS, "acl length %d does not match %d\n",
4046 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004047 if (*pbuflen > acl_len)
4048 *pbuflen = acl_len;
4049 }
Steve French0a4b92c2006-01-12 15:44:21 -08004050
Steve French630f3f0c2007-10-25 21:17:17 +00004051 /* check if buffer is big enough for the acl
4052 header followed by the smallest SID */
4053 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
4054 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004055 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004056 rc = -EINVAL;
4057 *pbuflen = 0;
4058 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02004059 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00004060 if (*acl_inf == NULL) {
4061 *pbuflen = 0;
4062 rc = -ENOMEM;
4063 }
Steve French630f3f0c2007-10-25 21:17:17 +00004064 }
Steve French0a4b92c2006-01-12 15:44:21 -08004065 }
4066qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004067 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08004068 return rc;
4069}
Steve French97837582007-12-31 07:47:21 +00004070
4071int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004072CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004073 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00004074{
4075 __u16 byte_count, param_count, data_count, param_offset, data_offset;
4076 int rc = 0;
4077 int bytes_returned = 0;
4078 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004079 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00004080
4081setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004082 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00004083 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004084 return rc;
Steve French97837582007-12-31 07:47:21 +00004085
4086 pSMB->MaxSetupCount = 0;
4087 pSMB->Reserved = 0;
4088
4089 param_count = 8;
4090 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
4091 data_count = acllen;
4092 data_offset = param_offset + param_count;
4093 byte_count = 3 /* pad */ + param_count;
4094
4095 pSMB->DataCount = cpu_to_le32(data_count);
4096 pSMB->TotalDataCount = pSMB->DataCount;
4097 pSMB->MaxParameterCount = cpu_to_le32(4);
4098 pSMB->MaxDataCount = cpu_to_le32(16384);
4099 pSMB->ParameterCount = cpu_to_le32(param_count);
4100 pSMB->ParameterOffset = cpu_to_le32(param_offset);
4101 pSMB->TotalParameterCount = pSMB->ParameterCount;
4102 pSMB->DataOffset = cpu_to_le32(data_offset);
4103 pSMB->SetupCount = 0;
4104 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
4105 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
4106
4107 pSMB->Fid = fid; /* file handle always le */
4108 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004109 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00004110
4111 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004112 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
4113 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004114 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00004115 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004116 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00004117
4118 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4119 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4120
Joe Perchesf96637b2013-05-04 22:12:25 -05004121 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4122 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00004123 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004124 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00004125 cifs_buf_release(pSMB);
4126
4127 if (rc == -EAGAIN)
4128 goto setCifsAclRetry;
4129
4130 return (rc);
4131}
4132
Steve French0a4b92c2006-01-12 15:44:21 -08004133
Steve French6b8edfe2005-08-23 20:26:03 -07004134/* Legacy Query Path Information call for lookup to old servers such
4135 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004136int
4137SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4138 const char *search_name, FILE_ALL_INFO *data,
4139 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07004140{
Steve Frenchad7a2922008-02-07 23:25:02 +00004141 QUERY_INFORMATION_REQ *pSMB;
4142 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07004143 int rc = 0;
4144 int bytes_returned;
4145 int name_len;
4146
Joe Perchesf96637b2013-05-04 22:12:25 -05004147 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004148QInfRetry:
4149 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004150 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004151 if (rc)
4152 return rc;
4153
4154 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4155 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004156 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004157 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004158 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004159 name_len++; /* trailing null */
4160 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004161 } else {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004162 name_len = copy_path_name(pSMB->FileName, search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004163 }
4164 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004165 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004166 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004167 pSMB->ByteCount = cpu_to_le16(name_len);
4168
4169 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004170 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004171 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004172 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004173 } else if (data) {
Arnd Bergmann95390202018-06-19 17:27:58 +02004174 struct timespec64 ts;
Steve French1bd5bbc2006-09-28 03:35:57 +00004175 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004176
4177 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004178 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004179 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004180 ts.tv_nsec = 0;
4181 ts.tv_sec = time;
4182 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004183 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4184 data->LastWriteTime = data->ChangeTime;
4185 data->LastAccessTime = 0;
4186 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004187 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004188 data->EndOfFile = data->AllocationSize;
4189 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004190 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004191 } else
4192 rc = -EIO; /* bad buffer passed in */
4193
4194 cifs_buf_release(pSMB);
4195
4196 if (rc == -EAGAIN)
4197 goto QInfRetry;
4198
4199 return rc;
4200}
4201
Jeff Laytonbcd53572010-02-12 07:44:16 -05004202int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004203CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004204 u16 netfid, FILE_ALL_INFO *pFindData)
4205{
4206 struct smb_t2_qfi_req *pSMB = NULL;
4207 struct smb_t2_qfi_rsp *pSMBr = NULL;
4208 int rc = 0;
4209 int bytes_returned;
4210 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004211
Jeff Laytonbcd53572010-02-12 07:44:16 -05004212QFileInfoRetry:
4213 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4214 (void **) &pSMBr);
4215 if (rc)
4216 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004217
Jeff Laytonbcd53572010-02-12 07:44:16 -05004218 params = 2 /* level */ + 2 /* fid */;
4219 pSMB->t2.TotalDataCount = 0;
4220 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4221 /* BB find exact max data count below from sess structure BB */
4222 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4223 pSMB->t2.MaxSetupCount = 0;
4224 pSMB->t2.Reserved = 0;
4225 pSMB->t2.Flags = 0;
4226 pSMB->t2.Timeout = 0;
4227 pSMB->t2.Reserved2 = 0;
4228 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4229 Fid) - 4);
4230 pSMB->t2.DataCount = 0;
4231 pSMB->t2.DataOffset = 0;
4232 pSMB->t2.SetupCount = 1;
4233 pSMB->t2.Reserved3 = 0;
4234 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4235 byte_count = params + 1 /* pad */ ;
4236 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4237 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4238 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4239 pSMB->Pad = 0;
4240 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004241 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004242 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004243
4244 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4245 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4246 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004247 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004248 } else { /* decode response */
4249 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4250
4251 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4252 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004253 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004254 rc = -EIO; /* bad smb */
4255 else if (pFindData) {
4256 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4257 memcpy((char *) pFindData,
4258 (char *) &pSMBr->hdr.Protocol +
4259 data_offset, sizeof(FILE_ALL_INFO));
4260 } else
4261 rc = -ENOMEM;
4262 }
4263 cifs_buf_release(pSMB);
4264 if (rc == -EAGAIN)
4265 goto QFileInfoRetry;
4266
4267 return rc;
4268}
Steve French6b8edfe2005-08-23 20:26:03 -07004269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004271CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004272 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004273 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004274 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004276 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 TRANSACTION2_QPI_REQ *pSMB = NULL;
4278 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4279 int rc = 0;
4280 int bytes_returned;
4281 int name_len;
4282 __u16 params, byte_count;
4283
Joe Perchesf96637b2013-05-04 22:12:25 -05004284 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285QPathInfoRetry:
4286 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4287 (void **) &pSMBr);
4288 if (rc)
4289 return rc;
4290
4291 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4292 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004293 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004294 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 name_len++; /* trailing null */
4296 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004297 } else {
4298 name_len = copy_path_name(pSMB->FileName, search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
4300
Steve French50c2f752007-07-13 00:33:32 +00004301 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 pSMB->TotalDataCount = 0;
4303 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004304 /* BB find exact max SMB PDU from sess structure BB */
4305 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 pSMB->MaxSetupCount = 0;
4307 pSMB->Reserved = 0;
4308 pSMB->Flags = 0;
4309 pSMB->Timeout = 0;
4310 pSMB->Reserved2 = 0;
4311 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004312 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 pSMB->DataCount = 0;
4314 pSMB->DataOffset = 0;
4315 pSMB->SetupCount = 1;
4316 pSMB->Reserved3 = 0;
4317 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4318 byte_count = params + 1 /* pad */ ;
4319 pSMB->TotalParameterCount = cpu_to_le16(params);
4320 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004321 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004322 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4323 else
4324 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004326 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 pSMB->ByteCount = cpu_to_le16(byte_count);
4328
4329 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4330 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4331 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004332 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 } else { /* decode response */
4334 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4335
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004336 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4337 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004338 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004340 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004341 rc = -EIO; /* 24 or 26 expected but we do not read
4342 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004343 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004344 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004346
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004347 /*
4348 * On legacy responses we do not read the last field,
4349 * EAsize, fortunately since it varies by subdialect and
4350 * also note it differs on Set vs Get, ie two bytes or 4
4351 * bytes depending but we don't care here.
4352 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004353 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004354 size = sizeof(FILE_INFO_STANDARD);
4355 else
4356 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004357 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004358 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 } else
4360 rc = -ENOMEM;
4361 }
4362 cifs_buf_release(pSMB);
4363 if (rc == -EAGAIN)
4364 goto QPathInfoRetry;
4365
4366 return rc;
4367}
4368
4369int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004370CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004371 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4372{
4373 struct smb_t2_qfi_req *pSMB = NULL;
4374 struct smb_t2_qfi_rsp *pSMBr = NULL;
4375 int rc = 0;
4376 int bytes_returned;
4377 __u16 params, byte_count;
4378
4379UnixQFileInfoRetry:
4380 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4381 (void **) &pSMBr);
4382 if (rc)
4383 return rc;
4384
4385 params = 2 /* level */ + 2 /* fid */;
4386 pSMB->t2.TotalDataCount = 0;
4387 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4388 /* BB find exact max data count below from sess structure BB */
4389 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4390 pSMB->t2.MaxSetupCount = 0;
4391 pSMB->t2.Reserved = 0;
4392 pSMB->t2.Flags = 0;
4393 pSMB->t2.Timeout = 0;
4394 pSMB->t2.Reserved2 = 0;
4395 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4396 Fid) - 4);
4397 pSMB->t2.DataCount = 0;
4398 pSMB->t2.DataOffset = 0;
4399 pSMB->t2.SetupCount = 1;
4400 pSMB->t2.Reserved3 = 0;
4401 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4402 byte_count = params + 1 /* pad */ ;
4403 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4404 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4405 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4406 pSMB->Pad = 0;
4407 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004408 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004409 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004410
4411 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4412 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4413 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004414 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004415 } else { /* decode response */
4416 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4417
Jeff Layton820a8032011-05-04 08:05:26 -04004418 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004419 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004420 rc = -EIO; /* bad smb */
4421 } else {
4422 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4423 memcpy((char *) pFindData,
4424 (char *) &pSMBr->hdr.Protocol +
4425 data_offset,
4426 sizeof(FILE_UNIX_BASIC_INFO));
4427 }
4428 }
4429
4430 cifs_buf_release(pSMB);
4431 if (rc == -EAGAIN)
4432 goto UnixQFileInfoRetry;
4433
4434 return rc;
4435}
4436
4437int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004438CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004440 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004441 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
4443/* SMB_QUERY_FILE_UNIX_BASIC */
4444 TRANSACTION2_QPI_REQ *pSMB = NULL;
4445 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4446 int rc = 0;
4447 int bytes_returned = 0;
4448 int name_len;
4449 __u16 params, byte_count;
4450
Joe Perchesf96637b2013-05-04 22:12:25 -05004451 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452UnixQPathInfoRetry:
4453 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4454 (void **) &pSMBr);
4455 if (rc)
4456 return rc;
4457
4458 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4459 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004460 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4461 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 name_len++; /* trailing null */
4463 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004464 } else {
4465 name_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
4467
Steve French50c2f752007-07-13 00:33:32 +00004468 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 pSMB->TotalDataCount = 0;
4470 pSMB->MaxParameterCount = cpu_to_le16(2);
4471 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004472 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 pSMB->MaxSetupCount = 0;
4474 pSMB->Reserved = 0;
4475 pSMB->Flags = 0;
4476 pSMB->Timeout = 0;
4477 pSMB->Reserved2 = 0;
4478 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004479 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 pSMB->DataCount = 0;
4481 pSMB->DataOffset = 0;
4482 pSMB->SetupCount = 1;
4483 pSMB->Reserved3 = 0;
4484 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4485 byte_count = params + 1 /* pad */ ;
4486 pSMB->TotalParameterCount = cpu_to_le16(params);
4487 pSMB->ParameterCount = pSMB->TotalParameterCount;
4488 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4489 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004490 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 pSMB->ByteCount = cpu_to_le16(byte_count);
4492
4493 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4494 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4495 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004496 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 } else { /* decode response */
4498 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4499
Jeff Layton820a8032011-05-04 08:05:26 -04004500 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004501 cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 rc = -EIO; /* bad smb */
4503 } else {
4504 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4505 memcpy((char *) pFindData,
4506 (char *) &pSMBr->hdr.Protocol +
4507 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004508 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 }
4510 }
4511 cifs_buf_release(pSMB);
4512 if (rc == -EAGAIN)
4513 goto UnixQPathInfoRetry;
4514
4515 return rc;
4516}
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518/* xid, tcon, searchName and codepage are input parms, rest are returned */
4519int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004520CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004521 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004522 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004523 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
4525/* level 257 SMB_ */
4526 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4527 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004528 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 int rc = 0;
4530 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004531 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004533 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Joe Perchesf96637b2013-05-04 22:12:25 -05004535 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537findFirstRetry:
4538 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4539 (void **) &pSMBr);
4540 if (rc)
4541 return rc;
4542
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004543 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004544 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4547 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004548 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4549 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004550 /* We can not add the asterik earlier in case
4551 it got remapped to 0xF03A as if it were part of the
4552 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004554 if (msearch) {
4555 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4556 pSMB->FileName[name_len+1] = 0;
4557 pSMB->FileName[name_len+2] = '*';
4558 pSMB->FileName[name_len+3] = 0;
4559 name_len += 4; /* now the trailing null */
4560 /* null terminate just in case */
4561 pSMB->FileName[name_len] = 0;
4562 pSMB->FileName[name_len+1] = 0;
4563 name_len += 2;
4564 }
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004565 } else {
4566 name_len = copy_path_name(pSMB->FileName, searchName);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004567 if (msearch) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004568 if (WARN_ON_ONCE(name_len > PATH_MAX-2))
4569 name_len = PATH_MAX-2;
4570 /* overwrite nul byte */
4571 pSMB->FileName[name_len-1] = CIFS_DIR_SEP(cifs_sb);
4572 pSMB->FileName[name_len] = '*';
4573 pSMB->FileName[name_len+1] = 0;
4574 name_len += 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 }
4577
4578 params = 12 + name_len /* includes null */ ;
4579 pSMB->TotalDataCount = 0; /* no EAs */
4580 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004581 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 pSMB->MaxSetupCount = 0;
4583 pSMB->Reserved = 0;
4584 pSMB->Flags = 0;
4585 pSMB->Timeout = 0;
4586 pSMB->Reserved2 = 0;
4587 byte_count = params + 1 /* pad */ ;
4588 pSMB->TotalParameterCount = cpu_to_le16(params);
4589 pSMB->ParameterCount = pSMB->TotalParameterCount;
4590 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004591 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4592 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 pSMB->DataCount = 0;
4594 pSMB->DataOffset = 0;
4595 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4596 pSMB->Reserved3 = 0;
4597 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4598 pSMB->SearchAttributes =
4599 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4600 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004601 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004602 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4604
4605 /* BB what should we set StorageType to? Does it matter? BB */
4606 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004607 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 pSMB->ByteCount = cpu_to_le16(byte_count);
4609
4610 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4611 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004612 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Steve French88274812006-03-09 22:21:45 +00004614 if (rc) {/* BB add logic to retry regular search if Unix search
4615 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004617 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004618
Steve French88274812006-03-09 22:21:45 +00004619 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 /* BB eventually could optimize out free and realloc of buf */
4622 /* for this case */
4623 if (rc == -EAGAIN)
4624 goto findFirstRetry;
4625 } else { /* decode response */
4626 /* BB remember to free buffer if error BB */
4627 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004628 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004629 unsigned int lnoff;
4630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004632 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 else
Steve French4b18f2a2008-04-29 00:06:05 +00004634 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
4636 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
zhengbin01d1bd72019-12-25 11:30:21 +08004637 psrch_inf->smallBuf = false;
Steve French50c2f752007-07-13 00:33:32 +00004638 psrch_inf->srch_entries_start =
4639 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4642 le16_to_cpu(pSMBr->t2.ParameterOffset));
4643
Steve French790fe572007-07-07 19:25:05 +00004644 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004645 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 else
Steve French4b18f2a2008-04-29 00:06:05 +00004647 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Steve French50c2f752007-07-13 00:33:32 +00004649 psrch_inf->entries_in_buffer =
4650 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004651 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004653 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004654 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004655 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004656 psrch_inf->last_entry = NULL;
4657 return rc;
4658 }
4659
Steve French0752f152008-10-07 20:03:33 +00004660 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004661 lnoff;
4662
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004663 if (pnetfid)
4664 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 } else {
4666 cifs_buf_release(pSMB);
4667 }
4668 }
4669
4670 return rc;
4671}
4672
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004673int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4674 __u16 searchHandle, __u16 search_flags,
4675 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
4677 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4678 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004679 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 char *response_data;
4681 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004682 int bytes_returned;
4683 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 __u16 params, byte_count;
4685
Joe Perchesf96637b2013-05-04 22:12:25 -05004686 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Steve French4b18f2a2008-04-29 00:06:05 +00004688 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return -ENOENT;
4690
4691 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4692 (void **) &pSMBr);
4693 if (rc)
4694 return rc;
4695
Steve French50c2f752007-07-13 00:33:32 +00004696 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 byte_count = 0;
4698 pSMB->TotalDataCount = 0; /* no EAs */
4699 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004700 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 pSMB->MaxSetupCount = 0;
4702 pSMB->Reserved = 0;
4703 pSMB->Flags = 0;
4704 pSMB->Timeout = 0;
4705 pSMB->Reserved2 = 0;
4706 pSMB->ParameterOffset = cpu_to_le16(
4707 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4708 pSMB->DataCount = 0;
4709 pSMB->DataOffset = 0;
4710 pSMB->SetupCount = 1;
4711 pSMB->Reserved3 = 0;
4712 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4713 pSMB->SearchHandle = searchHandle; /* always kept as le */
4714 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004715 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4717 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004718 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 name_len = psrch_inf->resume_name_len;
4721 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004722 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4724 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004725 /* 14 byte parm len above enough for 2 byte null terminator */
4726 pSMB->ResumeFileName[name_len] = 0;
4727 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 } else {
4729 rc = -EINVAL;
4730 goto FNext2_err_exit;
4731 }
4732 byte_count = params + 1 /* pad */ ;
4733 pSMB->TotalParameterCount = cpu_to_le16(params);
4734 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004735 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4739 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004740 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 if (rc) {
4742 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004743 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004744 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004745 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004747 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 } else { /* decode response */
4749 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004750
Steve French790fe572007-07-07 19:25:05 +00004751 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004752 unsigned int lnoff;
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 /* BB fixme add lock for file (srch_info) struct here */
4755 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004756 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 else
Steve French4b18f2a2008-04-29 00:06:05 +00004758 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 response_data = (char *) &pSMBr->hdr.Protocol +
4760 le16_to_cpu(pSMBr->t2.ParameterOffset);
4761 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4762 response_data = (char *)&pSMBr->hdr.Protocol +
4763 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004764 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004765 cifs_small_buf_release(
4766 psrch_inf->ntwrk_buf_start);
4767 else
4768 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 psrch_inf->srch_entries_start = response_data;
4770 psrch_inf->ntwrk_buf_start = (char *)pSMB;
zhengbin01d1bd72019-12-25 11:30:21 +08004771 psrch_inf->smallBuf = false;
Steve French790fe572007-07-07 19:25:05 +00004772 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004773 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 else
Steve French4b18f2a2008-04-29 00:06:05 +00004775 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004776 psrch_inf->entries_in_buffer =
4777 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 psrch_inf->index_of_last_entry +=
4779 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004780 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004781 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004782 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004783 psrch_inf->last_entry = NULL;
4784 return rc;
4785 } else
4786 psrch_inf->last_entry =
4787 psrch_inf->srch_entries_start + lnoff;
4788
Joe Perchesf96637b2013-05-04 22:12:25 -05004789/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4790 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 /* BB fixme add unlock here */
4793 }
4794
4795 }
4796
4797 /* BB On error, should we leave previous search buf (and count and
4798 last entry fields) intact or free the previous one? */
4799
4800 /* Note: On -EAGAIN error only caller can retry on handle based calls
4801 since file handle passed in no longer valid */
4802FNext2_err_exit:
4803 if (rc != 0)
4804 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 return rc;
4806}
4807
4808int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004809CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004810 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
4812 int rc = 0;
4813 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Joe Perchesf96637b2013-05-04 22:12:25 -05004815 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4817
4818 /* no sense returning error if session restarted
4819 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004820 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 return 0;
4822 if (rc)
4823 return rc;
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 pSMB->FileID = searchHandle;
4826 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004827 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004828 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004829 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004830 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004831
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004832 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 /* Since session is dead, search handle closed on server already */
4835 if (rc == -EAGAIN)
4836 rc = 0;
4837
4838 return rc;
4839}
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004842CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004843 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004844 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
4846 int rc = 0;
4847 TRANSACTION2_QPI_REQ *pSMB = NULL;
4848 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4849 int name_len, bytes_returned;
4850 __u16 params, byte_count;
4851
Joe Perchesf96637b2013-05-04 22:12:25 -05004852 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004853 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004854 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
4856GetInodeNumberRetry:
4857 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004858 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 if (rc)
4860 return rc;
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4863 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004864 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004865 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004866 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 name_len++; /* trailing null */
4868 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004869 } else {
4870 name_len = copy_path_name(pSMB->FileName, search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 }
4872
4873 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4874 pSMB->TotalDataCount = 0;
4875 pSMB->MaxParameterCount = cpu_to_le16(2);
4876 /* BB find exact max data count below from sess structure BB */
4877 pSMB->MaxDataCount = cpu_to_le16(4000);
4878 pSMB->MaxSetupCount = 0;
4879 pSMB->Reserved = 0;
4880 pSMB->Flags = 0;
4881 pSMB->Timeout = 0;
4882 pSMB->Reserved2 = 0;
4883 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004884 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 pSMB->DataCount = 0;
4886 pSMB->DataOffset = 0;
4887 pSMB->SetupCount = 1;
4888 pSMB->Reserved3 = 0;
4889 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4890 byte_count = params + 1 /* pad */ ;
4891 pSMB->TotalParameterCount = cpu_to_le16(params);
4892 pSMB->ParameterCount = pSMB->TotalParameterCount;
4893 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4894 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004895 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 pSMB->ByteCount = cpu_to_le16(byte_count);
4897
4898 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4899 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4900 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004901 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 } else {
4903 /* decode response */
4904 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004906 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 /* If rc should we check for EOPNOSUPP and
4908 disable the srvino flag? or in caller? */
4909 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004910 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4912 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004913 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004915 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004916 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 rc = -EIO;
4918 goto GetInodeNumOut;
4919 }
4920 pfinfo = (struct file_internal_info *)
4921 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004922 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 }
4924 }
4925GetInodeNumOut:
4926 cifs_buf_release(pSMB);
4927 if (rc == -EAGAIN)
4928 goto GetInodeNumberRetry;
4929 return rc;
4930}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004933CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004934 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004935 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004936 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937{
4938/* TRANS2_GET_DFS_REFERRAL */
4939 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4940 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 int rc = 0;
4942 int bytes_returned;
4943 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004945 *num_of_nodes = 0;
4946 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Joe Perchesf96637b2013-05-04 22:12:25 -05004948 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004949 if (ses == NULL || ses->tcon_ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 return -ENODEV;
Aurelien Aptelb327a712018-01-24 13:46:10 +01004951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952getDFSRetry:
Aurelien Aptelb327a712018-01-24 13:46:10 +01004953 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 (void **) &pSMBr);
4955 if (rc)
4956 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004957
4958 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004959 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004960 pSMB->hdr.Mid = get_next_mid(ses->server);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004961 pSMB->hdr.Tid = ses->tcon_ipc->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004963 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004965 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968 if (ses->capabilities & CAP_UNICODE) {
4969 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4970 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004971 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004972 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004973 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 name_len++; /* trailing null */
4975 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004976 } else { /* BB improve the check for buffer overruns BB */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004977 name_len = copy_path_name(pSMB->RequestFileName, search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 }
4979
Dan Carpenter65c3b202015-04-30 17:30:24 +03004980 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004981 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004982
Steve French50c2f752007-07-13 00:33:32 +00004983 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 params = 2 /* level */ + name_len /*includes null */ ;
4986 pSMB->TotalDataCount = 0;
4987 pSMB->DataCount = 0;
4988 pSMB->DataOffset = 0;
4989 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004990 /* BB find exact max SMB PDU from sess structure BB */
4991 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 pSMB->MaxSetupCount = 0;
4993 pSMB->Reserved = 0;
4994 pSMB->Flags = 0;
4995 pSMB->Timeout = 0;
4996 pSMB->Reserved2 = 0;
4997 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004998 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 pSMB->SetupCount = 1;
5000 pSMB->Reserved3 = 0;
5001 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
5002 byte_count = params + 3 /* pad */ ;
5003 pSMB->ParameterCount = cpu_to_le16(params);
5004 pSMB->TotalParameterCount = pSMB->ParameterCount;
5005 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005006 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 pSMB->ByteCount = cpu_to_le16(byte_count);
5008
5009 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
5010 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5011 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005012 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005013 goto GetDFSRefExit;
5014 }
5015 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005017 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04005018 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005019 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04005020 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
Igor Mammedovfec45852008-05-16 13:06:30 +04005022
Joe Perchesf96637b2013-05-04 22:12:25 -05005023 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5024 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04005025
5026 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01005027 rc = parse_dfs_referrals(&pSMBr->dfs_data,
5028 le16_to_cpu(pSMBr->t2.DataCount),
5029 num_of_nodes, target_nodes, nls_codepage,
5030 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06005031 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04005032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00005034 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
5036 if (rc == -EAGAIN)
5037 goto getDFSRetry;
5038
5039 return rc;
5040}
5041
Steve French20962432005-09-21 22:05:57 -07005042/* Query File System Info such as free space to old servers such as Win 9x */
5043int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005044SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5045 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005046{
5047/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5048 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5049 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5050 FILE_SYSTEM_ALLOC_INFO *response_data;
5051 int rc = 0;
5052 int bytes_returned = 0;
5053 __u16 params, byte_count;
5054
Joe Perchesf96637b2013-05-04 22:12:25 -05005055 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005056oldQFSInfoRetry:
5057 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5058 (void **) &pSMBr);
5059 if (rc)
5060 return rc;
Steve French20962432005-09-21 22:05:57 -07005061
5062 params = 2; /* level */
5063 pSMB->TotalDataCount = 0;
5064 pSMB->MaxParameterCount = cpu_to_le16(2);
5065 pSMB->MaxDataCount = cpu_to_le16(1000);
5066 pSMB->MaxSetupCount = 0;
5067 pSMB->Reserved = 0;
5068 pSMB->Flags = 0;
5069 pSMB->Timeout = 0;
5070 pSMB->Reserved2 = 0;
5071 byte_count = params + 1 /* pad */ ;
5072 pSMB->TotalParameterCount = cpu_to_le16(params);
5073 pSMB->ParameterCount = pSMB->TotalParameterCount;
5074 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5075 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5076 pSMB->DataCount = 0;
5077 pSMB->DataOffset = 0;
5078 pSMB->SetupCount = 1;
5079 pSMB->Reserved3 = 0;
5080 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5081 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005082 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005083 pSMB->ByteCount = cpu_to_le16(byte_count);
5084
5085 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5086 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5087 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005088 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005089 } else { /* decode response */
5090 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5091
Jeff Layton820a8032011-05-04 08:05:26 -04005092 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005093 rc = -EIO; /* bad smb */
5094 else {
5095 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005096 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005097 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005098
Steve French50c2f752007-07-13 00:33:32 +00005099 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005100 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5101 FSData->f_bsize =
5102 le16_to_cpu(response_data->BytesPerSector) *
5103 le32_to_cpu(response_data->
5104 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005105 /*
5106 * much prefer larger but if server doesn't report
5107 * a valid size than 4K is a reasonable minimum
5108 */
5109 if (FSData->f_bsize < 512)
5110 FSData->f_bsize = 4096;
5111
Steve French20962432005-09-21 22:05:57 -07005112 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005113 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005114 FSData->f_bfree = FSData->f_bavail =
5115 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005116 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5117 (unsigned long long)FSData->f_blocks,
5118 (unsigned long long)FSData->f_bfree,
5119 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005120 }
5121 }
5122 cifs_buf_release(pSMB);
5123
5124 if (rc == -EAGAIN)
5125 goto oldQFSInfoRetry;
5126
5127 return rc;
5128}
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005131CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5132 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133{
5134/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5135 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5136 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5137 FILE_SYSTEM_INFO *response_data;
5138 int rc = 0;
5139 int bytes_returned = 0;
5140 __u16 params, byte_count;
5141
Joe Perchesf96637b2013-05-04 22:12:25 -05005142 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143QFSInfoRetry:
5144 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5145 (void **) &pSMBr);
5146 if (rc)
5147 return rc;
5148
5149 params = 2; /* level */
5150 pSMB->TotalDataCount = 0;
5151 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005152 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 pSMB->MaxSetupCount = 0;
5154 pSMB->Reserved = 0;
5155 pSMB->Flags = 0;
5156 pSMB->Timeout = 0;
5157 pSMB->Reserved2 = 0;
5158 byte_count = params + 1 /* pad */ ;
5159 pSMB->TotalParameterCount = cpu_to_le16(params);
5160 pSMB->ParameterCount = pSMB->TotalParameterCount;
5161 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005162 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 pSMB->DataCount = 0;
5164 pSMB->DataOffset = 0;
5165 pSMB->SetupCount = 1;
5166 pSMB->Reserved3 = 0;
5167 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5168 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005169 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 pSMB->ByteCount = cpu_to_le16(byte_count);
5171
5172 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5173 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5174 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005175 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005177 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Jeff Layton820a8032011-05-04 08:05:26 -04005179 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 rc = -EIO; /* bad smb */
5181 else {
5182 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
5184 response_data =
5185 (FILE_SYSTEM_INFO
5186 *) (((char *) &pSMBr->hdr.Protocol) +
5187 data_offset);
5188 FSData->f_bsize =
5189 le32_to_cpu(response_data->BytesPerSector) *
5190 le32_to_cpu(response_data->
5191 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005192 /*
5193 * much prefer larger but if server doesn't report
5194 * a valid size than 4K is a reasonable minimum
5195 */
5196 if (FSData->f_bsize < 512)
5197 FSData->f_bsize = 4096;
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 FSData->f_blocks =
5200 le64_to_cpu(response_data->TotalAllocationUnits);
5201 FSData->f_bfree = FSData->f_bavail =
5202 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005203 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5204 (unsigned long long)FSData->f_blocks,
5205 (unsigned long long)FSData->f_bfree,
5206 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 }
5208 }
5209 cifs_buf_release(pSMB);
5210
5211 if (rc == -EAGAIN)
5212 goto QFSInfoRetry;
5213
5214 return rc;
5215}
5216
5217int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005218CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
5220/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5221 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5222 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5223 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5224 int rc = 0;
5225 int bytes_returned = 0;
5226 __u16 params, byte_count;
5227
Joe Perchesf96637b2013-05-04 22:12:25 -05005228 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229QFSAttributeRetry:
5230 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5231 (void **) &pSMBr);
5232 if (rc)
5233 return rc;
5234
5235 params = 2; /* level */
5236 pSMB->TotalDataCount = 0;
5237 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005238 /* BB find exact max SMB PDU from sess structure BB */
5239 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 pSMB->MaxSetupCount = 0;
5241 pSMB->Reserved = 0;
5242 pSMB->Flags = 0;
5243 pSMB->Timeout = 0;
5244 pSMB->Reserved2 = 0;
5245 byte_count = params + 1 /* pad */ ;
5246 pSMB->TotalParameterCount = cpu_to_le16(params);
5247 pSMB->ParameterCount = pSMB->TotalParameterCount;
5248 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005249 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 pSMB->DataCount = 0;
5251 pSMB->DataOffset = 0;
5252 pSMB->SetupCount = 1;
5253 pSMB->Reserved3 = 0;
5254 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5255 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005256 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 pSMB->ByteCount = cpu_to_le16(byte_count);
5258
5259 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5260 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5261 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005262 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 } else { /* decode response */
5264 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5265
Jeff Layton820a8032011-05-04 08:05:26 -04005266 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005267 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 rc = -EIO; /* bad smb */
5269 } else {
5270 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5271 response_data =
5272 (FILE_SYSTEM_ATTRIBUTE_INFO
5273 *) (((char *) &pSMBr->hdr.Protocol) +
5274 data_offset);
5275 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005276 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
5278 }
5279 cifs_buf_release(pSMB);
5280
5281 if (rc == -EAGAIN)
5282 goto QFSAttributeRetry;
5283
5284 return rc;
5285}
5286
5287int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005288CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
5290/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5291 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5292 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5293 FILE_SYSTEM_DEVICE_INFO *response_data;
5294 int rc = 0;
5295 int bytes_returned = 0;
5296 __u16 params, byte_count;
5297
Joe Perchesf96637b2013-05-04 22:12:25 -05005298 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299QFSDeviceRetry:
5300 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5301 (void **) &pSMBr);
5302 if (rc)
5303 return rc;
5304
5305 params = 2; /* level */
5306 pSMB->TotalDataCount = 0;
5307 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005308 /* BB find exact max SMB PDU from sess structure BB */
5309 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 pSMB->MaxSetupCount = 0;
5311 pSMB->Reserved = 0;
5312 pSMB->Flags = 0;
5313 pSMB->Timeout = 0;
5314 pSMB->Reserved2 = 0;
5315 byte_count = params + 1 /* pad */ ;
5316 pSMB->TotalParameterCount = cpu_to_le16(params);
5317 pSMB->ParameterCount = pSMB->TotalParameterCount;
5318 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005319 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
5321 pSMB->DataCount = 0;
5322 pSMB->DataOffset = 0;
5323 pSMB->SetupCount = 1;
5324 pSMB->Reserved3 = 0;
5325 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5326 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005327 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 pSMB->ByteCount = cpu_to_le16(byte_count);
5329
5330 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5331 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5332 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005333 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 } else { /* decode response */
5335 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5336
Jeff Layton820a8032011-05-04 08:05:26 -04005337 if (rc || get_bcc(&pSMBr->hdr) <
5338 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 rc = -EIO; /* bad smb */
5340 else {
5341 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5342 response_data =
Steve French737b7582005-04-28 22:41:06 -07005343 (FILE_SYSTEM_DEVICE_INFO *)
5344 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 data_offset);
5346 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005347 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 }
5349 }
5350 cifs_buf_release(pSMB);
5351
5352 if (rc == -EAGAIN)
5353 goto QFSDeviceRetry;
5354
5355 return rc;
5356}
5357
5358int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005359CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360{
5361/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5362 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5363 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5364 FILE_SYSTEM_UNIX_INFO *response_data;
5365 int rc = 0;
5366 int bytes_returned = 0;
5367 __u16 params, byte_count;
5368
Joe Perchesf96637b2013-05-04 22:12:25 -05005369 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005371 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5372 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 if (rc)
5374 return rc;
5375
5376 params = 2; /* level */
5377 pSMB->TotalDataCount = 0;
5378 pSMB->DataCount = 0;
5379 pSMB->DataOffset = 0;
5380 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005381 /* BB find exact max SMB PDU from sess structure BB */
5382 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 pSMB->MaxSetupCount = 0;
5384 pSMB->Reserved = 0;
5385 pSMB->Flags = 0;
5386 pSMB->Timeout = 0;
5387 pSMB->Reserved2 = 0;
5388 byte_count = params + 1 /* pad */ ;
5389 pSMB->ParameterCount = cpu_to_le16(params);
5390 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005391 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5392 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 pSMB->SetupCount = 1;
5394 pSMB->Reserved3 = 0;
5395 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5396 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005397 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 pSMB->ByteCount = cpu_to_le16(byte_count);
5399
5400 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5401 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5402 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005403 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 } else { /* decode response */
5405 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5406
Jeff Layton820a8032011-05-04 08:05:26 -04005407 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 rc = -EIO; /* bad smb */
5409 } else {
5410 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5411 response_data =
5412 (FILE_SYSTEM_UNIX_INFO
5413 *) (((char *) &pSMBr->hdr.Protocol) +
5414 data_offset);
5415 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005416 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 }
5418 }
5419 cifs_buf_release(pSMB);
5420
5421 if (rc == -EAGAIN)
5422 goto QFSUnixRetry;
5423
5424
5425 return rc;
5426}
5427
Jeremy Allisonac670552005-06-22 17:26:35 -07005428int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005429CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005430{
5431/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5432 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5433 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5434 int rc = 0;
5435 int bytes_returned = 0;
5436 __u16 params, param_offset, offset, byte_count;
5437
Joe Perchesf96637b2013-05-04 22:12:25 -05005438 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005439SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005440 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005441 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5442 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005443 if (rc)
5444 return rc;
5445
5446 params = 4; /* 2 bytes zero followed by info level. */
5447 pSMB->MaxSetupCount = 0;
5448 pSMB->Reserved = 0;
5449 pSMB->Flags = 0;
5450 pSMB->Timeout = 0;
5451 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005452 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5453 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005454 offset = param_offset + params;
5455
5456 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005457 /* BB find exact max SMB PDU from sess structure BB */
5458 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005459 pSMB->SetupCount = 1;
5460 pSMB->Reserved3 = 0;
5461 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5462 byte_count = 1 /* pad */ + params + 12;
5463
5464 pSMB->DataCount = cpu_to_le16(12);
5465 pSMB->ParameterCount = cpu_to_le16(params);
5466 pSMB->TotalDataCount = pSMB->DataCount;
5467 pSMB->TotalParameterCount = pSMB->ParameterCount;
5468 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5469 pSMB->DataOffset = cpu_to_le16(offset);
5470
5471 /* Params. */
5472 pSMB->FileNum = 0;
5473 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5474
5475 /* Data. */
5476 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5477 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5478 pSMB->ClientUnixCap = cpu_to_le64(cap);
5479
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005480 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005481 pSMB->ByteCount = cpu_to_le16(byte_count);
5482
5483 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5484 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5485 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005486 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005487 } else { /* decode response */
5488 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005489 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005490 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005491 }
5492 cifs_buf_release(pSMB);
5493
5494 if (rc == -EAGAIN)
5495 goto SETFSUnixRetry;
5496
5497 return rc;
5498}
5499
5500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005503CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005504 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505{
5506/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5507 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5508 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5509 FILE_SYSTEM_POSIX_INFO *response_data;
5510 int rc = 0;
5511 int bytes_returned = 0;
5512 __u16 params, byte_count;
5513
Joe Perchesf96637b2013-05-04 22:12:25 -05005514 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515QFSPosixRetry:
5516 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5517 (void **) &pSMBr);
5518 if (rc)
5519 return rc;
5520
5521 params = 2; /* level */
5522 pSMB->TotalDataCount = 0;
5523 pSMB->DataCount = 0;
5524 pSMB->DataOffset = 0;
5525 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005526 /* BB find exact max SMB PDU from sess structure BB */
5527 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 pSMB->MaxSetupCount = 0;
5529 pSMB->Reserved = 0;
5530 pSMB->Flags = 0;
5531 pSMB->Timeout = 0;
5532 pSMB->Reserved2 = 0;
5533 byte_count = params + 1 /* pad */ ;
5534 pSMB->ParameterCount = cpu_to_le16(params);
5535 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005536 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5537 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 pSMB->SetupCount = 1;
5539 pSMB->Reserved3 = 0;
5540 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5541 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005542 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 pSMB->ByteCount = cpu_to_le16(byte_count);
5544
5545 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5546 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5547 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005548 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 } else { /* decode response */
5550 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5551
Jeff Layton820a8032011-05-04 08:05:26 -04005552 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 rc = -EIO; /* bad smb */
5554 } else {
5555 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5556 response_data =
5557 (FILE_SYSTEM_POSIX_INFO
5558 *) (((char *) &pSMBr->hdr.Protocol) +
5559 data_offset);
5560 FSData->f_bsize =
5561 le32_to_cpu(response_data->BlockSize);
Steve French5a519be2018-09-15 14:07:09 -05005562 /*
5563 * much prefer larger but if server doesn't report
5564 * a valid size than 4K is a reasonable minimum
5565 */
5566 if (FSData->f_bsize < 512)
5567 FSData->f_bsize = 4096;
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 FSData->f_blocks =
5570 le64_to_cpu(response_data->TotalBlocks);
5571 FSData->f_bfree =
5572 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005573 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 FSData->f_bavail = FSData->f_bfree;
5575 } else {
5576 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005577 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
Steve French790fe572007-07-07 19:25:05 +00005579 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005581 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005582 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005584 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 }
5586 }
5587 cifs_buf_release(pSMB);
5588
5589 if (rc == -EAGAIN)
5590 goto QFSPosixRetry;
5591
5592 return rc;
5593}
5594
5595
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005596/*
5597 * We can not use write of zero bytes trick to set file size due to need for
5598 * large file support. Also note that this SetPathInfo is preferred to
5599 * SetFileInfo based method in next routine which is only needed to work around
5600 * a sharing violation bugin Samba which this routine can run into.
5601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005603CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005604 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5605 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
5607 struct smb_com_transaction2_spi_req *pSMB = NULL;
5608 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5609 struct file_end_of_file_info *parm_data;
5610 int name_len;
5611 int rc = 0;
5612 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005613 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 __u16 params, byte_count, data_count, param_offset, offset;
5616
Joe Perchesf96637b2013-05-04 22:12:25 -05005617 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618SetEOFRetry:
5619 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5620 (void **) &pSMBr);
5621 if (rc)
5622 return rc;
5623
5624 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5625 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005626 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5627 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 name_len++; /* trailing null */
5629 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10005630 } else {
5631 name_len = copy_path_name(pSMB->FileName, file_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 }
5633 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005634 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005636 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 pSMB->MaxSetupCount = 0;
5638 pSMB->Reserved = 0;
5639 pSMB->Flags = 0;
5640 pSMB->Timeout = 0;
5641 pSMB->Reserved2 = 0;
5642 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005643 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005645 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005646 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5647 pSMB->InformationLevel =
5648 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5649 else
5650 pSMB->InformationLevel =
5651 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5652 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5654 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005655 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 else
5657 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005658 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
5660
5661 parm_data =
5662 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5663 offset);
5664 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5665 pSMB->DataOffset = cpu_to_le16(offset);
5666 pSMB->SetupCount = 1;
5667 pSMB->Reserved3 = 0;
5668 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5669 byte_count = 3 /* pad */ + params + data_count;
5670 pSMB->DataCount = cpu_to_le16(data_count);
5671 pSMB->TotalDataCount = pSMB->DataCount;
5672 pSMB->ParameterCount = cpu_to_le16(params);
5673 pSMB->TotalParameterCount = pSMB->ParameterCount;
5674 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005675 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 parm_data->FileSize = cpu_to_le64(size);
5677 pSMB->ByteCount = cpu_to_le16(byte_count);
5678 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5679 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005680 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005681 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 cifs_buf_release(pSMB);
5684
5685 if (rc == -EAGAIN)
5686 goto SetEOFRetry;
5687
5688 return rc;
5689}
5690
5691int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005692CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5693 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
5695 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 struct file_end_of_file_info *parm_data;
5697 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 __u16 params, param_offset, offset, byte_count, count;
5699
Joe Perchesf96637b2013-05-04 22:12:25 -05005700 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5701 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005702 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 if (rc)
5705 return rc;
5706
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005707 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5708 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 params = 6;
5711 pSMB->MaxSetupCount = 0;
5712 pSMB->Reserved = 0;
5713 pSMB->Flags = 0;
5714 pSMB->Timeout = 0;
5715 pSMB->Reserved2 = 0;
5716 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5717 offset = param_offset + params;
5718
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 count = sizeof(struct file_end_of_file_info);
5720 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005721 /* BB find exact max SMB PDU from sess structure BB */
5722 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 pSMB->SetupCount = 1;
5724 pSMB->Reserved3 = 0;
5725 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5726 byte_count = 3 /* pad */ + params + count;
5727 pSMB->DataCount = cpu_to_le16(count);
5728 pSMB->ParameterCount = cpu_to_le16(params);
5729 pSMB->TotalDataCount = pSMB->DataCount;
5730 pSMB->TotalParameterCount = pSMB->ParameterCount;
5731 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5732 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005733 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5734 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 pSMB->DataOffset = cpu_to_le16(offset);
5736 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005737 pSMB->Fid = cfile->fid.netfid;
5738 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5740 pSMB->InformationLevel =
5741 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5742 else
5743 pSMB->InformationLevel =
5744 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005745 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5747 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005748 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 else
5750 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005751 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005754 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005756 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005757 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005759 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5760 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 }
5762
Steve French50c2f752007-07-13 00:33:32 +00005763 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 since file handle passed in no longer valid */
5765
5766 return rc;
5767}
5768
Steve French50c2f752007-07-13 00:33:32 +00005769/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 an open handle, rather than by pathname - this is awkward due to
5771 potential access conflicts on the open, but it is unavoidable for these
5772 old servers since the only other choice is to go from 100 nanosecond DCE
5773 time and resort to the original setpathinfo level which takes the ancient
5774 DOS time format with 2 second granularity */
5775int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005776CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005777 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
5779 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 char *data_offset;
5781 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 __u16 params, param_offset, offset, byte_count, count;
5783
Joe Perchesf96637b2013-05-04 22:12:25 -05005784 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005785 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5786
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 if (rc)
5788 return rc;
5789
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005790 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5791 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005792
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 params = 6;
5794 pSMB->MaxSetupCount = 0;
5795 pSMB->Reserved = 0;
5796 pSMB->Flags = 0;
5797 pSMB->Timeout = 0;
5798 pSMB->Reserved2 = 0;
5799 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5800 offset = param_offset + params;
5801
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005802 data_offset = (char *)pSMB +
5803 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
Steve French26f57362007-08-30 22:09:15 +00005805 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005807 /* BB find max SMB PDU from sess */
5808 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 pSMB->SetupCount = 1;
5810 pSMB->Reserved3 = 0;
5811 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5812 byte_count = 3 /* pad */ + params + count;
5813 pSMB->DataCount = cpu_to_le16(count);
5814 pSMB->ParameterCount = cpu_to_le16(params);
5815 pSMB->TotalDataCount = pSMB->DataCount;
5816 pSMB->TotalParameterCount = pSMB->ParameterCount;
5817 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5818 pSMB->DataOffset = cpu_to_le16(offset);
5819 pSMB->Fid = fid;
5820 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5821 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5822 else
5823 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5824 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005825 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005827 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005828 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005829 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005830 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005831 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5832 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Steve French50c2f752007-07-13 00:33:32 +00005834 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 since file handle passed in no longer valid */
5836
5837 return rc;
5838}
5839
Jeff Layton6d22f092008-09-23 11:48:35 -04005840int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005841CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005842 bool delete_file, __u16 fid, __u32 pid_of_opener)
5843{
5844 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5845 char *data_offset;
5846 int rc = 0;
5847 __u16 params, param_offset, offset, byte_count, count;
5848
Joe Perchesf96637b2013-05-04 22:12:25 -05005849 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005850 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5851
5852 if (rc)
5853 return rc;
5854
5855 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5856 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5857
5858 params = 6;
5859 pSMB->MaxSetupCount = 0;
5860 pSMB->Reserved = 0;
5861 pSMB->Flags = 0;
5862 pSMB->Timeout = 0;
5863 pSMB->Reserved2 = 0;
5864 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5865 offset = param_offset + params;
5866
5867 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5868
5869 count = 1;
5870 pSMB->MaxParameterCount = cpu_to_le16(2);
5871 /* BB find max SMB PDU from sess */
5872 pSMB->MaxDataCount = cpu_to_le16(1000);
5873 pSMB->SetupCount = 1;
5874 pSMB->Reserved3 = 0;
5875 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5876 byte_count = 3 /* pad */ + params + count;
5877 pSMB->DataCount = cpu_to_le16(count);
5878 pSMB->ParameterCount = cpu_to_le16(params);
5879 pSMB->TotalDataCount = pSMB->DataCount;
5880 pSMB->TotalParameterCount = pSMB->ParameterCount;
5881 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5882 pSMB->DataOffset = cpu_to_le16(offset);
5883 pSMB->Fid = fid;
5884 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5885 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005886 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005887 pSMB->ByteCount = cpu_to_le16(byte_count);
5888 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005889 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005890 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005891 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005892 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005893
5894 return rc;
5895}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
5897int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005898CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005899 const char *fileName, const FILE_BASIC_INFO *data,
5900 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901{
5902 TRANSACTION2_SPI_REQ *pSMB = NULL;
5903 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5904 int name_len;
5905 int rc = 0;
5906 int bytes_returned = 0;
5907 char *data_offset;
5908 __u16 params, param_offset, offset, byte_count, count;
5909
Joe Perchesf96637b2013-05-04 22:12:25 -05005910 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
5912SetTimesRetry:
5913 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5914 (void **) &pSMBr);
5915 if (rc)
5916 return rc;
5917
5918 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5919 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005920 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5921 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 name_len++; /* trailing null */
5923 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10005924 } else {
5925 name_len = copy_path_name(pSMB->FileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 }
5927
5928 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005929 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005931 /* BB find max SMB PDU from sess structure BB */
5932 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 pSMB->MaxSetupCount = 0;
5934 pSMB->Reserved = 0;
5935 pSMB->Flags = 0;
5936 pSMB->Timeout = 0;
5937 pSMB->Reserved2 = 0;
5938 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005939 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 offset = param_offset + params;
5941 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5942 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5943 pSMB->DataOffset = cpu_to_le16(offset);
5944 pSMB->SetupCount = 1;
5945 pSMB->Reserved3 = 0;
5946 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5947 byte_count = 3 /* pad */ + params + count;
5948
5949 pSMB->DataCount = cpu_to_le16(count);
5950 pSMB->ParameterCount = cpu_to_le16(params);
5951 pSMB->TotalDataCount = pSMB->DataCount;
5952 pSMB->TotalParameterCount = pSMB->ParameterCount;
5953 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5954 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5955 else
5956 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5957 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005958 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005959 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 pSMB->ByteCount = cpu_to_le16(byte_count);
5961 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5962 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005963 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005964 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
5966 cifs_buf_release(pSMB);
5967
5968 if (rc == -EAGAIN)
5969 goto SetTimesRetry;
5970
5971 return rc;
5972}
5973
5974/* Can not be used to set time stamps yet (due to old DOS time format) */
5975/* Can be used to set attributes */
5976#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5977 handling it anyway and NT4 was what we thought it would be needed for
5978 Do not delete it until we prove whether needed for Win9x though */
5979int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005980CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 __u16 dos_attrs, const struct nls_table *nls_codepage)
5982{
5983 SETATTR_REQ *pSMB = NULL;
5984 SETATTR_RSP *pSMBr = NULL;
5985 int rc = 0;
5986 int bytes_returned;
5987 int name_len;
5988
Joe Perchesf96637b2013-05-04 22:12:25 -05005989 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
5991SetAttrLgcyRetry:
5992 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5993 (void **) &pSMBr);
5994 if (rc)
5995 return rc;
5996
5997 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5998 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005999 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
6000 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 name_len++; /* trailing null */
6002 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006003 } else {
6004 name_len = copy_path_name(pSMB->fileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 }
6006 pSMB->attr = cpu_to_le16(dos_attrs);
6007 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006008 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 pSMB->ByteCount = cpu_to_le16(name_len + 1);
6010 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6011 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006012 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006013 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 cifs_buf_release(pSMB);
6016
6017 if (rc == -EAGAIN)
6018 goto SetAttrLgcyRetry;
6019
6020 return rc;
6021}
6022#endif /* temporarily unneeded SetAttr legacy function */
6023
Jeff Layton654cf142009-07-09 20:02:49 -04006024static void
6025cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6026 const struct cifs_unix_set_info_args *args)
6027{
Eric W. Biederman49418b22013-02-06 00:57:56 -08006028 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04006029 u64 mode = args->mode;
6030
Eric W. Biederman49418b22013-02-06 00:57:56 -08006031 if (uid_valid(args->uid))
6032 uid = from_kuid(&init_user_ns, args->uid);
6033 if (gid_valid(args->gid))
6034 gid = from_kgid(&init_user_ns, args->gid);
6035
Jeff Layton654cf142009-07-09 20:02:49 -04006036 /*
6037 * Samba server ignores set of file size to zero due to bugs in some
6038 * older clients, but we should be precise - we use SetFileSize to
6039 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006040 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04006041 * zero instead of -1 here
6042 */
6043 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6044 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6045 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6046 data_offset->LastAccessTime = cpu_to_le64(args->atime);
6047 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08006048 data_offset->Uid = cpu_to_le64(uid);
6049 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04006050 /* better to leave device as zero when it is */
6051 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6052 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6053 data_offset->Permissions = cpu_to_le64(mode);
6054
6055 if (S_ISREG(mode))
6056 data_offset->Type = cpu_to_le32(UNIX_FILE);
6057 else if (S_ISDIR(mode))
6058 data_offset->Type = cpu_to_le32(UNIX_DIR);
6059 else if (S_ISLNK(mode))
6060 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6061 else if (S_ISCHR(mode))
6062 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6063 else if (S_ISBLK(mode))
6064 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6065 else if (S_ISFIFO(mode))
6066 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6067 else if (S_ISSOCK(mode))
6068 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6069}
6070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006072CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006073 const struct cifs_unix_set_info_args *args,
6074 u16 fid, u32 pid_of_opener)
6075{
6076 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006077 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006078 int rc = 0;
6079 u16 params, param_offset, offset, byte_count, count;
6080
Joe Perchesf96637b2013-05-04 22:12:25 -05006081 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006082 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6083
6084 if (rc)
6085 return rc;
6086
6087 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6088 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6089
6090 params = 6;
6091 pSMB->MaxSetupCount = 0;
6092 pSMB->Reserved = 0;
6093 pSMB->Flags = 0;
6094 pSMB->Timeout = 0;
6095 pSMB->Reserved2 = 0;
6096 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6097 offset = param_offset + params;
6098
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006099 data_offset = (char *)pSMB +
6100 offsetof(struct smb_hdr, Protocol) + offset;
6101
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006102 count = sizeof(FILE_UNIX_BASIC_INFO);
6103
6104 pSMB->MaxParameterCount = cpu_to_le16(2);
6105 /* BB find max SMB PDU from sess */
6106 pSMB->MaxDataCount = cpu_to_le16(1000);
6107 pSMB->SetupCount = 1;
6108 pSMB->Reserved3 = 0;
6109 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6110 byte_count = 3 /* pad */ + params + count;
6111 pSMB->DataCount = cpu_to_le16(count);
6112 pSMB->ParameterCount = cpu_to_le16(params);
6113 pSMB->TotalDataCount = pSMB->DataCount;
6114 pSMB->TotalParameterCount = pSMB->ParameterCount;
6115 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6116 pSMB->DataOffset = cpu_to_le16(offset);
6117 pSMB->Fid = fid;
6118 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6119 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006120 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006121 pSMB->ByteCount = cpu_to_le16(byte_count);
6122
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006123 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006124
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006125 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006126 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006127 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006128 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6129 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006130
6131 /* Note: On -EAGAIN error only caller can retry on handle based calls
6132 since file handle passed in no longer valid */
6133
6134 return rc;
6135}
6136
6137int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006138CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006139 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006140 const struct cifs_unix_set_info_args *args,
6141 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142{
6143 TRANSACTION2_SPI_REQ *pSMB = NULL;
6144 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6145 int name_len;
6146 int rc = 0;
6147 int bytes_returned = 0;
6148 FILE_UNIX_BASIC_INFO *data_offset;
6149 __u16 params, param_offset, offset, count, byte_count;
6150
Joe Perchesf96637b2013-05-04 22:12:25 -05006151 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152setPermsRetry:
6153 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6154 (void **) &pSMBr);
6155 if (rc)
6156 return rc;
6157
6158 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6159 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006160 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006161 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 name_len++; /* trailing null */
6163 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006164 } else {
6165 name_len = copy_path_name(pSMB->FileName, file_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 }
6167
6168 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006169 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006171 /* BB find max SMB PDU from sess structure BB */
6172 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 pSMB->MaxSetupCount = 0;
6174 pSMB->Reserved = 0;
6175 pSMB->Flags = 0;
6176 pSMB->Timeout = 0;
6177 pSMB->Reserved2 = 0;
6178 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006179 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 offset = param_offset + params;
6181 data_offset =
6182 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6183 offset);
6184 memset(data_offset, 0, count);
6185 pSMB->DataOffset = cpu_to_le16(offset);
6186 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6187 pSMB->SetupCount = 1;
6188 pSMB->Reserved3 = 0;
6189 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6190 byte_count = 3 /* pad */ + params + count;
6191 pSMB->ParameterCount = cpu_to_le16(params);
6192 pSMB->DataCount = cpu_to_le16(count);
6193 pSMB->TotalParameterCount = pSMB->ParameterCount;
6194 pSMB->TotalDataCount = pSMB->DataCount;
6195 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6196 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006197 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006198
Jeff Layton654cf142009-07-09 20:02:49 -04006199 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201 pSMB->ByteCount = cpu_to_le16(byte_count);
6202 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6203 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006204 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006205 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
Steve French0d817bc2008-05-22 02:02:03 +00006207 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 if (rc == -EAGAIN)
6209 goto setPermsRetry;
6210 return rc;
6211}
6212
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006214/*
6215 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6216 * function used by listxattr and getxattr type calls. When ea_name is set,
6217 * it looks for that attribute name and stuffs that value into the EAData
6218 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6219 * buffer. In both cases, the return value is either the length of the
6220 * resulting data or a negative error code. If EAData is a NULL pointer then
6221 * the data isn't copied to it, but the length is returned.
6222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006224CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006225 const unsigned char *searchName, const unsigned char *ea_name,
6226 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006227 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228{
6229 /* BB assumes one setup word */
6230 TRANSACTION2_QPI_REQ *pSMB = NULL;
6231 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006232 int remap = cifs_remap(cifs_sb);
6233 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 int rc = 0;
6235 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006236 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006237 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006238 struct fea *temp_fea;
6239 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006240 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006241 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006242 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
Joe Perchesf96637b2013-05-04 22:12:25 -05006244 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245QAllEAsRetry:
6246 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6247 (void **) &pSMBr);
6248 if (rc)
6249 return rc;
6250
6251 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006252 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006253 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6254 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006255 list_len++; /* trailing null */
6256 list_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006257 } else {
6258 list_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 }
6260
Jeff Layton6e462b92010-02-10 16:18:26 -05006261 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 pSMB->TotalDataCount = 0;
6263 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006264 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006265 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 pSMB->MaxSetupCount = 0;
6267 pSMB->Reserved = 0;
6268 pSMB->Flags = 0;
6269 pSMB->Timeout = 0;
6270 pSMB->Reserved2 = 0;
6271 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006272 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 pSMB->DataCount = 0;
6274 pSMB->DataOffset = 0;
6275 pSMB->SetupCount = 1;
6276 pSMB->Reserved3 = 0;
6277 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6278 byte_count = params + 1 /* pad */ ;
6279 pSMB->TotalParameterCount = cpu_to_le16(params);
6280 pSMB->ParameterCount = pSMB->TotalParameterCount;
6281 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6282 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006283 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 pSMB->ByteCount = cpu_to_le16(byte_count);
6285
6286 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6287 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6288 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006289 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006290 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006292
6293
6294 /* BB also check enough total bytes returned */
6295 /* BB we need to improve the validity checking
6296 of these trans2 responses */
6297
6298 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006299 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006300 rc = -EIO; /* bad smb */
6301 goto QAllEAsOut;
6302 }
6303
6304 /* check that length of list is not more than bcc */
6305 /* check that each entry does not go beyond length
6306 of list */
6307 /* check that each element of each entry does not
6308 go beyond end of list */
6309 /* validate_trans2_offsets() */
6310 /* BB check if start of smb + data_offset > &bcc+ bcc */
6311
6312 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6313 ea_response_data = (struct fealist *)
6314 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6315
Jeff Layton6e462b92010-02-10 16:18:26 -05006316 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006317 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006318 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006319 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006320 /* didn't find the named attribute */
6321 if (ea_name)
6322 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006323 goto QAllEAsOut;
6324 }
6325
Jeff Layton0cd126b2010-02-10 16:18:26 -05006326 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006327 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006328 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006329 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006330 rc = -EIO;
6331 goto QAllEAsOut;
6332 }
6333
Jeff Laytonf0d38682010-02-10 16:18:26 -05006334 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006335 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006336 temp_fea = ea_response_data->list;
6337 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006338 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006339 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006340 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006341
Jeff Layton6e462b92010-02-10 16:18:26 -05006342 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006343 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006344 /* make sure we can read name_len and value_len */
6345 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006346 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006347 rc = -EIO;
6348 goto QAllEAsOut;
6349 }
6350
6351 name_len = temp_fea->name_len;
6352 value_len = le16_to_cpu(temp_fea->value_len);
6353 list_len -= name_len + 1 + value_len;
6354 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006355 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006356 rc = -EIO;
6357 goto QAllEAsOut;
6358 }
6359
Jeff Layton31c05192010-02-10 16:18:26 -05006360 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006361 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006362 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006363 temp_ptr += name_len + 1;
6364 rc = value_len;
6365 if (buf_size == 0)
6366 goto QAllEAsOut;
6367 if ((size_t)value_len > buf_size) {
6368 rc = -ERANGE;
6369 goto QAllEAsOut;
6370 }
6371 memcpy(EAData, temp_ptr, value_len);
6372 goto QAllEAsOut;
6373 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006374 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006375 /* account for prefix user. and trailing null */
6376 rc += (5 + 1 + name_len);
6377 if (rc < (int) buf_size) {
6378 memcpy(EAData, "user.", 5);
6379 EAData += 5;
6380 memcpy(EAData, temp_ptr, name_len);
6381 EAData += name_len;
6382 /* null terminate name */
6383 *EAData = 0;
6384 ++EAData;
6385 } else if (buf_size == 0) {
6386 /* skip copy - calc size only */
6387 } else {
6388 /* stop before overrun buffer */
6389 rc = -ERANGE;
6390 break;
6391 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006392 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006393 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006394 temp_fea = (struct fea *)temp_ptr;
6395 }
6396
Jeff Layton31c05192010-02-10 16:18:26 -05006397 /* didn't find the named attribute */
6398 if (ea_name)
6399 rc = -ENODATA;
6400
Jeff Laytonf0d38682010-02-10 16:18:26 -05006401QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006402 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 if (rc == -EAGAIN)
6404 goto QAllEAsRetry;
6405
6406 return (ssize_t)rc;
6407}
6408
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006410CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6411 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006412 const __u16 ea_value_len, const struct nls_table *nls_codepage,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006413 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414{
6415 struct smb_com_transaction2_spi_req *pSMB = NULL;
6416 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6417 struct fealist *parm_data;
6418 int name_len;
6419 int rc = 0;
6420 int bytes_returned = 0;
6421 __u16 params, param_offset, byte_count, offset, count;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006422 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
Joe Perchesf96637b2013-05-04 22:12:25 -05006424 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425SetEARetry:
6426 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6427 (void **) &pSMBr);
6428 if (rc)
6429 return rc;
6430
6431 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6432 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006433 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6434 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 name_len++; /* trailing null */
6436 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006437 } else {
6438 name_len = copy_path_name(pSMB->FileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 }
6440
6441 params = 6 + name_len;
6442
6443 /* done calculating parms using name_len of file name,
6444 now use name_len to calculate length of ea name
6445 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006446 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 name_len = 0;
6448 else
Steve French50c2f752007-07-13 00:33:32 +00006449 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006451 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006453 /* BB find max SMB PDU from sess */
6454 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 pSMB->MaxSetupCount = 0;
6456 pSMB->Reserved = 0;
6457 pSMB->Flags = 0;
6458 pSMB->Timeout = 0;
6459 pSMB->Reserved2 = 0;
6460 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006461 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 offset = param_offset + params;
6463 pSMB->InformationLevel =
6464 cpu_to_le16(SMB_SET_FILE_EA);
6465
Arnd Bergmannade7db92018-02-02 16:48:47 +01006466 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6468 pSMB->DataOffset = cpu_to_le16(offset);
6469 pSMB->SetupCount = 1;
6470 pSMB->Reserved3 = 0;
6471 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6472 byte_count = 3 /* pad */ + params + count;
6473 pSMB->DataCount = cpu_to_le16(count);
6474 parm_data->list_len = cpu_to_le32(count);
6475 parm_data->list[0].EA_flags = 0;
6476 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006477 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006479 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006480 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 parm_data->list[0].name[name_len] = 0;
6482 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6483 /* caller ensures that ea_value_len is less than 64K but
6484 we need to ensure that it fits within the smb */
6485
Steve French50c2f752007-07-13 00:33:32 +00006486 /*BB add length check to see if it would fit in
6487 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006488 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6489 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006490 memcpy(parm_data->list[0].name+name_len+1,
6491 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
6493 pSMB->TotalDataCount = pSMB->DataCount;
6494 pSMB->ParameterCount = cpu_to_le16(params);
6495 pSMB->TotalParameterCount = pSMB->ParameterCount;
6496 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006497 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 pSMB->ByteCount = cpu_to_le16(byte_count);
6499 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6500 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006501 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006502 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503
6504 cifs_buf_release(pSMB);
6505
6506 if (rc == -EAGAIN)
6507 goto SetEARetry;
6508
6509 return rc;
6510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511#endif