blob: e2f95965065d1d7fd42c8116d0d57cecb097bd6c [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"
Jeff Laytone28bc5b2011-10-19 15:30:07 -040045#include "fscache.h"
Long Lidb223a52017-11-22 17:38:45 -070046#include "smbdirect.h"
Paulo Alcantara08744012018-11-14 17:24:29 -020047#ifdef CONFIG_CIFS_DFS_UPCALL
48#include "dfs_cache.h"
49#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#ifdef CONFIG_CIFS_POSIX
52static struct {
53 int index;
54 char *name;
55} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000056#ifdef CONFIG_CIFS_WEAK_PW_HASH
57 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000058 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000059#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000060 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000061 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 {BAD_PROT, "\2"}
63};
64#else
65static struct {
66 int index;
67 char *name;
68} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000069#ifdef CONFIG_CIFS_WEAK_PW_HASH
70 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000071 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000072#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000073 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 {BAD_PROT, "\2"}
75};
76#endif
77
Steve French39798772006-05-31 22:40:51 +000078/* define the number of elements in the cifs dialect array */
79#ifdef CONFIG_CIFS_POSIX
80#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000081#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000082#else
83#define CIFS_NUM_PROT 2
84#endif /* CIFS_WEAK_PW_HASH */
85#else /* not posix */
86#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000087#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000088#else
89#define CIFS_NUM_PROT 1
90#endif /* CONFIG_CIFS_WEAK_PW_HASH */
91#endif /* CIFS_POSIX */
92
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +040093/*
94 * Mark as invalid, all open files on tree connections since they
95 * were closed when session to server was lost.
96 */
97void
98cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +0000101 struct list_head *tmp;
102 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400104 /* list all files open on tree connection and mark them invalid */
Steve French3afca262016-09-22 18:58:16 -0500105 spin_lock(&tcon->open_file_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400106 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000107 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000108 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400109 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Steve French3afca262016-09-22 18:58:16 -0500111 spin_unlock(&tcon->open_file_lock);
Steve French3d4ef9a2018-04-25 22:19:09 -0500112
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000113 mutex_lock(&tcon->crfid.fid_mutex);
114 tcon->crfid.is_valid = false;
115 memset(tcon->crfid.fid, 0, sizeof(struct cifs_fid));
116 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500117
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400118 /*
119 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
120 * to this tcon.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Paulo Alcantara08744012018-11-14 17:24:29 -0200124#ifdef CONFIG_CIFS_DFS_UPCALL
125static int __cifs_reconnect_tcon(const struct nls_table *nlsc,
126 struct cifs_tcon *tcon)
127{
128 int rc;
129 struct dfs_cache_tgt_list tl;
130 struct dfs_cache_tgt_iterator *it = NULL;
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100131 char *tree;
Paulo Alcantara08744012018-11-14 17:24:29 -0200132 const char *tcp_host;
133 size_t tcp_host_len;
134 const char *dfs_host;
135 size_t dfs_host_len;
136
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100137 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
138 if (!tree)
139 return -ENOMEM;
140
Paulo Alcantara08744012018-11-14 17:24:29 -0200141 if (tcon->ipc) {
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000142 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
143 tcon->ses->server->hostname);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100144 rc = CIFSTCon(0, tcon->ses, tree, tcon, nlsc);
145 goto out;
Paulo Alcantara08744012018-11-14 17:24:29 -0200146 }
147
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100148 if (!tcon->dfs_path) {
149 rc = CIFSTCon(0, tcon->ses, tcon->treeName, tcon, nlsc);
150 goto out;
151 }
Paulo Alcantara08744012018-11-14 17:24:29 -0200152
153 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, NULL, &tl);
154 if (rc)
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100155 goto out;
Paulo Alcantara08744012018-11-14 17:24:29 -0200156
157 extract_unc_hostname(tcon->ses->server->hostname, &tcp_host,
158 &tcp_host_len);
159
160 for (it = dfs_cache_get_tgt_iterator(&tl); it;
161 it = dfs_cache_get_next_tgt(&tl, it)) {
162 const char *tgt = dfs_cache_get_tgt_name(it);
163
164 extract_unc_hostname(tgt, &dfs_host, &dfs_host_len);
165
166 if (dfs_host_len != tcp_host_len
167 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
168 cifs_dbg(FYI, "%s: skipping %.*s, doesn't match %.*s",
169 __func__,
170 (int)dfs_host_len, dfs_host,
171 (int)tcp_host_len, tcp_host);
172 continue;
173 }
174
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000175 scnprintf(tree, MAX_TREE_SIZE, "\\%s", tgt);
Paulo Alcantara08744012018-11-14 17:24:29 -0200176
177 rc = CIFSTCon(0, tcon->ses, tree, tcon, nlsc);
178 if (!rc)
179 break;
180 if (rc == -EREMOTE)
181 break;
182 }
183
184 if (!rc) {
185 if (it)
186 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1,
187 it);
188 else
189 rc = -ENOENT;
190 }
191 dfs_cache_free_tgts(&tl);
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100192out:
193 kfree(tree);
Paulo Alcantara08744012018-11-14 17:24:29 -0200194 return rc;
195}
196#else
197static inline int __cifs_reconnect_tcon(const struct nls_table *nlsc,
198 struct cifs_tcon *tcon)
199{
200 return CIFSTCon(0, tcon->ses, tcon->treeName, tcon, nlsc);
201}
202#endif
203
Jeff Layton9162ab22009-09-03 12:07:17 -0400204/* reconnect the socket, tcon, and smb session if needed */
205static int
Steve French96daf2b2011-05-27 04:34:02 +0000206cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400207{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400208 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000209 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400210 struct TCP_Server_Info *server;
211 struct nls_table *nls_codepage;
Paulo Alcantara08744012018-11-14 17:24:29 -0200212 int retries;
Jeff Layton9162ab22009-09-03 12:07:17 -0400213
214 /*
215 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
216 * tcp and smb session status done differently for those three - in the
217 * calling routine
218 */
219 if (!tcon)
220 return 0;
221
222 ses = tcon->ses;
223 server = ses->server;
224
225 /*
226 * only tree disconnect, open, and write, (and ulogoff which does not
227 * have tcon) are allowed as we start force umount
228 */
229 if (tcon->tidStatus == CifsExiting) {
230 if (smb_command != SMB_COM_WRITE_ANDX &&
231 smb_command != SMB_COM_OPEN_ANDX &&
232 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500233 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
234 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400235 return -ENODEV;
236 }
237 }
238
Paulo Alcantara08744012018-11-14 17:24:29 -0200239 retries = server->nr_targets;
240
Jeff Layton9162ab22009-09-03 12:07:17 -0400241 /*
Paulo Alcantara08744012018-11-14 17:24:29 -0200242 * Give demultiplex thread up to 10 seconds to each target available for
243 * reconnect -- should be greater than cifs socket timeout which is 7
244 * seconds.
Jeff Layton9162ab22009-09-03 12:07:17 -0400245 */
246 while (server->tcpStatus == CifsNeedReconnect) {
Paulo Alcantara7ffbe652018-07-05 13:46:34 -0300247 rc = wait_event_interruptible_timeout(server->response_q,
248 (server->tcpStatus != CifsNeedReconnect),
249 10 * HZ);
250 if (rc < 0) {
251 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
252 " signal by the process\n", __func__);
253 return -ERESTARTSYS;
254 }
Jeff Layton9162ab22009-09-03 12:07:17 -0400255
Steve Frenchfd88ce92011-04-12 01:01:14 +0000256 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400257 if (server->tcpStatus != CifsNeedReconnect)
258 break;
259
Paulo Alcantara08744012018-11-14 17:24:29 -0200260 if (--retries)
261 continue;
262
Jeff Layton9162ab22009-09-03 12:07:17 -0400263 /*
264 * on "soft" mounts we wait once. Hard mounts keep
265 * retrying until process is killed or server comes
266 * back on-line
267 */
Jeff Laytond4025392011-02-07 08:54:35 -0500268 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500269 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400270 return -EHOSTDOWN;
271 }
Paulo Alcantara08744012018-11-14 17:24:29 -0200272 retries = server->nr_targets;
Jeff Layton9162ab22009-09-03 12:07:17 -0400273 }
274
275 if (!ses->need_reconnect && !tcon->need_reconnect)
276 return 0;
277
278 nls_codepage = load_nls_default();
279
280 /*
281 * need to prevent multiple threads trying to simultaneously
282 * reconnect the same SMB session
283 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000284 mutex_lock(&ses->session_mutex);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200285
286 /*
287 * Recheck after acquire mutex. If another thread is negotiating
288 * and the server never sends an answer the socket will be closed
289 * and tcpStatus set to reconnect.
290 */
291 if (server->tcpStatus == CifsNeedReconnect) {
292 rc = -EHOSTDOWN;
293 mutex_unlock(&ses->session_mutex);
294 goto out;
295 }
296
Jeff Layton198b5682010-04-24 07:57:48 -0400297 rc = cifs_negotiate_protocol(0, ses);
298 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400299 rc = cifs_setup_session(0, ses, nls_codepage);
300
301 /* do we need to reconnect tcon? */
302 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000303 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400304 goto out;
305 }
306
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400307 cifs_mark_open_files_invalid(tcon);
Paulo Alcantara08744012018-11-14 17:24:29 -0200308 rc = __cifs_reconnect_tcon(nls_codepage, tcon);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000309 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500310 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400311
Steve Frenchc318e6c2018-04-04 14:08:52 -0500312 if (rc) {
313 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400314 goto out;
Steve Frenchc318e6c2018-04-04 14:08:52 -0500315 }
Jeff Layton9162ab22009-09-03 12:07:17 -0400316
Jeff Layton9162ab22009-09-03 12:07:17 -0400317 atomic_inc(&tconInfoReconnectCount);
318
319 /* tell server Unix caps we support */
320 if (ses->capabilities & CAP_UNIX)
321 reset_cifs_unix_caps(0, tcon, NULL, NULL);
322
323 /*
324 * Removed call to reopen open files here. It is safer (and faster) to
325 * reopen files one at a time as needed in read and write.
326 *
327 * FIXME: what about file locks? don't we need to reclaim them ASAP?
328 */
329
330out:
331 /*
332 * Check if handle based operation so we know whether we can continue
333 * or not without returning to caller to reset file handle
334 */
335 switch (smb_command) {
336 case SMB_COM_READ_ANDX:
337 case SMB_COM_WRITE_ANDX:
338 case SMB_COM_CLOSE:
339 case SMB_COM_FIND_CLOSE2:
340 case SMB_COM_LOCKING_ANDX:
341 rc = -EAGAIN;
342 }
343
344 unload_nls(nls_codepage);
345 return rc;
346}
347
Steve Frenchad7a2922008-02-07 23:25:02 +0000348/* Allocate and return pointer to an SMB request buffer, and set basic
349 SMB information in the SMB header. If the return code is zero, this
350 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static int
Steve French96daf2b2011-05-27 04:34:02 +0000352small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000353 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Jeff Laytonf5695992010-09-29 15:27:08 -0400355 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jeff Layton9162ab22009-09-03 12:07:17 -0400357 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000358 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return rc;
360
361 *request_buf = cifs_small_buf_get();
362 if (*request_buf == NULL) {
363 /* BB should we add a retry in here if not a writepage? */
364 return -ENOMEM;
365 }
366
Steve French63135e02007-07-17 17:34:02 +0000367 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000368 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Steve French790fe572007-07-07 19:25:05 +0000370 if (tcon != NULL)
371 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700372
Jeff Laytonf5695992010-09-29 15:27:08 -0400373 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000374}
375
Steve French12b3b8f2006-02-09 21:12:47 +0000376int
Steve French50c2f752007-07-13 00:33:32 +0000377small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000378 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000379{
380 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000381 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000382
Steve French5815449d2006-02-14 01:36:20 +0000383 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000384 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000385 return rc;
386
Steve French04fdabe2006-02-10 05:52:50 +0000387 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400388 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000389 if (ses->capabilities & CAP_UNICODE)
390 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000391 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000392 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
393
394 /* uid, tid can stay at zero as set in header assemble */
395
Steve French50c2f752007-07-13 00:33:32 +0000396 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000397 this function is used after 1st of session setup requests */
398
399 return rc;
400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/* If the return code is zero, this function must fill in request_buf pointer */
403static int
Steve French96daf2b2011-05-27 04:34:02 +0000404__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400405 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *request_buf = cifs_buf_get();
408 if (*request_buf == NULL) {
409 /* BB should we add a retry in here if not a writepage? */
410 return -ENOMEM;
411 }
412 /* Although the original thought was we needed the response buf for */
413 /* potential retries of smb operations it turns out we can determine */
414 /* from the mid flags when the request buffer can be resent without */
415 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000416 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000417 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000420 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Steve French790fe572007-07-07 19:25:05 +0000422 if (tcon != NULL)
423 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700424
Jeff Laytonf5695992010-09-29 15:27:08 -0400425 return 0;
426}
427
428/* If the return code is zero, this function must fill in request_buf pointer */
429static int
Steve French96daf2b2011-05-27 04:34:02 +0000430smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400431 void **request_buf, void **response_buf)
432{
433 int rc;
434
435 rc = cifs_reconnect_tcon(tcon, smb_command);
436 if (rc)
437 return rc;
438
439 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
440}
441
442static int
Steve French96daf2b2011-05-27 04:34:02 +0000443smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400444 void **request_buf, void **response_buf)
445{
446 if (tcon->ses->need_reconnect || tcon->need_reconnect)
447 return -EHOSTDOWN;
448
449 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Steve French50c2f752007-07-13 00:33:32 +0000452static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Jeff Layton12df83c2011-01-20 13:36:51 -0500454 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jeff Layton12df83c2011-01-20 13:36:51 -0500456 /* check for plausible wct */
457 if (pSMB->hdr.WordCount < 10)
458 goto vt2_err;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500461 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
462 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
463 goto vt2_err;
464
Jeff Layton12df83c2011-01-20 13:36:51 -0500465 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
466 if (total_size >= 512)
467 goto vt2_err;
468
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400469 /* check that bcc is at least as big as parms + data, and that it is
470 * less than negotiated smb buffer
471 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500472 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
473 if (total_size > get_bcc(&pSMB->hdr) ||
474 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
475 goto vt2_err;
476
477 return 0;
478vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000479 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500481 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Jeff Layton690c5222011-01-20 13:36:51 -0500483
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400484static int
Jeff Layton3f618222013-06-12 19:52:14 -0500485decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400486{
487 int rc = 0;
488 u16 count;
489 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500490 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400491
492 count = get_bcc(&pSMBr->hdr);
493 if (count < SMB1_CLIENT_GUID_SIZE)
494 return -EIO;
495
496 spin_lock(&cifs_tcp_ses_lock);
497 if (server->srv_count > 1) {
498 spin_unlock(&cifs_tcp_ses_lock);
499 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
500 cifs_dbg(FYI, "server UID changed\n");
501 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
502 }
503 } else {
504 spin_unlock(&cifs_tcp_ses_lock);
505 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
506 }
507
508 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500509 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400510 } else {
511 count -= SMB1_CLIENT_GUID_SIZE;
512 rc = decode_negTokenInit(
513 pSMBr->u.extended_response.SecurityBlob, count, server);
514 if (rc != 1)
515 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400516 }
517
518 return 0;
519}
520
Jeff Layton9ddec562013-05-26 07:00:58 -0400521int
Jeff Layton38d77c52013-05-26 07:01:00 -0400522cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400523{
Jeff Layton502858822013-06-27 12:45:00 -0400524 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
525 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400526 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
527
528 /*
529 * Is signing required by mnt options? If not then check
530 * global_secflags to see if it is there.
531 */
532 if (!mnt_sign_required)
533 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
534 CIFSSEC_MUST_SIGN);
535
536 /*
537 * If signing is required then it's automatically enabled too,
538 * otherwise, check to see if the secflags allow it.
539 */
540 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
541 (global_secflags & CIFSSEC_MAY_SIGN);
542
543 /* If server requires signing, does client allow it? */
544 if (srv_sign_required) {
545 if (!mnt_sign_enabled) {
546 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
547 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400548 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400549 server->sign = true;
550 }
551
552 /* If client requires signing, does server allow it? */
553 if (mnt_sign_required) {
554 if (!srv_sign_enabled) {
555 cifs_dbg(VFS, "Server does not support signing!");
556 return -ENOTSUPP;
557 }
558 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400559 }
560
Long Libb4c0412018-04-17 12:17:08 -0700561 if (cifs_rdma_enabled(server) && server->sign)
562 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled");
563
Jeff Layton9ddec562013-05-26 07:00:58 -0400564 return 0;
565}
566
Jeff Layton2190eca2013-05-26 07:00:57 -0400567#ifdef CONFIG_CIFS_WEAK_PW_HASH
568static int
Jeff Layton3f618222013-06-12 19:52:14 -0500569decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400570{
571 __s16 tmp;
572 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
573
574 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
575 return -EOPNOTSUPP;
576
Jeff Layton2190eca2013-05-26 07:00:57 -0400577 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
578 server->maxReq = min_t(unsigned int,
579 le16_to_cpu(rsp->MaxMpxCount),
580 cifs_max_pending);
581 set_credits(server, server->maxReq);
582 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Jeff Layton2190eca2013-05-26 07:00:57 -0400583 /* even though we do not use raw we might as well set this
584 accurately, in case we ever find a need for it */
585 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
586 server->max_rw = 0xFF00;
587 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
588 } else {
589 server->max_rw = 0;/* do not need to use raw anyway */
590 server->capabilities = CAP_MPX_MODE;
591 }
592 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
593 if (tmp == -1) {
594 /* OS/2 often does not set timezone therefore
595 * we must use server time to calc time zone.
596 * Could deviate slightly from the right zone.
597 * Smallest defined timezone difference is 15 minutes
598 * (i.e. Nepal). Rounding up/down is done to match
599 * this requirement.
600 */
601 int val, seconds, remain, result;
Arnd Bergmann95390202018-06-19 17:27:58 +0200602 struct timespec64 ts;
603 time64_t utc = ktime_get_real_seconds();
Jeff Layton2190eca2013-05-26 07:00:57 -0400604 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
605 rsp->SrvTime.Time, 0);
Arnd Bergmann95390202018-06-19 17:27:58 +0200606 cifs_dbg(FYI, "SrvTime %lld sec since 1970 (utc: %lld) diff: %lld\n",
607 ts.tv_sec, utc,
608 utc - ts.tv_sec);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700609 val = (int)(utc - ts.tv_sec);
Jeff Layton2190eca2013-05-26 07:00:57 -0400610 seconds = abs(val);
611 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
612 remain = seconds % MIN_TZ_ADJ;
613 if (remain >= (MIN_TZ_ADJ / 2))
614 result += MIN_TZ_ADJ;
615 if (val < 0)
616 result = -result;
617 server->timeAdj = result;
618 } else {
619 server->timeAdj = (int)tmp;
620 server->timeAdj *= 60; /* also in seconds */
621 }
622 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
623
624
625 /* BB get server time for time conversions and add
626 code to use it and timezone since this is not UTC */
627
628 if (rsp->EncryptionKeyLength ==
629 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
630 memcpy(server->cryptkey, rsp->EncryptionKey,
631 CIFS_CRYPTO_KEY_SIZE);
632 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
633 return -EIO; /* need cryptkey unless plain text */
634 }
635
636 cifs_dbg(FYI, "LANMAN negotiated\n");
637 return 0;
638}
639#else
640static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500641decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400642{
643 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
644 return -EOPNOTSUPP;
645}
646#endif
647
Jeff Layton91934002013-05-26 07:00:58 -0400648static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500649should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400650{
Jeff Layton3f618222013-06-12 19:52:14 -0500651 switch (sectype) {
652 case RawNTLMSSP:
653 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400654 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500655 case Unspecified:
656 if (global_secflags &
657 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
658 return true;
659 /* Fallthrough */
660 default:
661 return false;
662 }
Jeff Layton91934002013-05-26 07:00:58 -0400663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400666CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 NEGOTIATE_REQ *pSMB;
669 NEGOTIATE_RSP *pSMBr;
670 int rc = 0;
671 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000672 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400673 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 u16 count;
675
Jeff Layton3534b852013-05-24 07:41:01 -0400676 if (!server) {
677 WARN(1, "%s: server is NULL!\n", __func__);
678 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Jeff Layton3534b852013-05-24 07:41:01 -0400680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
682 (void **) &pSMB, (void **) &pSMBr);
683 if (rc)
684 return rc;
Steve French750d1152006-06-27 06:28:30 +0000685
Pavel Shilovsky88257362012-05-23 14:01:59 +0400686 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000687 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000688
Jeff Layton3f618222013-06-12 19:52:14 -0500689 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400690 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000691 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
692 }
Steve French50c2f752007-07-13 00:33:32 +0000693
Steve French39798772006-05-31 22:40:51 +0000694 count = 0;
Stephen Rothwellbcfb84a2018-09-03 13:15:58 +1000695 /*
696 * We know that all the name entries in the protocols array
697 * are short (< 16 bytes anyway) and are NUL terminated.
698 */
Steve French50c2f752007-07-13 00:33:32 +0000699 for (i = 0; i < CIFS_NUM_PROT; i++) {
Stephen Rothwellbcfb84a2018-09-03 13:15:58 +1000700 size_t len = strlen(protocols[i].name) + 1;
701
702 memcpy(pSMB->DialectsArray+count, protocols[i].name, len);
703 count += len;
Steve French39798772006-05-31 22:40:51 +0000704 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000705 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 pSMB->ByteCount = cpu_to_le16(count);
707
708 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
709 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000710 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000711 goto neg_err_exit;
712
Jeff Layton9bf67e52010-04-24 07:57:46 -0400713 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500714 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000715 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400716 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000717 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000718 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000719 could not negotiate a common dialect */
720 rc = -EOPNOTSUPP;
721 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000722 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400723 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500724 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400725 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000726 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000727 /* unknown wct */
728 rc = -EOPNOTSUPP;
729 goto neg_err_exit;
730 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400731 /* else wct == 17, NTLM or better */
732
Steve French96daf2b2011-05-27 04:34:02 +0000733 server->sec_mode = pSMBr->SecurityMode;
734 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500735 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000736
Steve French254e55e2006-06-04 05:53:15 +0000737 /* one byte, so no need to convert this or EncryptionKeyLen from
738 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300739 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
740 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400741 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000742 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400743 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000744 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500745 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000746 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000747 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
748 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400749
Jeff Laytone598d1d82013-05-26 07:00:59 -0400750 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
751 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500752 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000753 CIFS_CRYPTO_KEY_SIZE);
Noel Powerf2910952015-05-27 09:22:10 +0100754 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
755 server->capabilities & CAP_EXTENDED_SECURITY) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400756 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500757 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400758 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000759 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400760 } else {
761 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000762 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400763 }
Steve French254e55e2006-06-04 05:53:15 +0000764
765signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400766 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400767 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000768neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700769 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000770
Joe Perchesf96637b2013-05-04 22:12:25 -0500771 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return rc;
773}
774
775int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400776CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Joe Perchesf96637b2013-05-04 22:12:25 -0500781 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500782
783 /* BB: do we need to check this? These should never be NULL. */
784 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
785 return -EIO;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500788 * No need to return error on this operation if tid invalidated and
789 * closed on server already e.g. due to tcp session crashing. Also,
790 * the tcon is no longer on the list, so no need to take lock before
791 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Steve French268875b2009-06-25 00:29:21 +0000793 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Steve French50c2f752007-07-13 00:33:32 +0000796 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700797 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500798 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return rc;
Steve French133672e2007-11-13 22:41:37 +0000800
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400801 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700802 cifs_small_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500804 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Steve French50c2f752007-07-13 00:33:32 +0000806 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500807 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (rc == -EAGAIN)
809 rc = 0;
810
811 return rc;
812}
813
Jeff Layton766fdbb2011-01-11 07:24:21 -0500814/*
815 * This is a no-op for now. We're not really interested in the reply, but
816 * rather in the fact that the server sent one and that server->lstrp
817 * gets updated.
818 *
819 * FIXME: maybe we should consider checking that the reply matches request?
820 */
821static void
822cifs_echo_callback(struct mid_q_entry *mid)
823{
824 struct TCP_Server_Info *server = mid->callback_data;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800825 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500826
827 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -0800828 add_credits(server, &credits, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500829}
830
831int
832CIFSSMBEcho(struct TCP_Server_Info *server)
833{
834 ECHO_REQ *smb;
835 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800836 struct kvec iov[2];
837 struct smb_rqst rqst = { .rq_iov = iov,
838 .rq_nvec = 2 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500839
Joe Perchesf96637b2013-05-04 22:12:25 -0500840 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500841
842 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
843 if (rc)
844 return rc;
845
Steve French26c9cb62017-05-02 13:35:20 -0500846 if (server->capabilities & CAP_UNICODE)
847 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
848
Jeff Layton766fdbb2011-01-11 07:24:21 -0500849 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000850 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500851 smb->hdr.WordCount = 1;
852 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400853 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500854 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000855 inc_rfc1001_len(smb, 3);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800856
857 iov[0].iov_len = 4;
858 iov[0].iov_base = smb;
859 iov[1].iov_len = get_rfc1002_length(smb);
860 iov[1].iov_base = (char *)smb + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500861
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800862 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +1000863 server, CIFS_NON_BLOCKING | CIFS_ECHO_OP, NULL);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500864 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500865 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500866
867 cifs_small_buf_release(smb);
868
869 return rc;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400873CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 LOGOFF_ANDX_REQ *pSMB;
876 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Joe Perchesf96637b2013-05-04 22:12:25 -0500878 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500879
880 /*
881 * BB: do we need to check validity of ses and server? They should
882 * always be valid since we have an active reference. If not, that
883 * should probably be a BUG()
884 */
885 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EIO;
887
Steve Frenchd7b619c2010-02-25 05:36:46 +0000888 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000889 if (ses->need_reconnect)
890 goto session_already_dead; /* no need to send SMBlogoff if uid
891 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
893 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000894 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return rc;
896 }
897
Pavel Shilovsky88257362012-05-23 14:01:59 +0400898 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700899
Jeff Layton38d77c52013-05-26 07:01:00 -0400900 if (ses->server->sign)
901 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 pSMB->hdr.Uid = ses->Suid;
904
905 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400906 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700907 cifs_small_buf_release(pSMB);
Steve French3b795212008-11-13 19:45:32 +0000908session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000909 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000912 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 error */
914 if (rc == -EAGAIN)
915 rc = 0;
916 return rc;
917}
918
919int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400920CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
921 const char *fileName, __u16 type,
922 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000923{
924 TRANSACTION2_SPI_REQ *pSMB = NULL;
925 TRANSACTION2_SPI_RSP *pSMBr = NULL;
926 struct unlink_psx_rq *pRqD;
927 int name_len;
928 int rc = 0;
929 int bytes_returned = 0;
930 __u16 params, param_offset, offset, byte_count;
931
Joe Perchesf96637b2013-05-04 22:12:25 -0500932 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000933PsxDelete:
934 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
935 (void **) &pSMBr);
936 if (rc)
937 return rc;
938
939 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
940 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600941 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
942 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000943 name_len++; /* trailing null */
944 name_len *= 2;
945 } else { /* BB add path length overrun check */
946 name_len = strnlen(fileName, PATH_MAX);
947 name_len++; /* trailing null */
948 strncpy(pSMB->FileName, fileName, name_len);
949 }
950
951 params = 6 + name_len;
952 pSMB->MaxParameterCount = cpu_to_le16(2);
953 pSMB->MaxDataCount = 0; /* BB double check this with jra */
954 pSMB->MaxSetupCount = 0;
955 pSMB->Reserved = 0;
956 pSMB->Flags = 0;
957 pSMB->Timeout = 0;
958 pSMB->Reserved2 = 0;
959 param_offset = offsetof(struct smb_com_transaction2_spi_req,
960 InformationLevel) - 4;
961 offset = param_offset + params;
962
963 /* Setup pointer to Request Data (inode type) */
964 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
965 pRqD->type = cpu_to_le16(type);
966 pSMB->ParameterOffset = cpu_to_le16(param_offset);
967 pSMB->DataOffset = cpu_to_le16(offset);
968 pSMB->SetupCount = 1;
969 pSMB->Reserved3 = 0;
970 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
971 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
972
973 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
974 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
975 pSMB->ParameterCount = cpu_to_le16(params);
976 pSMB->TotalParameterCount = pSMB->ParameterCount;
977 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
978 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000979 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000980 pSMB->ByteCount = cpu_to_le16(byte_count);
981 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
982 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000983 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500984 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000985 cifs_buf_release(pSMB);
986
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400987 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000988
989 if (rc == -EAGAIN)
990 goto PsxDelete;
991
992 return rc;
993}
994
995int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700996CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
997 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 DELETE_FILE_REQ *pSMB = NULL;
1000 DELETE_FILE_RSP *pSMBr = NULL;
1001 int rc = 0;
1002 int bytes_returned;
1003 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001004 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006DelFileRetry:
1007 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
1008 (void **) &pSMBr);
1009 if (rc)
1010 return rc;
1011
1012 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001013 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
1014 PATH_MAX, cifs_sb->local_nls,
1015 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 name_len++; /* trailing null */
1017 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001018 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001019 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001021 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 pSMB->SearchAttributes =
1024 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
1025 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001026 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1028 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1029 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001030 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +00001031 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001032 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 cifs_buf_release(pSMB);
1035 if (rc == -EAGAIN)
1036 goto DelFileRetry;
1037
1038 return rc;
1039}
1040
1041int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001042CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1043 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 DELETE_DIRECTORY_REQ *pSMB = NULL;
1046 DELETE_DIRECTORY_RSP *pSMBr = NULL;
1047 int rc = 0;
1048 int bytes_returned;
1049 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001050 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Joe Perchesf96637b2013-05-04 22:12:25 -05001052 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053RmDirRetry:
1054 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
1055 (void **) &pSMBr);
1056 if (rc)
1057 return rc;
1058
1059 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001060 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1061 PATH_MAX, cifs_sb->local_nls,
1062 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 name_len++; /* trailing null */
1064 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001065 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001066 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001068 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
1071 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001072 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1074 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1075 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001076 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001077 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001078 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 cifs_buf_release(pSMB);
1081 if (rc == -EAGAIN)
1082 goto RmDirRetry;
1083 return rc;
1084}
1085
1086int
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001087CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1088 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 int rc = 0;
1091 CREATE_DIRECTORY_REQ *pSMB = NULL;
1092 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1093 int bytes_returned;
1094 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001095 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Joe Perchesf96637b2013-05-04 22:12:25 -05001097 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098MkDirRetry:
1099 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1100 (void **) &pSMBr);
1101 if (rc)
1102 return rc;
1103
1104 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06001105 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001106 PATH_MAX, cifs_sb->local_nls,
1107 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 name_len++; /* trailing null */
1109 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001110 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 name_len = strnlen(name, PATH_MAX);
1112 name_len++; /* trailing null */
1113 strncpy(pSMB->DirName, name, name_len);
1114 }
1115
1116 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001117 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1119 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1120 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001121 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001122 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001123 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 cifs_buf_release(pSMB);
1126 if (rc == -EAGAIN)
1127 goto MkDirRetry;
1128 return rc;
1129}
1130
Steve French2dd29d32007-04-23 22:07:35 +00001131int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001132CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1133 __u32 posix_flags, __u64 mode, __u16 *netfid,
1134 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1135 const char *name, const struct nls_table *nls_codepage,
1136 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001137{
1138 TRANSACTION2_SPI_REQ *pSMB = NULL;
1139 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1140 int name_len;
1141 int rc = 0;
1142 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001143 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001144 OPEN_PSX_REQ *pdata;
1145 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001146
Joe Perchesf96637b2013-05-04 22:12:25 -05001147 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001148PsxCreat:
1149 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1150 (void **) &pSMBr);
1151 if (rc)
1152 return rc;
1153
1154 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1155 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001156 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1157 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001158 name_len++; /* trailing null */
1159 name_len *= 2;
1160 } else { /* BB improve the check for buffer overruns BB */
1161 name_len = strnlen(name, PATH_MAX);
1162 name_len++; /* trailing null */
1163 strncpy(pSMB->FileName, name, name_len);
1164 }
1165
1166 params = 6 + name_len;
1167 count = sizeof(OPEN_PSX_REQ);
1168 pSMB->MaxParameterCount = cpu_to_le16(2);
1169 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1170 pSMB->MaxSetupCount = 0;
1171 pSMB->Reserved = 0;
1172 pSMB->Flags = 0;
1173 pSMB->Timeout = 0;
1174 pSMB->Reserved2 = 0;
1175 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001176 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001177 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001178 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001179 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001180 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001181 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001182 pdata->OpenFlags = cpu_to_le32(*pOplock);
1183 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1184 pSMB->DataOffset = cpu_to_le16(offset);
1185 pSMB->SetupCount = 1;
1186 pSMB->Reserved3 = 0;
1187 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1188 byte_count = 3 /* pad */ + params + count;
1189
1190 pSMB->DataCount = cpu_to_le16(count);
1191 pSMB->ParameterCount = cpu_to_le16(params);
1192 pSMB->TotalDataCount = pSMB->DataCount;
1193 pSMB->TotalParameterCount = pSMB->ParameterCount;
1194 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1195 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001196 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001197 pSMB->ByteCount = cpu_to_le16(byte_count);
1198 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1199 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1200 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001201 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001202 goto psx_create_err;
1203 }
1204
Joe Perchesf96637b2013-05-04 22:12:25 -05001205 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001206 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1207
Jeff Layton820a8032011-05-04 08:05:26 -04001208 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001209 rc = -EIO; /* bad smb */
1210 goto psx_create_err;
1211 }
1212
1213 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001214 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001215 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001216
Steve French2dd29d32007-04-23 22:07:35 +00001217 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001218 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001219 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1220 /* Let caller know file was created so we can set the mode. */
1221 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001222 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001223 *pOplock |= CIFS_CREATE_ACTION;
1224 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001225 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1226 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001227 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001228 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001229 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001230 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001231 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001232 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001233 goto psx_create_err;
1234 }
Steve French50c2f752007-07-13 00:33:32 +00001235 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001236 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001237 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001238 }
Steve French2dd29d32007-04-23 22:07:35 +00001239
1240psx_create_err:
1241 cifs_buf_release(pSMB);
1242
Steve French65bc98b2009-07-10 15:27:25 +00001243 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001244 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001245 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001246 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001247
1248 if (rc == -EAGAIN)
1249 goto PsxCreat;
1250
Steve French50c2f752007-07-13 00:33:32 +00001251 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001252}
1253
Steve Frencha9d02ad2005-08-24 23:06:05 -07001254static __u16 convert_disposition(int disposition)
1255{
1256 __u16 ofun = 0;
1257
1258 switch (disposition) {
1259 case FILE_SUPERSEDE:
1260 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1261 break;
1262 case FILE_OPEN:
1263 ofun = SMBOPEN_OAPPEND;
1264 break;
1265 case FILE_CREATE:
1266 ofun = SMBOPEN_OCREATE;
1267 break;
1268 case FILE_OPEN_IF:
1269 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1270 break;
1271 case FILE_OVERWRITE:
1272 ofun = SMBOPEN_OTRUNC;
1273 break;
1274 case FILE_OVERWRITE_IF:
1275 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1276 break;
1277 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001278 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001279 ofun = SMBOPEN_OAPPEND; /* regular open */
1280 }
1281 return ofun;
1282}
1283
Jeff Layton35fc37d2008-05-14 10:22:03 -07001284static int
1285access_flags_to_smbopen_mode(const int access_flags)
1286{
1287 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1288
1289 if (masked_flags == GENERIC_READ)
1290 return SMBOPEN_READ;
1291 else if (masked_flags == GENERIC_WRITE)
1292 return SMBOPEN_WRITE;
1293
1294 /* just go for read/write */
1295 return SMBOPEN_READWRITE;
1296}
1297
Steve Frencha9d02ad2005-08-24 23:06:05 -07001298int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001299SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001300 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001301 const int access_flags, const int create_options, __u16 *netfid,
1302 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001303 const struct nls_table *nls_codepage, int remap)
1304{
1305 int rc = -EACCES;
1306 OPENX_REQ *pSMB = NULL;
1307 OPENX_RSP *pSMBr = NULL;
1308 int bytes_returned;
1309 int name_len;
1310 __u16 count;
1311
1312OldOpenRetry:
1313 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1314 (void **) &pSMBr);
1315 if (rc)
1316 return rc;
1317
1318 pSMB->AndXCommand = 0xFF; /* none */
1319
1320 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1321 count = 1; /* account for one byte pad to word boundary */
1322 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001323 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1324 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001325 name_len++; /* trailing null */
1326 name_len *= 2;
1327 } else { /* BB improve check for buffer overruns BB */
1328 count = 0; /* no pad */
1329 name_len = strnlen(fileName, PATH_MAX);
1330 name_len++; /* trailing null */
1331 strncpy(pSMB->fileName, fileName, name_len);
1332 }
1333 if (*pOplock & REQ_OPLOCK)
1334 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001335 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001336 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001337
Steve Frencha9d02ad2005-08-24 23:06:05 -07001338 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001339 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001340 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1341 /* set file as system file if special file such
1342 as fifo and server expecting SFU style and
1343 no Unix extensions */
1344
Steve French790fe572007-07-07 19:25:05 +00001345 if (create_options & CREATE_OPTION_SPECIAL)
1346 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001347 else /* BB FIXME BB */
1348 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001349
Jeff Layton67750fb2008-05-09 22:28:02 +00001350 if (create_options & CREATE_OPTION_READONLY)
1351 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001352
1353 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001354/* pSMB->CreateOptions = cpu_to_le32(create_options &
1355 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001356 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001357
1358 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001359 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001360 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001361 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001362
1363 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001364 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001365 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001366 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001367 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001368 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001369 } else {
1370 /* BB verify if wct == 15 */
1371
Steve French582d21e2008-05-13 04:54:12 +00001372/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001373
1374 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1375 /* Let caller know file was created so we can set the mode. */
1376 /* Do we care about the CreateAction in any other cases? */
1377 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001378/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001379 *pOplock |= CIFS_CREATE_ACTION; */
1380 /* BB FIXME END */
1381
Steve French790fe572007-07-07 19:25:05 +00001382 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001383 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1384 pfile_info->LastAccessTime = 0; /* BB fixme */
1385 pfile_info->LastWriteTime = 0; /* BB fixme */
1386 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001387 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001388 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001389 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001390 pfile_info->AllocationSize =
1391 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1392 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001393 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001394 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001395 }
1396 }
1397
1398 cifs_buf_release(pSMB);
1399 if (rc == -EAGAIN)
1400 goto OldOpenRetry;
1401 return rc;
1402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001405CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1406 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001409 OPEN_REQ *req = NULL;
1410 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int bytes_returned;
1412 int name_len;
1413 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001414 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1415 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001416 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001417 const struct nls_table *nls = cifs_sb->local_nls;
1418 int create_options = oparms->create_options;
1419 int desired_access = oparms->desired_access;
1420 int disposition = oparms->disposition;
1421 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001424 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1425 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (rc)
1427 return rc;
1428
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001429 /* no commands go after this */
1430 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001432 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1433 /* account for one byte pad to word boundary */
1434 count = 1;
1435 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1436 path, PATH_MAX, nls, remap);
1437 /* trailing null */
1438 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001440 req->NameLength = cpu_to_le16(name_len);
1441 } else {
1442 /* BB improve check for buffer overruns BB */
1443 /* no pad */
1444 count = 0;
1445 name_len = strnlen(path, PATH_MAX);
1446 /* trailing null */
1447 name_len++;
1448 req->NameLength = cpu_to_le16(name_len);
1449 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001451
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001452 if (*oplock & REQ_OPLOCK)
1453 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1454 else if (*oplock & REQ_BATCHOPLOCK)
1455 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1456
1457 req->DesiredAccess = cpu_to_le32(desired_access);
1458 req->AllocationSize = 0;
1459
1460 /*
1461 * Set file as system file if special file such as fifo and server
1462 * expecting SFU style and no Unix extensions.
1463 */
1464 if (create_options & CREATE_OPTION_SPECIAL)
1465 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1466 else
1467 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1468
1469 /*
1470 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1471 * sensitive checks for other servers such as Samba.
1472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001474 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Jeff Layton67750fb2008-05-09 22:28:02 +00001476 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001477 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001478
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001479 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1480 req->CreateDisposition = cpu_to_le32(disposition);
1481 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1482
Steve French09d1db52005-04-28 22:41:08 -07001483 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001484 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1485 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001488 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001490 req->ByteCount = cpu_to_le16(count);
1491 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1492 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001493 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001495 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001496 cifs_buf_release(req);
1497 if (rc == -EAGAIN)
1498 goto openRetry;
1499 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001501
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001502 /* 1 byte no need to le_to_cpu */
1503 *oplock = rsp->OplockLevel;
1504 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001505 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001506
1507 /* Let caller know file was created so we can set the mode. */
1508 /* Do we care about the CreateAction in any other cases? */
1509 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1510 *oplock |= CIFS_CREATE_ACTION;
1511
1512 if (buf) {
1513 /* copy from CreationTime to Attributes */
1514 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1515 /* the file_info buf is endian converted by caller */
1516 buf->AllocationSize = rsp->AllocationSize;
1517 buf->EndOfFile = rsp->EndOfFile;
1518 buf->NumberOfLinks = cpu_to_le32(1);
1519 buf->DeletePending = 0;
1520 }
1521
1522 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return rc;
1524}
1525
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001526/*
1527 * Discard any remaining data in the current SMB. To do this, we borrow the
1528 * current bigbuf.
1529 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001530int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001531cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001532{
Ronnie Sahlberg05432e22018-04-09 18:06:31 +10001533 unsigned int rfclen = server->pdu_size;
1534 int remaining = rfclen + server->vals->header_preamble_size -
1535 server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001536
1537 while (remaining > 0) {
1538 int length;
1539
1540 length = cifs_read_from_socket(server, server->bigbuf,
1541 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001542 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001543 if (length < 0)
1544 return length;
1545 server->total_read += length;
1546 remaining -= length;
1547 }
1548
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001549 return 0;
1550}
1551
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001552static int
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001553__cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1554 bool malformed)
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001555{
1556 int length;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001557
Pavel Shilovsky350be252017-04-10 10:31:33 -07001558 length = cifs_discard_remaining_data(server);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001559 dequeue_mid(mid, malformed);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001560 mid->resp_buf = server->smallbuf;
1561 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001562 return length;
1563}
1564
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001565static int
1566cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1567{
1568 struct cifs_readdata *rdata = mid->callback_data;
1569
1570 return __cifs_readv_discard(server, mid, rdata->result);
1571}
1572
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001573int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001574cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1575{
1576 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001577 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001578 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001579 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10001580 unsigned int buflen = server->pdu_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001581 server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07001582 bool use_rdma_mr = false;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001583
Joe Perchesf96637b2013-05-04 22:12:25 -05001584 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1585 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001586
1587 /*
1588 * read the rest of READ_RSP header (sans Data array), or whatever we
1589 * can if there's not enough data. At this point, we've read down to
1590 * the Mid.
1591 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001592 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001593 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001594
Al Viroa6137302016-01-09 19:37:16 -05001595 length = cifs_read_from_socket(server,
1596 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001597 if (length < 0)
1598 return length;
1599 server->total_read += length;
1600
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001601 if (server->ops->is_session_expired &&
1602 server->ops->is_session_expired(buf)) {
1603 cifs_reconnect(server);
1604 wake_up(&server->response_q);
1605 return -1;
1606 }
1607
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001608 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001609 server->ops->is_status_pending(buf, server)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001610 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001611 return -1;
1612 }
1613
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001614 /* set up first two iov for signature check and to get credits */
1615 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08001616 rdata->iov[0].iov_len = server->vals->header_preamble_size;
1617 rdata->iov[1].iov_base = buf + server->vals->header_preamble_size;
1618 rdata->iov[1].iov_len =
1619 server->total_read - server->vals->header_preamble_size;
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001620 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1621 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1622 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
1623 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
1624
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001625 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001626 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001627 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001628 cifs_dbg(FYI, "%s: server returned error %d\n",
1629 __func__, rdata->result);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001630 /* normal error on read response */
1631 return __cifs_readv_discard(server, mid, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001632 }
1633
1634 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001635 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001636 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1637 __func__, server->total_read,
1638 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001639 rdata->result = -EIO;
1640 return cifs_readv_discard(server, mid);
1641 }
1642
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001643 data_offset = server->ops->read_data_offset(buf) +
1644 server->vals->header_preamble_size;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001645 if (data_offset < server->total_read) {
1646 /*
1647 * win2k8 sometimes sends an offset of 0 when the read
1648 * is beyond the EOF. Treat it as if the data starts just after
1649 * the header.
1650 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001651 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1652 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001653 data_offset = server->total_read;
1654 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1655 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001656 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1657 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001658 rdata->result = -EIO;
1659 return cifs_readv_discard(server, mid);
1660 }
1661
Joe Perchesf96637b2013-05-04 22:12:25 -05001662 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1663 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001664
1665 len = data_offset - server->total_read;
1666 if (len > 0) {
1667 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001668 length = cifs_read_from_socket(server,
1669 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001670 if (length < 0)
1671 return length;
1672 server->total_read += length;
1673 }
1674
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001675 /* how much data is in the response? */
Long Li74dcf412017-11-22 17:38:46 -07001676#ifdef CONFIG_CIFS_SMB_DIRECT
1677 use_rdma_mr = rdata->mr;
1678#endif
1679 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1680 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001681 /* data_len is corrupt -- discard frame */
1682 rdata->result = -EIO;
1683 return cifs_readv_discard(server, mid);
1684 }
1685
Jeff Layton8321fec2012-09-19 06:22:32 -07001686 length = rdata->read_into_pages(server, rdata, data_len);
1687 if (length < 0)
1688 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001689
Jeff Layton8321fec2012-09-19 06:22:32 -07001690 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001691
Joe Perchesf96637b2013-05-04 22:12:25 -05001692 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1693 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001694
1695 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001696 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001697 return cifs_readv_discard(server, mid);
1698
1699 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001700 mid->resp_buf = server->smallbuf;
1701 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001702 return length;
1703}
1704
1705static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001706cifs_readv_callback(struct mid_q_entry *mid)
1707{
1708 struct cifs_readdata *rdata = mid->callback_data;
1709 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1710 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001711 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1712 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001713 .rq_pages = rdata->pages,
Long Li6d3adb22018-09-20 21:18:38 +00001714 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07001715 .rq_npages = rdata->nr_pages,
1716 .rq_pagesz = rdata->pagesz,
1717 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001718 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001719
Joe Perchesf96637b2013-05-04 22:12:25 -05001720 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1721 __func__, mid->mid, mid->mid_state, rdata->result,
1722 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001723
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001724 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001725 case MID_RESPONSE_RECEIVED:
1726 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001727 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001728 int rc = 0;
1729
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001730 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001731 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001732 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001733 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1734 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001735 }
1736 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001737 task_io_account_read(rdata->got_bytes);
1738 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001739 break;
1740 case MID_REQUEST_SUBMITTED:
1741 case MID_RETRY_NEEDED:
1742 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001743 if (server->sign && rdata->got_bytes)
1744 /* reset bytes number since we can not check a sign */
1745 rdata->got_bytes = 0;
1746 /* FIXME: should this be counted toward the initiating task? */
1747 task_io_account_read(rdata->got_bytes);
1748 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001749 break;
1750 default:
1751 rdata->result = -EIO;
1752 }
1753
Jeff Laytonda472fc2012-03-23 14:40:53 -04001754 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001755 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001756 add_credits(server, &credits, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001757}
1758
1759/* cifs_async_readv - send an async write, and set up mid to handle result */
1760int
1761cifs_async_readv(struct cifs_readdata *rdata)
1762{
1763 int rc;
1764 READ_REQ *smb = NULL;
1765 int wct;
1766 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001767 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1768 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001769
Joe Perchesf96637b2013-05-04 22:12:25 -05001770 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1771 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001772
1773 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1774 wct = 12;
1775 else {
1776 wct = 10; /* old style read */
1777 if ((rdata->offset >> 32) > 0) {
1778 /* can not handle this big offset for old */
1779 return -EIO;
1780 }
1781 }
1782
1783 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1784 if (rc)
1785 return rc;
1786
1787 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1788 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1789
1790 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001791 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001792 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1793 if (wct == 12)
1794 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1795 smb->Remaining = 0;
1796 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1797 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1798 if (wct == 12)
1799 smb->ByteCount = 0;
1800 else {
1801 /* old style read */
1802 struct smb_com_readx_req *smbr =
1803 (struct smb_com_readx_req *)smb;
1804 smbr->ByteCount = 0;
1805 }
1806
1807 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001808 rdata->iov[0].iov_base = smb;
1809 rdata->iov[0].iov_len = 4;
1810 rdata->iov[1].iov_base = (char *)smb + 4;
1811 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001812
Jeff Layton6993f742012-05-16 07:13:17 -04001813 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001814 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08001815 cifs_readv_callback, NULL, rdata, 0, NULL);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001816
1817 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001818 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001819 else
1820 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001821
1822 cifs_small_buf_release(smb);
1823 return rc;
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001827CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1828 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 int rc = -EACCES;
1831 READ_REQ *pSMB = NULL;
1832 READ_RSP *pSMBr = NULL;
1833 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001834 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001835 int resp_buf_type = 0;
1836 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001837 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001838 __u32 pid = io_parms->pid;
1839 __u16 netfid = io_parms->netfid;
1840 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001841 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001842 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Joe Perchesf96637b2013-05-04 22:12:25 -05001844 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001845 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001846 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001847 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001848 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001849 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001850 /* can not handle this big offset for old */
1851 return -EIO;
1852 }
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001856 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (rc)
1858 return rc;
1859
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001860 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1861 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* tcon and ses pointer are checked in smb_init */
1864 if (tcon->ses->server == NULL)
1865 return -ECONNABORTED;
1866
Steve Frenchec637e32005-12-12 20:53:18 -08001867 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001869 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001870 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001871 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 pSMB->Remaining = 0;
1874 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1875 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001876 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001877 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1878 else {
1879 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001880 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001881 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001882 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001883 }
Steve Frenchec637e32005-12-12 20:53:18 -08001884
1885 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001886 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001887 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1888 CIFS_LOG_ERROR, &rsp_iov);
1889 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001890 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001891 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001893 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 } else {
1895 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1896 data_length = data_length << 16;
1897 data_length += le16_to_cpu(pSMBr->DataLength);
1898 *nbytes = data_length;
1899
1900 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001901 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001903 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001904 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 rc = -EIO;
1906 *nbytes = 0;
1907 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001908 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001909 le16_to_cpu(pSMBr->DataOffset);
1910/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001911 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001912 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001913 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001914 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001915 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Steve French790fe572007-07-07 19:25:05 +00001919 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001920 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001921 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001922 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001923 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001924 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001925 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001926 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001927 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001928 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001929
1930 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 since file handle passed in no longer valid */
1932 return rc;
1933}
1934
Steve Frenchec637e32005-12-12 20:53:18 -08001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001937CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001938 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int rc = -EACCES;
1941 WRITE_REQ *pSMB = NULL;
1942 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001943 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 __u32 bytes_sent;
1945 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001946 __u32 pid = io_parms->pid;
1947 __u16 netfid = io_parms->netfid;
1948 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001949 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001950 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Steve Frencha24e2d72010-04-03 17:20:21 +00001952 *nbytes = 0;
1953
Joe Perchesf96637b2013-05-04 22:12:25 -05001954 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001955 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001956 return -ECONNABORTED;
1957
Steve French790fe572007-07-07 19:25:05 +00001958 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001959 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001960 else {
Steve French1c955182005-08-30 20:58:07 -07001961 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001962 if ((offset >> 32) > 0) {
1963 /* can not handle big offset for old srv */
1964 return -EIO;
1965 }
1966 }
Steve French1c955182005-08-30 20:58:07 -07001967
1968 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 (void **) &pSMBr);
1970 if (rc)
1971 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001972
1973 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1974 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* tcon and ses pointer are checked in smb_init */
1977 if (tcon->ses->server == NULL)
1978 return -ECONNABORTED;
1979
1980 pSMB->AndXCommand = 0xFF; /* none */
1981 pSMB->Fid = netfid;
1982 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001983 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001984 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 pSMB->Reserved = 0xFFFFFFFF;
1987 pSMB->WriteMode = 0;
1988 pSMB->Remaining = 0;
1989
Steve French50c2f752007-07-13 00:33:32 +00001990 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 can send more if LARGE_WRITE_X capability returned by the server and if
1992 our buffer is big enough or if we convert to iovecs on socket writes
1993 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001994 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1996 } else {
1997 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1998 & ~0xFF;
1999 }
2000
2001 if (bytes_sent > count)
2002 bytes_sent = count;
2003 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002004 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00002005 if (buf)
Steve French61e74802008-12-03 00:57:54 +00002006 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04002007 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* No buffer */
2009 cifs_buf_release(pSMB);
2010 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07002011 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00002012 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07002013 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00002014 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07002015 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
2018 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002019 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07002020
Steve French790fe572007-07-07 19:25:05 +00002021 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07002022 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00002023 else { /* old style write has byte count 4 bytes earlier
2024 so 4 bytes pad */
2025 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07002026 (struct smb_com_writex_req *)pSMB;
2027 pSMBW->ByteCount = cpu_to_le16(byte_count);
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04002031 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002032 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002034 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 } else {
2036 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2037 *nbytes = (*nbytes) << 16;
2038 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302039
2040 /*
2041 * Mask off high 16 bits when bytes written as returned by the
2042 * server is greater than bytes requested by the client. Some
2043 * OS/2 servers are known to set incorrect CountHigh values.
2044 */
2045 if (*nbytes > count)
2046 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
2049 cifs_buf_release(pSMB);
2050
Steve French50c2f752007-07-13 00:33:32 +00002051 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 since file handle passed in no longer valid */
2053
2054 return rc;
2055}
2056
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002057void
2058cifs_writedata_release(struct kref *refcount)
2059{
2060 struct cifs_writedata *wdata = container_of(refcount,
2061 struct cifs_writedata, refcount);
Long Lidb223a52017-11-22 17:38:45 -07002062#ifdef CONFIG_CIFS_SMB_DIRECT
2063 if (wdata->mr) {
2064 smbd_deregister_mr(wdata->mr);
2065 wdata->mr = NULL;
2066 }
2067#endif
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002068
2069 if (wdata->cfile)
2070 cifsFileInfo_put(wdata->cfile);
2071
Long Li8e7360f2018-05-30 12:47:56 -07002072 kvfree(wdata->pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002073 kfree(wdata);
2074}
2075
2076/*
2077 * Write failed with a retryable error. Resend the write request. It's also
2078 * possible that the page was redirtied so re-clean the page.
2079 */
2080static void
2081cifs_writev_requeue(struct cifs_writedata *wdata)
2082{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002083 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00002084 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07002085 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002086 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002087
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002088 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2089 i = 0;
2090 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002091 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002092 struct cifs_writedata *wdata2;
2093 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002094
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002095 wsize = server->ops->wp_retry_size(inode);
2096 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002097 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002098 if (!nr_pages) {
2099 rc = -ENOTSUPP;
2100 break;
2101 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002102 cur_len = nr_pages * PAGE_SIZE;
2103 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002104 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002105 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002106 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002107 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06002108 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002109
2110 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2111 if (!wdata2) {
2112 rc = -ENOMEM;
2113 break;
2114 }
2115
2116 for (j = 0; j < nr_pages; j++) {
2117 wdata2->pages[j] = wdata->pages[i + j];
2118 lock_page(wdata2->pages[j]);
2119 clear_page_dirty_for_io(wdata2->pages[j]);
2120 }
2121
2122 wdata2->sync_mode = wdata->sync_mode;
2123 wdata2->nr_pages = nr_pages;
2124 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002126 wdata2->tailsz = tailsz;
2127 wdata2->bytes = cur_len;
2128
Pavel Shilovskyfe768d52019-01-29 12:15:11 -08002129 rc = cifs_get_writable_file(CIFS_I(inode), false,
2130 &wdata2->cfile);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002131 if (!wdata2->cfile) {
Pavel Shilovskyfe768d52019-01-29 12:15:11 -08002132 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2133 rc);
2134 if (!is_retryable_error(rc))
2135 rc = -EBADF;
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002136 } else {
2137 wdata2->pid = wdata2->cfile->pid;
2138 rc = server->ops->async_writev(wdata2,
2139 cifs_writedata_release);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002140 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002141
2142 for (j = 0; j < nr_pages; j++) {
2143 unlock_page(wdata2->pages[j]);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002144 if (rc != 0 && !is_retryable_error(rc)) {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002145 SetPageError(wdata2->pages[j]);
2146 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002148 }
2149 }
2150
2151 if (rc) {
2152 kref_put(&wdata2->refcount, cifs_writedata_release);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002153 if (is_retryable_error(rc))
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002154 continue;
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002155 i += nr_pages;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002156 break;
2157 }
2158
2159 rest_len -= cur_len;
2160 i += nr_pages;
2161 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002162
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002163 /* cleanup remaining pages from the original wdata */
2164 for (; i < wdata->nr_pages; i++) {
2165 SetPageError(wdata->pages[i]);
2166 end_page_writeback(wdata->pages[i]);
2167 put_page(wdata->pages[i]);
2168 }
2169
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002170 if (rc != 0 && !is_retryable_error(rc))
2171 mapping_set_error(inode->i_mapping, rc);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002172 kref_put(&wdata->refcount, cifs_writedata_release);
2173}
2174
Jeff Laytonc2e87642012-03-23 14:40:55 -04002175void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002176cifs_writev_complete(struct work_struct *work)
2177{
2178 struct cifs_writedata *wdata = container_of(work,
2179 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002180 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002181 int i = 0;
2182
2183 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002184 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002185 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002186 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002187 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2188 wdata->bytes);
2189 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2190 return cifs_writev_requeue(wdata);
2191
2192 for (i = 0; i < wdata->nr_pages; i++) {
2193 struct page *page = wdata->pages[i];
2194 if (wdata->result == -EAGAIN)
2195 __set_page_dirty_nobuffers(page);
2196 else if (wdata->result < 0)
2197 SetPageError(page);
2198 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002199 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002200 }
2201 if (wdata->result != -EAGAIN)
2202 mapping_set_error(inode->i_mapping, wdata->result);
2203 kref_put(&wdata->refcount, cifs_writedata_release);
2204}
2205
2206struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002207cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002208{
Long Li8e7360f2018-05-30 12:47:56 -07002209 struct page **pages =
Kees Cook6396bb22018-06-12 14:03:40 -07002210 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Long Li8e7360f2018-05-30 12:47:56 -07002211 if (pages)
2212 return cifs_writedata_direct_alloc(pages, complete);
2213
2214 return NULL;
2215}
2216
2217struct cifs_writedata *
2218cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2219{
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002220 struct cifs_writedata *wdata;
2221
Long Li8e7360f2018-05-30 12:47:56 -07002222 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002223 if (wdata != NULL) {
Long Li8e7360f2018-05-30 12:47:56 -07002224 wdata->pages = pages;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002225 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002226 INIT_LIST_HEAD(&wdata->list);
2227 init_completion(&wdata->done);
2228 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002229 }
2230 return wdata;
2231}
2232
2233/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002234 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002235 * workqueue completion task.
2236 */
2237static void
2238cifs_writev_callback(struct mid_q_entry *mid)
2239{
2240 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002241 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002242 unsigned int written;
2243 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08002244 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002245
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002246 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002247 case MID_RESPONSE_RECEIVED:
2248 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2249 if (wdata->result != 0)
2250 break;
2251
2252 written = le16_to_cpu(smb->CountHigh);
2253 written <<= 16;
2254 written += le16_to_cpu(smb->Count);
2255 /*
2256 * Mask off high 16 bits when bytes written as returned
2257 * by the server is greater than bytes requested by the
2258 * client. OS/2 servers are known to set incorrect
2259 * CountHigh values.
2260 */
2261 if (written > wdata->bytes)
2262 written &= 0xFFFF;
2263
2264 if (written < wdata->bytes)
2265 wdata->result = -ENOSPC;
2266 else
2267 wdata->bytes = written;
2268 break;
2269 case MID_REQUEST_SUBMITTED:
2270 case MID_RETRY_NEEDED:
2271 wdata->result = -EAGAIN;
2272 break;
2273 default:
2274 wdata->result = -EIO;
2275 break;
2276 }
2277
Jeff Laytonda472fc2012-03-23 14:40:53 -04002278 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002279 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08002280 add_credits(tcon->ses->server, &credits, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002281}
2282
2283/* cifs_async_writev - send an async write, and set up mid to handle result */
2284int
Steve French4a5c80d2014-02-07 20:45:12 -06002285cifs_async_writev(struct cifs_writedata *wdata,
2286 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002287{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002288 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002289 WRITE_REQ *smb = NULL;
2290 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002291 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002292 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002293 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002294
2295 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2296 wct = 14;
2297 } else {
2298 wct = 12;
2299 if (wdata->offset >> 32 > 0) {
2300 /* can not handle big offset for old srv */
2301 return -EIO;
2302 }
2303 }
2304
2305 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2306 if (rc)
2307 goto async_writev_out;
2308
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002309 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2310 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002311
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002312 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002313 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002314 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2315 if (wct == 14)
2316 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2317 smb->Reserved = 0xFFFFFFFF;
2318 smb->WriteMode = 0;
2319 smb->Remaining = 0;
2320
2321 smb->DataOffset =
2322 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2323
2324 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002325 iov[0].iov_len = 4;
2326 iov[0].iov_base = smb;
2327 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2328 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002329
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002330 rqst.rq_iov = iov;
2331 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002332 rqst.rq_pages = wdata->pages;
Long Li6d3adb22018-09-20 21:18:38 +00002333 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002334 rqst.rq_npages = wdata->nr_pages;
2335 rqst.rq_pagesz = wdata->pagesz;
2336 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002337
Joe Perchesf96637b2013-05-04 22:12:25 -05002338 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2339 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002340
2341 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2342 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2343
2344 if (wct == 14) {
2345 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2346 put_bcc(wdata->bytes + 1, &smb->hdr);
2347 } else {
2348 /* wct == 12 */
2349 struct smb_com_writex_req *smbw =
2350 (struct smb_com_writex_req *)smb;
2351 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2352 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002353 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002354 }
2355
2356 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002357 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08002358 cifs_writev_callback, NULL, wdata, 0, NULL);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002359
2360 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002361 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002362 else
Steve French4a5c80d2014-02-07 20:45:12 -06002363 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002364
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002365async_writev_out:
2366 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002367 return rc;
2368}
2369
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002370int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002371CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002372 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 int rc = -EACCES;
2375 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002376 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002377 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002378 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002379 __u32 pid = io_parms->pid;
2380 __u16 netfid = io_parms->netfid;
2381 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002382 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002383 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002384 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002386 *nbytes = 0;
2387
Joe Perchesf96637b2013-05-04 22:12:25 -05002388 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002389
Steve French4c3130e2008-12-09 00:28:16 +00002390 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002391 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002392 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002393 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002394 if ((offset >> 32) > 0) {
2395 /* can not handle big offset for old srv */
2396 return -EIO;
2397 }
2398 }
Steve French8cc64c62005-10-03 13:49:43 -07002399 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (rc)
2401 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002402
2403 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2404 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* tcon and ses pointer are checked in smb_init */
2407 if (tcon->ses->server == NULL)
2408 return -ECONNABORTED;
2409
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002410 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 pSMB->Fid = netfid;
2412 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002413 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002414 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 pSMB->Reserved = 0xFFFFFFFF;
2416 pSMB->WriteMode = 0;
2417 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002420 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Steve French3e844692005-10-03 13:37:24 -07002422 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2423 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002424 /* header + 1 byte pad */
2425 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002426 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002427 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002428 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002429 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002430 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002431 pSMB->ByteCount = cpu_to_le16(count + 1);
2432 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002433 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002434 (struct smb_com_writex_req *)pSMB;
2435 pSMBW->ByteCount = cpu_to_le16(count + 5);
2436 }
Steve French3e844692005-10-03 13:37:24 -07002437 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002438 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002439 iov[0].iov_len = smb_hdr_len + 4;
2440 else /* wct == 12 pad bigger by four bytes */
2441 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002442
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002443 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2444 &rsp_iov);
2445 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002446 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002448 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002449 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002450 /* presumably this can not happen, but best to be safe */
2451 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002452 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002453 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002454 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2455 *nbytes = (*nbytes) << 16;
2456 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302457
2458 /*
2459 * Mask off high 16 bits when bytes written as returned by the
2460 * server is greater than bytes requested by the client. OS/2
2461 * servers are known to set incorrect CountHigh values.
2462 */
2463 if (*nbytes > count)
2464 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002467 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Steve French50c2f752007-07-13 00:33:32 +00002469 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 since file handle passed in no longer valid */
2471
2472 return rc;
2473}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002474
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002475int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2476 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002477 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2478{
2479 int rc = 0;
2480 LOCK_REQ *pSMB = NULL;
2481 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002482 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002483 int resp_buf_type;
2484 __u16 count;
2485
Joe Perchesf96637b2013-05-04 22:12:25 -05002486 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2487 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002488
2489 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2490 if (rc)
2491 return rc;
2492
2493 pSMB->Timeout = 0;
2494 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2495 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2496 pSMB->LockType = lock_type;
2497 pSMB->AndXCommand = 0xFF; /* none */
2498 pSMB->Fid = netfid; /* netfid stays le */
2499
2500 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2501 inc_rfc1001_len(pSMB, count);
2502 pSMB->ByteCount = cpu_to_le16(count);
2503
2504 iov[0].iov_base = (char *)pSMB;
2505 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2506 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2507 iov[1].iov_base = (char *)buf;
2508 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2509
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002510 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10002511 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type,
2512 CIFS_NO_RSP_BUF, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002513 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002514 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002515 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002516
2517 return rc;
2518}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002521CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002522 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002524 const __u32 numLock, const __u8 lockType,
2525 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 int rc = 0;
2528 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002529/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002531 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 __u16 count;
2533
Joe Perchesf96637b2013-05-04 22:12:25 -05002534 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2535 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002536 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 if (rc)
2539 return rc;
2540
Steve French790fe572007-07-07 19:25:05 +00002541 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002542 /* no response expected */
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10002543 flags = CIFS_NO_SRV_RSP | CIFS_NON_BLOCKING | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002545 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002546 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2548 } else {
2549 pSMB->Timeout = 0;
2550 }
2551
2552 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2553 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2554 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002555 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 pSMB->AndXCommand = 0xFF; /* none */
2557 pSMB->Fid = smb_file_id; /* netfid stays le */
2558
Steve French790fe572007-07-07 19:25:05 +00002559 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002560 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /* BB where to store pid high? */
2562 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2563 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2564 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2565 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2566 count = sizeof(LOCKING_ANDX_RANGE);
2567 } else {
2568 /* oplock break */
2569 count = 0;
2570 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002571 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 pSMB->ByteCount = cpu_to_le16(count);
2573
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002574 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002575 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002576 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002577 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002578 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002579 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002580 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002581 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002582 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Steve French50c2f752007-07-13 00:33:32 +00002584 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 since file handle passed in no longer valid */
2586 return rc;
2587}
2588
2589int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002590CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002591 const __u16 smb_file_id, const __u32 netpid,
2592 const loff_t start_offset, const __u64 len,
2593 struct file_lock *pLockData, const __u16 lock_type,
2594 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002595{
2596 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2597 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002598 struct cifs_posix_lock *parm_data;
2599 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002600 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002601 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002602 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002603 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002604 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002605 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002606
Joe Perchesf96637b2013-05-04 22:12:25 -05002607 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002608
Steve French08547b02006-02-28 22:39:25 +00002609 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2610
2611 if (rc)
2612 return rc;
2613
2614 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2615
Steve French50c2f752007-07-13 00:33:32 +00002616 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002617 pSMB->MaxSetupCount = 0;
2618 pSMB->Reserved = 0;
2619 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002620 pSMB->Reserved2 = 0;
2621 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2622 offset = param_offset + params;
2623
Steve French08547b02006-02-28 22:39:25 +00002624 count = sizeof(struct cifs_posix_lock);
2625 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002626 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002627 pSMB->SetupCount = 1;
2628 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002629 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002630 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2631 else
2632 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2633 byte_count = 3 /* pad */ + params + count;
2634 pSMB->DataCount = cpu_to_le16(count);
2635 pSMB->ParameterCount = cpu_to_le16(params);
2636 pSMB->TotalDataCount = pSMB->DataCount;
2637 pSMB->TotalParameterCount = pSMB->ParameterCount;
2638 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002639 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002640 (((char *) &pSMB->hdr.Protocol) + offset);
2641
2642 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002643 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002644 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002645 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002646 pSMB->Timeout = cpu_to_le32(-1);
2647 } else
2648 pSMB->Timeout = 0;
2649
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002650 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002651 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002652 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002653
2654 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002655 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002656 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2657 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002658 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002659 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002660 if (waitFlag) {
2661 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2662 (struct smb_hdr *) pSMBr, &bytes_returned);
2663 } else {
Steve French133672e2007-11-13 22:41:37 +00002664 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002665 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002666 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002667 &resp_buf_type, timeout, &rsp_iov);
2668 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002669 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002670 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002671
Steve French08547b02006-02-28 22:39:25 +00002672 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002673 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002674 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002675 /* lock structure can be returned on get */
2676 __u16 data_offset;
2677 __u16 data_count;
2678 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002679
Jeff Layton820a8032011-05-04 08:05:26 -04002680 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002681 rc = -EIO; /* bad smb */
2682 goto plk_err_exit;
2683 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002684 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2685 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002686 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002687 rc = -EIO;
2688 goto plk_err_exit;
2689 }
2690 parm_data = (struct cifs_posix_lock *)
2691 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002692 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002693 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002694 else {
2695 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002696 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002697 pLockData->fl_type = F_RDLCK;
2698 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002699 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002700 pLockData->fl_type = F_WRLCK;
2701
Steve French5443d132011-03-13 05:08:25 +00002702 pLockData->fl_start = le64_to_cpu(parm_data->start);
2703 pLockData->fl_end = pLockData->fl_start +
2704 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002705 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002706 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002707 }
Steve French50c2f752007-07-13 00:33:32 +00002708
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002709plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002710 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002711
Steve French08547b02006-02-28 22:39:25 +00002712 /* Note: On -EAGAIN error only caller can retry on handle based calls
2713 since file handle passed in no longer valid */
2714
2715 return rc;
2716}
2717
2718
2719int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002720CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 int rc = 0;
2723 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002724 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726/* do not retry on dead session on close */
2727 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002728 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return 0;
2730 if (rc)
2731 return rc;
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002734 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002736 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002737 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002738 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002740 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002742 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 }
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002747 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 rc = 0;
2749
2750 return rc;
2751}
2752
2753int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002754CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002755{
2756 int rc = 0;
2757 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002758 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002759
2760 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2761 if (rc)
2762 return rc;
2763
2764 pSMB->FileID = (__u16) smb_file_id;
2765 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002766 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002767 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002768 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002769 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002770 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002771
2772 return rc;
2773}
2774
2775int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002776CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002777 const char *from_name, const char *to_name,
2778 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 int rc = 0;
2781 RENAME_REQ *pSMB = NULL;
2782 RENAME_RSP *pSMBr = NULL;
2783 int bytes_returned;
2784 int name_len, name_len2;
2785 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002786 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Joe Perchesf96637b2013-05-04 22:12:25 -05002788 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789renameRetry:
2790 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2791 (void **) &pSMBr);
2792 if (rc)
2793 return rc;
2794
2795 pSMB->BufferFormat = 0x04;
2796 pSMB->SearchAttributes =
2797 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2798 ATTR_DIRECTORY);
2799
2800 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002801 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2802 from_name, PATH_MAX,
2803 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 name_len++; /* trailing null */
2805 name_len *= 2;
2806 pSMB->OldFileName[name_len] = 0x04; /* pad */
2807 /* protocol requires ASCII signature byte on Unicode string */
2808 pSMB->OldFileName[name_len + 1] = 0x00;
2809 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002810 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002811 to_name, PATH_MAX, cifs_sb->local_nls,
2812 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2814 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002815 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002816 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002818 strncpy(pSMB->OldFileName, from_name, name_len);
2819 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 name_len2++; /* trailing null */
2821 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002822 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 name_len2++; /* trailing null */
2824 name_len2++; /* signature byte */
2825 }
2826
2827 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002828 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 pSMB->ByteCount = cpu_to_le16(count);
2830
2831 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2832 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002833 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002834 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002835 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 cifs_buf_release(pSMB);
2838
2839 if (rc == -EAGAIN)
2840 goto renameRetry;
2841
2842 return rc;
2843}
2844
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002845int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002846 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002847 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2850 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002851 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 char *data_offset;
2853 char dummy_string[30];
2854 int rc = 0;
2855 int bytes_returned = 0;
2856 int len_of_str;
2857 __u16 params, param_offset, offset, count, byte_count;
2858
Joe Perchesf96637b2013-05-04 22:12:25 -05002859 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2861 (void **) &pSMBr);
2862 if (rc)
2863 return rc;
2864
2865 params = 6;
2866 pSMB->MaxSetupCount = 0;
2867 pSMB->Reserved = 0;
2868 pSMB->Flags = 0;
2869 pSMB->Timeout = 0;
2870 pSMB->Reserved2 = 0;
2871 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2872 offset = param_offset + params;
2873
2874 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2875 rename_info = (struct set_file_rename *) data_offset;
2876 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002877 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 pSMB->SetupCount = 1;
2879 pSMB->Reserved3 = 0;
2880 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2881 byte_count = 3 /* pad */ + params;
2882 pSMB->ParameterCount = cpu_to_le16(params);
2883 pSMB->TotalParameterCount = pSMB->ParameterCount;
2884 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2885 pSMB->DataOffset = cpu_to_le16(offset);
2886 /* construct random name ".cifs_tmp<inodenum><mid>" */
2887 rename_info->overwrite = cpu_to_le32(1);
2888 rename_info->root_fid = 0;
2889 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002890 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002891 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002892 len_of_str =
2893 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002894 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002896 len_of_str =
2897 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002898 target_name, PATH_MAX, nls_codepage,
2899 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002902 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 byte_count += count;
2904 pSMB->DataCount = cpu_to_le16(count);
2905 pSMB->TotalDataCount = pSMB->DataCount;
2906 pSMB->Fid = netfid;
2907 pSMB->InformationLevel =
2908 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2909 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002910 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 pSMB->ByteCount = cpu_to_le16(byte_count);
2912 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002913 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002914 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002915 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002916 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2917 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 cifs_buf_release(pSMB);
2920
2921 /* Note: On -EAGAIN error only caller can retry on handle based calls
2922 since file handle passed in no longer valid */
2923
2924 return rc;
2925}
2926
2927int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002928CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2929 const char *fromName, const __u16 target_tid, const char *toName,
2930 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 int rc = 0;
2933 COPY_REQ *pSMB = NULL;
2934 COPY_RSP *pSMBr = NULL;
2935 int bytes_returned;
2936 int name_len, name_len2;
2937 __u16 count;
2938
Joe Perchesf96637b2013-05-04 22:12:25 -05002939 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940copyRetry:
2941 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2942 (void **) &pSMBr);
2943 if (rc)
2944 return rc;
2945
2946 pSMB->BufferFormat = 0x04;
2947 pSMB->Tid2 = target_tid;
2948
2949 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2950
2951 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002952 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2953 fromName, PATH_MAX, nls_codepage,
2954 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 name_len++; /* trailing null */
2956 name_len *= 2;
2957 pSMB->OldFileName[name_len] = 0x04; /* pad */
2958 /* protocol requires ASCII signature byte on Unicode string */
2959 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002960 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002961 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2962 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2964 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002965 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 name_len = strnlen(fromName, PATH_MAX);
2967 name_len++; /* trailing null */
2968 strncpy(pSMB->OldFileName, fromName, name_len);
2969 name_len2 = strnlen(toName, PATH_MAX);
2970 name_len2++; /* trailing null */
2971 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2972 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2973 name_len2++; /* trailing null */
2974 name_len2++; /* signature byte */
2975 }
2976
2977 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002978 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 pSMB->ByteCount = cpu_to_le16(count);
2980
2981 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2982 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2983 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002984 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2985 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Steve French0d817bc2008-05-22 02:02:03 +00002987 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 if (rc == -EAGAIN)
2990 goto copyRetry;
2991
2992 return rc;
2993}
2994
2995int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002996CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002998 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
3000 TRANSACTION2_SPI_REQ *pSMB = NULL;
3001 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3002 char *data_offset;
3003 int name_len;
3004 int name_len_target;
3005 int rc = 0;
3006 int bytes_returned = 0;
3007 __u16 params, param_offset, offset, byte_count;
3008
Joe Perchesf96637b2013-05-04 22:12:25 -05003009 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010createSymLinkRetry:
3011 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3012 (void **) &pSMBr);
3013 if (rc)
3014 return rc;
3015
3016 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3017 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003018 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
3019 /* find define for this maxpathcomponent */
3020 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 name_len++; /* trailing null */
3022 name_len *= 2;
3023
Steve French50c2f752007-07-13 00:33:32 +00003024 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 name_len = strnlen(fromName, PATH_MAX);
3026 name_len++; /* trailing null */
3027 strncpy(pSMB->FileName, fromName, name_len);
3028 }
3029 params = 6 + name_len;
3030 pSMB->MaxSetupCount = 0;
3031 pSMB->Reserved = 0;
3032 pSMB->Flags = 0;
3033 pSMB->Timeout = 0;
3034 pSMB->Reserved2 = 0;
3035 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003036 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 offset = param_offset + params;
3038
3039 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3040 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3041 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003042 cifsConvertToUTF16((__le16 *) data_offset, toName,
3043 /* find define for this maxpathcomponent */
3044 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 name_len_target++; /* trailing null */
3046 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003047 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 name_len_target = strnlen(toName, PATH_MAX);
3049 name_len_target++; /* trailing null */
3050 strncpy(data_offset, toName, name_len_target);
3051 }
3052
3053 pSMB->MaxParameterCount = cpu_to_le16(2);
3054 /* BB find exact max on data count below from sess */
3055 pSMB->MaxDataCount = cpu_to_le16(1000);
3056 pSMB->SetupCount = 1;
3057 pSMB->Reserved3 = 0;
3058 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3059 byte_count = 3 /* pad */ + params + name_len_target;
3060 pSMB->DataCount = cpu_to_le16(name_len_target);
3061 pSMB->ParameterCount = cpu_to_le16(params);
3062 pSMB->TotalDataCount = pSMB->DataCount;
3063 pSMB->TotalParameterCount = pSMB->ParameterCount;
3064 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3065 pSMB->DataOffset = cpu_to_le16(offset);
3066 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
3067 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003068 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 pSMB->ByteCount = cpu_to_le16(byte_count);
3070 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3071 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003072 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003073 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003074 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
3075 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Steve French0d817bc2008-05-22 02:02:03 +00003077 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 if (rc == -EAGAIN)
3080 goto createSymLinkRetry;
3081
3082 return rc;
3083}
3084
3085int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003086CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003088 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
3090 TRANSACTION2_SPI_REQ *pSMB = NULL;
3091 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3092 char *data_offset;
3093 int name_len;
3094 int name_len_target;
3095 int rc = 0;
3096 int bytes_returned = 0;
3097 __u16 params, param_offset, offset, byte_count;
3098
Joe Perchesf96637b2013-05-04 22:12:25 -05003099 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100createHardLinkRetry:
3101 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3102 (void **) &pSMBr);
3103 if (rc)
3104 return rc;
3105
3106 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06003107 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
3108 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 name_len++; /* trailing null */
3110 name_len *= 2;
3111
Steve French50c2f752007-07-13 00:33:32 +00003112 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 name_len = strnlen(toName, PATH_MAX);
3114 name_len++; /* trailing null */
3115 strncpy(pSMB->FileName, toName, name_len);
3116 }
3117 params = 6 + name_len;
3118 pSMB->MaxSetupCount = 0;
3119 pSMB->Reserved = 0;
3120 pSMB->Flags = 0;
3121 pSMB->Timeout = 0;
3122 pSMB->Reserved2 = 0;
3123 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003124 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 offset = param_offset + params;
3126
3127 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3128 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3129 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06003130 cifsConvertToUTF16((__le16 *) data_offset, fromName,
3131 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 name_len_target++; /* trailing null */
3133 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003134 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 name_len_target = strnlen(fromName, PATH_MAX);
3136 name_len_target++; /* trailing null */
3137 strncpy(data_offset, fromName, name_len_target);
3138 }
3139
3140 pSMB->MaxParameterCount = cpu_to_le16(2);
3141 /* BB find exact max on data count below from sess*/
3142 pSMB->MaxDataCount = cpu_to_le16(1000);
3143 pSMB->SetupCount = 1;
3144 pSMB->Reserved3 = 0;
3145 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3146 byte_count = 3 /* pad */ + params + name_len_target;
3147 pSMB->ParameterCount = cpu_to_le16(params);
3148 pSMB->TotalParameterCount = pSMB->ParameterCount;
3149 pSMB->DataCount = cpu_to_le16(name_len_target);
3150 pSMB->TotalDataCount = pSMB->DataCount;
3151 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3152 pSMB->DataOffset = cpu_to_le16(offset);
3153 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3154 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003155 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 pSMB->ByteCount = cpu_to_le16(byte_count);
3157 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3158 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003159 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003160 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003161 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3162 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 cifs_buf_release(pSMB);
3165 if (rc == -EAGAIN)
3166 goto createHardLinkRetry;
3167
3168 return rc;
3169}
3170
3171int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003172CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07003173 const char *from_name, const char *to_name,
3174 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
3176 int rc = 0;
3177 NT_RENAME_REQ *pSMB = NULL;
3178 RENAME_RSP *pSMBr = NULL;
3179 int bytes_returned;
3180 int name_len, name_len2;
3181 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003182 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Joe Perchesf96637b2013-05-04 22:12:25 -05003184 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185winCreateHardLinkRetry:
3186
3187 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3188 (void **) &pSMBr);
3189 if (rc)
3190 return rc;
3191
3192 pSMB->SearchAttributes =
3193 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3194 ATTR_DIRECTORY);
3195 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3196 pSMB->ClusterCount = 0;
3197
3198 pSMB->BufferFormat = 0x04;
3199
3200 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3201 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003202 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3203 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 name_len++; /* trailing null */
3205 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003206
3207 /* protocol specifies ASCII buffer format (0x04) for unicode */
3208 pSMB->OldFileName[name_len] = 0x04;
3209 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003211 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003212 to_name, PATH_MAX, cifs_sb->local_nls,
3213 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3215 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003216 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003217 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003219 strncpy(pSMB->OldFileName, from_name, name_len);
3220 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 name_len2++; /* trailing null */
3222 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003223 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 name_len2++; /* trailing null */
3225 name_len2++; /* signature byte */
3226 }
3227
3228 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003229 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 pSMB->ByteCount = cpu_to_le16(count);
3231
3232 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3233 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003234 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003235 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003236 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 cifs_buf_release(pSMB);
3239 if (rc == -EAGAIN)
3240 goto winCreateHardLinkRetry;
3241
3242 return rc;
3243}
3244
3245int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003246CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003247 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003248 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
3250/* SMB_QUERY_FILE_UNIX_LINK */
3251 TRANSACTION2_QPI_REQ *pSMB = NULL;
3252 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3253 int rc = 0;
3254 int bytes_returned;
3255 int name_len;
3256 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003257 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Joe Perchesf96637b2013-05-04 22:12:25 -05003259 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261querySymLinkRetry:
3262 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3263 (void **) &pSMBr);
3264 if (rc)
3265 return rc;
3266
3267 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3268 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003269 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3270 searchName, PATH_MAX, nls_codepage,
3271 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 name_len++; /* trailing null */
3273 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003274 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 name_len = strnlen(searchName, PATH_MAX);
3276 name_len++; /* trailing null */
3277 strncpy(pSMB->FileName, searchName, name_len);
3278 }
3279
3280 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3281 pSMB->TotalDataCount = 0;
3282 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003283 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 pSMB->MaxSetupCount = 0;
3285 pSMB->Reserved = 0;
3286 pSMB->Flags = 0;
3287 pSMB->Timeout = 0;
3288 pSMB->Reserved2 = 0;
3289 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003290 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 pSMB->DataCount = 0;
3292 pSMB->DataOffset = 0;
3293 pSMB->SetupCount = 1;
3294 pSMB->Reserved3 = 0;
3295 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3296 byte_count = params + 1 /* pad */ ;
3297 pSMB->TotalParameterCount = cpu_to_le16(params);
3298 pSMB->ParameterCount = pSMB->TotalParameterCount;
3299 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3300 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003301 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 pSMB->ByteCount = cpu_to_le16(byte_count);
3303
3304 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3305 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3306 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003307 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 } else {
3309 /* decode response */
3310
3311 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003313 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003314 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003316 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003317 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Jeff Layton460b9692009-04-30 07:17:56 -04003319 data_start = ((char *) &pSMBr->hdr.Protocol) +
3320 le16_to_cpu(pSMBr->t2.DataOffset);
3321
Steve French0e0d2cf2009-05-01 05:27:32 +00003322 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3323 is_unicode = true;
3324 else
3325 is_unicode = false;
3326
Steve French737b7582005-04-28 22:41:06 -07003327 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003328 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3329 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003330 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003331 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333 }
3334 cifs_buf_release(pSMB);
3335 if (rc == -EAGAIN)
3336 goto querySymLinkRetry;
3337 return rc;
3338}
3339
Steve Frenchc52a95542011-02-24 06:16:22 +00003340/*
3341 * Recent Windows versions now create symlinks more frequently
3342 * and they use the "reparse point" mechanism below. We can of course
3343 * do symlinks nicely to Samba and other servers which support the
3344 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3345 * "MF" symlinks optionally, but for recent Windows we really need to
3346 * reenable the code below and fix the cifs_symlink callers to handle this.
3347 * In the interim this code has been moved to its own config option so
3348 * it is not compiled in by default until callers fixed up and more tested.
3349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003351CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3352 __u16 fid, char **symlinkinfo,
3353 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 int rc = 0;
3356 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003357 struct smb_com_transaction_ioctl_req *pSMB;
3358 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003359 bool is_unicode;
3360 unsigned int sub_len;
3361 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003362 struct reparse_symlink_data *reparse_buf;
3363 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003364 __u32 data_offset, data_count;
3365 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003367 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3369 (void **) &pSMBr);
3370 if (rc)
3371 return rc;
3372
3373 pSMB->TotalParameterCount = 0 ;
3374 pSMB->TotalDataCount = 0;
3375 pSMB->MaxParameterCount = cpu_to_le32(2);
3376 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003377 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 pSMB->MaxSetupCount = 4;
3379 pSMB->Reserved = 0;
3380 pSMB->ParameterOffset = 0;
3381 pSMB->DataCount = 0;
3382 pSMB->DataOffset = 0;
3383 pSMB->SetupCount = 4;
3384 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3385 pSMB->ParameterCount = pSMB->TotalParameterCount;
3386 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3387 pSMB->IsFsctl = 1; /* FSCTL */
3388 pSMB->IsRootFlag = 0;
3389 pSMB->Fid = fid; /* file handle always le */
3390 pSMB->ByteCount = 0;
3391
3392 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3393 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3394 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003395 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003396 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
Steve French989c7e52009-05-02 05:32:20 +00003398
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003399 data_offset = le32_to_cpu(pSMBr->DataOffset);
3400 data_count = le32_to_cpu(pSMBr->DataCount);
3401 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3402 /* BB also check enough total bytes returned */
3403 rc = -EIO; /* bad smb */
3404 goto qreparse_out;
3405 }
3406 if (!data_count || (data_count > 2048)) {
3407 rc = -EIO;
3408 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3409 goto qreparse_out;
3410 }
3411 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003412 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003413 ((char *)&pSMBr->hdr.Protocol + data_offset);
3414 if ((char *)reparse_buf >= end_of_smb) {
3415 rc = -EIO;
3416 goto qreparse_out;
3417 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003418 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3419 cifs_dbg(FYI, "NFS style reparse tag\n");
3420 posix_buf = (struct reparse_posix_data *)reparse_buf;
3421
3422 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3423 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3424 le64_to_cpu(posix_buf->InodeType));
3425 rc = -EOPNOTSUPP;
3426 goto qreparse_out;
3427 }
3428 is_unicode = true;
3429 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3430 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3431 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3432 rc = -EIO;
3433 goto qreparse_out;
3434 }
3435 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3436 sub_len, is_unicode, nls_codepage);
3437 goto qreparse_out;
3438 } else if (reparse_buf->ReparseTag !=
3439 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3440 rc = -EOPNOTSUPP;
3441 goto qreparse_out;
3442 }
3443
3444 /* Reparse tag is NTFS symlink */
3445 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3446 reparse_buf->PathBuffer;
3447 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3448 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003449 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3450 rc = -EIO;
3451 goto qreparse_out;
3452 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003453 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3454 is_unicode = true;
3455 else
3456 is_unicode = false;
3457
3458 /* BB FIXME investigate remapping reserved chars here */
3459 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3460 nls_codepage);
3461 if (!*symlinkinfo)
3462 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003464 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003466 /*
3467 * Note: On -EAGAIN error only caller can retry on handle based calls
3468 * since file handle passed in no longer valid.
3469 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return rc;
3471}
3472
Steve Frenchc7f508a2013-10-14 15:27:32 -05003473int
3474CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3475 __u16 fid)
3476{
3477 int rc = 0;
3478 int bytes_returned;
3479 struct smb_com_transaction_compr_ioctl_req *pSMB;
3480 struct smb_com_transaction_ioctl_rsp *pSMBr;
3481
3482 cifs_dbg(FYI, "Set compression for %u\n", fid);
3483 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3484 (void **) &pSMBr);
3485 if (rc)
3486 return rc;
3487
3488 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3489
3490 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003491 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003492 pSMB->MaxParameterCount = 0;
3493 pSMB->MaxDataCount = 0;
3494 pSMB->MaxSetupCount = 4;
3495 pSMB->Reserved = 0;
3496 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003497 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003498 pSMB->DataOffset =
3499 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3500 compression_state) - 4); /* 84 */
3501 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003502 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003503 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003504 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003505 pSMB->IsFsctl = 1; /* FSCTL */
3506 pSMB->IsRootFlag = 0;
3507 pSMB->Fid = fid; /* file handle always le */
3508 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003509 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003510 inc_rfc1001_len(pSMB, 5);
3511
3512 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3513 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3514 if (rc)
3515 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3516
3517 cifs_buf_release(pSMB);
3518
3519 /*
3520 * Note: On -EAGAIN error only caller can retry on handle based calls
3521 * since file handle passed in no longer valid.
3522 */
3523 return rc;
3524}
3525
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527#ifdef CONFIG_CIFS_POSIX
3528
3529/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003530static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003531 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003534 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3535 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3536 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003537/*
3538 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3539 ace->e_perm, ace->e_tag, ace->e_id);
3540*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 return;
3543}
3544
3545/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003546static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3547 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 int size = 0;
3550 int i;
3551 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003552 struct cifs_posix_ace *pACE;
3553 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003554 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3557 return -EOPNOTSUPP;
3558
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003559 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 count = le16_to_cpu(cifs_acl->access_entry_count);
3561 pACE = &cifs_acl->ace_array[0];
3562 size = sizeof(struct cifs_posix_acl);
3563 size += sizeof(struct cifs_posix_ace) * count;
3564 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003565 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003566 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3567 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 return -EINVAL;
3569 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003570 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 count = le16_to_cpu(cifs_acl->access_entry_count);
3572 size = sizeof(struct cifs_posix_acl);
3573 size += sizeof(struct cifs_posix_ace) * count;
3574/* skip past access ACEs to get to default ACEs */
3575 pACE = &cifs_acl->ace_array[count];
3576 count = le16_to_cpu(cifs_acl->default_entry_count);
3577 size += sizeof(struct cifs_posix_ace) * count;
3578 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003579 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 return -EINVAL;
3581 } else {
3582 /* illegal type */
3583 return -EINVAL;
3584 }
3585
3586 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003587 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003588 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003589 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return -ERANGE;
3591 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003592 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3593
Steve Frenchff7feac2005-11-15 16:45:16 -08003594 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003595 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003596 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003597 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599 }
3600 return size;
3601}
3602
Hariprasad Kelam0aa3a242019-07-02 23:50:02 +05303603static void convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003604 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Steve Frenchff7feac2005-11-15 16:45:16 -08003606 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3607 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003609 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* Probably no need to le convert -1 on any arch but can not hurt */
3611 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003612 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003613 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003614/*
3615 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3616 ace->e_perm, ace->e_tag, ace->e_id);
3617*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
3620/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003621static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3622 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003625 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003626 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003627 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 int count;
3629 int i;
3630
Steve French790fe572007-07-07 19:25:05 +00003631 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 return 0;
3633
3634 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003635 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3636 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003637 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003638 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3639 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return 0;
3641 }
3642 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003643 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003644 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003645 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003646 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003647 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003648 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003649 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003650 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return 0;
3652 }
Hariprasad Kelam0aa3a242019-07-02 23:50:02 +05303653 for (i = 0; i < count; i++)
3654 convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003655 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3657 rc += sizeof(struct cifs_posix_acl);
3658 /* BB add check to make sure ACL does not overflow SMB */
3659 }
3660 return rc;
3661}
3662
3663int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003664CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003665 const unsigned char *searchName,
3666 char *acl_inf, const int buflen, const int acl_type,
3667 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669/* SMB_QUERY_POSIX_ACL */
3670 TRANSACTION2_QPI_REQ *pSMB = NULL;
3671 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3672 int rc = 0;
3673 int bytes_returned;
3674 int name_len;
3675 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003676
Joe Perchesf96637b2013-05-04 22:12:25 -05003677 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679queryAclRetry:
3680 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3681 (void **) &pSMBr);
3682 if (rc)
3683 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3686 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003687 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3688 searchName, PATH_MAX, nls_codepage,
3689 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 name_len++; /* trailing null */
3691 name_len *= 2;
3692 pSMB->FileName[name_len] = 0;
3693 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003694 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 name_len = strnlen(searchName, PATH_MAX);
3696 name_len++; /* trailing null */
3697 strncpy(pSMB->FileName, searchName, name_len);
3698 }
3699
3700 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3701 pSMB->TotalDataCount = 0;
3702 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003703 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 pSMB->MaxDataCount = cpu_to_le16(4000);
3705 pSMB->MaxSetupCount = 0;
3706 pSMB->Reserved = 0;
3707 pSMB->Flags = 0;
3708 pSMB->Timeout = 0;
3709 pSMB->Reserved2 = 0;
3710 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003711 offsetof(struct smb_com_transaction2_qpi_req,
3712 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 pSMB->DataCount = 0;
3714 pSMB->DataOffset = 0;
3715 pSMB->SetupCount = 1;
3716 pSMB->Reserved3 = 0;
3717 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3718 byte_count = params + 1 /* pad */ ;
3719 pSMB->TotalParameterCount = cpu_to_le16(params);
3720 pSMB->ParameterCount = pSMB->TotalParameterCount;
3721 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3722 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003723 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 pSMB->ByteCount = cpu_to_le16(byte_count);
3725
3726 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3727 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003728 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003730 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 } else {
3732 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003736 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 rc = -EIO; /* bad smb */
3738 else {
3739 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3740 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3741 rc = cifs_copy_posix_acl(acl_inf,
3742 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003743 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745 }
3746 cifs_buf_release(pSMB);
3747 if (rc == -EAGAIN)
3748 goto queryAclRetry;
3749 return rc;
3750}
3751
3752int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003753CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003754 const unsigned char *fileName,
3755 const char *local_acl, const int buflen,
3756 const int acl_type,
3757 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
3759 struct smb_com_transaction2_spi_req *pSMB = NULL;
3760 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3761 char *parm_data;
3762 int name_len;
3763 int rc = 0;
3764 int bytes_returned = 0;
3765 __u16 params, byte_count, data_count, param_offset, offset;
3766
Joe Perchesf96637b2013-05-04 22:12:25 -05003767 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768setAclRetry:
3769 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003770 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (rc)
3772 return rc;
3773 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3774 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003775 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3776 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 name_len++; /* trailing null */
3778 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003779 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 name_len = strnlen(fileName, PATH_MAX);
3781 name_len++; /* trailing null */
3782 strncpy(pSMB->FileName, fileName, name_len);
3783 }
3784 params = 6 + name_len;
3785 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003786 /* BB find max SMB size from sess */
3787 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 pSMB->MaxSetupCount = 0;
3789 pSMB->Reserved = 0;
3790 pSMB->Flags = 0;
3791 pSMB->Timeout = 0;
3792 pSMB->Reserved2 = 0;
3793 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003794 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 offset = param_offset + params;
3796 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3797 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3798
3799 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003800 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Steve French790fe572007-07-07 19:25:05 +00003802 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 rc = -EOPNOTSUPP;
3804 goto setACLerrorExit;
3805 }
3806 pSMB->DataOffset = cpu_to_le16(offset);
3807 pSMB->SetupCount = 1;
3808 pSMB->Reserved3 = 0;
3809 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3810 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3811 byte_count = 3 /* pad */ + params + data_count;
3812 pSMB->DataCount = cpu_to_le16(data_count);
3813 pSMB->TotalDataCount = pSMB->DataCount;
3814 pSMB->ParameterCount = cpu_to_le16(params);
3815 pSMB->TotalParameterCount = pSMB->ParameterCount;
3816 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003817 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 pSMB->ByteCount = cpu_to_le16(byte_count);
3819 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003820 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003821 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003822 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824setACLerrorExit:
3825 cifs_buf_release(pSMB);
3826 if (rc == -EAGAIN)
3827 goto setAclRetry;
3828 return rc;
3829}
3830
Steve Frenchf654bac2005-04-28 22:41:04 -07003831/* BB fix tabs in this function FIXME BB */
3832int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003833CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003834 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003835{
Steve French50c2f752007-07-13 00:33:32 +00003836 int rc = 0;
3837 struct smb_t2_qfi_req *pSMB = NULL;
3838 struct smb_t2_qfi_rsp *pSMBr = NULL;
3839 int bytes_returned;
3840 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003841
Joe Perchesf96637b2013-05-04 22:12:25 -05003842 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003843 if (tcon == NULL)
3844 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003845
3846GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003847 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3848 (void **) &pSMBr);
3849 if (rc)
3850 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003851
Steve Frenchad7a2922008-02-07 23:25:02 +00003852 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003853 pSMB->t2.TotalDataCount = 0;
3854 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3855 /* BB find exact max data count below from sess structure BB */
3856 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3857 pSMB->t2.MaxSetupCount = 0;
3858 pSMB->t2.Reserved = 0;
3859 pSMB->t2.Flags = 0;
3860 pSMB->t2.Timeout = 0;
3861 pSMB->t2.Reserved2 = 0;
3862 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3863 Fid) - 4);
3864 pSMB->t2.DataCount = 0;
3865 pSMB->t2.DataOffset = 0;
3866 pSMB->t2.SetupCount = 1;
3867 pSMB->t2.Reserved3 = 0;
3868 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3869 byte_count = params + 1 /* pad */ ;
3870 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3871 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3872 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3873 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003874 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003875 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003876 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003877
Steve French790fe572007-07-07 19:25:05 +00003878 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3879 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3880 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003881 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003882 } else {
3883 /* decode response */
3884 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003885 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003886 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003887 /* If rc should we check for EOPNOSUPP and
3888 disable the srvino flag? or in caller? */
3889 rc = -EIO; /* bad smb */
3890 else {
3891 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3892 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3893 struct file_chattr_info *pfinfo;
3894 /* BB Do we need a cast or hash here ? */
3895 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003896 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003897 rc = -EIO;
3898 goto GetExtAttrOut;
3899 }
3900 pfinfo = (struct file_chattr_info *)
3901 (data_offset + (char *) &pSMBr->hdr.Protocol);
3902 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003903 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003904 }
3905 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003906GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003907 cifs_buf_release(pSMB);
3908 if (rc == -EAGAIN)
3909 goto GetExtAttrRetry;
3910 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003911}
3912
Steve Frenchf654bac2005-04-28 22:41:04 -07003913#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Jeff Layton79df1ba2010-12-06 12:52:08 -05003915/*
3916 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3917 * all NT TRANSACTS that we init here have total parm and data under about 400
3918 * bytes (to fit in small cifs buffer size), which is the case so far, it
3919 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3920 * returned setup area) and MaxParameterCount (returned parms size) must be set
3921 * by caller
3922 */
3923static int
3924smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003925 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003926 void **ret_buf)
3927{
3928 int rc;
3929 __u32 temp_offset;
3930 struct smb_com_ntransact_req *pSMB;
3931
3932 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3933 (void **)&pSMB);
3934 if (rc)
3935 return rc;
3936 *ret_buf = (void *)pSMB;
3937 pSMB->Reserved = 0;
3938 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3939 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003940 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003941 pSMB->ParameterCount = pSMB->TotalParameterCount;
3942 pSMB->DataCount = pSMB->TotalDataCount;
3943 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3944 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3945 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3946 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3947 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3948 pSMB->SubCommand = cpu_to_le16(sub_command);
3949 return 0;
3950}
3951
3952static int
3953validate_ntransact(char *buf, char **ppparm, char **ppdata,
3954 __u32 *pparmlen, __u32 *pdatalen)
3955{
3956 char *end_of_smb;
3957 __u32 data_count, data_offset, parm_count, parm_offset;
3958 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003959 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003960
3961 *pdatalen = 0;
3962 *pparmlen = 0;
3963
3964 if (buf == NULL)
3965 return -EINVAL;
3966
3967 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3968
Jeff Layton820a8032011-05-04 08:05:26 -04003969 bcc = get_bcc(&pSMBr->hdr);
3970 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003971 (char *)&pSMBr->ByteCount;
3972
3973 data_offset = le32_to_cpu(pSMBr->DataOffset);
3974 data_count = le32_to_cpu(pSMBr->DataCount);
3975 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3976 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3977
3978 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3979 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3980
3981 /* should we also check that parm and data areas do not overlap? */
3982 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003983 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003984 return -EINVAL;
3985 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003986 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003987 return -EINVAL;
3988 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003989 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003990 return -EINVAL;
3991 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003992 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3993 *ppdata, data_count, (data_count + *ppdata),
3994 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003995 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003996 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003997 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003998 return -EINVAL;
3999 }
4000 *pdatalen = data_count;
4001 *pparmlen = parm_count;
4002 return 0;
4003}
4004
Steve French0a4b92c2006-01-12 15:44:21 -08004005/* Get Security Descriptor (by handle) from remote server for a file or dir */
4006int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004007CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00004008 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08004009{
4010 int rc = 0;
4011 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00004012 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08004013 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004014 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08004015
Joe Perchesf96637b2013-05-04 22:12:25 -05004016 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08004017
Steve French630f3f0c2007-10-25 21:17:17 +00004018 *pbuflen = 0;
4019 *acl_inf = NULL;
4020
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00004021 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08004022 8 /* parm len */, tcon, (void **) &pSMB);
4023 if (rc)
4024 return rc;
4025
4026 pSMB->MaxParameterCount = cpu_to_le32(4);
4027 /* BB TEST with big acls that might need to be e.g. larger than 16K */
4028 pSMB->MaxSetupCount = 0;
4029 pSMB->Fid = fid; /* file handle always le */
4030 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
4031 CIFS_ACL_DACL);
4032 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004033 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08004034 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004035 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08004036
Steve Frencha761ac52007-10-18 21:45:27 +00004037 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004038 0, &rsp_iov);
4039 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004040 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08004041 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004042 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08004043 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00004044 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00004045 __u32 parm_len;
4046 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00004047 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00004048 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08004049
4050/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004051 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00004052 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00004053 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08004054 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004055 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08004056
Joe Perchesf96637b2013-05-04 22:12:25 -05004057 cifs_dbg(FYI, "smb %p parm %p data %p\n",
4058 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08004059
4060 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
4061 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00004062 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08004063 goto qsec_out;
4064 }
4065
4066/* BB check that data area is minimum length and as big as acl_len */
4067
Steve Frenchaf6f4612007-10-16 18:40:37 +00004068 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00004069 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004070 cifs_dbg(VFS, "acl length %d does not match %d\n",
4071 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004072 if (*pbuflen > acl_len)
4073 *pbuflen = acl_len;
4074 }
Steve French0a4b92c2006-01-12 15:44:21 -08004075
Steve French630f3f0c2007-10-25 21:17:17 +00004076 /* check if buffer is big enough for the acl
4077 header followed by the smallest SID */
4078 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
4079 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004080 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004081 rc = -EINVAL;
4082 *pbuflen = 0;
4083 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02004084 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00004085 if (*acl_inf == NULL) {
4086 *pbuflen = 0;
4087 rc = -ENOMEM;
4088 }
Steve French630f3f0c2007-10-25 21:17:17 +00004089 }
Steve French0a4b92c2006-01-12 15:44:21 -08004090 }
4091qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004092 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08004093 return rc;
4094}
Steve French97837582007-12-31 07:47:21 +00004095
4096int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004097CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004098 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00004099{
4100 __u16 byte_count, param_count, data_count, param_offset, data_offset;
4101 int rc = 0;
4102 int bytes_returned = 0;
4103 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004104 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00004105
4106setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004107 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00004108 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004109 return rc;
Steve French97837582007-12-31 07:47:21 +00004110
4111 pSMB->MaxSetupCount = 0;
4112 pSMB->Reserved = 0;
4113
4114 param_count = 8;
4115 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
4116 data_count = acllen;
4117 data_offset = param_offset + param_count;
4118 byte_count = 3 /* pad */ + param_count;
4119
4120 pSMB->DataCount = cpu_to_le32(data_count);
4121 pSMB->TotalDataCount = pSMB->DataCount;
4122 pSMB->MaxParameterCount = cpu_to_le32(4);
4123 pSMB->MaxDataCount = cpu_to_le32(16384);
4124 pSMB->ParameterCount = cpu_to_le32(param_count);
4125 pSMB->ParameterOffset = cpu_to_le32(param_offset);
4126 pSMB->TotalParameterCount = pSMB->ParameterCount;
4127 pSMB->DataOffset = cpu_to_le32(data_offset);
4128 pSMB->SetupCount = 0;
4129 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
4130 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
4131
4132 pSMB->Fid = fid; /* file handle always le */
4133 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004134 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00004135
4136 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004137 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
4138 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004139 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00004140 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004141 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00004142
4143 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4144 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4145
Joe Perchesf96637b2013-05-04 22:12:25 -05004146 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4147 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00004148 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004149 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00004150 cifs_buf_release(pSMB);
4151
4152 if (rc == -EAGAIN)
4153 goto setCifsAclRetry;
4154
4155 return (rc);
4156}
4157
Steve French0a4b92c2006-01-12 15:44:21 -08004158
Steve French6b8edfe2005-08-23 20:26:03 -07004159/* Legacy Query Path Information call for lookup to old servers such
4160 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004161int
4162SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4163 const char *search_name, FILE_ALL_INFO *data,
4164 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07004165{
Steve Frenchad7a2922008-02-07 23:25:02 +00004166 QUERY_INFORMATION_REQ *pSMB;
4167 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07004168 int rc = 0;
4169 int bytes_returned;
4170 int name_len;
4171
Joe Perchesf96637b2013-05-04 22:12:25 -05004172 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004173QInfRetry:
4174 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004175 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004176 if (rc)
4177 return rc;
4178
4179 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4180 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004181 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004182 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004183 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004184 name_len++; /* trailing null */
4185 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004186 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004187 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004188 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004189 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004190 }
4191 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004192 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004193 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004194 pSMB->ByteCount = cpu_to_le16(name_len);
4195
4196 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004197 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004198 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004199 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004200 } else if (data) {
Arnd Bergmann95390202018-06-19 17:27:58 +02004201 struct timespec64 ts;
Steve French1bd5bbc2006-09-28 03:35:57 +00004202 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004203
4204 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004205 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004206 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004207 ts.tv_nsec = 0;
4208 ts.tv_sec = time;
4209 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004210 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4211 data->LastWriteTime = data->ChangeTime;
4212 data->LastAccessTime = 0;
4213 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004214 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004215 data->EndOfFile = data->AllocationSize;
4216 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004217 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004218 } else
4219 rc = -EIO; /* bad buffer passed in */
4220
4221 cifs_buf_release(pSMB);
4222
4223 if (rc == -EAGAIN)
4224 goto QInfRetry;
4225
4226 return rc;
4227}
4228
Jeff Laytonbcd53572010-02-12 07:44:16 -05004229int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004230CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004231 u16 netfid, FILE_ALL_INFO *pFindData)
4232{
4233 struct smb_t2_qfi_req *pSMB = NULL;
4234 struct smb_t2_qfi_rsp *pSMBr = NULL;
4235 int rc = 0;
4236 int bytes_returned;
4237 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004238
Jeff Laytonbcd53572010-02-12 07:44:16 -05004239QFileInfoRetry:
4240 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4241 (void **) &pSMBr);
4242 if (rc)
4243 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004244
Jeff Laytonbcd53572010-02-12 07:44:16 -05004245 params = 2 /* level */ + 2 /* fid */;
4246 pSMB->t2.TotalDataCount = 0;
4247 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4248 /* BB find exact max data count below from sess structure BB */
4249 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4250 pSMB->t2.MaxSetupCount = 0;
4251 pSMB->t2.Reserved = 0;
4252 pSMB->t2.Flags = 0;
4253 pSMB->t2.Timeout = 0;
4254 pSMB->t2.Reserved2 = 0;
4255 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4256 Fid) - 4);
4257 pSMB->t2.DataCount = 0;
4258 pSMB->t2.DataOffset = 0;
4259 pSMB->t2.SetupCount = 1;
4260 pSMB->t2.Reserved3 = 0;
4261 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4262 byte_count = params + 1 /* pad */ ;
4263 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4264 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4265 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4266 pSMB->Pad = 0;
4267 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004268 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004269 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004270
4271 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4272 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4273 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004274 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004275 } else { /* decode response */
4276 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4277
4278 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4279 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004280 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004281 rc = -EIO; /* bad smb */
4282 else if (pFindData) {
4283 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4284 memcpy((char *) pFindData,
4285 (char *) &pSMBr->hdr.Protocol +
4286 data_offset, sizeof(FILE_ALL_INFO));
4287 } else
4288 rc = -ENOMEM;
4289 }
4290 cifs_buf_release(pSMB);
4291 if (rc == -EAGAIN)
4292 goto QFileInfoRetry;
4293
4294 return rc;
4295}
Steve French6b8edfe2005-08-23 20:26:03 -07004296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004298CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004299 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004300 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004301 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004303 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 TRANSACTION2_QPI_REQ *pSMB = NULL;
4305 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4306 int rc = 0;
4307 int bytes_returned;
4308 int name_len;
4309 __u16 params, byte_count;
4310
Joe Perchesf96637b2013-05-04 22:12:25 -05004311 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312QPathInfoRetry:
4313 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4314 (void **) &pSMBr);
4315 if (rc)
4316 return rc;
4317
4318 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4319 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004320 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004321 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 name_len++; /* trailing null */
4323 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004324 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004325 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004327 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
4329
Steve French50c2f752007-07-13 00:33:32 +00004330 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 pSMB->TotalDataCount = 0;
4332 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004333 /* BB find exact max SMB PDU from sess structure BB */
4334 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 pSMB->MaxSetupCount = 0;
4336 pSMB->Reserved = 0;
4337 pSMB->Flags = 0;
4338 pSMB->Timeout = 0;
4339 pSMB->Reserved2 = 0;
4340 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004341 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 pSMB->DataCount = 0;
4343 pSMB->DataOffset = 0;
4344 pSMB->SetupCount = 1;
4345 pSMB->Reserved3 = 0;
4346 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4347 byte_count = params + 1 /* pad */ ;
4348 pSMB->TotalParameterCount = cpu_to_le16(params);
4349 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004350 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004351 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4352 else
4353 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004355 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 pSMB->ByteCount = cpu_to_le16(byte_count);
4357
4358 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4359 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4360 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004361 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 } else { /* decode response */
4363 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4364
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004365 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4366 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004367 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004369 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004370 rc = -EIO; /* 24 or 26 expected but we do not read
4371 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004372 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004373 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004375
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004376 /*
4377 * On legacy responses we do not read the last field,
4378 * EAsize, fortunately since it varies by subdialect and
4379 * also note it differs on Set vs Get, ie two bytes or 4
4380 * bytes depending but we don't care here.
4381 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004382 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004383 size = sizeof(FILE_INFO_STANDARD);
4384 else
4385 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004386 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004387 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 } else
4389 rc = -ENOMEM;
4390 }
4391 cifs_buf_release(pSMB);
4392 if (rc == -EAGAIN)
4393 goto QPathInfoRetry;
4394
4395 return rc;
4396}
4397
4398int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004399CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004400 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4401{
4402 struct smb_t2_qfi_req *pSMB = NULL;
4403 struct smb_t2_qfi_rsp *pSMBr = NULL;
4404 int rc = 0;
4405 int bytes_returned;
4406 __u16 params, byte_count;
4407
4408UnixQFileInfoRetry:
4409 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4410 (void **) &pSMBr);
4411 if (rc)
4412 return rc;
4413
4414 params = 2 /* level */ + 2 /* fid */;
4415 pSMB->t2.TotalDataCount = 0;
4416 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4417 /* BB find exact max data count below from sess structure BB */
4418 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4419 pSMB->t2.MaxSetupCount = 0;
4420 pSMB->t2.Reserved = 0;
4421 pSMB->t2.Flags = 0;
4422 pSMB->t2.Timeout = 0;
4423 pSMB->t2.Reserved2 = 0;
4424 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4425 Fid) - 4);
4426 pSMB->t2.DataCount = 0;
4427 pSMB->t2.DataOffset = 0;
4428 pSMB->t2.SetupCount = 1;
4429 pSMB->t2.Reserved3 = 0;
4430 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4431 byte_count = params + 1 /* pad */ ;
4432 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4433 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4434 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4435 pSMB->Pad = 0;
4436 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004437 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004438 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004439
4440 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4441 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4442 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004443 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004444 } else { /* decode response */
4445 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4446
Jeff Layton820a8032011-05-04 08:05:26 -04004447 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004448 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 -05004449 rc = -EIO; /* bad smb */
4450 } else {
4451 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4452 memcpy((char *) pFindData,
4453 (char *) &pSMBr->hdr.Protocol +
4454 data_offset,
4455 sizeof(FILE_UNIX_BASIC_INFO));
4456 }
4457 }
4458
4459 cifs_buf_release(pSMB);
4460 if (rc == -EAGAIN)
4461 goto UnixQFileInfoRetry;
4462
4463 return rc;
4464}
4465
4466int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004467CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004469 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004470 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
4472/* SMB_QUERY_FILE_UNIX_BASIC */
4473 TRANSACTION2_QPI_REQ *pSMB = NULL;
4474 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4475 int rc = 0;
4476 int bytes_returned = 0;
4477 int name_len;
4478 __u16 params, byte_count;
4479
Joe Perchesf96637b2013-05-04 22:12:25 -05004480 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481UnixQPathInfoRetry:
4482 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4483 (void **) &pSMBr);
4484 if (rc)
4485 return rc;
4486
4487 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4488 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004489 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4490 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 name_len++; /* trailing null */
4492 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004493 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 name_len = strnlen(searchName, PATH_MAX);
4495 name_len++; /* trailing null */
4496 strncpy(pSMB->FileName, searchName, name_len);
4497 }
4498
Steve French50c2f752007-07-13 00:33:32 +00004499 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 pSMB->TotalDataCount = 0;
4501 pSMB->MaxParameterCount = cpu_to_le16(2);
4502 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004503 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 pSMB->MaxSetupCount = 0;
4505 pSMB->Reserved = 0;
4506 pSMB->Flags = 0;
4507 pSMB->Timeout = 0;
4508 pSMB->Reserved2 = 0;
4509 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004510 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 pSMB->DataCount = 0;
4512 pSMB->DataOffset = 0;
4513 pSMB->SetupCount = 1;
4514 pSMB->Reserved3 = 0;
4515 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4516 byte_count = params + 1 /* pad */ ;
4517 pSMB->TotalParameterCount = cpu_to_le16(params);
4518 pSMB->ParameterCount = pSMB->TotalParameterCount;
4519 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4520 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004521 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 pSMB->ByteCount = cpu_to_le16(byte_count);
4523
4524 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4525 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4526 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004527 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 } else { /* decode response */
4529 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4530
Jeff Layton820a8032011-05-04 08:05:26 -04004531 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004532 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 -07004533 rc = -EIO; /* bad smb */
4534 } else {
4535 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4536 memcpy((char *) pFindData,
4537 (char *) &pSMBr->hdr.Protocol +
4538 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004539 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 }
4541 }
4542 cifs_buf_release(pSMB);
4543 if (rc == -EAGAIN)
4544 goto UnixQPathInfoRetry;
4545
4546 return rc;
4547}
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549/* xid, tcon, searchName and codepage are input parms, rest are returned */
4550int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004551CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004552 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004553 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004554 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
4556/* level 257 SMB_ */
4557 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4558 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004559 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 int rc = 0;
4561 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004562 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004564 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
Joe Perchesf96637b2013-05-04 22:12:25 -05004566 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568findFirstRetry:
4569 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4570 (void **) &pSMBr);
4571 if (rc)
4572 return rc;
4573
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004574 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004575 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4578 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004579 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4580 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004581 /* We can not add the asterik earlier in case
4582 it got remapped to 0xF03A as if it were part of the
4583 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004585 if (msearch) {
4586 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4587 pSMB->FileName[name_len+1] = 0;
4588 pSMB->FileName[name_len+2] = '*';
4589 pSMB->FileName[name_len+3] = 0;
4590 name_len += 4; /* now the trailing null */
4591 /* null terminate just in case */
4592 pSMB->FileName[name_len] = 0;
4593 pSMB->FileName[name_len+1] = 0;
4594 name_len += 2;
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 } else { /* BB add check for overrun of SMB buf BB */
4597 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004599 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 free buffer exit; BB */
4601 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004602 if (msearch) {
4603 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4604 pSMB->FileName[name_len+1] = '*';
4605 pSMB->FileName[name_len+2] = 0;
4606 name_len += 3;
4607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 }
4609
4610 params = 12 + name_len /* includes null */ ;
4611 pSMB->TotalDataCount = 0; /* no EAs */
4612 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004613 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 pSMB->MaxSetupCount = 0;
4615 pSMB->Reserved = 0;
4616 pSMB->Flags = 0;
4617 pSMB->Timeout = 0;
4618 pSMB->Reserved2 = 0;
4619 byte_count = params + 1 /* pad */ ;
4620 pSMB->TotalParameterCount = cpu_to_le16(params);
4621 pSMB->ParameterCount = pSMB->TotalParameterCount;
4622 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004623 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4624 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 pSMB->DataCount = 0;
4626 pSMB->DataOffset = 0;
4627 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4628 pSMB->Reserved3 = 0;
4629 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4630 pSMB->SearchAttributes =
4631 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4632 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004633 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004634 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4636
4637 /* BB what should we set StorageType to? Does it matter? BB */
4638 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004639 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 pSMB->ByteCount = cpu_to_le16(byte_count);
4641
4642 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4643 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004644 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Steve French88274812006-03-09 22:21:45 +00004646 if (rc) {/* BB add logic to retry regular search if Unix search
4647 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004649 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004650
Steve French88274812006-03-09 22:21:45 +00004651 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
4653 /* BB eventually could optimize out free and realloc of buf */
4654 /* for this case */
4655 if (rc == -EAGAIN)
4656 goto findFirstRetry;
4657 } else { /* decode response */
4658 /* BB remember to free buffer if error BB */
4659 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004660 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004661 unsigned int lnoff;
4662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004664 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 else
Steve French4b18f2a2008-04-29 00:06:05 +00004666 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004669 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004670 psrch_inf->srch_entries_start =
4671 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4674 le16_to_cpu(pSMBr->t2.ParameterOffset));
4675
Steve French790fe572007-07-07 19:25:05 +00004676 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004677 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 else
Steve French4b18f2a2008-04-29 00:06:05 +00004679 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Steve French50c2f752007-07-13 00:33:32 +00004681 psrch_inf->entries_in_buffer =
4682 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004683 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004685 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004686 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004687 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004688 psrch_inf->last_entry = NULL;
4689 return rc;
4690 }
4691
Steve French0752f152008-10-07 20:03:33 +00004692 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004693 lnoff;
4694
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004695 if (pnetfid)
4696 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 } else {
4698 cifs_buf_release(pSMB);
4699 }
4700 }
4701
4702 return rc;
4703}
4704
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004705int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4706 __u16 searchHandle, __u16 search_flags,
4707 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4710 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004711 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 char *response_data;
4713 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004714 int bytes_returned;
4715 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 __u16 params, byte_count;
4717
Joe Perchesf96637b2013-05-04 22:12:25 -05004718 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Steve French4b18f2a2008-04-29 00:06:05 +00004720 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return -ENOENT;
4722
4723 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4724 (void **) &pSMBr);
4725 if (rc)
4726 return rc;
4727
Steve French50c2f752007-07-13 00:33:32 +00004728 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 byte_count = 0;
4730 pSMB->TotalDataCount = 0; /* no EAs */
4731 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004732 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 pSMB->MaxSetupCount = 0;
4734 pSMB->Reserved = 0;
4735 pSMB->Flags = 0;
4736 pSMB->Timeout = 0;
4737 pSMB->Reserved2 = 0;
4738 pSMB->ParameterOffset = cpu_to_le16(
4739 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4740 pSMB->DataCount = 0;
4741 pSMB->DataOffset = 0;
4742 pSMB->SetupCount = 1;
4743 pSMB->Reserved3 = 0;
4744 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4745 pSMB->SearchHandle = searchHandle; /* always kept as le */
4746 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004747 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4749 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004750 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
4752 name_len = psrch_inf->resume_name_len;
4753 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004754 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4756 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004757 /* 14 byte parm len above enough for 2 byte null terminator */
4758 pSMB->ResumeFileName[name_len] = 0;
4759 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 } else {
4761 rc = -EINVAL;
4762 goto FNext2_err_exit;
4763 }
4764 byte_count = params + 1 /* pad */ ;
4765 pSMB->TotalParameterCount = cpu_to_le16(params);
4766 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004767 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4771 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004772 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 if (rc) {
4774 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004775 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004776 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004777 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004779 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 } else { /* decode response */
4781 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004782
Steve French790fe572007-07-07 19:25:05 +00004783 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004784 unsigned int lnoff;
4785
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 /* BB fixme add lock for file (srch_info) struct here */
4787 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004788 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 else
Steve French4b18f2a2008-04-29 00:06:05 +00004790 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 response_data = (char *) &pSMBr->hdr.Protocol +
4792 le16_to_cpu(pSMBr->t2.ParameterOffset);
4793 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4794 response_data = (char *)&pSMBr->hdr.Protocol +
4795 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004796 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004797 cifs_small_buf_release(
4798 psrch_inf->ntwrk_buf_start);
4799 else
4800 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 psrch_inf->srch_entries_start = response_data;
4802 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004803 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004804 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004805 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 else
Steve French4b18f2a2008-04-29 00:06:05 +00004807 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004808 psrch_inf->entries_in_buffer =
4809 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 psrch_inf->index_of_last_entry +=
4811 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004812 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004813 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004814 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004815 psrch_inf->last_entry = NULL;
4816 return rc;
4817 } else
4818 psrch_inf->last_entry =
4819 psrch_inf->srch_entries_start + lnoff;
4820
Joe Perchesf96637b2013-05-04 22:12:25 -05004821/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4822 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 /* BB fixme add unlock here */
4825 }
4826
4827 }
4828
4829 /* BB On error, should we leave previous search buf (and count and
4830 last entry fields) intact or free the previous one? */
4831
4832 /* Note: On -EAGAIN error only caller can retry on handle based calls
4833 since file handle passed in no longer valid */
4834FNext2_err_exit:
4835 if (rc != 0)
4836 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return rc;
4838}
4839
4840int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004841CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004842 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
4844 int rc = 0;
4845 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Joe Perchesf96637b2013-05-04 22:12:25 -05004847 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4849
4850 /* no sense returning error if session restarted
4851 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004852 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 return 0;
4854 if (rc)
4855 return rc;
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 pSMB->FileID = searchHandle;
4858 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004859 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004860 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004861 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004862 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004863
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004864 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 /* Since session is dead, search handle closed on server already */
4867 if (rc == -EAGAIN)
4868 rc = 0;
4869
4870 return rc;
4871}
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004874CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004875 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004876 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
4878 int rc = 0;
4879 TRANSACTION2_QPI_REQ *pSMB = NULL;
4880 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4881 int name_len, bytes_returned;
4882 __u16 params, byte_count;
4883
Joe Perchesf96637b2013-05-04 22:12:25 -05004884 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004885 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004886 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
4888GetInodeNumberRetry:
4889 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004890 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 if (rc)
4892 return rc;
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4895 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004896 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004897 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004898 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 name_len++; /* trailing null */
4900 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004901 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004902 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004904 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 }
4906
4907 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4908 pSMB->TotalDataCount = 0;
4909 pSMB->MaxParameterCount = cpu_to_le16(2);
4910 /* BB find exact max data count below from sess structure BB */
4911 pSMB->MaxDataCount = cpu_to_le16(4000);
4912 pSMB->MaxSetupCount = 0;
4913 pSMB->Reserved = 0;
4914 pSMB->Flags = 0;
4915 pSMB->Timeout = 0;
4916 pSMB->Reserved2 = 0;
4917 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004918 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 pSMB->DataCount = 0;
4920 pSMB->DataOffset = 0;
4921 pSMB->SetupCount = 1;
4922 pSMB->Reserved3 = 0;
4923 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4924 byte_count = params + 1 /* pad */ ;
4925 pSMB->TotalParameterCount = cpu_to_le16(params);
4926 pSMB->ParameterCount = pSMB->TotalParameterCount;
4927 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4928 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004929 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 pSMB->ByteCount = cpu_to_le16(byte_count);
4931
4932 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4933 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4934 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004935 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 } else {
4937 /* decode response */
4938 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004940 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 /* If rc should we check for EOPNOSUPP and
4942 disable the srvino flag? or in caller? */
4943 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004944 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4946 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004947 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004949 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004950 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 rc = -EIO;
4952 goto GetInodeNumOut;
4953 }
4954 pfinfo = (struct file_internal_info *)
4955 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004956 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 }
4958 }
4959GetInodeNumOut:
4960 cifs_buf_release(pSMB);
4961 if (rc == -EAGAIN)
4962 goto GetInodeNumberRetry;
4963 return rc;
4964}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004967CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004968 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004969 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004970 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
4972/* TRANS2_GET_DFS_REFERRAL */
4973 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4974 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 int rc = 0;
4976 int bytes_returned;
4977 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004979 *num_of_nodes = 0;
4980 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
Joe Perchesf96637b2013-05-04 22:12:25 -05004982 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004983 if (ses == NULL || ses->tcon_ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 return -ENODEV;
Aurelien Aptelb327a712018-01-24 13:46:10 +01004985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986getDFSRetry:
Aurelien Aptelb327a712018-01-24 13:46:10 +01004987 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 (void **) &pSMBr);
4989 if (rc)
4990 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004991
4992 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004993 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004994 pSMB->hdr.Mid = get_next_mid(ses->server);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004995 pSMB->hdr.Tid = ses->tcon_ipc->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004997 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004999 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 if (ses->capabilities & CAP_UNICODE) {
5003 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
5004 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005005 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04005006 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06005007 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 name_len++; /* trailing null */
5009 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005010 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04005011 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04005013 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 }
5015
Dan Carpenter65c3b202015-04-30 17:30:24 +03005016 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04005017 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00005018
Steve French50c2f752007-07-13 00:33:32 +00005019 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00005020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 params = 2 /* level */ + name_len /*includes null */ ;
5022 pSMB->TotalDataCount = 0;
5023 pSMB->DataCount = 0;
5024 pSMB->DataOffset = 0;
5025 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00005026 /* BB find exact max SMB PDU from sess structure BB */
5027 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 pSMB->MaxSetupCount = 0;
5029 pSMB->Reserved = 0;
5030 pSMB->Flags = 0;
5031 pSMB->Timeout = 0;
5032 pSMB->Reserved2 = 0;
5033 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005034 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 pSMB->SetupCount = 1;
5036 pSMB->Reserved3 = 0;
5037 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
5038 byte_count = params + 3 /* pad */ ;
5039 pSMB->ParameterCount = cpu_to_le16(params);
5040 pSMB->TotalParameterCount = pSMB->ParameterCount;
5041 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005042 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 pSMB->ByteCount = cpu_to_le16(byte_count);
5044
5045 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
5046 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5047 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005048 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005049 goto GetDFSRefExit;
5050 }
5051 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005053 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04005054 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005055 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04005056 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 }
Igor Mammedovfec45852008-05-16 13:06:30 +04005058
Joe Perchesf96637b2013-05-04 22:12:25 -05005059 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5060 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04005061
5062 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01005063 rc = parse_dfs_referrals(&pSMBr->dfs_data,
5064 le16_to_cpu(pSMBr->t2.DataCount),
5065 num_of_nodes, target_nodes, nls_codepage,
5066 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06005067 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04005068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00005070 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
5072 if (rc == -EAGAIN)
5073 goto getDFSRetry;
5074
5075 return rc;
5076}
5077
Steve French20962432005-09-21 22:05:57 -07005078/* Query File System Info such as free space to old servers such as Win 9x */
5079int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005080SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5081 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005082{
5083/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5084 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5085 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5086 FILE_SYSTEM_ALLOC_INFO *response_data;
5087 int rc = 0;
5088 int bytes_returned = 0;
5089 __u16 params, byte_count;
5090
Joe Perchesf96637b2013-05-04 22:12:25 -05005091 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005092oldQFSInfoRetry:
5093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5094 (void **) &pSMBr);
5095 if (rc)
5096 return rc;
Steve French20962432005-09-21 22:05:57 -07005097
5098 params = 2; /* level */
5099 pSMB->TotalDataCount = 0;
5100 pSMB->MaxParameterCount = cpu_to_le16(2);
5101 pSMB->MaxDataCount = cpu_to_le16(1000);
5102 pSMB->MaxSetupCount = 0;
5103 pSMB->Reserved = 0;
5104 pSMB->Flags = 0;
5105 pSMB->Timeout = 0;
5106 pSMB->Reserved2 = 0;
5107 byte_count = params + 1 /* pad */ ;
5108 pSMB->TotalParameterCount = cpu_to_le16(params);
5109 pSMB->ParameterCount = pSMB->TotalParameterCount;
5110 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5111 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5112 pSMB->DataCount = 0;
5113 pSMB->DataOffset = 0;
5114 pSMB->SetupCount = 1;
5115 pSMB->Reserved3 = 0;
5116 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5117 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005118 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005119 pSMB->ByteCount = cpu_to_le16(byte_count);
5120
5121 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5122 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5123 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005124 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005125 } else { /* decode response */
5126 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5127
Jeff Layton820a8032011-05-04 08:05:26 -04005128 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005129 rc = -EIO; /* bad smb */
5130 else {
5131 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005132 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005133 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005134
Steve French50c2f752007-07-13 00:33:32 +00005135 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005136 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5137 FSData->f_bsize =
5138 le16_to_cpu(response_data->BytesPerSector) *
5139 le32_to_cpu(response_data->
5140 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005141 /*
5142 * much prefer larger but if server doesn't report
5143 * a valid size than 4K is a reasonable minimum
5144 */
5145 if (FSData->f_bsize < 512)
5146 FSData->f_bsize = 4096;
5147
Steve French20962432005-09-21 22:05:57 -07005148 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005149 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005150 FSData->f_bfree = FSData->f_bavail =
5151 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005152 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5153 (unsigned long long)FSData->f_blocks,
5154 (unsigned long long)FSData->f_bfree,
5155 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005156 }
5157 }
5158 cifs_buf_release(pSMB);
5159
5160 if (rc == -EAGAIN)
5161 goto oldQFSInfoRetry;
5162
5163 return rc;
5164}
5165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005167CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5168 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
5170/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5171 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5172 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5173 FILE_SYSTEM_INFO *response_data;
5174 int rc = 0;
5175 int bytes_returned = 0;
5176 __u16 params, byte_count;
5177
Joe Perchesf96637b2013-05-04 22:12:25 -05005178 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179QFSInfoRetry:
5180 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5181 (void **) &pSMBr);
5182 if (rc)
5183 return rc;
5184
5185 params = 2; /* level */
5186 pSMB->TotalDataCount = 0;
5187 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005188 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 pSMB->MaxSetupCount = 0;
5190 pSMB->Reserved = 0;
5191 pSMB->Flags = 0;
5192 pSMB->Timeout = 0;
5193 pSMB->Reserved2 = 0;
5194 byte_count = params + 1 /* pad */ ;
5195 pSMB->TotalParameterCount = cpu_to_le16(params);
5196 pSMB->ParameterCount = pSMB->TotalParameterCount;
5197 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005198 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 pSMB->DataCount = 0;
5200 pSMB->DataOffset = 0;
5201 pSMB->SetupCount = 1;
5202 pSMB->Reserved3 = 0;
5203 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5204 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005205 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 pSMB->ByteCount = cpu_to_le16(byte_count);
5207
5208 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5209 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5210 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005211 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005213 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Jeff Layton820a8032011-05-04 08:05:26 -04005215 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 rc = -EIO; /* bad smb */
5217 else {
5218 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
5220 response_data =
5221 (FILE_SYSTEM_INFO
5222 *) (((char *) &pSMBr->hdr.Protocol) +
5223 data_offset);
5224 FSData->f_bsize =
5225 le32_to_cpu(response_data->BytesPerSector) *
5226 le32_to_cpu(response_data->
5227 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005228 /*
5229 * much prefer larger but if server doesn't report
5230 * a valid size than 4K is a reasonable minimum
5231 */
5232 if (FSData->f_bsize < 512)
5233 FSData->f_bsize = 4096;
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 FSData->f_blocks =
5236 le64_to_cpu(response_data->TotalAllocationUnits);
5237 FSData->f_bfree = FSData->f_bavail =
5238 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005239 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5240 (unsigned long long)FSData->f_blocks,
5241 (unsigned long long)FSData->f_bfree,
5242 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244 }
5245 cifs_buf_release(pSMB);
5246
5247 if (rc == -EAGAIN)
5248 goto QFSInfoRetry;
5249
5250 return rc;
5251}
5252
5253int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005254CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255{
5256/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5257 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5258 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5259 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5260 int rc = 0;
5261 int bytes_returned = 0;
5262 __u16 params, byte_count;
5263
Joe Perchesf96637b2013-05-04 22:12:25 -05005264 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265QFSAttributeRetry:
5266 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5267 (void **) &pSMBr);
5268 if (rc)
5269 return rc;
5270
5271 params = 2; /* level */
5272 pSMB->TotalDataCount = 0;
5273 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005274 /* BB find exact max SMB PDU from sess structure BB */
5275 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 pSMB->MaxSetupCount = 0;
5277 pSMB->Reserved = 0;
5278 pSMB->Flags = 0;
5279 pSMB->Timeout = 0;
5280 pSMB->Reserved2 = 0;
5281 byte_count = params + 1 /* pad */ ;
5282 pSMB->TotalParameterCount = cpu_to_le16(params);
5283 pSMB->ParameterCount = pSMB->TotalParameterCount;
5284 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005285 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 pSMB->DataCount = 0;
5287 pSMB->DataOffset = 0;
5288 pSMB->SetupCount = 1;
5289 pSMB->Reserved3 = 0;
5290 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5291 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005292 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 pSMB->ByteCount = cpu_to_le16(byte_count);
5294
5295 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5296 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5297 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005298 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 } else { /* decode response */
5300 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5301
Jeff Layton820a8032011-05-04 08:05:26 -04005302 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005303 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 rc = -EIO; /* bad smb */
5305 } else {
5306 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5307 response_data =
5308 (FILE_SYSTEM_ATTRIBUTE_INFO
5309 *) (((char *) &pSMBr->hdr.Protocol) +
5310 data_offset);
5311 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005312 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 }
5314 }
5315 cifs_buf_release(pSMB);
5316
5317 if (rc == -EAGAIN)
5318 goto QFSAttributeRetry;
5319
5320 return rc;
5321}
5322
5323int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005324CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325{
5326/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5327 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5328 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5329 FILE_SYSTEM_DEVICE_INFO *response_data;
5330 int rc = 0;
5331 int bytes_returned = 0;
5332 __u16 params, byte_count;
5333
Joe Perchesf96637b2013-05-04 22:12:25 -05005334 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335QFSDeviceRetry:
5336 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5337 (void **) &pSMBr);
5338 if (rc)
5339 return rc;
5340
5341 params = 2; /* level */
5342 pSMB->TotalDataCount = 0;
5343 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005344 /* BB find exact max SMB PDU from sess structure BB */
5345 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 pSMB->MaxSetupCount = 0;
5347 pSMB->Reserved = 0;
5348 pSMB->Flags = 0;
5349 pSMB->Timeout = 0;
5350 pSMB->Reserved2 = 0;
5351 byte_count = params + 1 /* pad */ ;
5352 pSMB->TotalParameterCount = cpu_to_le16(params);
5353 pSMB->ParameterCount = pSMB->TotalParameterCount;
5354 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005355 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 pSMB->DataCount = 0;
5358 pSMB->DataOffset = 0;
5359 pSMB->SetupCount = 1;
5360 pSMB->Reserved3 = 0;
5361 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5362 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005363 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 pSMB->ByteCount = cpu_to_le16(byte_count);
5365
5366 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5367 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5368 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005369 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 } else { /* decode response */
5371 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5372
Jeff Layton820a8032011-05-04 08:05:26 -04005373 if (rc || get_bcc(&pSMBr->hdr) <
5374 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 rc = -EIO; /* bad smb */
5376 else {
5377 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5378 response_data =
Steve French737b7582005-04-28 22:41:06 -07005379 (FILE_SYSTEM_DEVICE_INFO *)
5380 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 data_offset);
5382 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005383 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
5385 }
5386 cifs_buf_release(pSMB);
5387
5388 if (rc == -EAGAIN)
5389 goto QFSDeviceRetry;
5390
5391 return rc;
5392}
5393
5394int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005395CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
5397/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5398 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5399 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5400 FILE_SYSTEM_UNIX_INFO *response_data;
5401 int rc = 0;
5402 int bytes_returned = 0;
5403 __u16 params, byte_count;
5404
Joe Perchesf96637b2013-05-04 22:12:25 -05005405 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005407 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5408 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 if (rc)
5410 return rc;
5411
5412 params = 2; /* level */
5413 pSMB->TotalDataCount = 0;
5414 pSMB->DataCount = 0;
5415 pSMB->DataOffset = 0;
5416 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005417 /* BB find exact max SMB PDU from sess structure BB */
5418 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 pSMB->MaxSetupCount = 0;
5420 pSMB->Reserved = 0;
5421 pSMB->Flags = 0;
5422 pSMB->Timeout = 0;
5423 pSMB->Reserved2 = 0;
5424 byte_count = params + 1 /* pad */ ;
5425 pSMB->ParameterCount = cpu_to_le16(params);
5426 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005427 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5428 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 pSMB->SetupCount = 1;
5430 pSMB->Reserved3 = 0;
5431 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5432 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005433 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 pSMB->ByteCount = cpu_to_le16(byte_count);
5435
5436 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5437 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5438 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005439 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 } else { /* decode response */
5441 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5442
Jeff Layton820a8032011-05-04 08:05:26 -04005443 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 rc = -EIO; /* bad smb */
5445 } else {
5446 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5447 response_data =
5448 (FILE_SYSTEM_UNIX_INFO
5449 *) (((char *) &pSMBr->hdr.Protocol) +
5450 data_offset);
5451 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005452 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454 }
5455 cifs_buf_release(pSMB);
5456
5457 if (rc == -EAGAIN)
5458 goto QFSUnixRetry;
5459
5460
5461 return rc;
5462}
5463
Jeremy Allisonac670552005-06-22 17:26:35 -07005464int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005465CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005466{
5467/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5468 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5469 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5470 int rc = 0;
5471 int bytes_returned = 0;
5472 __u16 params, param_offset, offset, byte_count;
5473
Joe Perchesf96637b2013-05-04 22:12:25 -05005474 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005475SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005476 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005477 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5478 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005479 if (rc)
5480 return rc;
5481
5482 params = 4; /* 2 bytes zero followed by info level. */
5483 pSMB->MaxSetupCount = 0;
5484 pSMB->Reserved = 0;
5485 pSMB->Flags = 0;
5486 pSMB->Timeout = 0;
5487 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005488 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5489 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005490 offset = param_offset + params;
5491
5492 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005493 /* BB find exact max SMB PDU from sess structure BB */
5494 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005495 pSMB->SetupCount = 1;
5496 pSMB->Reserved3 = 0;
5497 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5498 byte_count = 1 /* pad */ + params + 12;
5499
5500 pSMB->DataCount = cpu_to_le16(12);
5501 pSMB->ParameterCount = cpu_to_le16(params);
5502 pSMB->TotalDataCount = pSMB->DataCount;
5503 pSMB->TotalParameterCount = pSMB->ParameterCount;
5504 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5505 pSMB->DataOffset = cpu_to_le16(offset);
5506
5507 /* Params. */
5508 pSMB->FileNum = 0;
5509 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5510
5511 /* Data. */
5512 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5513 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5514 pSMB->ClientUnixCap = cpu_to_le64(cap);
5515
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005516 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005517 pSMB->ByteCount = cpu_to_le16(byte_count);
5518
5519 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5520 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5521 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005522 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005523 } else { /* decode response */
5524 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005525 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005526 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005527 }
5528 cifs_buf_release(pSMB);
5529
5530 if (rc == -EAGAIN)
5531 goto SETFSUnixRetry;
5532
5533 return rc;
5534}
5535
5536
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005539CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005540 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
5542/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5543 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5544 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5545 FILE_SYSTEM_POSIX_INFO *response_data;
5546 int rc = 0;
5547 int bytes_returned = 0;
5548 __u16 params, byte_count;
5549
Joe Perchesf96637b2013-05-04 22:12:25 -05005550 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551QFSPosixRetry:
5552 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5553 (void **) &pSMBr);
5554 if (rc)
5555 return rc;
5556
5557 params = 2; /* level */
5558 pSMB->TotalDataCount = 0;
5559 pSMB->DataCount = 0;
5560 pSMB->DataOffset = 0;
5561 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005562 /* BB find exact max SMB PDU from sess structure BB */
5563 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 pSMB->MaxSetupCount = 0;
5565 pSMB->Reserved = 0;
5566 pSMB->Flags = 0;
5567 pSMB->Timeout = 0;
5568 pSMB->Reserved2 = 0;
5569 byte_count = params + 1 /* pad */ ;
5570 pSMB->ParameterCount = cpu_to_le16(params);
5571 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005572 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5573 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 pSMB->SetupCount = 1;
5575 pSMB->Reserved3 = 0;
5576 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5577 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005578 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 pSMB->ByteCount = cpu_to_le16(byte_count);
5580
5581 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5582 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5583 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005584 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 } else { /* decode response */
5586 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5587
Jeff Layton820a8032011-05-04 08:05:26 -04005588 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 rc = -EIO; /* bad smb */
5590 } else {
5591 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5592 response_data =
5593 (FILE_SYSTEM_POSIX_INFO
5594 *) (((char *) &pSMBr->hdr.Protocol) +
5595 data_offset);
5596 FSData->f_bsize =
5597 le32_to_cpu(response_data->BlockSize);
Steve French5a519be2018-09-15 14:07:09 -05005598 /*
5599 * much prefer larger but if server doesn't report
5600 * a valid size than 4K is a reasonable minimum
5601 */
5602 if (FSData->f_bsize < 512)
5603 FSData->f_bsize = 4096;
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 FSData->f_blocks =
5606 le64_to_cpu(response_data->TotalBlocks);
5607 FSData->f_bfree =
5608 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005609 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 FSData->f_bavail = FSData->f_bfree;
5611 } else {
5612 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005613 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
Steve French790fe572007-07-07 19:25:05 +00005615 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005617 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005618 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005620 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 }
5622 }
5623 cifs_buf_release(pSMB);
5624
5625 if (rc == -EAGAIN)
5626 goto QFSPosixRetry;
5627
5628 return rc;
5629}
5630
5631
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005632/*
5633 * We can not use write of zero bytes trick to set file size due to need for
5634 * large file support. Also note that this SetPathInfo is preferred to
5635 * SetFileInfo based method in next routine which is only needed to work around
5636 * a sharing violation bugin Samba which this routine can run into.
5637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005639CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005640 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5641 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642{
5643 struct smb_com_transaction2_spi_req *pSMB = NULL;
5644 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5645 struct file_end_of_file_info *parm_data;
5646 int name_len;
5647 int rc = 0;
5648 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005649 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 __u16 params, byte_count, data_count, param_offset, offset;
5652
Joe Perchesf96637b2013-05-04 22:12:25 -05005653 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654SetEOFRetry:
5655 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5656 (void **) &pSMBr);
5657 if (rc)
5658 return rc;
5659
5660 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5661 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005662 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5663 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 name_len++; /* trailing null */
5665 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005666 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005667 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005669 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
5671 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005672 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005674 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 pSMB->MaxSetupCount = 0;
5676 pSMB->Reserved = 0;
5677 pSMB->Flags = 0;
5678 pSMB->Timeout = 0;
5679 pSMB->Reserved2 = 0;
5680 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005681 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005683 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005684 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5685 pSMB->InformationLevel =
5686 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5687 else
5688 pSMB->InformationLevel =
5689 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5690 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5692 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005693 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 else
5695 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005696 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 }
5698
5699 parm_data =
5700 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5701 offset);
5702 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5703 pSMB->DataOffset = cpu_to_le16(offset);
5704 pSMB->SetupCount = 1;
5705 pSMB->Reserved3 = 0;
5706 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5707 byte_count = 3 /* pad */ + params + data_count;
5708 pSMB->DataCount = cpu_to_le16(data_count);
5709 pSMB->TotalDataCount = pSMB->DataCount;
5710 pSMB->ParameterCount = cpu_to_le16(params);
5711 pSMB->TotalParameterCount = pSMB->ParameterCount;
5712 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005713 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 parm_data->FileSize = cpu_to_le64(size);
5715 pSMB->ByteCount = cpu_to_le16(byte_count);
5716 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5717 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005718 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005719 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 cifs_buf_release(pSMB);
5722
5723 if (rc == -EAGAIN)
5724 goto SetEOFRetry;
5725
5726 return rc;
5727}
5728
5729int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005730CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5731 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732{
5733 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 struct file_end_of_file_info *parm_data;
5735 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 __u16 params, param_offset, offset, byte_count, count;
5737
Joe Perchesf96637b2013-05-04 22:12:25 -05005738 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5739 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005740 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 if (rc)
5743 return rc;
5744
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005745 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5746 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 params = 6;
5749 pSMB->MaxSetupCount = 0;
5750 pSMB->Reserved = 0;
5751 pSMB->Flags = 0;
5752 pSMB->Timeout = 0;
5753 pSMB->Reserved2 = 0;
5754 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5755 offset = param_offset + params;
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 count = sizeof(struct file_end_of_file_info);
5758 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005759 /* BB find exact max SMB PDU from sess structure BB */
5760 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 pSMB->SetupCount = 1;
5762 pSMB->Reserved3 = 0;
5763 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5764 byte_count = 3 /* pad */ + params + count;
5765 pSMB->DataCount = cpu_to_le16(count);
5766 pSMB->ParameterCount = cpu_to_le16(params);
5767 pSMB->TotalDataCount = pSMB->DataCount;
5768 pSMB->TotalParameterCount = pSMB->ParameterCount;
5769 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5770 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005771 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5772 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 pSMB->DataOffset = cpu_to_le16(offset);
5774 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005775 pSMB->Fid = cfile->fid.netfid;
5776 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5778 pSMB->InformationLevel =
5779 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5780 else
5781 pSMB->InformationLevel =
5782 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005783 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5785 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005786 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 else
5788 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005789 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 }
5791 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005792 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005794 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005795 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005797 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5798 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 }
5800
Steve French50c2f752007-07-13 00:33:32 +00005801 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 since file handle passed in no longer valid */
5803
5804 return rc;
5805}
5806
Steve French50c2f752007-07-13 00:33:32 +00005807/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 an open handle, rather than by pathname - this is awkward due to
5809 potential access conflicts on the open, but it is unavoidable for these
5810 old servers since the only other choice is to go from 100 nanosecond DCE
5811 time and resort to the original setpathinfo level which takes the ancient
5812 DOS time format with 2 second granularity */
5813int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005814CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005815 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816{
5817 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 char *data_offset;
5819 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 __u16 params, param_offset, offset, byte_count, count;
5821
Joe Perchesf96637b2013-05-04 22:12:25 -05005822 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005823 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 if (rc)
5826 return rc;
5827
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005828 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5829 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 params = 6;
5832 pSMB->MaxSetupCount = 0;
5833 pSMB->Reserved = 0;
5834 pSMB->Flags = 0;
5835 pSMB->Timeout = 0;
5836 pSMB->Reserved2 = 0;
5837 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5838 offset = param_offset + params;
5839
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005840 data_offset = (char *)pSMB +
5841 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Steve French26f57362007-08-30 22:09:15 +00005843 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005845 /* BB find max SMB PDU from sess */
5846 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 pSMB->SetupCount = 1;
5848 pSMB->Reserved3 = 0;
5849 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5850 byte_count = 3 /* pad */ + params + count;
5851 pSMB->DataCount = cpu_to_le16(count);
5852 pSMB->ParameterCount = cpu_to_le16(params);
5853 pSMB->TotalDataCount = pSMB->DataCount;
5854 pSMB->TotalParameterCount = pSMB->ParameterCount;
5855 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5856 pSMB->DataOffset = cpu_to_le16(offset);
5857 pSMB->Fid = fid;
5858 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5859 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5860 else
5861 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5862 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005863 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005865 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005866 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005867 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005868 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005869 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5870 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
Steve French50c2f752007-07-13 00:33:32 +00005872 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 since file handle passed in no longer valid */
5874
5875 return rc;
5876}
5877
Jeff Layton6d22f092008-09-23 11:48:35 -04005878int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005879CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005880 bool delete_file, __u16 fid, __u32 pid_of_opener)
5881{
5882 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5883 char *data_offset;
5884 int rc = 0;
5885 __u16 params, param_offset, offset, byte_count, count;
5886
Joe Perchesf96637b2013-05-04 22:12:25 -05005887 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005888 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5889
5890 if (rc)
5891 return rc;
5892
5893 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5894 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5895
5896 params = 6;
5897 pSMB->MaxSetupCount = 0;
5898 pSMB->Reserved = 0;
5899 pSMB->Flags = 0;
5900 pSMB->Timeout = 0;
5901 pSMB->Reserved2 = 0;
5902 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5903 offset = param_offset + params;
5904
5905 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5906
5907 count = 1;
5908 pSMB->MaxParameterCount = cpu_to_le16(2);
5909 /* BB find max SMB PDU from sess */
5910 pSMB->MaxDataCount = cpu_to_le16(1000);
5911 pSMB->SetupCount = 1;
5912 pSMB->Reserved3 = 0;
5913 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5914 byte_count = 3 /* pad */ + params + count;
5915 pSMB->DataCount = cpu_to_le16(count);
5916 pSMB->ParameterCount = cpu_to_le16(params);
5917 pSMB->TotalDataCount = pSMB->DataCount;
5918 pSMB->TotalParameterCount = pSMB->ParameterCount;
5919 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5920 pSMB->DataOffset = cpu_to_le16(offset);
5921 pSMB->Fid = fid;
5922 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5923 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005924 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005925 pSMB->ByteCount = cpu_to_le16(byte_count);
5926 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005927 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005928 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005929 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005930 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005931
5932 return rc;
5933}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
5935int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005936CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005937 const char *fileName, const FILE_BASIC_INFO *data,
5938 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939{
5940 TRANSACTION2_SPI_REQ *pSMB = NULL;
5941 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5942 int name_len;
5943 int rc = 0;
5944 int bytes_returned = 0;
5945 char *data_offset;
5946 __u16 params, param_offset, offset, byte_count, count;
5947
Joe Perchesf96637b2013-05-04 22:12:25 -05005948 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950SetTimesRetry:
5951 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5952 (void **) &pSMBr);
5953 if (rc)
5954 return rc;
5955
5956 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5957 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005958 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5959 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 name_len++; /* trailing null */
5961 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005962 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 name_len = strnlen(fileName, PATH_MAX);
5964 name_len++; /* trailing null */
5965 strncpy(pSMB->FileName, fileName, name_len);
5966 }
5967
5968 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005969 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005971 /* BB find max SMB PDU from sess structure BB */
5972 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 pSMB->MaxSetupCount = 0;
5974 pSMB->Reserved = 0;
5975 pSMB->Flags = 0;
5976 pSMB->Timeout = 0;
5977 pSMB->Reserved2 = 0;
5978 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005979 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 offset = param_offset + params;
5981 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5982 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5983 pSMB->DataOffset = cpu_to_le16(offset);
5984 pSMB->SetupCount = 1;
5985 pSMB->Reserved3 = 0;
5986 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5987 byte_count = 3 /* pad */ + params + count;
5988
5989 pSMB->DataCount = cpu_to_le16(count);
5990 pSMB->ParameterCount = cpu_to_le16(params);
5991 pSMB->TotalDataCount = pSMB->DataCount;
5992 pSMB->TotalParameterCount = pSMB->ParameterCount;
5993 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5994 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5995 else
5996 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5997 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005998 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005999 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 pSMB->ByteCount = cpu_to_le16(byte_count);
6001 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6002 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006003 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006004 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
6006 cifs_buf_release(pSMB);
6007
6008 if (rc == -EAGAIN)
6009 goto SetTimesRetry;
6010
6011 return rc;
6012}
6013
6014/* Can not be used to set time stamps yet (due to old DOS time format) */
6015/* Can be used to set attributes */
6016#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
6017 handling it anyway and NT4 was what we thought it would be needed for
6018 Do not delete it until we prove whether needed for Win9x though */
6019int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006020CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 __u16 dos_attrs, const struct nls_table *nls_codepage)
6022{
6023 SETATTR_REQ *pSMB = NULL;
6024 SETATTR_RSP *pSMBr = NULL;
6025 int rc = 0;
6026 int bytes_returned;
6027 int name_len;
6028
Joe Perchesf96637b2013-05-04 22:12:25 -05006029 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
6031SetAttrLgcyRetry:
6032 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
6033 (void **) &pSMBr);
6034 if (rc)
6035 return rc;
6036
6037 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6038 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006039 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
6040 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 name_len++; /* trailing null */
6042 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006043 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 name_len = strnlen(fileName, PATH_MAX);
6045 name_len++; /* trailing null */
6046 strncpy(pSMB->fileName, fileName, name_len);
6047 }
6048 pSMB->attr = cpu_to_le16(dos_attrs);
6049 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006050 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 pSMB->ByteCount = cpu_to_le16(name_len + 1);
6052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006054 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006055 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
6057 cifs_buf_release(pSMB);
6058
6059 if (rc == -EAGAIN)
6060 goto SetAttrLgcyRetry;
6061
6062 return rc;
6063}
6064#endif /* temporarily unneeded SetAttr legacy function */
6065
Jeff Layton654cf142009-07-09 20:02:49 -04006066static void
6067cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6068 const struct cifs_unix_set_info_args *args)
6069{
Eric W. Biederman49418b22013-02-06 00:57:56 -08006070 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04006071 u64 mode = args->mode;
6072
Eric W. Biederman49418b22013-02-06 00:57:56 -08006073 if (uid_valid(args->uid))
6074 uid = from_kuid(&init_user_ns, args->uid);
6075 if (gid_valid(args->gid))
6076 gid = from_kgid(&init_user_ns, args->gid);
6077
Jeff Layton654cf142009-07-09 20:02:49 -04006078 /*
6079 * Samba server ignores set of file size to zero due to bugs in some
6080 * older clients, but we should be precise - we use SetFileSize to
6081 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006082 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04006083 * zero instead of -1 here
6084 */
6085 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6086 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6087 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6088 data_offset->LastAccessTime = cpu_to_le64(args->atime);
6089 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08006090 data_offset->Uid = cpu_to_le64(uid);
6091 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04006092 /* better to leave device as zero when it is */
6093 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6094 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6095 data_offset->Permissions = cpu_to_le64(mode);
6096
6097 if (S_ISREG(mode))
6098 data_offset->Type = cpu_to_le32(UNIX_FILE);
6099 else if (S_ISDIR(mode))
6100 data_offset->Type = cpu_to_le32(UNIX_DIR);
6101 else if (S_ISLNK(mode))
6102 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6103 else if (S_ISCHR(mode))
6104 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6105 else if (S_ISBLK(mode))
6106 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6107 else if (S_ISFIFO(mode))
6108 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6109 else if (S_ISSOCK(mode))
6110 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6111}
6112
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006114CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006115 const struct cifs_unix_set_info_args *args,
6116 u16 fid, u32 pid_of_opener)
6117{
6118 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006119 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006120 int rc = 0;
6121 u16 params, param_offset, offset, byte_count, count;
6122
Joe Perchesf96637b2013-05-04 22:12:25 -05006123 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006124 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6125
6126 if (rc)
6127 return rc;
6128
6129 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6130 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6131
6132 params = 6;
6133 pSMB->MaxSetupCount = 0;
6134 pSMB->Reserved = 0;
6135 pSMB->Flags = 0;
6136 pSMB->Timeout = 0;
6137 pSMB->Reserved2 = 0;
6138 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6139 offset = param_offset + params;
6140
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006141 data_offset = (char *)pSMB +
6142 offsetof(struct smb_hdr, Protocol) + offset;
6143
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006144 count = sizeof(FILE_UNIX_BASIC_INFO);
6145
6146 pSMB->MaxParameterCount = cpu_to_le16(2);
6147 /* BB find max SMB PDU from sess */
6148 pSMB->MaxDataCount = cpu_to_le16(1000);
6149 pSMB->SetupCount = 1;
6150 pSMB->Reserved3 = 0;
6151 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6152 byte_count = 3 /* pad */ + params + count;
6153 pSMB->DataCount = cpu_to_le16(count);
6154 pSMB->ParameterCount = cpu_to_le16(params);
6155 pSMB->TotalDataCount = pSMB->DataCount;
6156 pSMB->TotalParameterCount = pSMB->ParameterCount;
6157 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6158 pSMB->DataOffset = cpu_to_le16(offset);
6159 pSMB->Fid = fid;
6160 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6161 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006162 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006163 pSMB->ByteCount = cpu_to_le16(byte_count);
6164
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006165 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006166
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006167 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006168 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006169 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006170 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6171 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006172
6173 /* Note: On -EAGAIN error only caller can retry on handle based calls
6174 since file handle passed in no longer valid */
6175
6176 return rc;
6177}
6178
6179int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006180CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006181 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006182 const struct cifs_unix_set_info_args *args,
6183 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184{
6185 TRANSACTION2_SPI_REQ *pSMB = NULL;
6186 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6187 int name_len;
6188 int rc = 0;
6189 int bytes_returned = 0;
6190 FILE_UNIX_BASIC_INFO *data_offset;
6191 __u16 params, param_offset, offset, count, byte_count;
6192
Joe Perchesf96637b2013-05-04 22:12:25 -05006193 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194setPermsRetry:
6195 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6196 (void **) &pSMBr);
6197 if (rc)
6198 return rc;
6199
6200 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6201 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006202 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006203 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 name_len++; /* trailing null */
6205 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006206 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006207 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006209 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 }
6211
6212 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006213 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006215 /* BB find max SMB PDU from sess structure BB */
6216 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 pSMB->MaxSetupCount = 0;
6218 pSMB->Reserved = 0;
6219 pSMB->Flags = 0;
6220 pSMB->Timeout = 0;
6221 pSMB->Reserved2 = 0;
6222 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006223 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 offset = param_offset + params;
6225 data_offset =
6226 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6227 offset);
6228 memset(data_offset, 0, count);
6229 pSMB->DataOffset = cpu_to_le16(offset);
6230 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6231 pSMB->SetupCount = 1;
6232 pSMB->Reserved3 = 0;
6233 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6234 byte_count = 3 /* pad */ + params + count;
6235 pSMB->ParameterCount = cpu_to_le16(params);
6236 pSMB->DataCount = cpu_to_le16(count);
6237 pSMB->TotalParameterCount = pSMB->ParameterCount;
6238 pSMB->TotalDataCount = pSMB->DataCount;
6239 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6240 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006241 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006242
Jeff Layton654cf142009-07-09 20:02:49 -04006243 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 pSMB->ByteCount = cpu_to_le16(byte_count);
6246 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6247 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006248 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006249 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
Steve French0d817bc2008-05-22 02:02:03 +00006251 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 if (rc == -EAGAIN)
6253 goto setPermsRetry;
6254 return rc;
6255}
6256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006258/*
6259 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6260 * function used by listxattr and getxattr type calls. When ea_name is set,
6261 * it looks for that attribute name and stuffs that value into the EAData
6262 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6263 * buffer. In both cases, the return value is either the length of the
6264 * resulting data or a negative error code. If EAData is a NULL pointer then
6265 * the data isn't copied to it, but the length is returned.
6266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006268CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006269 const unsigned char *searchName, const unsigned char *ea_name,
6270 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006271 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272{
6273 /* BB assumes one setup word */
6274 TRANSACTION2_QPI_REQ *pSMB = NULL;
6275 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006276 int remap = cifs_remap(cifs_sb);
6277 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 int rc = 0;
6279 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006280 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006281 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006282 struct fea *temp_fea;
6283 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006284 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006285 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006286 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
Joe Perchesf96637b2013-05-04 22:12:25 -05006288 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289QAllEAsRetry:
6290 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6291 (void **) &pSMBr);
6292 if (rc)
6293 return rc;
6294
6295 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006296 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006297 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6298 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006299 list_len++; /* trailing null */
6300 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006302 list_len = strnlen(searchName, PATH_MAX);
6303 list_len++; /* trailing null */
6304 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 }
6306
Jeff Layton6e462b92010-02-10 16:18:26 -05006307 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 pSMB->TotalDataCount = 0;
6309 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006310 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006311 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 pSMB->MaxSetupCount = 0;
6313 pSMB->Reserved = 0;
6314 pSMB->Flags = 0;
6315 pSMB->Timeout = 0;
6316 pSMB->Reserved2 = 0;
6317 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006318 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 pSMB->DataCount = 0;
6320 pSMB->DataOffset = 0;
6321 pSMB->SetupCount = 1;
6322 pSMB->Reserved3 = 0;
6323 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6324 byte_count = params + 1 /* pad */ ;
6325 pSMB->TotalParameterCount = cpu_to_le16(params);
6326 pSMB->ParameterCount = pSMB->TotalParameterCount;
6327 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6328 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006329 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 pSMB->ByteCount = cpu_to_le16(byte_count);
6331
6332 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6333 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6334 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006335 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006336 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006338
6339
6340 /* BB also check enough total bytes returned */
6341 /* BB we need to improve the validity checking
6342 of these trans2 responses */
6343
6344 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006345 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006346 rc = -EIO; /* bad smb */
6347 goto QAllEAsOut;
6348 }
6349
6350 /* check that length of list is not more than bcc */
6351 /* check that each entry does not go beyond length
6352 of list */
6353 /* check that each element of each entry does not
6354 go beyond end of list */
6355 /* validate_trans2_offsets() */
6356 /* BB check if start of smb + data_offset > &bcc+ bcc */
6357
6358 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6359 ea_response_data = (struct fealist *)
6360 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6361
Jeff Layton6e462b92010-02-10 16:18:26 -05006362 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006363 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006364 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006365 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006366 /* didn't find the named attribute */
6367 if (ea_name)
6368 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006369 goto QAllEAsOut;
6370 }
6371
Jeff Layton0cd126b2010-02-10 16:18:26 -05006372 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006373 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006374 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006375 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006376 rc = -EIO;
6377 goto QAllEAsOut;
6378 }
6379
Jeff Laytonf0d38682010-02-10 16:18:26 -05006380 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006381 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006382 temp_fea = ea_response_data->list;
6383 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006384 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006385 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006386 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006387
Jeff Layton6e462b92010-02-10 16:18:26 -05006388 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006389 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006390 /* make sure we can read name_len and value_len */
6391 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006392 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006393 rc = -EIO;
6394 goto QAllEAsOut;
6395 }
6396
6397 name_len = temp_fea->name_len;
6398 value_len = le16_to_cpu(temp_fea->value_len);
6399 list_len -= name_len + 1 + value_len;
6400 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006401 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006402 rc = -EIO;
6403 goto QAllEAsOut;
6404 }
6405
Jeff Layton31c05192010-02-10 16:18:26 -05006406 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006407 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006408 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006409 temp_ptr += name_len + 1;
6410 rc = value_len;
6411 if (buf_size == 0)
6412 goto QAllEAsOut;
6413 if ((size_t)value_len > buf_size) {
6414 rc = -ERANGE;
6415 goto QAllEAsOut;
6416 }
6417 memcpy(EAData, temp_ptr, value_len);
6418 goto QAllEAsOut;
6419 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006420 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006421 /* account for prefix user. and trailing null */
6422 rc += (5 + 1 + name_len);
6423 if (rc < (int) buf_size) {
6424 memcpy(EAData, "user.", 5);
6425 EAData += 5;
6426 memcpy(EAData, temp_ptr, name_len);
6427 EAData += name_len;
6428 /* null terminate name */
6429 *EAData = 0;
6430 ++EAData;
6431 } else if (buf_size == 0) {
6432 /* skip copy - calc size only */
6433 } else {
6434 /* stop before overrun buffer */
6435 rc = -ERANGE;
6436 break;
6437 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006438 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006439 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006440 temp_fea = (struct fea *)temp_ptr;
6441 }
6442
Jeff Layton31c05192010-02-10 16:18:26 -05006443 /* didn't find the named attribute */
6444 if (ea_name)
6445 rc = -ENODATA;
6446
Jeff Laytonf0d38682010-02-10 16:18:26 -05006447QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006448 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 if (rc == -EAGAIN)
6450 goto QAllEAsRetry;
6451
6452 return (ssize_t)rc;
6453}
6454
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006456CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6457 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006458 const __u16 ea_value_len, const struct nls_table *nls_codepage,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006459 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460{
6461 struct smb_com_transaction2_spi_req *pSMB = NULL;
6462 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6463 struct fealist *parm_data;
6464 int name_len;
6465 int rc = 0;
6466 int bytes_returned = 0;
6467 __u16 params, param_offset, byte_count, offset, count;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006468 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
Joe Perchesf96637b2013-05-04 22:12:25 -05006470 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471SetEARetry:
6472 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6473 (void **) &pSMBr);
6474 if (rc)
6475 return rc;
6476
6477 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6478 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006479 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6480 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 name_len++; /* trailing null */
6482 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006483 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 name_len = strnlen(fileName, PATH_MAX);
6485 name_len++; /* trailing null */
6486 strncpy(pSMB->FileName, fileName, name_len);
6487 }
6488
6489 params = 6 + name_len;
6490
6491 /* done calculating parms using name_len of file name,
6492 now use name_len to calculate length of ea name
6493 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006494 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 name_len = 0;
6496 else
Steve French50c2f752007-07-13 00:33:32 +00006497 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006499 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006501 /* BB find max SMB PDU from sess */
6502 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 pSMB->MaxSetupCount = 0;
6504 pSMB->Reserved = 0;
6505 pSMB->Flags = 0;
6506 pSMB->Timeout = 0;
6507 pSMB->Reserved2 = 0;
6508 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006509 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 offset = param_offset + params;
6511 pSMB->InformationLevel =
6512 cpu_to_le16(SMB_SET_FILE_EA);
6513
Arnd Bergmannade7db92018-02-02 16:48:47 +01006514 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6516 pSMB->DataOffset = cpu_to_le16(offset);
6517 pSMB->SetupCount = 1;
6518 pSMB->Reserved3 = 0;
6519 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6520 byte_count = 3 /* pad */ + params + count;
6521 pSMB->DataCount = cpu_to_le16(count);
6522 parm_data->list_len = cpu_to_le32(count);
6523 parm_data->list[0].EA_flags = 0;
6524 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006525 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006527 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006528 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 parm_data->list[0].name[name_len] = 0;
6530 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6531 /* caller ensures that ea_value_len is less than 64K but
6532 we need to ensure that it fits within the smb */
6533
Steve French50c2f752007-07-13 00:33:32 +00006534 /*BB add length check to see if it would fit in
6535 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006536 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6537 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006538 memcpy(parm_data->list[0].name+name_len+1,
6539 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
6541 pSMB->TotalDataCount = pSMB->DataCount;
6542 pSMB->ParameterCount = cpu_to_le16(params);
6543 pSMB->TotalParameterCount = pSMB->ParameterCount;
6544 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006545 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 pSMB->ByteCount = cpu_to_le16(byte_count);
6547 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6548 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006549 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006550 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 cifs_buf_release(pSMB);
6553
6554 if (rc == -EAGAIN)
6555 goto SetEARetry;
6556
6557 return rc;
6558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559#endif