blob: 4f554f019a98984fe3a2914d2f1365da6e82a6d6 [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;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +1000945 } else {
946 name_len = copy_path_name(pSMB->FileName, fileName);
Steve French2d785a52007-07-15 01:48:57 +0000947 }
948
949 params = 6 + name_len;
950 pSMB->MaxParameterCount = cpu_to_le16(2);
951 pSMB->MaxDataCount = 0; /* BB double check this with jra */
952 pSMB->MaxSetupCount = 0;
953 pSMB->Reserved = 0;
954 pSMB->Flags = 0;
955 pSMB->Timeout = 0;
956 pSMB->Reserved2 = 0;
957 param_offset = offsetof(struct smb_com_transaction2_spi_req,
958 InformationLevel) - 4;
959 offset = param_offset + params;
960
961 /* Setup pointer to Request Data (inode type) */
962 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
963 pRqD->type = cpu_to_le16(type);
964 pSMB->ParameterOffset = cpu_to_le16(param_offset);
965 pSMB->DataOffset = cpu_to_le16(offset);
966 pSMB->SetupCount = 1;
967 pSMB->Reserved3 = 0;
968 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
969 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
970
971 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
972 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
973 pSMB->ParameterCount = cpu_to_le16(params);
974 pSMB->TotalParameterCount = pSMB->ParameterCount;
975 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
976 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000977 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000978 pSMB->ByteCount = cpu_to_le16(byte_count);
979 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
980 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000981 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500982 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000983 cifs_buf_release(pSMB);
984
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400985 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000986
987 if (rc == -EAGAIN)
988 goto PsxDelete;
989
990 return rc;
991}
992
993int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700994CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
995 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 DELETE_FILE_REQ *pSMB = NULL;
998 DELETE_FILE_RSP *pSMBr = NULL;
999 int rc = 0;
1000 int bytes_returned;
1001 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001002 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004DelFileRetry:
1005 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
1006 (void **) &pSMBr);
1007 if (rc)
1008 return rc;
1009
1010 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001011 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
1012 PATH_MAX, cifs_sb->local_nls,
1013 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 name_len++; /* trailing null */
1015 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001016 } else {
1017 name_len = copy_path_name(pSMB->fileName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 pSMB->SearchAttributes =
1020 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
1021 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001022 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001026 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +00001027 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001028 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 cifs_buf_release(pSMB);
1031 if (rc == -EAGAIN)
1032 goto DelFileRetry;
1033
1034 return rc;
1035}
1036
1037int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001038CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1039 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 DELETE_DIRECTORY_REQ *pSMB = NULL;
1042 DELETE_DIRECTORY_RSP *pSMBr = NULL;
1043 int rc = 0;
1044 int bytes_returned;
1045 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001046 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Joe Perchesf96637b2013-05-04 22:12:25 -05001048 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049RmDirRetry:
1050 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
1051 (void **) &pSMBr);
1052 if (rc)
1053 return rc;
1054
1055 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001056 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1057 PATH_MAX, cifs_sb->local_nls,
1058 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 name_len++; /* trailing null */
1060 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001061 } else {
1062 name_len = copy_path_name(pSMB->DirName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
1065 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001066 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1068 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1069 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001070 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001071 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001072 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 cifs_buf_release(pSMB);
1075 if (rc == -EAGAIN)
1076 goto RmDirRetry;
1077 return rc;
1078}
1079
1080int
Steve Frenchc3ca78e2019-09-25 00:32:13 -05001081CIFSSMBMkDir(const unsigned int xid, struct inode *inode, umode_t mode,
1082 struct cifs_tcon *tcon, const char *name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001083 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 int rc = 0;
1086 CREATE_DIRECTORY_REQ *pSMB = NULL;
1087 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1088 int bytes_returned;
1089 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001090 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Joe Perchesf96637b2013-05-04 22:12:25 -05001092 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093MkDirRetry:
1094 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1095 (void **) &pSMBr);
1096 if (rc)
1097 return rc;
1098
1099 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06001100 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001101 PATH_MAX, cifs_sb->local_nls,
1102 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 name_len++; /* trailing null */
1104 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001105 } else {
1106 name_len = copy_path_name(pSMB->DirName, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001110 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1112 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1113 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001114 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001115 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001116 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 cifs_buf_release(pSMB);
1119 if (rc == -EAGAIN)
1120 goto MkDirRetry;
1121 return rc;
1122}
1123
Steve French2dd29d32007-04-23 22:07:35 +00001124int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001125CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1126 __u32 posix_flags, __u64 mode, __u16 *netfid,
1127 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1128 const char *name, const struct nls_table *nls_codepage,
1129 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001130{
1131 TRANSACTION2_SPI_REQ *pSMB = NULL;
1132 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1133 int name_len;
1134 int rc = 0;
1135 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001136 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001137 OPEN_PSX_REQ *pdata;
1138 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001139
Joe Perchesf96637b2013-05-04 22:12:25 -05001140 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001141PsxCreat:
1142 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1143 (void **) &pSMBr);
1144 if (rc)
1145 return rc;
1146
1147 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1148 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001149 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1150 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001151 name_len++; /* trailing null */
1152 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001153 } else {
1154 name_len = copy_path_name(pSMB->FileName, name);
Steve French2dd29d32007-04-23 22:07:35 +00001155 }
1156
1157 params = 6 + name_len;
1158 count = sizeof(OPEN_PSX_REQ);
1159 pSMB->MaxParameterCount = cpu_to_le16(2);
1160 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1161 pSMB->MaxSetupCount = 0;
1162 pSMB->Reserved = 0;
1163 pSMB->Flags = 0;
1164 pSMB->Timeout = 0;
1165 pSMB->Reserved2 = 0;
1166 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001167 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001168 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001169 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001170 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001171 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001172 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001173 pdata->OpenFlags = cpu_to_le32(*pOplock);
1174 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1175 pSMB->DataOffset = cpu_to_le16(offset);
1176 pSMB->SetupCount = 1;
1177 pSMB->Reserved3 = 0;
1178 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1179 byte_count = 3 /* pad */ + params + count;
1180
1181 pSMB->DataCount = cpu_to_le16(count);
1182 pSMB->ParameterCount = cpu_to_le16(params);
1183 pSMB->TotalDataCount = pSMB->DataCount;
1184 pSMB->TotalParameterCount = pSMB->ParameterCount;
1185 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1186 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001187 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001188 pSMB->ByteCount = cpu_to_le16(byte_count);
1189 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1190 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1191 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001192 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001193 goto psx_create_err;
1194 }
1195
Joe Perchesf96637b2013-05-04 22:12:25 -05001196 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001197 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1198
Jeff Layton820a8032011-05-04 08:05:26 -04001199 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001200 rc = -EIO; /* bad smb */
1201 goto psx_create_err;
1202 }
1203
1204 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001205 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001206 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001207
Steve French2dd29d32007-04-23 22:07:35 +00001208 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001209 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001210 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1211 /* Let caller know file was created so we can set the mode. */
1212 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001213 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001214 *pOplock |= CIFS_CREATE_ACTION;
1215 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001216 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1217 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001218 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001219 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001220 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001221 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001222 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001223 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001224 goto psx_create_err;
1225 }
Steve French50c2f752007-07-13 00:33:32 +00001226 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001227 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001228 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001229 }
Steve French2dd29d32007-04-23 22:07:35 +00001230
1231psx_create_err:
1232 cifs_buf_release(pSMB);
1233
Steve French65bc98b2009-07-10 15:27:25 +00001234 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001235 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001236 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001237 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001238
1239 if (rc == -EAGAIN)
1240 goto PsxCreat;
1241
Steve French50c2f752007-07-13 00:33:32 +00001242 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001243}
1244
Steve Frencha9d02ad2005-08-24 23:06:05 -07001245static __u16 convert_disposition(int disposition)
1246{
1247 __u16 ofun = 0;
1248
1249 switch (disposition) {
1250 case FILE_SUPERSEDE:
1251 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1252 break;
1253 case FILE_OPEN:
1254 ofun = SMBOPEN_OAPPEND;
1255 break;
1256 case FILE_CREATE:
1257 ofun = SMBOPEN_OCREATE;
1258 break;
1259 case FILE_OPEN_IF:
1260 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1261 break;
1262 case FILE_OVERWRITE:
1263 ofun = SMBOPEN_OTRUNC;
1264 break;
1265 case FILE_OVERWRITE_IF:
1266 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1267 break;
1268 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001269 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001270 ofun = SMBOPEN_OAPPEND; /* regular open */
1271 }
1272 return ofun;
1273}
1274
Jeff Layton35fc37d2008-05-14 10:22:03 -07001275static int
1276access_flags_to_smbopen_mode(const int access_flags)
1277{
1278 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1279
1280 if (masked_flags == GENERIC_READ)
1281 return SMBOPEN_READ;
1282 else if (masked_flags == GENERIC_WRITE)
1283 return SMBOPEN_WRITE;
1284
1285 /* just go for read/write */
1286 return SMBOPEN_READWRITE;
1287}
1288
Steve Frencha9d02ad2005-08-24 23:06:05 -07001289int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001290SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001291 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001292 const int access_flags, const int create_options, __u16 *netfid,
1293 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001294 const struct nls_table *nls_codepage, int remap)
1295{
1296 int rc = -EACCES;
1297 OPENX_REQ *pSMB = NULL;
1298 OPENX_RSP *pSMBr = NULL;
1299 int bytes_returned;
1300 int name_len;
1301 __u16 count;
1302
1303OldOpenRetry:
1304 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1305 (void **) &pSMBr);
1306 if (rc)
1307 return rc;
1308
1309 pSMB->AndXCommand = 0xFF; /* none */
1310
1311 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1312 count = 1; /* account for one byte pad to word boundary */
1313 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001314 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1315 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001316 name_len++; /* trailing null */
1317 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001318 } else {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001319 count = 0; /* no pad */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001320 name_len = copy_path_name(pSMB->fileName, fileName);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001321 }
1322 if (*pOplock & REQ_OPLOCK)
1323 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001324 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001325 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001326
Steve Frencha9d02ad2005-08-24 23:06:05 -07001327 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001328 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001329 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1330 /* set file as system file if special file such
1331 as fifo and server expecting SFU style and
1332 no Unix extensions */
1333
Steve French790fe572007-07-07 19:25:05 +00001334 if (create_options & CREATE_OPTION_SPECIAL)
1335 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001336 else /* BB FIXME BB */
1337 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001338
Jeff Layton67750fb2008-05-09 22:28:02 +00001339 if (create_options & CREATE_OPTION_READONLY)
1340 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001341
1342 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001343/* pSMB->CreateOptions = cpu_to_le32(create_options &
1344 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001345 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001346
1347 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001348 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001349 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001350 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001351
1352 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001353 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001354 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001355 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001356 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001357 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001358 } else {
1359 /* BB verify if wct == 15 */
1360
Steve French582d21e2008-05-13 04:54:12 +00001361/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001362
1363 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1364 /* Let caller know file was created so we can set the mode. */
1365 /* Do we care about the CreateAction in any other cases? */
1366 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001367/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001368 *pOplock |= CIFS_CREATE_ACTION; */
1369 /* BB FIXME END */
1370
Steve French790fe572007-07-07 19:25:05 +00001371 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001372 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1373 pfile_info->LastAccessTime = 0; /* BB fixme */
1374 pfile_info->LastWriteTime = 0; /* BB fixme */
1375 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001376 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001377 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001378 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001379 pfile_info->AllocationSize =
1380 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1381 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001382 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001383 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001384 }
1385 }
1386
1387 cifs_buf_release(pSMB);
1388 if (rc == -EAGAIN)
1389 goto OldOpenRetry;
1390 return rc;
1391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001394CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1395 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Colin Ian King1afdea42019-07-23 16:09:19 +01001397 int rc;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001398 OPEN_REQ *req = NULL;
1399 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int bytes_returned;
1401 int name_len;
1402 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001403 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1404 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001405 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001406 const struct nls_table *nls = cifs_sb->local_nls;
1407 int create_options = oparms->create_options;
1408 int desired_access = oparms->desired_access;
1409 int disposition = oparms->disposition;
1410 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001413 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1414 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (rc)
1416 return rc;
1417
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001418 /* no commands go after this */
1419 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001421 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1422 /* account for one byte pad to word boundary */
1423 count = 1;
1424 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1425 path, PATH_MAX, nls, remap);
1426 /* trailing null */
1427 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001429 req->NameLength = cpu_to_le16(name_len);
1430 } else {
1431 /* BB improve check for buffer overruns BB */
1432 /* no pad */
1433 count = 0;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10001434 name_len = copy_path_name(req->fileName, path);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001435 req->NameLength = cpu_to_le16(name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001437
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001438 if (*oplock & REQ_OPLOCK)
1439 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1440 else if (*oplock & REQ_BATCHOPLOCK)
1441 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1442
1443 req->DesiredAccess = cpu_to_le32(desired_access);
1444 req->AllocationSize = 0;
1445
1446 /*
1447 * Set file as system file if special file such as fifo and server
1448 * expecting SFU style and no Unix extensions.
1449 */
1450 if (create_options & CREATE_OPTION_SPECIAL)
1451 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1452 else
1453 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1454
1455 /*
1456 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1457 * sensitive checks for other servers such as Samba.
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001460 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jeff Layton67750fb2008-05-09 22:28:02 +00001462 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001463 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001464
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001465 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1466 req->CreateDisposition = cpu_to_le32(disposition);
1467 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1468
Steve French09d1db52005-04-28 22:41:08 -07001469 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001470 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1471 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001474 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001476 req->ByteCount = cpu_to_le16(count);
1477 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1478 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001479 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001481 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001482 cifs_buf_release(req);
1483 if (rc == -EAGAIN)
1484 goto openRetry;
1485 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001487
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001488 /* 1 byte no need to le_to_cpu */
1489 *oplock = rsp->OplockLevel;
1490 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001491 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001492
1493 /* Let caller know file was created so we can set the mode. */
1494 /* Do we care about the CreateAction in any other cases? */
1495 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1496 *oplock |= CIFS_CREATE_ACTION;
1497
1498 if (buf) {
1499 /* copy from CreationTime to Attributes */
1500 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1501 /* the file_info buf is endian converted by caller */
1502 buf->AllocationSize = rsp->AllocationSize;
1503 buf->EndOfFile = rsp->EndOfFile;
1504 buf->NumberOfLinks = cpu_to_le32(1);
1505 buf->DeletePending = 0;
1506 }
1507
1508 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return rc;
1510}
1511
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001512/*
1513 * Discard any remaining data in the current SMB. To do this, we borrow the
1514 * current bigbuf.
1515 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001516int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001517cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001518{
Ronnie Sahlberg05432e22018-04-09 18:06:31 +10001519 unsigned int rfclen = server->pdu_size;
1520 int remaining = rfclen + server->vals->header_preamble_size -
1521 server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001522
1523 while (remaining > 0) {
1524 int length;
1525
1526 length = cifs_read_from_socket(server, server->bigbuf,
1527 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001528 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529 if (length < 0)
1530 return length;
1531 server->total_read += length;
1532 remaining -= length;
1533 }
1534
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535 return 0;
1536}
1537
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001538static int
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001539__cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1540 bool malformed)
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001541{
1542 int length;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001543
Pavel Shilovsky350be252017-04-10 10:31:33 -07001544 length = cifs_discard_remaining_data(server);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001545 dequeue_mid(mid, malformed);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001546 mid->resp_buf = server->smallbuf;
1547 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001548 return length;
1549}
1550
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001551static int
1552cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1553{
1554 struct cifs_readdata *rdata = mid->callback_data;
1555
1556 return __cifs_readv_discard(server, mid, rdata->result);
1557}
1558
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001559int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001560cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1561{
1562 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001563 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001564 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001565 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10001566 unsigned int buflen = server->pdu_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001567 server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07001568 bool use_rdma_mr = false;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001569
Joe Perchesf96637b2013-05-04 22:12:25 -05001570 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1571 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001572
1573 /*
1574 * read the rest of READ_RSP header (sans Data array), or whatever we
1575 * can if there's not enough data. At this point, we've read down to
1576 * the Mid.
1577 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001578 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001579 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001580
Al Viroa6137302016-01-09 19:37:16 -05001581 length = cifs_read_from_socket(server,
1582 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001583 if (length < 0)
1584 return length;
1585 server->total_read += length;
1586
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001587 if (server->ops->is_session_expired &&
1588 server->ops->is_session_expired(buf)) {
1589 cifs_reconnect(server);
1590 wake_up(&server->response_q);
1591 return -1;
1592 }
1593
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001594 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001595 server->ops->is_status_pending(buf, server)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001596 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001597 return -1;
1598 }
1599
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001600 /* set up first two iov for signature check and to get credits */
1601 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08001602 rdata->iov[0].iov_len = server->vals->header_preamble_size;
1603 rdata->iov[1].iov_base = buf + server->vals->header_preamble_size;
1604 rdata->iov[1].iov_len =
1605 server->total_read - server->vals->header_preamble_size;
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001606 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1607 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1608 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
1609 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
1610
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001611 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001612 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001613 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001614 cifs_dbg(FYI, "%s: server returned error %d\n",
1615 __func__, rdata->result);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001616 /* normal error on read response */
1617 return __cifs_readv_discard(server, mid, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001618 }
1619
1620 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001621 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001622 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1623 __func__, server->total_read,
1624 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001625 rdata->result = -EIO;
1626 return cifs_readv_discard(server, mid);
1627 }
1628
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001629 data_offset = server->ops->read_data_offset(buf) +
1630 server->vals->header_preamble_size;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001631 if (data_offset < server->total_read) {
1632 /*
1633 * win2k8 sometimes sends an offset of 0 when the read
1634 * is beyond the EOF. Treat it as if the data starts just after
1635 * the header.
1636 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001637 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1638 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001639 data_offset = server->total_read;
1640 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1641 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001642 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1643 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001644 rdata->result = -EIO;
1645 return cifs_readv_discard(server, mid);
1646 }
1647
Joe Perchesf96637b2013-05-04 22:12:25 -05001648 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1649 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001650
1651 len = data_offset - server->total_read;
1652 if (len > 0) {
1653 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001654 length = cifs_read_from_socket(server,
1655 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001656 if (length < 0)
1657 return length;
1658 server->total_read += length;
1659 }
1660
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001661 /* how much data is in the response? */
Long Li74dcf412017-11-22 17:38:46 -07001662#ifdef CONFIG_CIFS_SMB_DIRECT
1663 use_rdma_mr = rdata->mr;
1664#endif
1665 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1666 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001667 /* data_len is corrupt -- discard frame */
1668 rdata->result = -EIO;
1669 return cifs_readv_discard(server, mid);
1670 }
1671
Jeff Layton8321fec2012-09-19 06:22:32 -07001672 length = rdata->read_into_pages(server, rdata, data_len);
1673 if (length < 0)
1674 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001675
Jeff Layton8321fec2012-09-19 06:22:32 -07001676 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001677
Joe Perchesf96637b2013-05-04 22:12:25 -05001678 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1679 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001680
1681 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001682 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001683 return cifs_readv_discard(server, mid);
1684
1685 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001686 mid->resp_buf = server->smallbuf;
1687 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001688 return length;
1689}
1690
1691static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001692cifs_readv_callback(struct mid_q_entry *mid)
1693{
1694 struct cifs_readdata *rdata = mid->callback_data;
1695 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1696 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001697 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1698 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001699 .rq_pages = rdata->pages,
Long Li6d3adb22018-09-20 21:18:38 +00001700 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07001701 .rq_npages = rdata->nr_pages,
1702 .rq_pagesz = rdata->pagesz,
1703 .rq_tailsz = rdata->tailsz };
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001704 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001705
Joe Perchesf96637b2013-05-04 22:12:25 -05001706 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1707 __func__, mid->mid, mid->mid_state, rdata->result,
1708 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001709
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001710 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001711 case MID_RESPONSE_RECEIVED:
1712 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001713 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001714 int rc = 0;
1715
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001716 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001717 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001718 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001719 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1720 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001721 }
1722 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001723 task_io_account_read(rdata->got_bytes);
1724 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001725 break;
1726 case MID_REQUEST_SUBMITTED:
1727 case MID_RETRY_NEEDED:
1728 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001729 if (server->sign && rdata->got_bytes)
1730 /* reset bytes number since we can not check a sign */
1731 rdata->got_bytes = 0;
1732 /* FIXME: should this be counted toward the initiating task? */
1733 task_io_account_read(rdata->got_bytes);
1734 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001735 break;
1736 default:
1737 rdata->result = -EIO;
1738 }
1739
Jeff Laytonda472fc2012-03-23 14:40:53 -04001740 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001741 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08001742 add_credits(server, &credits, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001743}
1744
1745/* cifs_async_readv - send an async write, and set up mid to handle result */
1746int
1747cifs_async_readv(struct cifs_readdata *rdata)
1748{
1749 int rc;
1750 READ_REQ *smb = NULL;
1751 int wct;
1752 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001753 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1754 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001755
Joe Perchesf96637b2013-05-04 22:12:25 -05001756 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1757 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001758
1759 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1760 wct = 12;
1761 else {
1762 wct = 10; /* old style read */
1763 if ((rdata->offset >> 32) > 0) {
1764 /* can not handle this big offset for old */
1765 return -EIO;
1766 }
1767 }
1768
1769 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1770 if (rc)
1771 return rc;
1772
1773 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1774 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1775
1776 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001777 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001778 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1779 if (wct == 12)
1780 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1781 smb->Remaining = 0;
1782 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1783 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1784 if (wct == 12)
1785 smb->ByteCount = 0;
1786 else {
1787 /* old style read */
1788 struct smb_com_readx_req *smbr =
1789 (struct smb_com_readx_req *)smb;
1790 smbr->ByteCount = 0;
1791 }
1792
1793 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001794 rdata->iov[0].iov_base = smb;
1795 rdata->iov[0].iov_len = 4;
1796 rdata->iov[1].iov_base = (char *)smb + 4;
1797 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001798
Jeff Layton6993f742012-05-16 07:13:17 -04001799 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001800 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08001801 cifs_readv_callback, NULL, rdata, 0, NULL);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001802
1803 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001804 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001805 else
1806 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001807
1808 cifs_small_buf_release(smb);
1809 return rc;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001813CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1814 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 int rc = -EACCES;
1817 READ_REQ *pSMB = NULL;
1818 READ_RSP *pSMBr = NULL;
1819 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001820 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001821 int resp_buf_type = 0;
1822 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001823 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001824 __u32 pid = io_parms->pid;
1825 __u16 netfid = io_parms->netfid;
1826 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001827 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001828 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Joe Perchesf96637b2013-05-04 22:12:25 -05001830 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001831 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001832 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001833 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001834 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001835 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001836 /* can not handle this big offset for old */
1837 return -EIO;
1838 }
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001842 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (rc)
1844 return rc;
1845
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001846 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1847 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /* tcon and ses pointer are checked in smb_init */
1850 if (tcon->ses->server == NULL)
1851 return -ECONNABORTED;
1852
Steve Frenchec637e32005-12-12 20:53:18 -08001853 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001855 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001856 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001857 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 pSMB->Remaining = 0;
1860 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1861 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001862 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001863 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1864 else {
1865 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001866 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001867 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001868 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001869 }
Steve Frenchec637e32005-12-12 20:53:18 -08001870
1871 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001872 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001873 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1874 CIFS_LOG_ERROR, &rsp_iov);
1875 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001876 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001877 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001879 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 } else {
1881 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1882 data_length = data_length << 16;
1883 data_length += le16_to_cpu(pSMBr->DataLength);
1884 *nbytes = data_length;
1885
1886 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001887 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001889 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001890 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rc = -EIO;
1892 *nbytes = 0;
1893 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001894 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001895 le16_to_cpu(pSMBr->DataOffset);
1896/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001897 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001898 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001899 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001900 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001901 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Steve French790fe572007-07-07 19:25:05 +00001905 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001906 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001907 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001908 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001909 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001910 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001911 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001912 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001913 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001914 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001915
1916 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 since file handle passed in no longer valid */
1918 return rc;
1919}
1920
Steve Frenchec637e32005-12-12 20:53:18 -08001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001923CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001924 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 int rc = -EACCES;
1927 WRITE_REQ *pSMB = NULL;
1928 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001929 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 __u32 bytes_sent;
1931 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001932 __u32 pid = io_parms->pid;
1933 __u16 netfid = io_parms->netfid;
1934 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001935 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001936 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Steve Frencha24e2d72010-04-03 17:20:21 +00001938 *nbytes = 0;
1939
Joe Perchesf96637b2013-05-04 22:12:25 -05001940 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001941 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001942 return -ECONNABORTED;
1943
Steve French790fe572007-07-07 19:25:05 +00001944 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001945 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001946 else {
Steve French1c955182005-08-30 20:58:07 -07001947 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001948 if ((offset >> 32) > 0) {
1949 /* can not handle big offset for old srv */
1950 return -EIO;
1951 }
1952 }
Steve French1c955182005-08-30 20:58:07 -07001953
1954 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 (void **) &pSMBr);
1956 if (rc)
1957 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001958
1959 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1960 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* tcon and ses pointer are checked in smb_init */
1963 if (tcon->ses->server == NULL)
1964 return -ECONNABORTED;
1965
1966 pSMB->AndXCommand = 0xFF; /* none */
1967 pSMB->Fid = netfid;
1968 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001969 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001970 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 pSMB->Reserved = 0xFFFFFFFF;
1973 pSMB->WriteMode = 0;
1974 pSMB->Remaining = 0;
1975
Steve French50c2f752007-07-13 00:33:32 +00001976 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 can send more if LARGE_WRITE_X capability returned by the server and if
1978 our buffer is big enough or if we convert to iovecs on socket writes
1979 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001980 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1982 } else {
1983 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1984 & ~0xFF;
1985 }
1986
1987 if (bytes_sent > count)
1988 bytes_sent = count;
1989 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001990 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001991 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001992 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04001993 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* No buffer */
1995 cifs_buf_release(pSMB);
1996 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001997 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001998 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001999 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00002000 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07002001 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
2004 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002005 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07002006
Steve French790fe572007-07-07 19:25:05 +00002007 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07002008 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00002009 else { /* old style write has byte count 4 bytes earlier
2010 so 4 bytes pad */
2011 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07002012 (struct smb_com_writex_req *)pSMB;
2013 pSMBW->ByteCount = cpu_to_le16(byte_count);
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04002017 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002018 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002020 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 } else {
2022 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2023 *nbytes = (*nbytes) << 16;
2024 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302025
2026 /*
2027 * Mask off high 16 bits when bytes written as returned by the
2028 * server is greater than bytes requested by the client. Some
2029 * OS/2 servers are known to set incorrect CountHigh values.
2030 */
2031 if (*nbytes > count)
2032 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
2035 cifs_buf_release(pSMB);
2036
Steve French50c2f752007-07-13 00:33:32 +00002037 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 since file handle passed in no longer valid */
2039
2040 return rc;
2041}
2042
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002043void
2044cifs_writedata_release(struct kref *refcount)
2045{
2046 struct cifs_writedata *wdata = container_of(refcount,
2047 struct cifs_writedata, refcount);
Long Lidb223a52017-11-22 17:38:45 -07002048#ifdef CONFIG_CIFS_SMB_DIRECT
2049 if (wdata->mr) {
2050 smbd_deregister_mr(wdata->mr);
2051 wdata->mr = NULL;
2052 }
2053#endif
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002054
2055 if (wdata->cfile)
2056 cifsFileInfo_put(wdata->cfile);
2057
Long Li8e7360f2018-05-30 12:47:56 -07002058 kvfree(wdata->pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002059 kfree(wdata);
2060}
2061
2062/*
2063 * Write failed with a retryable error. Resend the write request. It's also
2064 * possible that the page was redirtied so re-clean the page.
2065 */
2066static void
2067cifs_writev_requeue(struct cifs_writedata *wdata)
2068{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002069 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00002070 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07002071 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002072 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002073
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002074 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2075 i = 0;
2076 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002077 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002078 struct cifs_writedata *wdata2;
2079 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002080
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002081 wsize = server->ops->wp_retry_size(inode);
2082 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002083 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002084 if (!nr_pages) {
2085 rc = -ENOTSUPP;
2086 break;
2087 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002088 cur_len = nr_pages * PAGE_SIZE;
2089 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002090 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002091 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002092 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002093 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06002094 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002095
2096 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2097 if (!wdata2) {
2098 rc = -ENOMEM;
2099 break;
2100 }
2101
2102 for (j = 0; j < nr_pages; j++) {
2103 wdata2->pages[j] = wdata->pages[i + j];
2104 lock_page(wdata2->pages[j]);
2105 clear_page_dirty_for_io(wdata2->pages[j]);
2106 }
2107
2108 wdata2->sync_mode = wdata->sync_mode;
2109 wdata2->nr_pages = nr_pages;
2110 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002112 wdata2->tailsz = tailsz;
2113 wdata2->bytes = cur_len;
2114
Pavel Shilovskyfe768d52019-01-29 12:15:11 -08002115 rc = cifs_get_writable_file(CIFS_I(inode), false,
2116 &wdata2->cfile);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002117 if (!wdata2->cfile) {
Pavel Shilovskyfe768d52019-01-29 12:15:11 -08002118 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2119 rc);
2120 if (!is_retryable_error(rc))
2121 rc = -EBADF;
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002122 } else {
2123 wdata2->pid = wdata2->cfile->pid;
2124 rc = server->ops->async_writev(wdata2,
2125 cifs_writedata_release);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002126 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002127
2128 for (j = 0; j < nr_pages; j++) {
2129 unlock_page(wdata2->pages[j]);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002130 if (rc != 0 && !is_retryable_error(rc)) {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002131 SetPageError(wdata2->pages[j]);
2132 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002133 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002134 }
2135 }
2136
2137 if (rc) {
2138 kref_put(&wdata2->refcount, cifs_writedata_release);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002139 if (is_retryable_error(rc))
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002140 continue;
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002141 i += nr_pages;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002142 break;
2143 }
2144
2145 rest_len -= cur_len;
2146 i += nr_pages;
2147 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002148
Pavel Shilovsky165df9a2019-01-29 16:40:28 -08002149 /* cleanup remaining pages from the original wdata */
2150 for (; i < wdata->nr_pages; i++) {
2151 SetPageError(wdata->pages[i]);
2152 end_page_writeback(wdata->pages[i]);
2153 put_page(wdata->pages[i]);
2154 }
2155
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002156 if (rc != 0 && !is_retryable_error(rc))
2157 mapping_set_error(inode->i_mapping, rc);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002158 kref_put(&wdata->refcount, cifs_writedata_release);
2159}
2160
Jeff Laytonc2e87642012-03-23 14:40:55 -04002161void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002162cifs_writev_complete(struct work_struct *work)
2163{
2164 struct cifs_writedata *wdata = container_of(work,
2165 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002166 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002167 int i = 0;
2168
2169 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002170 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002171 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002172 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002173 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2174 wdata->bytes);
2175 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2176 return cifs_writev_requeue(wdata);
2177
2178 for (i = 0; i < wdata->nr_pages; i++) {
2179 struct page *page = wdata->pages[i];
2180 if (wdata->result == -EAGAIN)
2181 __set_page_dirty_nobuffers(page);
2182 else if (wdata->result < 0)
2183 SetPageError(page);
2184 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002185 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002186 }
2187 if (wdata->result != -EAGAIN)
2188 mapping_set_error(inode->i_mapping, wdata->result);
2189 kref_put(&wdata->refcount, cifs_writedata_release);
2190}
2191
2192struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002193cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002194{
Long Li8e7360f2018-05-30 12:47:56 -07002195 struct page **pages =
Kees Cook6396bb22018-06-12 14:03:40 -07002196 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Long Li8e7360f2018-05-30 12:47:56 -07002197 if (pages)
2198 return cifs_writedata_direct_alloc(pages, complete);
2199
2200 return NULL;
2201}
2202
2203struct cifs_writedata *
2204cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2205{
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002206 struct cifs_writedata *wdata;
2207
Long Li8e7360f2018-05-30 12:47:56 -07002208 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002209 if (wdata != NULL) {
Long Li8e7360f2018-05-30 12:47:56 -07002210 wdata->pages = pages;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002211 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002212 INIT_LIST_HEAD(&wdata->list);
2213 init_completion(&wdata->done);
2214 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002215 }
2216 return wdata;
2217}
2218
2219/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002220 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002221 * workqueue completion task.
2222 */
2223static void
2224cifs_writev_callback(struct mid_q_entry *mid)
2225{
2226 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002227 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002228 unsigned int written;
2229 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08002230 struct cifs_credits credits = { .value = 1, .instance = 0 };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002231
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002232 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002233 case MID_RESPONSE_RECEIVED:
2234 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2235 if (wdata->result != 0)
2236 break;
2237
2238 written = le16_to_cpu(smb->CountHigh);
2239 written <<= 16;
2240 written += le16_to_cpu(smb->Count);
2241 /*
2242 * Mask off high 16 bits when bytes written as returned
2243 * by the server is greater than bytes requested by the
2244 * client. OS/2 servers are known to set incorrect
2245 * CountHigh values.
2246 */
2247 if (written > wdata->bytes)
2248 written &= 0xFFFF;
2249
2250 if (written < wdata->bytes)
2251 wdata->result = -ENOSPC;
2252 else
2253 wdata->bytes = written;
2254 break;
2255 case MID_REQUEST_SUBMITTED:
2256 case MID_RETRY_NEEDED:
2257 wdata->result = -EAGAIN;
2258 break;
2259 default:
2260 wdata->result = -EIO;
2261 break;
2262 }
2263
Jeff Laytonda472fc2012-03-23 14:40:53 -04002264 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002265 DeleteMidQEntry(mid);
Pavel Shilovsky34f4deb2019-01-16 11:22:29 -08002266 add_credits(tcon->ses->server, &credits, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002267}
2268
2269/* cifs_async_writev - send an async write, and set up mid to handle result */
2270int
Steve French4a5c80d2014-02-07 20:45:12 -06002271cifs_async_writev(struct cifs_writedata *wdata,
2272 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002273{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002274 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002275 WRITE_REQ *smb = NULL;
2276 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002277 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002278 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002279 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002280
2281 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2282 wct = 14;
2283 } else {
2284 wct = 12;
2285 if (wdata->offset >> 32 > 0) {
2286 /* can not handle big offset for old srv */
2287 return -EIO;
2288 }
2289 }
2290
2291 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2292 if (rc)
2293 goto async_writev_out;
2294
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002295 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2296 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002297
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002298 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002299 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002300 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2301 if (wct == 14)
2302 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2303 smb->Reserved = 0xFFFFFFFF;
2304 smb->WriteMode = 0;
2305 smb->Remaining = 0;
2306
2307 smb->DataOffset =
2308 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2309
2310 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002311 iov[0].iov_len = 4;
2312 iov[0].iov_base = smb;
2313 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2314 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002315
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002316 rqst.rq_iov = iov;
2317 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002318 rqst.rq_pages = wdata->pages;
Long Li6d3adb22018-09-20 21:18:38 +00002319 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002320 rqst.rq_npages = wdata->nr_pages;
2321 rqst.rq_pagesz = wdata->pagesz;
2322 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002323
Joe Perchesf96637b2013-05-04 22:12:25 -05002324 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2325 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002326
2327 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2328 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2329
2330 if (wct == 14) {
2331 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2332 put_bcc(wdata->bytes + 1, &smb->hdr);
2333 } else {
2334 /* wct == 12 */
2335 struct smb_com_writex_req *smbw =
2336 (struct smb_com_writex_req *)smb;
2337 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2338 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002339 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002340 }
2341
2342 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002343 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky3349c3a2019-01-15 15:52:29 -08002344 cifs_writev_callback, NULL, wdata, 0, NULL);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002345
2346 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002347 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002348 else
Steve French4a5c80d2014-02-07 20:45:12 -06002349 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002350
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002351async_writev_out:
2352 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002353 return rc;
2354}
2355
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002356int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002357CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002358 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 int rc = -EACCES;
2361 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002362 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002363 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002364 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002365 __u32 pid = io_parms->pid;
2366 __u16 netfid = io_parms->netfid;
2367 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002368 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002369 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002370 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002372 *nbytes = 0;
2373
Joe Perchesf96637b2013-05-04 22:12:25 -05002374 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002375
Steve French4c3130e2008-12-09 00:28:16 +00002376 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002377 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002378 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002379 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002380 if ((offset >> 32) > 0) {
2381 /* can not handle big offset for old srv */
2382 return -EIO;
2383 }
2384 }
Steve French8cc64c62005-10-03 13:49:43 -07002385 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (rc)
2387 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002388
2389 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2390 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /* tcon and ses pointer are checked in smb_init */
2393 if (tcon->ses->server == NULL)
2394 return -ECONNABORTED;
2395
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002396 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 pSMB->Fid = netfid;
2398 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002399 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002400 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pSMB->Reserved = 0xFFFFFFFF;
2402 pSMB->WriteMode = 0;
2403 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002406 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Steve French3e844692005-10-03 13:37:24 -07002408 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2409 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002410 /* header + 1 byte pad */
2411 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002412 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002413 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002414 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002415 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002416 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002417 pSMB->ByteCount = cpu_to_le16(count + 1);
2418 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002419 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002420 (struct smb_com_writex_req *)pSMB;
2421 pSMBW->ByteCount = cpu_to_le16(count + 5);
2422 }
Steve French3e844692005-10-03 13:37:24 -07002423 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002424 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002425 iov[0].iov_len = smb_hdr_len + 4;
2426 else /* wct == 12 pad bigger by four bytes */
2427 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002428
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002429 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2430 &rsp_iov);
2431 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002432 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002434 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002435 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002436 /* presumably this can not happen, but best to be safe */
2437 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002438 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002439 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002440 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2441 *nbytes = (*nbytes) << 16;
2442 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302443
2444 /*
2445 * Mask off high 16 bits when bytes written as returned by the
2446 * server is greater than bytes requested by the client. OS/2
2447 * servers are known to set incorrect CountHigh values.
2448 */
2449 if (*nbytes > count)
2450 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002453 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Steve French50c2f752007-07-13 00:33:32 +00002455 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 since file handle passed in no longer valid */
2457
2458 return rc;
2459}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002460
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002461int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2462 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002463 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2464{
2465 int rc = 0;
2466 LOCK_REQ *pSMB = NULL;
2467 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002468 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002469 int resp_buf_type;
2470 __u16 count;
2471
Joe Perchesf96637b2013-05-04 22:12:25 -05002472 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2473 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002474
2475 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2476 if (rc)
2477 return rc;
2478
2479 pSMB->Timeout = 0;
2480 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2481 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2482 pSMB->LockType = lock_type;
2483 pSMB->AndXCommand = 0xFF; /* none */
2484 pSMB->Fid = netfid; /* netfid stays le */
2485
2486 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2487 inc_rfc1001_len(pSMB, count);
2488 pSMB->ByteCount = cpu_to_le16(count);
2489
2490 iov[0].iov_base = (char *)pSMB;
2491 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2492 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2493 iov[1].iov_base = (char *)buf;
2494 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2495
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002496 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10002497 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type,
2498 CIFS_NO_RSP_BUF, &rsp_iov);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002499 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002500 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002501 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002502
2503 return rc;
2504}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002507CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002508 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002510 const __u32 numLock, const __u8 lockType,
2511 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 int rc = 0;
2514 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002515/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002517 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 __u16 count;
2519
Joe Perchesf96637b2013-05-04 22:12:25 -05002520 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2521 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002522 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (rc)
2525 return rc;
2526
Steve French790fe572007-07-07 19:25:05 +00002527 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002528 /* no response expected */
Ronnie Sahlberg392e1c52019-05-06 10:00:02 +10002529 flags = CIFS_NO_SRV_RSP | CIFS_NON_BLOCKING | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002531 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002532 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2534 } else {
2535 pSMB->Timeout = 0;
2536 }
2537
2538 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2539 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2540 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002541 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 pSMB->AndXCommand = 0xFF; /* none */
2543 pSMB->Fid = smb_file_id; /* netfid stays le */
2544
Steve French790fe572007-07-07 19:25:05 +00002545 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002546 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /* BB where to store pid high? */
2548 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2549 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2550 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2551 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2552 count = sizeof(LOCKING_ANDX_RANGE);
2553 } else {
2554 /* oplock break */
2555 count = 0;
2556 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002557 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 pSMB->ByteCount = cpu_to_le16(count);
2559
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002560 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002561 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002562 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002563 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002564 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002565 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002566 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002567 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002568 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Steve French50c2f752007-07-13 00:33:32 +00002570 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 since file handle passed in no longer valid */
2572 return rc;
2573}
2574
2575int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002576CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002577 const __u16 smb_file_id, const __u32 netpid,
2578 const loff_t start_offset, const __u64 len,
2579 struct file_lock *pLockData, const __u16 lock_type,
2580 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002581{
2582 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2583 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002584 struct cifs_posix_lock *parm_data;
2585 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002586 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002587 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002588 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002589 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002590 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002591 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002592
Joe Perchesf96637b2013-05-04 22:12:25 -05002593 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002594
Steve French08547b02006-02-28 22:39:25 +00002595 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2596
2597 if (rc)
2598 return rc;
2599
2600 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2601
Steve French50c2f752007-07-13 00:33:32 +00002602 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002603 pSMB->MaxSetupCount = 0;
2604 pSMB->Reserved = 0;
2605 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002606 pSMB->Reserved2 = 0;
2607 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2608 offset = param_offset + params;
2609
Steve French08547b02006-02-28 22:39:25 +00002610 count = sizeof(struct cifs_posix_lock);
2611 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002612 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002613 pSMB->SetupCount = 1;
2614 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002615 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002616 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2617 else
2618 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2619 byte_count = 3 /* pad */ + params + count;
2620 pSMB->DataCount = cpu_to_le16(count);
2621 pSMB->ParameterCount = cpu_to_le16(params);
2622 pSMB->TotalDataCount = pSMB->DataCount;
2623 pSMB->TotalParameterCount = pSMB->ParameterCount;
2624 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002625 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002626 (((char *) &pSMB->hdr.Protocol) + offset);
2627
2628 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002629 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002630 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002631 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002632 pSMB->Timeout = cpu_to_le32(-1);
2633 } else
2634 pSMB->Timeout = 0;
2635
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002636 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002637 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002638 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002639
2640 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002641 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002642 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2643 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002644 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002645 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002646 if (waitFlag) {
2647 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2648 (struct smb_hdr *) pSMBr, &bytes_returned);
2649 } else {
Steve French133672e2007-11-13 22:41:37 +00002650 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002651 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002652 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002653 &resp_buf_type, timeout, &rsp_iov);
2654 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002655 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002656 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002657
Steve French08547b02006-02-28 22:39:25 +00002658 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002659 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002660 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002661 /* lock structure can be returned on get */
2662 __u16 data_offset;
2663 __u16 data_count;
2664 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002665
Jeff Layton820a8032011-05-04 08:05:26 -04002666 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002667 rc = -EIO; /* bad smb */
2668 goto plk_err_exit;
2669 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002670 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2671 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002672 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002673 rc = -EIO;
2674 goto plk_err_exit;
2675 }
2676 parm_data = (struct cifs_posix_lock *)
2677 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002678 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002679 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002680 else {
2681 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002682 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002683 pLockData->fl_type = F_RDLCK;
2684 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002685 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002686 pLockData->fl_type = F_WRLCK;
2687
Steve French5443d132011-03-13 05:08:25 +00002688 pLockData->fl_start = le64_to_cpu(parm_data->start);
2689 pLockData->fl_end = pLockData->fl_start +
2690 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002691 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002692 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002693 }
Steve French50c2f752007-07-13 00:33:32 +00002694
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002695plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002696 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002697
Steve French08547b02006-02-28 22:39:25 +00002698 /* Note: On -EAGAIN error only caller can retry on handle based calls
2699 since file handle passed in no longer valid */
2700
2701 return rc;
2702}
2703
2704
2705int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002706CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
2708 int rc = 0;
2709 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002710 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712/* do not retry on dead session on close */
2713 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002714 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return 0;
2716 if (rc)
2717 return rc;
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002720 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002722 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002723 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002724 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002726 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002728 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
2730 }
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002733 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 rc = 0;
2735
2736 return rc;
2737}
2738
2739int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002740CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002741{
2742 int rc = 0;
2743 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002744 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002745
2746 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2747 if (rc)
2748 return rc;
2749
2750 pSMB->FileID = (__u16) smb_file_id;
2751 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002752 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002753 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002754 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002755 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002756 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002757
2758 return rc;
2759}
2760
2761int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002762CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002763 const char *from_name, const char *to_name,
2764 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 int rc = 0;
2767 RENAME_REQ *pSMB = NULL;
2768 RENAME_RSP *pSMBr = NULL;
2769 int bytes_returned;
2770 int name_len, name_len2;
2771 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002772 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Joe Perchesf96637b2013-05-04 22:12:25 -05002774 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775renameRetry:
2776 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2777 (void **) &pSMBr);
2778 if (rc)
2779 return rc;
2780
2781 pSMB->BufferFormat = 0x04;
2782 pSMB->SearchAttributes =
2783 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2784 ATTR_DIRECTORY);
2785
2786 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002787 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2788 from_name, PATH_MAX,
2789 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 name_len++; /* trailing null */
2791 name_len *= 2;
2792 pSMB->OldFileName[name_len] = 0x04; /* pad */
2793 /* protocol requires ASCII signature byte on Unicode string */
2794 pSMB->OldFileName[name_len + 1] = 0x00;
2795 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002796 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002797 to_name, PATH_MAX, cifs_sb->local_nls,
2798 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2800 name_len2 *= 2; /* convert to bytes */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002801 } else {
2802 name_len = copy_path_name(pSMB->OldFileName, from_name);
2803 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 name_len2++; /* signature byte */
2806 }
2807
2808 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002809 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 pSMB->ByteCount = cpu_to_le16(count);
2811
2812 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2813 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002814 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002815 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002816 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 cifs_buf_release(pSMB);
2819
2820 if (rc == -EAGAIN)
2821 goto renameRetry;
2822
2823 return rc;
2824}
2825
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002826int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002827 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002828 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2831 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002832 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 char *data_offset;
2834 char dummy_string[30];
2835 int rc = 0;
2836 int bytes_returned = 0;
2837 int len_of_str;
2838 __u16 params, param_offset, offset, count, byte_count;
2839
Joe Perchesf96637b2013-05-04 22:12:25 -05002840 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2842 (void **) &pSMBr);
2843 if (rc)
2844 return rc;
2845
2846 params = 6;
2847 pSMB->MaxSetupCount = 0;
2848 pSMB->Reserved = 0;
2849 pSMB->Flags = 0;
2850 pSMB->Timeout = 0;
2851 pSMB->Reserved2 = 0;
2852 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2853 offset = param_offset + params;
2854
2855 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2856 rename_info = (struct set_file_rename *) data_offset;
2857 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002858 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 pSMB->SetupCount = 1;
2860 pSMB->Reserved3 = 0;
2861 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2862 byte_count = 3 /* pad */ + params;
2863 pSMB->ParameterCount = cpu_to_le16(params);
2864 pSMB->TotalParameterCount = pSMB->ParameterCount;
2865 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2866 pSMB->DataOffset = cpu_to_le16(offset);
2867 /* construct random name ".cifs_tmp<inodenum><mid>" */
2868 rename_info->overwrite = cpu_to_le32(1);
2869 rename_info->root_fid = 0;
2870 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002871 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002872 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002873 len_of_str =
2874 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002875 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002877 len_of_str =
2878 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002879 target_name, PATH_MAX, nls_codepage,
2880 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
2882 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002883 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 byte_count += count;
2885 pSMB->DataCount = cpu_to_le16(count);
2886 pSMB->TotalDataCount = pSMB->DataCount;
2887 pSMB->Fid = netfid;
2888 pSMB->InformationLevel =
2889 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2890 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002891 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 pSMB->ByteCount = cpu_to_le16(byte_count);
2893 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002894 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002895 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002896 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002897 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2898 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 cifs_buf_release(pSMB);
2901
2902 /* Note: On -EAGAIN error only caller can retry on handle based calls
2903 since file handle passed in no longer valid */
2904
2905 return rc;
2906}
2907
2908int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002909CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2910 const char *fromName, const __u16 target_tid, const char *toName,
2911 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 int rc = 0;
2914 COPY_REQ *pSMB = NULL;
2915 COPY_RSP *pSMBr = NULL;
2916 int bytes_returned;
2917 int name_len, name_len2;
2918 __u16 count;
2919
Joe Perchesf96637b2013-05-04 22:12:25 -05002920 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921copyRetry:
2922 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2923 (void **) &pSMBr);
2924 if (rc)
2925 return rc;
2926
2927 pSMB->BufferFormat = 0x04;
2928 pSMB->Tid2 = target_tid;
2929
2930 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2931
2932 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002933 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2934 fromName, PATH_MAX, nls_codepage,
2935 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 name_len++; /* trailing null */
2937 name_len *= 2;
2938 pSMB->OldFileName[name_len] = 0x04; /* pad */
2939 /* protocol requires ASCII signature byte on Unicode string */
2940 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002941 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002942 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2943 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2945 name_len2 *= 2; /* convert to bytes */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002946 } else {
2947 name_len = copy_path_name(pSMB->OldFileName, fromName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002949 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 name_len2++; /* signature byte */
2951 }
2952
2953 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002954 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 pSMB->ByteCount = cpu_to_le16(count);
2956
2957 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2958 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2959 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002960 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2961 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
Steve French0d817bc2008-05-22 02:02:03 +00002963 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 if (rc == -EAGAIN)
2966 goto copyRetry;
2967
2968 return rc;
2969}
2970
2971int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002972CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002974 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 TRANSACTION2_SPI_REQ *pSMB = NULL;
2977 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2978 char *data_offset;
2979 int name_len;
2980 int name_len_target;
2981 int rc = 0;
2982 int bytes_returned = 0;
2983 __u16 params, param_offset, offset, byte_count;
2984
Joe Perchesf96637b2013-05-04 22:12:25 -05002985 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986createSymLinkRetry:
2987 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2988 (void **) &pSMBr);
2989 if (rc)
2990 return rc;
2991
2992 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2993 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002994 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2995 /* find define for this maxpathcomponent */
2996 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 name_len++; /* trailing null */
2998 name_len *= 2;
2999
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003000 } else {
3001 name_len = copy_path_name(pSMB->FileName, fromName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
3003 params = 6 + name_len;
3004 pSMB->MaxSetupCount = 0;
3005 pSMB->Reserved = 0;
3006 pSMB->Flags = 0;
3007 pSMB->Timeout = 0;
3008 pSMB->Reserved2 = 0;
3009 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003010 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 offset = param_offset + params;
3012
3013 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3014 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3015 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003016 cifsConvertToUTF16((__le16 *) data_offset, toName,
3017 /* find define for this maxpathcomponent */
3018 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 name_len_target++; /* trailing null */
3020 name_len_target *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003021 } else {
3022 name_len_target = copy_path_name(data_offset, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024
3025 pSMB->MaxParameterCount = cpu_to_le16(2);
3026 /* BB find exact max on data count below from sess */
3027 pSMB->MaxDataCount = cpu_to_le16(1000);
3028 pSMB->SetupCount = 1;
3029 pSMB->Reserved3 = 0;
3030 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3031 byte_count = 3 /* pad */ + params + name_len_target;
3032 pSMB->DataCount = cpu_to_le16(name_len_target);
3033 pSMB->ParameterCount = cpu_to_le16(params);
3034 pSMB->TotalDataCount = pSMB->DataCount;
3035 pSMB->TotalParameterCount = pSMB->ParameterCount;
3036 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3037 pSMB->DataOffset = cpu_to_le16(offset);
3038 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
3039 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003040 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 pSMB->ByteCount = cpu_to_le16(byte_count);
3042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003044 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003045 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003046 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
3047 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Steve French0d817bc2008-05-22 02:02:03 +00003049 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 if (rc == -EAGAIN)
3052 goto createSymLinkRetry;
3053
3054 return rc;
3055}
3056
3057int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003058CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003060 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 TRANSACTION2_SPI_REQ *pSMB = NULL;
3063 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3064 char *data_offset;
3065 int name_len;
3066 int name_len_target;
3067 int rc = 0;
3068 int bytes_returned = 0;
3069 __u16 params, param_offset, offset, byte_count;
3070
Joe Perchesf96637b2013-05-04 22:12:25 -05003071 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072createHardLinkRetry:
3073 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3074 (void **) &pSMBr);
3075 if (rc)
3076 return rc;
3077
3078 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06003079 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
3080 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 name_len++; /* trailing null */
3082 name_len *= 2;
3083
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003084 } else {
3085 name_len = copy_path_name(pSMB->FileName, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087 params = 6 + name_len;
3088 pSMB->MaxSetupCount = 0;
3089 pSMB->Reserved = 0;
3090 pSMB->Flags = 0;
3091 pSMB->Timeout = 0;
3092 pSMB->Reserved2 = 0;
3093 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003094 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 offset = param_offset + params;
3096
3097 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3098 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3099 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06003100 cifsConvertToUTF16((__le16 *) data_offset, fromName,
3101 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 name_len_target++; /* trailing null */
3103 name_len_target *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003104 } else {
3105 name_len_target = copy_path_name(data_offset, fromName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
3107
3108 pSMB->MaxParameterCount = cpu_to_le16(2);
3109 /* BB find exact max on data count below from sess*/
3110 pSMB->MaxDataCount = cpu_to_le16(1000);
3111 pSMB->SetupCount = 1;
3112 pSMB->Reserved3 = 0;
3113 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3114 byte_count = 3 /* pad */ + params + name_len_target;
3115 pSMB->ParameterCount = cpu_to_le16(params);
3116 pSMB->TotalParameterCount = pSMB->ParameterCount;
3117 pSMB->DataCount = cpu_to_le16(name_len_target);
3118 pSMB->TotalDataCount = pSMB->DataCount;
3119 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3120 pSMB->DataOffset = cpu_to_le16(offset);
3121 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3122 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003123 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 pSMB->ByteCount = cpu_to_le16(byte_count);
3125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003127 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003128 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003129 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3130 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 cifs_buf_release(pSMB);
3133 if (rc == -EAGAIN)
3134 goto createHardLinkRetry;
3135
3136 return rc;
3137}
3138
3139int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003140CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07003141 const char *from_name, const char *to_name,
3142 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 int rc = 0;
3145 NT_RENAME_REQ *pSMB = NULL;
3146 RENAME_RSP *pSMBr = NULL;
3147 int bytes_returned;
3148 int name_len, name_len2;
3149 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003150 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Joe Perchesf96637b2013-05-04 22:12:25 -05003152 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153winCreateHardLinkRetry:
3154
3155 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3156 (void **) &pSMBr);
3157 if (rc)
3158 return rc;
3159
3160 pSMB->SearchAttributes =
3161 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3162 ATTR_DIRECTORY);
3163 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3164 pSMB->ClusterCount = 0;
3165
3166 pSMB->BufferFormat = 0x04;
3167
3168 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3169 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003170 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3171 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 name_len++; /* trailing null */
3173 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003174
3175 /* protocol specifies ASCII buffer format (0x04) for unicode */
3176 pSMB->OldFileName[name_len] = 0x04;
3177 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003179 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003180 to_name, PATH_MAX, cifs_sb->local_nls,
3181 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3183 name_len2 *= 2; /* convert to bytes */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003184 } else {
3185 name_len = copy_path_name(pSMB->OldFileName, from_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003187 name_len2 = copy_path_name(pSMB->OldFileName+name_len+1, to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 name_len2++; /* signature byte */
3189 }
3190
3191 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003192 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 pSMB->ByteCount = cpu_to_le16(count);
3194
3195 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3196 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003197 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003198 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003199 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 cifs_buf_release(pSMB);
3202 if (rc == -EAGAIN)
3203 goto winCreateHardLinkRetry;
3204
3205 return rc;
3206}
3207
3208int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003209CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003210 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003211 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213/* SMB_QUERY_FILE_UNIX_LINK */
3214 TRANSACTION2_QPI_REQ *pSMB = NULL;
3215 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3216 int rc = 0;
3217 int bytes_returned;
3218 int name_len;
3219 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003220 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Joe Perchesf96637b2013-05-04 22:12:25 -05003222 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224querySymLinkRetry:
3225 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3226 (void **) &pSMBr);
3227 if (rc)
3228 return rc;
3229
3230 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3231 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003232 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3233 searchName, PATH_MAX, nls_codepage,
3234 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 name_len++; /* trailing null */
3236 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003237 } else {
3238 name_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240
3241 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3242 pSMB->TotalDataCount = 0;
3243 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003244 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 pSMB->MaxSetupCount = 0;
3246 pSMB->Reserved = 0;
3247 pSMB->Flags = 0;
3248 pSMB->Timeout = 0;
3249 pSMB->Reserved2 = 0;
3250 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003251 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 pSMB->DataCount = 0;
3253 pSMB->DataOffset = 0;
3254 pSMB->SetupCount = 1;
3255 pSMB->Reserved3 = 0;
3256 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3257 byte_count = params + 1 /* pad */ ;
3258 pSMB->TotalParameterCount = cpu_to_le16(params);
3259 pSMB->ParameterCount = pSMB->TotalParameterCount;
3260 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3261 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003262 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 pSMB->ByteCount = cpu_to_le16(byte_count);
3264
3265 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3266 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3267 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003268 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 } else {
3270 /* decode response */
3271
3272 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003274 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003275 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003277 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003278 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Jeff Layton460b9692009-04-30 07:17:56 -04003280 data_start = ((char *) &pSMBr->hdr.Protocol) +
3281 le16_to_cpu(pSMBr->t2.DataOffset);
3282
Steve French0e0d2cf2009-05-01 05:27:32 +00003283 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3284 is_unicode = true;
3285 else
3286 is_unicode = false;
3287
Steve French737b7582005-04-28 22:41:06 -07003288 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003289 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3290 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003291 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003292 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
3294 }
3295 cifs_buf_release(pSMB);
3296 if (rc == -EAGAIN)
3297 goto querySymLinkRetry;
3298 return rc;
3299}
3300
Steve Frenchc52a95542011-02-24 06:16:22 +00003301/*
3302 * Recent Windows versions now create symlinks more frequently
3303 * and they use the "reparse point" mechanism below. We can of course
3304 * do symlinks nicely to Samba and other servers which support the
3305 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3306 * "MF" symlinks optionally, but for recent Windows we really need to
3307 * reenable the code below and fix the cifs_symlink callers to handle this.
3308 * In the interim this code has been moved to its own config option so
3309 * it is not compiled in by default until callers fixed up and more tested.
3310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003312CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3313 __u16 fid, char **symlinkinfo,
3314 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 int rc = 0;
3317 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003318 struct smb_com_transaction_ioctl_req *pSMB;
3319 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003320 bool is_unicode;
3321 unsigned int sub_len;
3322 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003323 struct reparse_symlink_data *reparse_buf;
3324 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003325 __u32 data_offset, data_count;
3326 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003328 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3330 (void **) &pSMBr);
3331 if (rc)
3332 return rc;
3333
3334 pSMB->TotalParameterCount = 0 ;
3335 pSMB->TotalDataCount = 0;
3336 pSMB->MaxParameterCount = cpu_to_le32(2);
3337 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003338 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 pSMB->MaxSetupCount = 4;
3340 pSMB->Reserved = 0;
3341 pSMB->ParameterOffset = 0;
3342 pSMB->DataCount = 0;
3343 pSMB->DataOffset = 0;
3344 pSMB->SetupCount = 4;
3345 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3346 pSMB->ParameterCount = pSMB->TotalParameterCount;
3347 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3348 pSMB->IsFsctl = 1; /* FSCTL */
3349 pSMB->IsRootFlag = 0;
3350 pSMB->Fid = fid; /* file handle always le */
3351 pSMB->ByteCount = 0;
3352
3353 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3354 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3355 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003356 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003357 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Steve French989c7e52009-05-02 05:32:20 +00003359
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003360 data_offset = le32_to_cpu(pSMBr->DataOffset);
3361 data_count = le32_to_cpu(pSMBr->DataCount);
3362 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3363 /* BB also check enough total bytes returned */
3364 rc = -EIO; /* bad smb */
3365 goto qreparse_out;
3366 }
3367 if (!data_count || (data_count > 2048)) {
3368 rc = -EIO;
3369 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3370 goto qreparse_out;
3371 }
3372 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003373 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003374 ((char *)&pSMBr->hdr.Protocol + data_offset);
3375 if ((char *)reparse_buf >= end_of_smb) {
3376 rc = -EIO;
3377 goto qreparse_out;
3378 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003379 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3380 cifs_dbg(FYI, "NFS style reparse tag\n");
3381 posix_buf = (struct reparse_posix_data *)reparse_buf;
3382
3383 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3384 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3385 le64_to_cpu(posix_buf->InodeType));
3386 rc = -EOPNOTSUPP;
3387 goto qreparse_out;
3388 }
3389 is_unicode = true;
3390 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3391 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3392 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3393 rc = -EIO;
3394 goto qreparse_out;
3395 }
3396 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3397 sub_len, is_unicode, nls_codepage);
3398 goto qreparse_out;
3399 } else if (reparse_buf->ReparseTag !=
3400 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3401 rc = -EOPNOTSUPP;
3402 goto qreparse_out;
3403 }
3404
3405 /* Reparse tag is NTFS symlink */
3406 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3407 reparse_buf->PathBuffer;
3408 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3409 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003410 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3411 rc = -EIO;
3412 goto qreparse_out;
3413 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003414 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3415 is_unicode = true;
3416 else
3417 is_unicode = false;
3418
3419 /* BB FIXME investigate remapping reserved chars here */
3420 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3421 nls_codepage);
3422 if (!*symlinkinfo)
3423 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003425 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003427 /*
3428 * Note: On -EAGAIN error only caller can retry on handle based calls
3429 * since file handle passed in no longer valid.
3430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return rc;
3432}
3433
Steve Frenchc7f508a2013-10-14 15:27:32 -05003434int
3435CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3436 __u16 fid)
3437{
3438 int rc = 0;
3439 int bytes_returned;
3440 struct smb_com_transaction_compr_ioctl_req *pSMB;
3441 struct smb_com_transaction_ioctl_rsp *pSMBr;
3442
3443 cifs_dbg(FYI, "Set compression for %u\n", fid);
3444 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3445 (void **) &pSMBr);
3446 if (rc)
3447 return rc;
3448
3449 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3450
3451 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003452 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003453 pSMB->MaxParameterCount = 0;
3454 pSMB->MaxDataCount = 0;
3455 pSMB->MaxSetupCount = 4;
3456 pSMB->Reserved = 0;
3457 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003458 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003459 pSMB->DataOffset =
3460 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3461 compression_state) - 4); /* 84 */
3462 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003463 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003464 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003465 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003466 pSMB->IsFsctl = 1; /* FSCTL */
3467 pSMB->IsRootFlag = 0;
3468 pSMB->Fid = fid; /* file handle always le */
3469 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003470 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003471 inc_rfc1001_len(pSMB, 5);
3472
3473 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3474 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3475 if (rc)
3476 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3477
3478 cifs_buf_release(pSMB);
3479
3480 /*
3481 * Note: On -EAGAIN error only caller can retry on handle based calls
3482 * since file handle passed in no longer valid.
3483 */
3484 return rc;
3485}
3486
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488#ifdef CONFIG_CIFS_POSIX
3489
3490/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003491static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003492 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003495 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3496 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3497 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003498/*
3499 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3500 ace->e_perm, ace->e_tag, ace->e_id);
3501*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 return;
3504}
3505
3506/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003507static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3508 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
3510 int size = 0;
3511 int i;
3512 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003513 struct cifs_posix_ace *pACE;
3514 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003515 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3518 return -EOPNOTSUPP;
3519
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003520 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 count = le16_to_cpu(cifs_acl->access_entry_count);
3522 pACE = &cifs_acl->ace_array[0];
3523 size = sizeof(struct cifs_posix_acl);
3524 size += sizeof(struct cifs_posix_ace) * count;
3525 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003526 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003527 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3528 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return -EINVAL;
3530 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003531 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 count = le16_to_cpu(cifs_acl->access_entry_count);
3533 size = sizeof(struct cifs_posix_acl);
3534 size += sizeof(struct cifs_posix_ace) * count;
3535/* skip past access ACEs to get to default ACEs */
3536 pACE = &cifs_acl->ace_array[count];
3537 count = le16_to_cpu(cifs_acl->default_entry_count);
3538 size += sizeof(struct cifs_posix_ace) * count;
3539 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003540 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return -EINVAL;
3542 } else {
3543 /* illegal type */
3544 return -EINVAL;
3545 }
3546
3547 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003548 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003549 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003550 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return -ERANGE;
3552 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003553 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3554
Steve Frenchff7feac2005-11-15 16:45:16 -08003555 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003556 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003557 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003558 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 }
3561 return size;
3562}
3563
Hariprasad Kelam0aa3a242019-07-02 23:50:02 +05303564static void convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003565 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Steve Frenchff7feac2005-11-15 16:45:16 -08003567 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3568 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003570 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 /* Probably no need to le convert -1 on any arch but can not hurt */
3572 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003573 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003574 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003575/*
3576 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3577 ace->e_perm, ace->e_tag, ace->e_id);
3578*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
3581/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003582static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3583 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003586 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003587 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003588 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 int count;
3590 int i;
3591
Steve French790fe572007-07-07 19:25:05 +00003592 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return 0;
3594
3595 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003596 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3597 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003598 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003599 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3600 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 return 0;
3602 }
3603 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003604 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003605 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003606 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003607 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003608 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003609 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003610 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003611 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return 0;
3613 }
Hariprasad Kelam0aa3a242019-07-02 23:50:02 +05303614 for (i = 0; i < count; i++)
3615 convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003616 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3618 rc += sizeof(struct cifs_posix_acl);
3619 /* BB add check to make sure ACL does not overflow SMB */
3620 }
3621 return rc;
3622}
3623
3624int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003625CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003626 const unsigned char *searchName,
3627 char *acl_inf, const int buflen, const int acl_type,
3628 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630/* SMB_QUERY_POSIX_ACL */
3631 TRANSACTION2_QPI_REQ *pSMB = NULL;
3632 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3633 int rc = 0;
3634 int bytes_returned;
3635 int name_len;
3636 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003637
Joe Perchesf96637b2013-05-04 22:12:25 -05003638 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640queryAclRetry:
3641 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3642 (void **) &pSMBr);
3643 if (rc)
3644 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3647 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003648 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3649 searchName, PATH_MAX, nls_codepage,
3650 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 name_len++; /* trailing null */
3652 name_len *= 2;
3653 pSMB->FileName[name_len] = 0;
3654 pSMB->FileName[name_len+1] = 0;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003655 } else {
3656 name_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
3658
3659 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3660 pSMB->TotalDataCount = 0;
3661 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003662 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 pSMB->MaxDataCount = cpu_to_le16(4000);
3664 pSMB->MaxSetupCount = 0;
3665 pSMB->Reserved = 0;
3666 pSMB->Flags = 0;
3667 pSMB->Timeout = 0;
3668 pSMB->Reserved2 = 0;
3669 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003670 offsetof(struct smb_com_transaction2_qpi_req,
3671 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 pSMB->DataCount = 0;
3673 pSMB->DataOffset = 0;
3674 pSMB->SetupCount = 1;
3675 pSMB->Reserved3 = 0;
3676 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3677 byte_count = params + 1 /* pad */ ;
3678 pSMB->TotalParameterCount = cpu_to_le16(params);
3679 pSMB->ParameterCount = pSMB->TotalParameterCount;
3680 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3681 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003682 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 pSMB->ByteCount = cpu_to_le16(byte_count);
3684
3685 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3686 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003687 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003689 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 } else {
3691 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003695 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 rc = -EIO; /* bad smb */
3697 else {
3698 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3699 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3700 rc = cifs_copy_posix_acl(acl_inf,
3701 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003702 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
3704 }
3705 cifs_buf_release(pSMB);
3706 if (rc == -EAGAIN)
3707 goto queryAclRetry;
3708 return rc;
3709}
3710
3711int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003712CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003713 const unsigned char *fileName,
3714 const char *local_acl, const int buflen,
3715 const int acl_type,
3716 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
3718 struct smb_com_transaction2_spi_req *pSMB = NULL;
3719 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3720 char *parm_data;
3721 int name_len;
3722 int rc = 0;
3723 int bytes_returned = 0;
3724 __u16 params, byte_count, data_count, param_offset, offset;
3725
Joe Perchesf96637b2013-05-04 22:12:25 -05003726 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727setAclRetry:
3728 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003729 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 if (rc)
3731 return rc;
3732 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3733 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003734 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3735 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 name_len++; /* trailing null */
3737 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003738 } else {
3739 name_len = copy_path_name(pSMB->FileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741 params = 6 + name_len;
3742 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003743 /* BB find max SMB size from sess */
3744 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 pSMB->MaxSetupCount = 0;
3746 pSMB->Reserved = 0;
3747 pSMB->Flags = 0;
3748 pSMB->Timeout = 0;
3749 pSMB->Reserved2 = 0;
3750 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003751 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 offset = param_offset + params;
3753 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3754 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3755
3756 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003757 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Steve French790fe572007-07-07 19:25:05 +00003759 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 rc = -EOPNOTSUPP;
3761 goto setACLerrorExit;
3762 }
3763 pSMB->DataOffset = cpu_to_le16(offset);
3764 pSMB->SetupCount = 1;
3765 pSMB->Reserved3 = 0;
3766 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3767 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3768 byte_count = 3 /* pad */ + params + data_count;
3769 pSMB->DataCount = cpu_to_le16(data_count);
3770 pSMB->TotalDataCount = pSMB->DataCount;
3771 pSMB->ParameterCount = cpu_to_le16(params);
3772 pSMB->TotalParameterCount = pSMB->ParameterCount;
3773 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003774 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 pSMB->ByteCount = cpu_to_le16(byte_count);
3776 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003777 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003778 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003779 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781setACLerrorExit:
3782 cifs_buf_release(pSMB);
3783 if (rc == -EAGAIN)
3784 goto setAclRetry;
3785 return rc;
3786}
3787
Steve Frenchf654bac2005-04-28 22:41:04 -07003788/* BB fix tabs in this function FIXME BB */
3789int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003790CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003791 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003792{
Steve French50c2f752007-07-13 00:33:32 +00003793 int rc = 0;
3794 struct smb_t2_qfi_req *pSMB = NULL;
3795 struct smb_t2_qfi_rsp *pSMBr = NULL;
3796 int bytes_returned;
3797 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003798
Joe Perchesf96637b2013-05-04 22:12:25 -05003799 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003800 if (tcon == NULL)
3801 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003802
3803GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003804 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3805 (void **) &pSMBr);
3806 if (rc)
3807 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003808
Steve Frenchad7a2922008-02-07 23:25:02 +00003809 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003810 pSMB->t2.TotalDataCount = 0;
3811 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3812 /* BB find exact max data count below from sess structure BB */
3813 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3814 pSMB->t2.MaxSetupCount = 0;
3815 pSMB->t2.Reserved = 0;
3816 pSMB->t2.Flags = 0;
3817 pSMB->t2.Timeout = 0;
3818 pSMB->t2.Reserved2 = 0;
3819 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3820 Fid) - 4);
3821 pSMB->t2.DataCount = 0;
3822 pSMB->t2.DataOffset = 0;
3823 pSMB->t2.SetupCount = 1;
3824 pSMB->t2.Reserved3 = 0;
3825 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3826 byte_count = params + 1 /* pad */ ;
3827 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3828 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3829 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3830 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003831 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003832 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003833 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003834
Steve French790fe572007-07-07 19:25:05 +00003835 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3836 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3837 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003838 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003839 } else {
3840 /* decode response */
3841 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003842 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003843 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003844 /* If rc should we check for EOPNOSUPP and
3845 disable the srvino flag? or in caller? */
3846 rc = -EIO; /* bad smb */
3847 else {
3848 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3849 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3850 struct file_chattr_info *pfinfo;
3851 /* BB Do we need a cast or hash here ? */
3852 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003853 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003854 rc = -EIO;
3855 goto GetExtAttrOut;
3856 }
3857 pfinfo = (struct file_chattr_info *)
3858 (data_offset + (char *) &pSMBr->hdr.Protocol);
3859 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003860 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003861 }
3862 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003863GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003864 cifs_buf_release(pSMB);
3865 if (rc == -EAGAIN)
3866 goto GetExtAttrRetry;
3867 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003868}
3869
Steve Frenchf654bac2005-04-28 22:41:04 -07003870#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Jeff Layton79df1ba2010-12-06 12:52:08 -05003872/*
3873 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3874 * all NT TRANSACTS that we init here have total parm and data under about 400
3875 * bytes (to fit in small cifs buffer size), which is the case so far, it
3876 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3877 * returned setup area) and MaxParameterCount (returned parms size) must be set
3878 * by caller
3879 */
3880static int
3881smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003882 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003883 void **ret_buf)
3884{
3885 int rc;
3886 __u32 temp_offset;
3887 struct smb_com_ntransact_req *pSMB;
3888
3889 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3890 (void **)&pSMB);
3891 if (rc)
3892 return rc;
3893 *ret_buf = (void *)pSMB;
3894 pSMB->Reserved = 0;
3895 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3896 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003897 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003898 pSMB->ParameterCount = pSMB->TotalParameterCount;
3899 pSMB->DataCount = pSMB->TotalDataCount;
3900 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3901 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3902 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3903 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3904 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3905 pSMB->SubCommand = cpu_to_le16(sub_command);
3906 return 0;
3907}
3908
3909static int
3910validate_ntransact(char *buf, char **ppparm, char **ppdata,
3911 __u32 *pparmlen, __u32 *pdatalen)
3912{
3913 char *end_of_smb;
3914 __u32 data_count, data_offset, parm_count, parm_offset;
3915 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003916 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003917
3918 *pdatalen = 0;
3919 *pparmlen = 0;
3920
3921 if (buf == NULL)
3922 return -EINVAL;
3923
3924 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3925
Jeff Layton820a8032011-05-04 08:05:26 -04003926 bcc = get_bcc(&pSMBr->hdr);
3927 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003928 (char *)&pSMBr->ByteCount;
3929
3930 data_offset = le32_to_cpu(pSMBr->DataOffset);
3931 data_count = le32_to_cpu(pSMBr->DataCount);
3932 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3933 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3934
3935 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3936 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3937
3938 /* should we also check that parm and data areas do not overlap? */
3939 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003940 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003941 return -EINVAL;
3942 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003943 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003944 return -EINVAL;
3945 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003946 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003947 return -EINVAL;
3948 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003949 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3950 *ppdata, data_count, (data_count + *ppdata),
3951 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003952 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003953 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003954 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003955 return -EINVAL;
3956 }
3957 *pdatalen = data_count;
3958 *pparmlen = parm_count;
3959 return 0;
3960}
3961
Steve French0a4b92c2006-01-12 15:44:21 -08003962/* Get Security Descriptor (by handle) from remote server for a file or dir */
3963int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003964CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003965 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003966{
3967 int rc = 0;
3968 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003969 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003970 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003971 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003972
Joe Perchesf96637b2013-05-04 22:12:25 -05003973 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003974
Steve French630f3f0c2007-10-25 21:17:17 +00003975 *pbuflen = 0;
3976 *acl_inf = NULL;
3977
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003978 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003979 8 /* parm len */, tcon, (void **) &pSMB);
3980 if (rc)
3981 return rc;
3982
3983 pSMB->MaxParameterCount = cpu_to_le32(4);
3984 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3985 pSMB->MaxSetupCount = 0;
3986 pSMB->Fid = fid; /* file handle always le */
3987 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3988 CIFS_ACL_DACL);
3989 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003990 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003991 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003992 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003993
Steve Frencha761ac52007-10-18 21:45:27 +00003994 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003995 0, &rsp_iov);
3996 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003997 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003998 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003999 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08004000 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00004001 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00004002 __u32 parm_len;
4003 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00004004 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00004005 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08004006
4007/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004008 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00004009 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00004010 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08004011 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004012 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08004013
Joe Perchesf96637b2013-05-04 22:12:25 -05004014 cifs_dbg(FYI, "smb %p parm %p data %p\n",
4015 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08004016
4017 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
4018 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00004019 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08004020 goto qsec_out;
4021 }
4022
4023/* BB check that data area is minimum length and as big as acl_len */
4024
Steve Frenchaf6f4612007-10-16 18:40:37 +00004025 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00004026 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004027 cifs_dbg(VFS, "acl length %d does not match %d\n",
4028 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004029 if (*pbuflen > acl_len)
4030 *pbuflen = acl_len;
4031 }
Steve French0a4b92c2006-01-12 15:44:21 -08004032
Steve French630f3f0c2007-10-25 21:17:17 +00004033 /* check if buffer is big enough for the acl
4034 header followed by the smallest SID */
4035 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
4036 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004037 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004038 rc = -EINVAL;
4039 *pbuflen = 0;
4040 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02004041 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00004042 if (*acl_inf == NULL) {
4043 *pbuflen = 0;
4044 rc = -ENOMEM;
4045 }
Steve French630f3f0c2007-10-25 21:17:17 +00004046 }
Steve French0a4b92c2006-01-12 15:44:21 -08004047 }
4048qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004049 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08004050 return rc;
4051}
Steve French97837582007-12-31 07:47:21 +00004052
4053int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004054CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004055 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00004056{
4057 __u16 byte_count, param_count, data_count, param_offset, data_offset;
4058 int rc = 0;
4059 int bytes_returned = 0;
4060 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004061 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00004062
4063setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004064 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00004065 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004066 return rc;
Steve French97837582007-12-31 07:47:21 +00004067
4068 pSMB->MaxSetupCount = 0;
4069 pSMB->Reserved = 0;
4070
4071 param_count = 8;
4072 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
4073 data_count = acllen;
4074 data_offset = param_offset + param_count;
4075 byte_count = 3 /* pad */ + param_count;
4076
4077 pSMB->DataCount = cpu_to_le32(data_count);
4078 pSMB->TotalDataCount = pSMB->DataCount;
4079 pSMB->MaxParameterCount = cpu_to_le32(4);
4080 pSMB->MaxDataCount = cpu_to_le32(16384);
4081 pSMB->ParameterCount = cpu_to_le32(param_count);
4082 pSMB->ParameterOffset = cpu_to_le32(param_offset);
4083 pSMB->TotalParameterCount = pSMB->ParameterCount;
4084 pSMB->DataOffset = cpu_to_le32(data_offset);
4085 pSMB->SetupCount = 0;
4086 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
4087 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
4088
4089 pSMB->Fid = fid; /* file handle always le */
4090 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004091 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00004092
4093 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004094 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
4095 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004096 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00004097 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004098 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00004099
4100 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4101 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4102
Joe Perchesf96637b2013-05-04 22:12:25 -05004103 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4104 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00004105 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004106 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00004107 cifs_buf_release(pSMB);
4108
4109 if (rc == -EAGAIN)
4110 goto setCifsAclRetry;
4111
4112 return (rc);
4113}
4114
Steve French0a4b92c2006-01-12 15:44:21 -08004115
Steve French6b8edfe2005-08-23 20:26:03 -07004116/* Legacy Query Path Information call for lookup to old servers such
4117 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004118int
4119SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4120 const char *search_name, FILE_ALL_INFO *data,
4121 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07004122{
Steve Frenchad7a2922008-02-07 23:25:02 +00004123 QUERY_INFORMATION_REQ *pSMB;
4124 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07004125 int rc = 0;
4126 int bytes_returned;
4127 int name_len;
4128
Joe Perchesf96637b2013-05-04 22:12:25 -05004129 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004130QInfRetry:
4131 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004132 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004133 if (rc)
4134 return rc;
4135
4136 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4137 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004138 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004139 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004140 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004141 name_len++; /* trailing null */
4142 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004143 } else {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004144 name_len = copy_path_name(pSMB->FileName, search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004145 }
4146 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004147 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004148 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004149 pSMB->ByteCount = cpu_to_le16(name_len);
4150
4151 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004152 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004153 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004154 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004155 } else if (data) {
Arnd Bergmann95390202018-06-19 17:27:58 +02004156 struct timespec64 ts;
Steve French1bd5bbc2006-09-28 03:35:57 +00004157 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004158
4159 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004160 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004161 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004162 ts.tv_nsec = 0;
4163 ts.tv_sec = time;
4164 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004165 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4166 data->LastWriteTime = data->ChangeTime;
4167 data->LastAccessTime = 0;
4168 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004169 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004170 data->EndOfFile = data->AllocationSize;
4171 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004172 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004173 } else
4174 rc = -EIO; /* bad buffer passed in */
4175
4176 cifs_buf_release(pSMB);
4177
4178 if (rc == -EAGAIN)
4179 goto QInfRetry;
4180
4181 return rc;
4182}
4183
Jeff Laytonbcd53572010-02-12 07:44:16 -05004184int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004185CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004186 u16 netfid, FILE_ALL_INFO *pFindData)
4187{
4188 struct smb_t2_qfi_req *pSMB = NULL;
4189 struct smb_t2_qfi_rsp *pSMBr = NULL;
4190 int rc = 0;
4191 int bytes_returned;
4192 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004193
Jeff Laytonbcd53572010-02-12 07:44:16 -05004194QFileInfoRetry:
4195 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4196 (void **) &pSMBr);
4197 if (rc)
4198 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004199
Jeff Laytonbcd53572010-02-12 07:44:16 -05004200 params = 2 /* level */ + 2 /* fid */;
4201 pSMB->t2.TotalDataCount = 0;
4202 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4203 /* BB find exact max data count below from sess structure BB */
4204 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4205 pSMB->t2.MaxSetupCount = 0;
4206 pSMB->t2.Reserved = 0;
4207 pSMB->t2.Flags = 0;
4208 pSMB->t2.Timeout = 0;
4209 pSMB->t2.Reserved2 = 0;
4210 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4211 Fid) - 4);
4212 pSMB->t2.DataCount = 0;
4213 pSMB->t2.DataOffset = 0;
4214 pSMB->t2.SetupCount = 1;
4215 pSMB->t2.Reserved3 = 0;
4216 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4217 byte_count = params + 1 /* pad */ ;
4218 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4219 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4220 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4221 pSMB->Pad = 0;
4222 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004223 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004224 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004225
4226 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4227 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4228 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004229 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004230 } else { /* decode response */
4231 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4232
4233 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4234 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004235 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004236 rc = -EIO; /* bad smb */
4237 else if (pFindData) {
4238 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4239 memcpy((char *) pFindData,
4240 (char *) &pSMBr->hdr.Protocol +
4241 data_offset, sizeof(FILE_ALL_INFO));
4242 } else
4243 rc = -ENOMEM;
4244 }
4245 cifs_buf_release(pSMB);
4246 if (rc == -EAGAIN)
4247 goto QFileInfoRetry;
4248
4249 return rc;
4250}
Steve French6b8edfe2005-08-23 20:26:03 -07004251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004253CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004254 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004255 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004256 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004258 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 TRANSACTION2_QPI_REQ *pSMB = NULL;
4260 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4261 int rc = 0;
4262 int bytes_returned;
4263 int name_len;
4264 __u16 params, byte_count;
4265
Joe Perchesf96637b2013-05-04 22:12:25 -05004266 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267QPathInfoRetry:
4268 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4269 (void **) &pSMBr);
4270 if (rc)
4271 return rc;
4272
4273 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4274 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004275 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004276 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 name_len++; /* trailing null */
4278 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004279 } else {
4280 name_len = copy_path_name(pSMB->FileName, search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
4282
Steve French50c2f752007-07-13 00:33:32 +00004283 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 pSMB->TotalDataCount = 0;
4285 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004286 /* BB find exact max SMB PDU from sess structure BB */
4287 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 pSMB->MaxSetupCount = 0;
4289 pSMB->Reserved = 0;
4290 pSMB->Flags = 0;
4291 pSMB->Timeout = 0;
4292 pSMB->Reserved2 = 0;
4293 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004294 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 pSMB->DataCount = 0;
4296 pSMB->DataOffset = 0;
4297 pSMB->SetupCount = 1;
4298 pSMB->Reserved3 = 0;
4299 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4300 byte_count = params + 1 /* pad */ ;
4301 pSMB->TotalParameterCount = cpu_to_le16(params);
4302 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004303 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004304 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4305 else
4306 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004308 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 pSMB->ByteCount = cpu_to_le16(byte_count);
4310
4311 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4312 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4313 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004314 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 } else { /* decode response */
4316 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4317
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004318 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4319 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004320 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004322 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004323 rc = -EIO; /* 24 or 26 expected but we do not read
4324 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004325 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004326 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004328
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004329 /*
4330 * On legacy responses we do not read the last field,
4331 * EAsize, fortunately since it varies by subdialect and
4332 * also note it differs on Set vs Get, ie two bytes or 4
4333 * bytes depending but we don't care here.
4334 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004335 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004336 size = sizeof(FILE_INFO_STANDARD);
4337 else
4338 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004339 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004340 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 } else
4342 rc = -ENOMEM;
4343 }
4344 cifs_buf_release(pSMB);
4345 if (rc == -EAGAIN)
4346 goto QPathInfoRetry;
4347
4348 return rc;
4349}
4350
4351int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004352CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004353 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4354{
4355 struct smb_t2_qfi_req *pSMB = NULL;
4356 struct smb_t2_qfi_rsp *pSMBr = NULL;
4357 int rc = 0;
4358 int bytes_returned;
4359 __u16 params, byte_count;
4360
4361UnixQFileInfoRetry:
4362 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4363 (void **) &pSMBr);
4364 if (rc)
4365 return rc;
4366
4367 params = 2 /* level */ + 2 /* fid */;
4368 pSMB->t2.TotalDataCount = 0;
4369 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4370 /* BB find exact max data count below from sess structure BB */
4371 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4372 pSMB->t2.MaxSetupCount = 0;
4373 pSMB->t2.Reserved = 0;
4374 pSMB->t2.Flags = 0;
4375 pSMB->t2.Timeout = 0;
4376 pSMB->t2.Reserved2 = 0;
4377 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4378 Fid) - 4);
4379 pSMB->t2.DataCount = 0;
4380 pSMB->t2.DataOffset = 0;
4381 pSMB->t2.SetupCount = 1;
4382 pSMB->t2.Reserved3 = 0;
4383 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4384 byte_count = params + 1 /* pad */ ;
4385 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4386 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4387 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4388 pSMB->Pad = 0;
4389 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004390 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004391 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004392
4393 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4394 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4395 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004396 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004397 } else { /* decode response */
4398 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4399
Jeff Layton820a8032011-05-04 08:05:26 -04004400 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004401 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 -05004402 rc = -EIO; /* bad smb */
4403 } else {
4404 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4405 memcpy((char *) pFindData,
4406 (char *) &pSMBr->hdr.Protocol +
4407 data_offset,
4408 sizeof(FILE_UNIX_BASIC_INFO));
4409 }
4410 }
4411
4412 cifs_buf_release(pSMB);
4413 if (rc == -EAGAIN)
4414 goto UnixQFileInfoRetry;
4415
4416 return rc;
4417}
4418
4419int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004420CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004422 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004423 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
4425/* SMB_QUERY_FILE_UNIX_BASIC */
4426 TRANSACTION2_QPI_REQ *pSMB = NULL;
4427 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4428 int rc = 0;
4429 int bytes_returned = 0;
4430 int name_len;
4431 __u16 params, byte_count;
4432
Joe Perchesf96637b2013-05-04 22:12:25 -05004433 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434UnixQPathInfoRetry:
4435 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4436 (void **) &pSMBr);
4437 if (rc)
4438 return rc;
4439
4440 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4441 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004442 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4443 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 name_len++; /* trailing null */
4445 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004446 } else {
4447 name_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 }
4449
Steve French50c2f752007-07-13 00:33:32 +00004450 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 pSMB->TotalDataCount = 0;
4452 pSMB->MaxParameterCount = cpu_to_le16(2);
4453 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004454 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 pSMB->MaxSetupCount = 0;
4456 pSMB->Reserved = 0;
4457 pSMB->Flags = 0;
4458 pSMB->Timeout = 0;
4459 pSMB->Reserved2 = 0;
4460 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004461 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 pSMB->DataCount = 0;
4463 pSMB->DataOffset = 0;
4464 pSMB->SetupCount = 1;
4465 pSMB->Reserved3 = 0;
4466 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4467 byte_count = params + 1 /* pad */ ;
4468 pSMB->TotalParameterCount = cpu_to_le16(params);
4469 pSMB->ParameterCount = pSMB->TotalParameterCount;
4470 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4471 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004472 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 pSMB->ByteCount = cpu_to_le16(byte_count);
4474
4475 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4476 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4477 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004478 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 } else { /* decode response */
4480 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4481
Jeff Layton820a8032011-05-04 08:05:26 -04004482 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004483 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 -07004484 rc = -EIO; /* bad smb */
4485 } else {
4486 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4487 memcpy((char *) pFindData,
4488 (char *) &pSMBr->hdr.Protocol +
4489 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004490 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
4492 }
4493 cifs_buf_release(pSMB);
4494 if (rc == -EAGAIN)
4495 goto UnixQPathInfoRetry;
4496
4497 return rc;
4498}
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500/* xid, tcon, searchName and codepage are input parms, rest are returned */
4501int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004502CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004503 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004504 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004505 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
4507/* level 257 SMB_ */
4508 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4509 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004510 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 int rc = 0;
4512 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004513 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004515 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Joe Perchesf96637b2013-05-04 22:12:25 -05004517 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519findFirstRetry:
4520 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4521 (void **) &pSMBr);
4522 if (rc)
4523 return rc;
4524
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004525 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004526 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4529 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004530 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4531 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004532 /* We can not add the asterik earlier in case
4533 it got remapped to 0xF03A as if it were part of the
4534 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004536 if (msearch) {
4537 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4538 pSMB->FileName[name_len+1] = 0;
4539 pSMB->FileName[name_len+2] = '*';
4540 pSMB->FileName[name_len+3] = 0;
4541 name_len += 4; /* now the trailing null */
4542 /* null terminate just in case */
4543 pSMB->FileName[name_len] = 0;
4544 pSMB->FileName[name_len+1] = 0;
4545 name_len += 2;
4546 }
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004547 } else {
4548 name_len = copy_path_name(pSMB->FileName, searchName);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004549 if (msearch) {
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004550 if (WARN_ON_ONCE(name_len > PATH_MAX-2))
4551 name_len = PATH_MAX-2;
4552 /* overwrite nul byte */
4553 pSMB->FileName[name_len-1] = CIFS_DIR_SEP(cifs_sb);
4554 pSMB->FileName[name_len] = '*';
4555 pSMB->FileName[name_len+1] = 0;
4556 name_len += 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 }
4559
4560 params = 12 + name_len /* includes null */ ;
4561 pSMB->TotalDataCount = 0; /* no EAs */
4562 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004563 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 pSMB->MaxSetupCount = 0;
4565 pSMB->Reserved = 0;
4566 pSMB->Flags = 0;
4567 pSMB->Timeout = 0;
4568 pSMB->Reserved2 = 0;
4569 byte_count = params + 1 /* pad */ ;
4570 pSMB->TotalParameterCount = cpu_to_le16(params);
4571 pSMB->ParameterCount = pSMB->TotalParameterCount;
4572 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004573 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4574 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 pSMB->DataCount = 0;
4576 pSMB->DataOffset = 0;
4577 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4578 pSMB->Reserved3 = 0;
4579 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4580 pSMB->SearchAttributes =
4581 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4582 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004583 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004584 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4586
4587 /* BB what should we set StorageType to? Does it matter? BB */
4588 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004589 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 pSMB->ByteCount = cpu_to_le16(byte_count);
4591
4592 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4593 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004594 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Steve French88274812006-03-09 22:21:45 +00004596 if (rc) {/* BB add logic to retry regular search if Unix search
4597 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004599 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004600
Steve French88274812006-03-09 22:21:45 +00004601 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
4603 /* BB eventually could optimize out free and realloc of buf */
4604 /* for this case */
4605 if (rc == -EAGAIN)
4606 goto findFirstRetry;
4607 } else { /* decode response */
4608 /* BB remember to free buffer if error BB */
4609 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004610 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004611 unsigned int lnoff;
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004614 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 else
Steve French4b18f2a2008-04-29 00:06:05 +00004616 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004619 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004620 psrch_inf->srch_entries_start =
4621 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4624 le16_to_cpu(pSMBr->t2.ParameterOffset));
4625
Steve French790fe572007-07-07 19:25:05 +00004626 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004627 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 else
Steve French4b18f2a2008-04-29 00:06:05 +00004629 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Steve French50c2f752007-07-13 00:33:32 +00004631 psrch_inf->entries_in_buffer =
4632 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004633 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004635 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004636 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004637 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004638 psrch_inf->last_entry = NULL;
4639 return rc;
4640 }
4641
Steve French0752f152008-10-07 20:03:33 +00004642 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004643 lnoff;
4644
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004645 if (pnetfid)
4646 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 } else {
4648 cifs_buf_release(pSMB);
4649 }
4650 }
4651
4652 return rc;
4653}
4654
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004655int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4656 __u16 searchHandle, __u16 search_flags,
4657 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
4659 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4660 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004661 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 char *response_data;
4663 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004664 int bytes_returned;
4665 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 __u16 params, byte_count;
4667
Joe Perchesf96637b2013-05-04 22:12:25 -05004668 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Steve French4b18f2a2008-04-29 00:06:05 +00004670 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 return -ENOENT;
4672
4673 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4674 (void **) &pSMBr);
4675 if (rc)
4676 return rc;
4677
Steve French50c2f752007-07-13 00:33:32 +00004678 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 byte_count = 0;
4680 pSMB->TotalDataCount = 0; /* no EAs */
4681 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004682 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 pSMB->MaxSetupCount = 0;
4684 pSMB->Reserved = 0;
4685 pSMB->Flags = 0;
4686 pSMB->Timeout = 0;
4687 pSMB->Reserved2 = 0;
4688 pSMB->ParameterOffset = cpu_to_le16(
4689 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4690 pSMB->DataCount = 0;
4691 pSMB->DataOffset = 0;
4692 pSMB->SetupCount = 1;
4693 pSMB->Reserved3 = 0;
4694 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4695 pSMB->SearchHandle = searchHandle; /* always kept as le */
4696 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004697 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4699 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004700 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 name_len = psrch_inf->resume_name_len;
4703 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004704 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4706 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004707 /* 14 byte parm len above enough for 2 byte null terminator */
4708 pSMB->ResumeFileName[name_len] = 0;
4709 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 } else {
4711 rc = -EINVAL;
4712 goto FNext2_err_exit;
4713 }
4714 byte_count = params + 1 /* pad */ ;
4715 pSMB->TotalParameterCount = cpu_to_le16(params);
4716 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004717 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4721 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004722 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 if (rc) {
4724 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004725 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004726 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004727 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004729 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 } else { /* decode response */
4731 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004732
Steve French790fe572007-07-07 19:25:05 +00004733 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004734 unsigned int lnoff;
4735
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 /* BB fixme add lock for file (srch_info) struct here */
4737 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004738 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 else
Steve French4b18f2a2008-04-29 00:06:05 +00004740 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 response_data = (char *) &pSMBr->hdr.Protocol +
4742 le16_to_cpu(pSMBr->t2.ParameterOffset);
4743 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4744 response_data = (char *)&pSMBr->hdr.Protocol +
4745 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004746 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004747 cifs_small_buf_release(
4748 psrch_inf->ntwrk_buf_start);
4749 else
4750 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 psrch_inf->srch_entries_start = response_data;
4752 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004753 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004754 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004755 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 else
Steve French4b18f2a2008-04-29 00:06:05 +00004757 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004758 psrch_inf->entries_in_buffer =
4759 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 psrch_inf->index_of_last_entry +=
4761 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004762 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004763 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004764 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004765 psrch_inf->last_entry = NULL;
4766 return rc;
4767 } else
4768 psrch_inf->last_entry =
4769 psrch_inf->srch_entries_start + lnoff;
4770
Joe Perchesf96637b2013-05-04 22:12:25 -05004771/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4772 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 /* BB fixme add unlock here */
4775 }
4776
4777 }
4778
4779 /* BB On error, should we leave previous search buf (and count and
4780 last entry fields) intact or free the previous one? */
4781
4782 /* Note: On -EAGAIN error only caller can retry on handle based calls
4783 since file handle passed in no longer valid */
4784FNext2_err_exit:
4785 if (rc != 0)
4786 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 return rc;
4788}
4789
4790int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004791CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004792 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
4794 int rc = 0;
4795 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Joe Perchesf96637b2013-05-04 22:12:25 -05004797 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4799
4800 /* no sense returning error if session restarted
4801 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004802 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 return 0;
4804 if (rc)
4805 return rc;
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 pSMB->FileID = searchHandle;
4808 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004809 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004810 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004811 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004812 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004813
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004814 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 /* Since session is dead, search handle closed on server already */
4817 if (rc == -EAGAIN)
4818 rc = 0;
4819
4820 return rc;
4821}
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004824CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004825 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004826 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
4828 int rc = 0;
4829 TRANSACTION2_QPI_REQ *pSMB = NULL;
4830 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4831 int name_len, bytes_returned;
4832 __u16 params, byte_count;
4833
Joe Perchesf96637b2013-05-04 22:12:25 -05004834 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004835 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004836 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838GetInodeNumberRetry:
4839 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004840 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 if (rc)
4842 return rc;
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4845 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004846 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004847 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004848 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 name_len++; /* trailing null */
4850 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004851 } else {
4852 name_len = copy_path_name(pSMB->FileName, search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 }
4854
4855 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4856 pSMB->TotalDataCount = 0;
4857 pSMB->MaxParameterCount = cpu_to_le16(2);
4858 /* BB find exact max data count below from sess structure BB */
4859 pSMB->MaxDataCount = cpu_to_le16(4000);
4860 pSMB->MaxSetupCount = 0;
4861 pSMB->Reserved = 0;
4862 pSMB->Flags = 0;
4863 pSMB->Timeout = 0;
4864 pSMB->Reserved2 = 0;
4865 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004866 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 pSMB->DataCount = 0;
4868 pSMB->DataOffset = 0;
4869 pSMB->SetupCount = 1;
4870 pSMB->Reserved3 = 0;
4871 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4872 byte_count = params + 1 /* pad */ ;
4873 pSMB->TotalParameterCount = cpu_to_le16(params);
4874 pSMB->ParameterCount = pSMB->TotalParameterCount;
4875 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4876 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004877 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 pSMB->ByteCount = cpu_to_le16(byte_count);
4879
4880 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4881 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4882 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004883 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 } else {
4885 /* decode response */
4886 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004888 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 /* If rc should we check for EOPNOSUPP and
4890 disable the srvino flag? or in caller? */
4891 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004892 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4894 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004895 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004897 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004898 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 rc = -EIO;
4900 goto GetInodeNumOut;
4901 }
4902 pfinfo = (struct file_internal_info *)
4903 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004904 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 }
4906 }
4907GetInodeNumOut:
4908 cifs_buf_release(pSMB);
4909 if (rc == -EAGAIN)
4910 goto GetInodeNumberRetry;
4911 return rc;
4912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
4914int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004915CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004916 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004917 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004918 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919{
4920/* TRANS2_GET_DFS_REFERRAL */
4921 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4922 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 int rc = 0;
4924 int bytes_returned;
4925 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004927 *num_of_nodes = 0;
4928 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Joe Perchesf96637b2013-05-04 22:12:25 -05004930 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004931 if (ses == NULL || ses->tcon_ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 return -ENODEV;
Aurelien Aptelb327a712018-01-24 13:46:10 +01004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934getDFSRetry:
Aurelien Aptelb327a712018-01-24 13:46:10 +01004935 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 (void **) &pSMBr);
4937 if (rc)
4938 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004939
4940 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004941 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004942 pSMB->hdr.Mid = get_next_mid(ses->server);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004943 pSMB->hdr.Tid = ses->tcon_ipc->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004945 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004947 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 if (ses->capabilities & CAP_UNICODE) {
4951 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4952 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004953 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004954 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004955 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 name_len++; /* trailing null */
4957 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004958 } else { /* BB improve the check for buffer overruns BB */
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004959 name_len = copy_path_name(pSMB->RequestFileName, search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961
Dan Carpenter65c3b202015-04-30 17:30:24 +03004962 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004963 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004964
Steve French50c2f752007-07-13 00:33:32 +00004965 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 params = 2 /* level */ + name_len /*includes null */ ;
4968 pSMB->TotalDataCount = 0;
4969 pSMB->DataCount = 0;
4970 pSMB->DataOffset = 0;
4971 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004972 /* BB find exact max SMB PDU from sess structure BB */
4973 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 pSMB->MaxSetupCount = 0;
4975 pSMB->Reserved = 0;
4976 pSMB->Flags = 0;
4977 pSMB->Timeout = 0;
4978 pSMB->Reserved2 = 0;
4979 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004980 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 pSMB->SetupCount = 1;
4982 pSMB->Reserved3 = 0;
4983 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4984 byte_count = params + 3 /* pad */ ;
4985 pSMB->ParameterCount = cpu_to_le16(params);
4986 pSMB->TotalParameterCount = pSMB->ParameterCount;
4987 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004988 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 pSMB->ByteCount = cpu_to_le16(byte_count);
4990
4991 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4992 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4993 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004994 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004995 goto GetDFSRefExit;
4996 }
4997 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004999 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04005000 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005001 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04005002 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 }
Igor Mammedovfec45852008-05-16 13:06:30 +04005004
Joe Perchesf96637b2013-05-04 22:12:25 -05005005 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5006 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04005007
5008 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01005009 rc = parse_dfs_referrals(&pSMBr->dfs_data,
5010 le16_to_cpu(pSMBr->t2.DataCount),
5011 num_of_nodes, target_nodes, nls_codepage,
5012 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06005013 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04005014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00005016 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 if (rc == -EAGAIN)
5019 goto getDFSRetry;
5020
5021 return rc;
5022}
5023
Steve French20962432005-09-21 22:05:57 -07005024/* Query File System Info such as free space to old servers such as Win 9x */
5025int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005026SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5027 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005028{
5029/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5030 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5031 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5032 FILE_SYSTEM_ALLOC_INFO *response_data;
5033 int rc = 0;
5034 int bytes_returned = 0;
5035 __u16 params, byte_count;
5036
Joe Perchesf96637b2013-05-04 22:12:25 -05005037 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005038oldQFSInfoRetry:
5039 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5040 (void **) &pSMBr);
5041 if (rc)
5042 return rc;
Steve French20962432005-09-21 22:05:57 -07005043
5044 params = 2; /* level */
5045 pSMB->TotalDataCount = 0;
5046 pSMB->MaxParameterCount = cpu_to_le16(2);
5047 pSMB->MaxDataCount = cpu_to_le16(1000);
5048 pSMB->MaxSetupCount = 0;
5049 pSMB->Reserved = 0;
5050 pSMB->Flags = 0;
5051 pSMB->Timeout = 0;
5052 pSMB->Reserved2 = 0;
5053 byte_count = params + 1 /* pad */ ;
5054 pSMB->TotalParameterCount = cpu_to_le16(params);
5055 pSMB->ParameterCount = pSMB->TotalParameterCount;
5056 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5057 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5058 pSMB->DataCount = 0;
5059 pSMB->DataOffset = 0;
5060 pSMB->SetupCount = 1;
5061 pSMB->Reserved3 = 0;
5062 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5063 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005064 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005065 pSMB->ByteCount = cpu_to_le16(byte_count);
5066
5067 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5068 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5069 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005070 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005071 } else { /* decode response */
5072 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5073
Jeff Layton820a8032011-05-04 08:05:26 -04005074 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005075 rc = -EIO; /* bad smb */
5076 else {
5077 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005078 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005079 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005080
Steve French50c2f752007-07-13 00:33:32 +00005081 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005082 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5083 FSData->f_bsize =
5084 le16_to_cpu(response_data->BytesPerSector) *
5085 le32_to_cpu(response_data->
5086 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005087 /*
5088 * much prefer larger but if server doesn't report
5089 * a valid size than 4K is a reasonable minimum
5090 */
5091 if (FSData->f_bsize < 512)
5092 FSData->f_bsize = 4096;
5093
Steve French20962432005-09-21 22:05:57 -07005094 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005095 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005096 FSData->f_bfree = FSData->f_bavail =
5097 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005098 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5099 (unsigned long long)FSData->f_blocks,
5100 (unsigned long long)FSData->f_bfree,
5101 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005102 }
5103 }
5104 cifs_buf_release(pSMB);
5105
5106 if (rc == -EAGAIN)
5107 goto oldQFSInfoRetry;
5108
5109 return rc;
5110}
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005113CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5114 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115{
5116/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5117 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5118 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5119 FILE_SYSTEM_INFO *response_data;
5120 int rc = 0;
5121 int bytes_returned = 0;
5122 __u16 params, byte_count;
5123
Joe Perchesf96637b2013-05-04 22:12:25 -05005124 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125QFSInfoRetry:
5126 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5127 (void **) &pSMBr);
5128 if (rc)
5129 return rc;
5130
5131 params = 2; /* level */
5132 pSMB->TotalDataCount = 0;
5133 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005134 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 pSMB->MaxSetupCount = 0;
5136 pSMB->Reserved = 0;
5137 pSMB->Flags = 0;
5138 pSMB->Timeout = 0;
5139 pSMB->Reserved2 = 0;
5140 byte_count = params + 1 /* pad */ ;
5141 pSMB->TotalParameterCount = cpu_to_le16(params);
5142 pSMB->ParameterCount = pSMB->TotalParameterCount;
5143 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005144 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 pSMB->DataCount = 0;
5146 pSMB->DataOffset = 0;
5147 pSMB->SetupCount = 1;
5148 pSMB->Reserved3 = 0;
5149 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5150 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005151 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 pSMB->ByteCount = cpu_to_le16(byte_count);
5153
5154 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5155 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5156 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005157 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005159 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Jeff Layton820a8032011-05-04 08:05:26 -04005161 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 rc = -EIO; /* bad smb */
5163 else {
5164 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 response_data =
5167 (FILE_SYSTEM_INFO
5168 *) (((char *) &pSMBr->hdr.Protocol) +
5169 data_offset);
5170 FSData->f_bsize =
5171 le32_to_cpu(response_data->BytesPerSector) *
5172 le32_to_cpu(response_data->
5173 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005174 /*
5175 * much prefer larger but if server doesn't report
5176 * a valid size than 4K is a reasonable minimum
5177 */
5178 if (FSData->f_bsize < 512)
5179 FSData->f_bsize = 4096;
5180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 FSData->f_blocks =
5182 le64_to_cpu(response_data->TotalAllocationUnits);
5183 FSData->f_bfree = FSData->f_bavail =
5184 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005185 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5186 (unsigned long long)FSData->f_blocks,
5187 (unsigned long long)FSData->f_bfree,
5188 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 }
5190 }
5191 cifs_buf_release(pSMB);
5192
5193 if (rc == -EAGAIN)
5194 goto QFSInfoRetry;
5195
5196 return rc;
5197}
5198
5199int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005200CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201{
5202/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5203 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5204 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5205 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5206 int rc = 0;
5207 int bytes_returned = 0;
5208 __u16 params, byte_count;
5209
Joe Perchesf96637b2013-05-04 22:12:25 -05005210 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211QFSAttributeRetry:
5212 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5213 (void **) &pSMBr);
5214 if (rc)
5215 return rc;
5216
5217 params = 2; /* level */
5218 pSMB->TotalDataCount = 0;
5219 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005220 /* BB find exact max SMB PDU from sess structure BB */
5221 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 pSMB->MaxSetupCount = 0;
5223 pSMB->Reserved = 0;
5224 pSMB->Flags = 0;
5225 pSMB->Timeout = 0;
5226 pSMB->Reserved2 = 0;
5227 byte_count = params + 1 /* pad */ ;
5228 pSMB->TotalParameterCount = cpu_to_le16(params);
5229 pSMB->ParameterCount = pSMB->TotalParameterCount;
5230 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005231 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 pSMB->DataCount = 0;
5233 pSMB->DataOffset = 0;
5234 pSMB->SetupCount = 1;
5235 pSMB->Reserved3 = 0;
5236 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5237 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005238 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 pSMB->ByteCount = cpu_to_le16(byte_count);
5240
5241 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5242 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5243 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005244 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 } else { /* decode response */
5246 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5247
Jeff Layton820a8032011-05-04 08:05:26 -04005248 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005249 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 rc = -EIO; /* bad smb */
5251 } else {
5252 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5253 response_data =
5254 (FILE_SYSTEM_ATTRIBUTE_INFO
5255 *) (((char *) &pSMBr->hdr.Protocol) +
5256 data_offset);
5257 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005258 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 }
5260 }
5261 cifs_buf_release(pSMB);
5262
5263 if (rc == -EAGAIN)
5264 goto QFSAttributeRetry;
5265
5266 return rc;
5267}
5268
5269int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005270CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
5272/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5273 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5274 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5275 FILE_SYSTEM_DEVICE_INFO *response_data;
5276 int rc = 0;
5277 int bytes_returned = 0;
5278 __u16 params, byte_count;
5279
Joe Perchesf96637b2013-05-04 22:12:25 -05005280 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281QFSDeviceRetry:
5282 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5283 (void **) &pSMBr);
5284 if (rc)
5285 return rc;
5286
5287 params = 2; /* level */
5288 pSMB->TotalDataCount = 0;
5289 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005290 /* BB find exact max SMB PDU from sess structure BB */
5291 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 pSMB->MaxSetupCount = 0;
5293 pSMB->Reserved = 0;
5294 pSMB->Flags = 0;
5295 pSMB->Timeout = 0;
5296 pSMB->Reserved2 = 0;
5297 byte_count = params + 1 /* pad */ ;
5298 pSMB->TotalParameterCount = cpu_to_le16(params);
5299 pSMB->ParameterCount = pSMB->TotalParameterCount;
5300 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005301 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303 pSMB->DataCount = 0;
5304 pSMB->DataOffset = 0;
5305 pSMB->SetupCount = 1;
5306 pSMB->Reserved3 = 0;
5307 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5308 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005309 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 pSMB->ByteCount = cpu_to_le16(byte_count);
5311
5312 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5313 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5314 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005315 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 } else { /* decode response */
5317 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5318
Jeff Layton820a8032011-05-04 08:05:26 -04005319 if (rc || get_bcc(&pSMBr->hdr) <
5320 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 rc = -EIO; /* bad smb */
5322 else {
5323 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5324 response_data =
Steve French737b7582005-04-28 22:41:06 -07005325 (FILE_SYSTEM_DEVICE_INFO *)
5326 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 data_offset);
5328 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005329 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 }
5331 }
5332 cifs_buf_release(pSMB);
5333
5334 if (rc == -EAGAIN)
5335 goto QFSDeviceRetry;
5336
5337 return rc;
5338}
5339
5340int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005341CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342{
5343/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5344 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5345 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5346 FILE_SYSTEM_UNIX_INFO *response_data;
5347 int rc = 0;
5348 int bytes_returned = 0;
5349 __u16 params, byte_count;
5350
Joe Perchesf96637b2013-05-04 22:12:25 -05005351 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005353 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5354 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 if (rc)
5356 return rc;
5357
5358 params = 2; /* level */
5359 pSMB->TotalDataCount = 0;
5360 pSMB->DataCount = 0;
5361 pSMB->DataOffset = 0;
5362 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005363 /* BB find exact max SMB PDU from sess structure BB */
5364 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 pSMB->MaxSetupCount = 0;
5366 pSMB->Reserved = 0;
5367 pSMB->Flags = 0;
5368 pSMB->Timeout = 0;
5369 pSMB->Reserved2 = 0;
5370 byte_count = params + 1 /* pad */ ;
5371 pSMB->ParameterCount = cpu_to_le16(params);
5372 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005373 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5374 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 pSMB->SetupCount = 1;
5376 pSMB->Reserved3 = 0;
5377 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5378 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005379 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 pSMB->ByteCount = cpu_to_le16(byte_count);
5381
5382 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5383 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5384 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005385 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 } else { /* decode response */
5387 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5388
Jeff Layton820a8032011-05-04 08:05:26 -04005389 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 rc = -EIO; /* bad smb */
5391 } else {
5392 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5393 response_data =
5394 (FILE_SYSTEM_UNIX_INFO
5395 *) (((char *) &pSMBr->hdr.Protocol) +
5396 data_offset);
5397 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005398 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 }
5400 }
5401 cifs_buf_release(pSMB);
5402
5403 if (rc == -EAGAIN)
5404 goto QFSUnixRetry;
5405
5406
5407 return rc;
5408}
5409
Jeremy Allisonac670552005-06-22 17:26:35 -07005410int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005411CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005412{
5413/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5414 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5415 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5416 int rc = 0;
5417 int bytes_returned = 0;
5418 __u16 params, param_offset, offset, byte_count;
5419
Joe Perchesf96637b2013-05-04 22:12:25 -05005420 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005421SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005422 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005423 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5424 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005425 if (rc)
5426 return rc;
5427
5428 params = 4; /* 2 bytes zero followed by info level. */
5429 pSMB->MaxSetupCount = 0;
5430 pSMB->Reserved = 0;
5431 pSMB->Flags = 0;
5432 pSMB->Timeout = 0;
5433 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005434 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5435 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005436 offset = param_offset + params;
5437
5438 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005439 /* BB find exact max SMB PDU from sess structure BB */
5440 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005441 pSMB->SetupCount = 1;
5442 pSMB->Reserved3 = 0;
5443 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5444 byte_count = 1 /* pad */ + params + 12;
5445
5446 pSMB->DataCount = cpu_to_le16(12);
5447 pSMB->ParameterCount = cpu_to_le16(params);
5448 pSMB->TotalDataCount = pSMB->DataCount;
5449 pSMB->TotalParameterCount = pSMB->ParameterCount;
5450 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5451 pSMB->DataOffset = cpu_to_le16(offset);
5452
5453 /* Params. */
5454 pSMB->FileNum = 0;
5455 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5456
5457 /* Data. */
5458 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5459 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5460 pSMB->ClientUnixCap = cpu_to_le64(cap);
5461
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005462 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005463 pSMB->ByteCount = cpu_to_le16(byte_count);
5464
5465 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5466 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5467 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005468 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005469 } else { /* decode response */
5470 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005471 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005472 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005473 }
5474 cifs_buf_release(pSMB);
5475
5476 if (rc == -EAGAIN)
5477 goto SETFSUnixRetry;
5478
5479 return rc;
5480}
5481
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005485CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005486 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487{
5488/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5489 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5490 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5491 FILE_SYSTEM_POSIX_INFO *response_data;
5492 int rc = 0;
5493 int bytes_returned = 0;
5494 __u16 params, byte_count;
5495
Joe Perchesf96637b2013-05-04 22:12:25 -05005496 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497QFSPosixRetry:
5498 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5499 (void **) &pSMBr);
5500 if (rc)
5501 return rc;
5502
5503 params = 2; /* level */
5504 pSMB->TotalDataCount = 0;
5505 pSMB->DataCount = 0;
5506 pSMB->DataOffset = 0;
5507 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005508 /* BB find exact max SMB PDU from sess structure BB */
5509 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 pSMB->MaxSetupCount = 0;
5511 pSMB->Reserved = 0;
5512 pSMB->Flags = 0;
5513 pSMB->Timeout = 0;
5514 pSMB->Reserved2 = 0;
5515 byte_count = params + 1 /* pad */ ;
5516 pSMB->ParameterCount = cpu_to_le16(params);
5517 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005518 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5519 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 pSMB->SetupCount = 1;
5521 pSMB->Reserved3 = 0;
5522 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5523 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005524 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 pSMB->ByteCount = cpu_to_le16(byte_count);
5526
5527 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5528 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5529 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005530 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 } else { /* decode response */
5532 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5533
Jeff Layton820a8032011-05-04 08:05:26 -04005534 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 rc = -EIO; /* bad smb */
5536 } else {
5537 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5538 response_data =
5539 (FILE_SYSTEM_POSIX_INFO
5540 *) (((char *) &pSMBr->hdr.Protocol) +
5541 data_offset);
5542 FSData->f_bsize =
5543 le32_to_cpu(response_data->BlockSize);
Steve French5a519be2018-09-15 14:07:09 -05005544 /*
5545 * much prefer larger but if server doesn't report
5546 * a valid size than 4K is a reasonable minimum
5547 */
5548 if (FSData->f_bsize < 512)
5549 FSData->f_bsize = 4096;
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 FSData->f_blocks =
5552 le64_to_cpu(response_data->TotalBlocks);
5553 FSData->f_bfree =
5554 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005555 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 FSData->f_bavail = FSData->f_bfree;
5557 } else {
5558 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005559 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 }
Steve French790fe572007-07-07 19:25:05 +00005561 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005563 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005564 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005566 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
5568 }
5569 cifs_buf_release(pSMB);
5570
5571 if (rc == -EAGAIN)
5572 goto QFSPosixRetry;
5573
5574 return rc;
5575}
5576
5577
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005578/*
5579 * We can not use write of zero bytes trick to set file size due to need for
5580 * large file support. Also note that this SetPathInfo is preferred to
5581 * SetFileInfo based method in next routine which is only needed to work around
5582 * a sharing violation bugin Samba which this routine can run into.
5583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005585CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005586 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5587 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
5589 struct smb_com_transaction2_spi_req *pSMB = NULL;
5590 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5591 struct file_end_of_file_info *parm_data;
5592 int name_len;
5593 int rc = 0;
5594 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005595 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 __u16 params, byte_count, data_count, param_offset, offset;
5598
Joe Perchesf96637b2013-05-04 22:12:25 -05005599 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600SetEOFRetry:
5601 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5602 (void **) &pSMBr);
5603 if (rc)
5604 return rc;
5605
5606 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5607 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005608 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5609 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 name_len++; /* trailing null */
5611 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10005612 } else {
5613 name_len = copy_path_name(pSMB->FileName, file_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
5615 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005616 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005618 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 pSMB->MaxSetupCount = 0;
5620 pSMB->Reserved = 0;
5621 pSMB->Flags = 0;
5622 pSMB->Timeout = 0;
5623 pSMB->Reserved2 = 0;
5624 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005625 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005627 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005628 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5629 pSMB->InformationLevel =
5630 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5631 else
5632 pSMB->InformationLevel =
5633 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5634 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5636 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005637 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 else
5639 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005640 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 }
5642
5643 parm_data =
5644 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5645 offset);
5646 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5647 pSMB->DataOffset = cpu_to_le16(offset);
5648 pSMB->SetupCount = 1;
5649 pSMB->Reserved3 = 0;
5650 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5651 byte_count = 3 /* pad */ + params + data_count;
5652 pSMB->DataCount = cpu_to_le16(data_count);
5653 pSMB->TotalDataCount = pSMB->DataCount;
5654 pSMB->ParameterCount = cpu_to_le16(params);
5655 pSMB->TotalParameterCount = pSMB->ParameterCount;
5656 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005657 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 parm_data->FileSize = cpu_to_le64(size);
5659 pSMB->ByteCount = cpu_to_le16(byte_count);
5660 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5661 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005662 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005663 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
5665 cifs_buf_release(pSMB);
5666
5667 if (rc == -EAGAIN)
5668 goto SetEOFRetry;
5669
5670 return rc;
5671}
5672
5673int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005674CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5675 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
5677 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 struct file_end_of_file_info *parm_data;
5679 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 __u16 params, param_offset, offset, byte_count, count;
5681
Joe Perchesf96637b2013-05-04 22:12:25 -05005682 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5683 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005684 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 if (rc)
5687 return rc;
5688
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005689 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5690 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 params = 6;
5693 pSMB->MaxSetupCount = 0;
5694 pSMB->Reserved = 0;
5695 pSMB->Flags = 0;
5696 pSMB->Timeout = 0;
5697 pSMB->Reserved2 = 0;
5698 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5699 offset = param_offset + params;
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 count = sizeof(struct file_end_of_file_info);
5702 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005703 /* BB find exact max SMB PDU from sess structure BB */
5704 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 pSMB->SetupCount = 1;
5706 pSMB->Reserved3 = 0;
5707 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5708 byte_count = 3 /* pad */ + params + count;
5709 pSMB->DataCount = cpu_to_le16(count);
5710 pSMB->ParameterCount = cpu_to_le16(params);
5711 pSMB->TotalDataCount = pSMB->DataCount;
5712 pSMB->TotalParameterCount = pSMB->ParameterCount;
5713 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5714 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005715 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5716 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 pSMB->DataOffset = cpu_to_le16(offset);
5718 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005719 pSMB->Fid = cfile->fid.netfid;
5720 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5722 pSMB->InformationLevel =
5723 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5724 else
5725 pSMB->InformationLevel =
5726 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005727 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5729 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005730 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 else
5732 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005733 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 }
5735 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005736 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005738 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005739 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005741 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5742 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 }
5744
Steve French50c2f752007-07-13 00:33:32 +00005745 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 since file handle passed in no longer valid */
5747
5748 return rc;
5749}
5750
Steve French50c2f752007-07-13 00:33:32 +00005751/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 an open handle, rather than by pathname - this is awkward due to
5753 potential access conflicts on the open, but it is unavoidable for these
5754 old servers since the only other choice is to go from 100 nanosecond DCE
5755 time and resort to the original setpathinfo level which takes the ancient
5756 DOS time format with 2 second granularity */
5757int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005758CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005759 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760{
5761 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 char *data_offset;
5763 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 __u16 params, param_offset, offset, byte_count, count;
5765
Joe Perchesf96637b2013-05-04 22:12:25 -05005766 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005767 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5768
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 if (rc)
5770 return rc;
5771
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005772 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5773 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 params = 6;
5776 pSMB->MaxSetupCount = 0;
5777 pSMB->Reserved = 0;
5778 pSMB->Flags = 0;
5779 pSMB->Timeout = 0;
5780 pSMB->Reserved2 = 0;
5781 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5782 offset = param_offset + params;
5783
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005784 data_offset = (char *)pSMB +
5785 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
Steve French26f57362007-08-30 22:09:15 +00005787 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005789 /* BB find max SMB PDU from sess */
5790 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 pSMB->SetupCount = 1;
5792 pSMB->Reserved3 = 0;
5793 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5794 byte_count = 3 /* pad */ + params + count;
5795 pSMB->DataCount = cpu_to_le16(count);
5796 pSMB->ParameterCount = cpu_to_le16(params);
5797 pSMB->TotalDataCount = pSMB->DataCount;
5798 pSMB->TotalParameterCount = pSMB->ParameterCount;
5799 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5800 pSMB->DataOffset = cpu_to_le16(offset);
5801 pSMB->Fid = fid;
5802 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5803 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5804 else
5805 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5806 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005807 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005809 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005810 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005811 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005812 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005813 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5814 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Steve French50c2f752007-07-13 00:33:32 +00005816 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 since file handle passed in no longer valid */
5818
5819 return rc;
5820}
5821
Jeff Layton6d22f092008-09-23 11:48:35 -04005822int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005823CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005824 bool delete_file, __u16 fid, __u32 pid_of_opener)
5825{
5826 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5827 char *data_offset;
5828 int rc = 0;
5829 __u16 params, param_offset, offset, byte_count, count;
5830
Joe Perchesf96637b2013-05-04 22:12:25 -05005831 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005832 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5833
5834 if (rc)
5835 return rc;
5836
5837 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5838 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5839
5840 params = 6;
5841 pSMB->MaxSetupCount = 0;
5842 pSMB->Reserved = 0;
5843 pSMB->Flags = 0;
5844 pSMB->Timeout = 0;
5845 pSMB->Reserved2 = 0;
5846 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5847 offset = param_offset + params;
5848
5849 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5850
5851 count = 1;
5852 pSMB->MaxParameterCount = cpu_to_le16(2);
5853 /* BB find max SMB PDU from sess */
5854 pSMB->MaxDataCount = cpu_to_le16(1000);
5855 pSMB->SetupCount = 1;
5856 pSMB->Reserved3 = 0;
5857 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5858 byte_count = 3 /* pad */ + params + count;
5859 pSMB->DataCount = cpu_to_le16(count);
5860 pSMB->ParameterCount = cpu_to_le16(params);
5861 pSMB->TotalDataCount = pSMB->DataCount;
5862 pSMB->TotalParameterCount = pSMB->ParameterCount;
5863 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5864 pSMB->DataOffset = cpu_to_le16(offset);
5865 pSMB->Fid = fid;
5866 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5867 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005868 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005869 pSMB->ByteCount = cpu_to_le16(byte_count);
5870 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005871 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005872 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005873 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005874 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005875
5876 return rc;
5877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
5879int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005880CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005881 const char *fileName, const FILE_BASIC_INFO *data,
5882 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883{
5884 TRANSACTION2_SPI_REQ *pSMB = NULL;
5885 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5886 int name_len;
5887 int rc = 0;
5888 int bytes_returned = 0;
5889 char *data_offset;
5890 __u16 params, param_offset, offset, byte_count, count;
5891
Joe Perchesf96637b2013-05-04 22:12:25 -05005892 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
5894SetTimesRetry:
5895 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5896 (void **) &pSMBr);
5897 if (rc)
5898 return rc;
5899
5900 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5901 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005902 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5903 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 name_len++; /* trailing null */
5905 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10005906 } else {
5907 name_len = copy_path_name(pSMB->FileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 }
5909
5910 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005911 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005913 /* BB find max SMB PDU from sess structure BB */
5914 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 pSMB->MaxSetupCount = 0;
5916 pSMB->Reserved = 0;
5917 pSMB->Flags = 0;
5918 pSMB->Timeout = 0;
5919 pSMB->Reserved2 = 0;
5920 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005921 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 offset = param_offset + params;
5923 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5924 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5925 pSMB->DataOffset = cpu_to_le16(offset);
5926 pSMB->SetupCount = 1;
5927 pSMB->Reserved3 = 0;
5928 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5929 byte_count = 3 /* pad */ + params + count;
5930
5931 pSMB->DataCount = cpu_to_le16(count);
5932 pSMB->ParameterCount = cpu_to_le16(params);
5933 pSMB->TotalDataCount = pSMB->DataCount;
5934 pSMB->TotalParameterCount = pSMB->ParameterCount;
5935 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5936 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5937 else
5938 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5939 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005940 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005941 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 pSMB->ByteCount = cpu_to_le16(byte_count);
5943 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5944 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005945 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005946 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
5948 cifs_buf_release(pSMB);
5949
5950 if (rc == -EAGAIN)
5951 goto SetTimesRetry;
5952
5953 return rc;
5954}
5955
5956/* Can not be used to set time stamps yet (due to old DOS time format) */
5957/* Can be used to set attributes */
5958#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5959 handling it anyway and NT4 was what we thought it would be needed for
5960 Do not delete it until we prove whether needed for Win9x though */
5961int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005962CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 __u16 dos_attrs, const struct nls_table *nls_codepage)
5964{
5965 SETATTR_REQ *pSMB = NULL;
5966 SETATTR_RSP *pSMBr = NULL;
5967 int rc = 0;
5968 int bytes_returned;
5969 int name_len;
5970
Joe Perchesf96637b2013-05-04 22:12:25 -05005971 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973SetAttrLgcyRetry:
5974 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5975 (void **) &pSMBr);
5976 if (rc)
5977 return rc;
5978
5979 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5980 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005981 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5982 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 name_len++; /* trailing null */
5984 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10005985 } else {
5986 name_len = copy_path_name(pSMB->fileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
5988 pSMB->attr = cpu_to_le16(dos_attrs);
5989 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005990 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5992 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5993 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005994 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005995 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
5997 cifs_buf_release(pSMB);
5998
5999 if (rc == -EAGAIN)
6000 goto SetAttrLgcyRetry;
6001
6002 return rc;
6003}
6004#endif /* temporarily unneeded SetAttr legacy function */
6005
Jeff Layton654cf142009-07-09 20:02:49 -04006006static void
6007cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6008 const struct cifs_unix_set_info_args *args)
6009{
Eric W. Biederman49418b22013-02-06 00:57:56 -08006010 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04006011 u64 mode = args->mode;
6012
Eric W. Biederman49418b22013-02-06 00:57:56 -08006013 if (uid_valid(args->uid))
6014 uid = from_kuid(&init_user_ns, args->uid);
6015 if (gid_valid(args->gid))
6016 gid = from_kgid(&init_user_ns, args->gid);
6017
Jeff Layton654cf142009-07-09 20:02:49 -04006018 /*
6019 * Samba server ignores set of file size to zero due to bugs in some
6020 * older clients, but we should be precise - we use SetFileSize to
6021 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006022 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04006023 * zero instead of -1 here
6024 */
6025 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6026 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6027 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6028 data_offset->LastAccessTime = cpu_to_le64(args->atime);
6029 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08006030 data_offset->Uid = cpu_to_le64(uid);
6031 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04006032 /* better to leave device as zero when it is */
6033 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6034 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6035 data_offset->Permissions = cpu_to_le64(mode);
6036
6037 if (S_ISREG(mode))
6038 data_offset->Type = cpu_to_le32(UNIX_FILE);
6039 else if (S_ISDIR(mode))
6040 data_offset->Type = cpu_to_le32(UNIX_DIR);
6041 else if (S_ISLNK(mode))
6042 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6043 else if (S_ISCHR(mode))
6044 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6045 else if (S_ISBLK(mode))
6046 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6047 else if (S_ISFIFO(mode))
6048 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6049 else if (S_ISSOCK(mode))
6050 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6051}
6052
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006054CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006055 const struct cifs_unix_set_info_args *args,
6056 u16 fid, u32 pid_of_opener)
6057{
6058 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006059 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006060 int rc = 0;
6061 u16 params, param_offset, offset, byte_count, count;
6062
Joe Perchesf96637b2013-05-04 22:12:25 -05006063 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006064 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6065
6066 if (rc)
6067 return rc;
6068
6069 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6070 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6071
6072 params = 6;
6073 pSMB->MaxSetupCount = 0;
6074 pSMB->Reserved = 0;
6075 pSMB->Flags = 0;
6076 pSMB->Timeout = 0;
6077 pSMB->Reserved2 = 0;
6078 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6079 offset = param_offset + params;
6080
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006081 data_offset = (char *)pSMB +
6082 offsetof(struct smb_hdr, Protocol) + offset;
6083
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006084 count = sizeof(FILE_UNIX_BASIC_INFO);
6085
6086 pSMB->MaxParameterCount = cpu_to_le16(2);
6087 /* BB find max SMB PDU from sess */
6088 pSMB->MaxDataCount = cpu_to_le16(1000);
6089 pSMB->SetupCount = 1;
6090 pSMB->Reserved3 = 0;
6091 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6092 byte_count = 3 /* pad */ + params + count;
6093 pSMB->DataCount = cpu_to_le16(count);
6094 pSMB->ParameterCount = cpu_to_le16(params);
6095 pSMB->TotalDataCount = pSMB->DataCount;
6096 pSMB->TotalParameterCount = pSMB->ParameterCount;
6097 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6098 pSMB->DataOffset = cpu_to_le16(offset);
6099 pSMB->Fid = fid;
6100 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6101 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006102 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006103 pSMB->ByteCount = cpu_to_le16(byte_count);
6104
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006105 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006106
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006107 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006108 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006109 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006110 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6111 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006112
6113 /* Note: On -EAGAIN error only caller can retry on handle based calls
6114 since file handle passed in no longer valid */
6115
6116 return rc;
6117}
6118
6119int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006120CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006121 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006122 const struct cifs_unix_set_info_args *args,
6123 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124{
6125 TRANSACTION2_SPI_REQ *pSMB = NULL;
6126 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6127 int name_len;
6128 int rc = 0;
6129 int bytes_returned = 0;
6130 FILE_UNIX_BASIC_INFO *data_offset;
6131 __u16 params, param_offset, offset, count, byte_count;
6132
Joe Perchesf96637b2013-05-04 22:12:25 -05006133 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134setPermsRetry:
6135 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6136 (void **) &pSMBr);
6137 if (rc)
6138 return rc;
6139
6140 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6141 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006142 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006143 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 name_len++; /* trailing null */
6145 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006146 } else {
6147 name_len = copy_path_name(pSMB->FileName, file_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 }
6149
6150 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006151 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006153 /* BB find max SMB PDU from sess structure BB */
6154 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 pSMB->MaxSetupCount = 0;
6156 pSMB->Reserved = 0;
6157 pSMB->Flags = 0;
6158 pSMB->Timeout = 0;
6159 pSMB->Reserved2 = 0;
6160 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006161 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 offset = param_offset + params;
6163 data_offset =
6164 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6165 offset);
6166 memset(data_offset, 0, count);
6167 pSMB->DataOffset = cpu_to_le16(offset);
6168 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6169 pSMB->SetupCount = 1;
6170 pSMB->Reserved3 = 0;
6171 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6172 byte_count = 3 /* pad */ + params + count;
6173 pSMB->ParameterCount = cpu_to_le16(params);
6174 pSMB->DataCount = cpu_to_le16(count);
6175 pSMB->TotalParameterCount = pSMB->ParameterCount;
6176 pSMB->TotalDataCount = pSMB->DataCount;
6177 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6178 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006179 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006180
Jeff Layton654cf142009-07-09 20:02:49 -04006181 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
6183 pSMB->ByteCount = cpu_to_le16(byte_count);
6184 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6185 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006186 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006187 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
Steve French0d817bc2008-05-22 02:02:03 +00006189 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 if (rc == -EAGAIN)
6191 goto setPermsRetry;
6192 return rc;
6193}
6194
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006196/*
6197 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6198 * function used by listxattr and getxattr type calls. When ea_name is set,
6199 * it looks for that attribute name and stuffs that value into the EAData
6200 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6201 * buffer. In both cases, the return value is either the length of the
6202 * resulting data or a negative error code. If EAData is a NULL pointer then
6203 * the data isn't copied to it, but the length is returned.
6204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006206CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006207 const unsigned char *searchName, const unsigned char *ea_name,
6208 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006209 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
6211 /* BB assumes one setup word */
6212 TRANSACTION2_QPI_REQ *pSMB = NULL;
6213 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006214 int remap = cifs_remap(cifs_sb);
6215 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 int rc = 0;
6217 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006218 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006219 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006220 struct fea *temp_fea;
6221 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006222 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006223 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006224 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
Joe Perchesf96637b2013-05-04 22:12:25 -05006226 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227QAllEAsRetry:
6228 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6229 (void **) &pSMBr);
6230 if (rc)
6231 return rc;
6232
6233 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006234 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006235 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6236 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006237 list_len++; /* trailing null */
6238 list_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006239 } else {
6240 list_len = copy_path_name(pSMB->FileName, searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 }
6242
Jeff Layton6e462b92010-02-10 16:18:26 -05006243 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 pSMB->TotalDataCount = 0;
6245 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006246 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006247 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 pSMB->MaxSetupCount = 0;
6249 pSMB->Reserved = 0;
6250 pSMB->Flags = 0;
6251 pSMB->Timeout = 0;
6252 pSMB->Reserved2 = 0;
6253 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006254 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 pSMB->DataCount = 0;
6256 pSMB->DataOffset = 0;
6257 pSMB->SetupCount = 1;
6258 pSMB->Reserved3 = 0;
6259 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6260 byte_count = params + 1 /* pad */ ;
6261 pSMB->TotalParameterCount = cpu_to_le16(params);
6262 pSMB->ParameterCount = pSMB->TotalParameterCount;
6263 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6264 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006265 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 pSMB->ByteCount = cpu_to_le16(byte_count);
6267
6268 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6269 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6270 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006271 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006272 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006274
6275
6276 /* BB also check enough total bytes returned */
6277 /* BB we need to improve the validity checking
6278 of these trans2 responses */
6279
6280 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006281 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006282 rc = -EIO; /* bad smb */
6283 goto QAllEAsOut;
6284 }
6285
6286 /* check that length of list is not more than bcc */
6287 /* check that each entry does not go beyond length
6288 of list */
6289 /* check that each element of each entry does not
6290 go beyond end of list */
6291 /* validate_trans2_offsets() */
6292 /* BB check if start of smb + data_offset > &bcc+ bcc */
6293
6294 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6295 ea_response_data = (struct fealist *)
6296 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6297
Jeff Layton6e462b92010-02-10 16:18:26 -05006298 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006299 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006300 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006301 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006302 /* didn't find the named attribute */
6303 if (ea_name)
6304 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006305 goto QAllEAsOut;
6306 }
6307
Jeff Layton0cd126b2010-02-10 16:18:26 -05006308 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006309 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006310 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006311 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006312 rc = -EIO;
6313 goto QAllEAsOut;
6314 }
6315
Jeff Laytonf0d38682010-02-10 16:18:26 -05006316 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006317 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006318 temp_fea = ea_response_data->list;
6319 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006320 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006321 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006322 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006323
Jeff Layton6e462b92010-02-10 16:18:26 -05006324 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006325 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006326 /* make sure we can read name_len and value_len */
6327 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006328 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006329 rc = -EIO;
6330 goto QAllEAsOut;
6331 }
6332
6333 name_len = temp_fea->name_len;
6334 value_len = le16_to_cpu(temp_fea->value_len);
6335 list_len -= name_len + 1 + value_len;
6336 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006337 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006338 rc = -EIO;
6339 goto QAllEAsOut;
6340 }
6341
Jeff Layton31c05192010-02-10 16:18:26 -05006342 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006343 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006344 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006345 temp_ptr += name_len + 1;
6346 rc = value_len;
6347 if (buf_size == 0)
6348 goto QAllEAsOut;
6349 if ((size_t)value_len > buf_size) {
6350 rc = -ERANGE;
6351 goto QAllEAsOut;
6352 }
6353 memcpy(EAData, temp_ptr, value_len);
6354 goto QAllEAsOut;
6355 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006356 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006357 /* account for prefix user. and trailing null */
6358 rc += (5 + 1 + name_len);
6359 if (rc < (int) buf_size) {
6360 memcpy(EAData, "user.", 5);
6361 EAData += 5;
6362 memcpy(EAData, temp_ptr, name_len);
6363 EAData += name_len;
6364 /* null terminate name */
6365 *EAData = 0;
6366 ++EAData;
6367 } else if (buf_size == 0) {
6368 /* skip copy - calc size only */
6369 } else {
6370 /* stop before overrun buffer */
6371 rc = -ERANGE;
6372 break;
6373 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006374 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006375 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006376 temp_fea = (struct fea *)temp_ptr;
6377 }
6378
Jeff Layton31c05192010-02-10 16:18:26 -05006379 /* didn't find the named attribute */
6380 if (ea_name)
6381 rc = -ENODATA;
6382
Jeff Laytonf0d38682010-02-10 16:18:26 -05006383QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006384 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 if (rc == -EAGAIN)
6386 goto QAllEAsRetry;
6387
6388 return (ssize_t)rc;
6389}
6390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006392CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6393 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006394 const __u16 ea_value_len, const struct nls_table *nls_codepage,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006395 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396{
6397 struct smb_com_transaction2_spi_req *pSMB = NULL;
6398 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6399 struct fealist *parm_data;
6400 int name_len;
6401 int rc = 0;
6402 int bytes_returned = 0;
6403 __u16 params, param_offset, byte_count, offset, count;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006404 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Joe Perchesf96637b2013-05-04 22:12:25 -05006406 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407SetEARetry:
6408 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6409 (void **) &pSMBr);
6410 if (rc)
6411 return rc;
6412
6413 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6414 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006415 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6416 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 name_len++; /* trailing null */
6418 name_len *= 2;
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10006419 } else {
6420 name_len = copy_path_name(pSMB->FileName, fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 }
6422
6423 params = 6 + name_len;
6424
6425 /* done calculating parms using name_len of file name,
6426 now use name_len to calculate length of ea name
6427 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006428 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 name_len = 0;
6430 else
Steve French50c2f752007-07-13 00:33:32 +00006431 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006433 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006435 /* BB find max SMB PDU from sess */
6436 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 pSMB->MaxSetupCount = 0;
6438 pSMB->Reserved = 0;
6439 pSMB->Flags = 0;
6440 pSMB->Timeout = 0;
6441 pSMB->Reserved2 = 0;
6442 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006443 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 offset = param_offset + params;
6445 pSMB->InformationLevel =
6446 cpu_to_le16(SMB_SET_FILE_EA);
6447
Arnd Bergmannade7db92018-02-02 16:48:47 +01006448 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6450 pSMB->DataOffset = cpu_to_le16(offset);
6451 pSMB->SetupCount = 1;
6452 pSMB->Reserved3 = 0;
6453 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6454 byte_count = 3 /* pad */ + params + count;
6455 pSMB->DataCount = cpu_to_le16(count);
6456 parm_data->list_len = cpu_to_le32(count);
6457 parm_data->list[0].EA_flags = 0;
6458 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006459 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006461 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006462 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 parm_data->list[0].name[name_len] = 0;
6464 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6465 /* caller ensures that ea_value_len is less than 64K but
6466 we need to ensure that it fits within the smb */
6467
Steve French50c2f752007-07-13 00:33:32 +00006468 /*BB add length check to see if it would fit in
6469 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006470 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6471 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006472 memcpy(parm_data->list[0].name+name_len+1,
6473 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
6475 pSMB->TotalDataCount = pSMB->DataCount;
6476 pSMB->ParameterCount = cpu_to_le16(params);
6477 pSMB->TotalParameterCount = pSMB->ParameterCount;
6478 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006479 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 pSMB->ByteCount = cpu_to_le16(byte_count);
6481 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6482 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006483 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006484 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485
6486 cifs_buf_release(pSMB);
6487
6488 if (rc == -EAGAIN)
6489 goto SetEARetry;
6490
6491 return rc;
6492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493#endif