blob: bb54ccf8481c33937d3dfa6b0a782971112a74fc [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) {
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100142 snprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$",
Paulo Alcantara08744012018-11-14 17:24:29 -0200143 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
Aurelien Aptel15bc77f2019-01-08 13:41:00 +0100175 snprintf(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;
825
826 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400827 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500828}
829
830int
831CIFSSMBEcho(struct TCP_Server_Info *server)
832{
833 ECHO_REQ *smb;
834 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800835 struct kvec iov[2];
836 struct smb_rqst rqst = { .rq_iov = iov,
837 .rq_nvec = 2 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500838
Joe Perchesf96637b2013-05-04 22:12:25 -0500839 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500840
841 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
842 if (rc)
843 return rc;
844
Steve French26c9cb62017-05-02 13:35:20 -0500845 if (server->capabilities & CAP_UNICODE)
846 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
847
Jeff Layton766fdbb2011-01-11 07:24:21 -0500848 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000849 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500850 smb->hdr.WordCount = 1;
851 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400852 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500853 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000854 inc_rfc1001_len(smb, 3);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800855
856 iov[0].iov_len = 4;
857 iov[0].iov_base = smb;
858 iov[1].iov_len = get_rfc1002_length(smb);
859 iov[1].iov_base = (char *)smb + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500860
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800861 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400862 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500863 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500864 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500865
866 cifs_small_buf_release(smb);
867
868 return rc;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400872CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 LOGOFF_ANDX_REQ *pSMB;
875 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Joe Perchesf96637b2013-05-04 22:12:25 -0500877 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500878
879 /*
880 * BB: do we need to check validity of ses and server? They should
881 * always be valid since we have an active reference. If not, that
882 * should probably be a BUG()
883 */
884 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -EIO;
886
Steve Frenchd7b619c2010-02-25 05:36:46 +0000887 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000888 if (ses->need_reconnect)
889 goto session_already_dead; /* no need to send SMBlogoff if uid
890 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
892 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000893 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return rc;
895 }
896
Pavel Shilovsky88257362012-05-23 14:01:59 +0400897 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700898
Jeff Layton38d77c52013-05-26 07:01:00 -0400899 if (ses->server->sign)
900 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 pSMB->hdr.Uid = ses->Suid;
903
904 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400905 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700906 cifs_small_buf_release(pSMB);
Steve French3b795212008-11-13 19:45:32 +0000907session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000908 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000911 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 error */
913 if (rc == -EAGAIN)
914 rc = 0;
915 return rc;
916}
917
918int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400919CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
920 const char *fileName, __u16 type,
921 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000922{
923 TRANSACTION2_SPI_REQ *pSMB = NULL;
924 TRANSACTION2_SPI_RSP *pSMBr = NULL;
925 struct unlink_psx_rq *pRqD;
926 int name_len;
927 int rc = 0;
928 int bytes_returned = 0;
929 __u16 params, param_offset, offset, byte_count;
930
Joe Perchesf96637b2013-05-04 22:12:25 -0500931 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000932PsxDelete:
933 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
934 (void **) &pSMBr);
935 if (rc)
936 return rc;
937
938 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
939 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600940 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
941 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000942 name_len++; /* trailing null */
943 name_len *= 2;
944 } else { /* BB add path length overrun check */
945 name_len = strnlen(fileName, PATH_MAX);
946 name_len++; /* trailing null */
947 strncpy(pSMB->FileName, fileName, name_len);
948 }
949
950 params = 6 + name_len;
951 pSMB->MaxParameterCount = cpu_to_le16(2);
952 pSMB->MaxDataCount = 0; /* BB double check this with jra */
953 pSMB->MaxSetupCount = 0;
954 pSMB->Reserved = 0;
955 pSMB->Flags = 0;
956 pSMB->Timeout = 0;
957 pSMB->Reserved2 = 0;
958 param_offset = offsetof(struct smb_com_transaction2_spi_req,
959 InformationLevel) - 4;
960 offset = param_offset + params;
961
962 /* Setup pointer to Request Data (inode type) */
963 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
964 pRqD->type = cpu_to_le16(type);
965 pSMB->ParameterOffset = cpu_to_le16(param_offset);
966 pSMB->DataOffset = cpu_to_le16(offset);
967 pSMB->SetupCount = 1;
968 pSMB->Reserved3 = 0;
969 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
970 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
971
972 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
973 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
974 pSMB->ParameterCount = cpu_to_le16(params);
975 pSMB->TotalParameterCount = pSMB->ParameterCount;
976 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
977 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000978 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000979 pSMB->ByteCount = cpu_to_le16(byte_count);
980 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
981 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000982 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500983 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000984 cifs_buf_release(pSMB);
985
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400986 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000987
988 if (rc == -EAGAIN)
989 goto PsxDelete;
990
991 return rc;
992}
993
994int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700995CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
996 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 DELETE_FILE_REQ *pSMB = NULL;
999 DELETE_FILE_RSP *pSMBr = NULL;
1000 int rc = 0;
1001 int bytes_returned;
1002 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001003 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005DelFileRetry:
1006 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
1007 (void **) &pSMBr);
1008 if (rc)
1009 return rc;
1010
1011 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001012 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
1013 PATH_MAX, cifs_sb->local_nls,
1014 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 name_len++; /* trailing null */
1016 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001017 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001018 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001020 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 pSMB->SearchAttributes =
1023 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
1024 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001025 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001029 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +00001030 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001031 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 cifs_buf_release(pSMB);
1034 if (rc == -EAGAIN)
1035 goto DelFileRetry;
1036
1037 return rc;
1038}
1039
1040int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001041CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1042 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 DELETE_DIRECTORY_REQ *pSMB = NULL;
1045 DELETE_DIRECTORY_RSP *pSMBr = NULL;
1046 int rc = 0;
1047 int bytes_returned;
1048 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001049 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Joe Perchesf96637b2013-05-04 22:12:25 -05001051 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052RmDirRetry:
1053 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
1054 (void **) &pSMBr);
1055 if (rc)
1056 return rc;
1057
1058 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001059 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
1060 PATH_MAX, cifs_sb->local_nls,
1061 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 name_len++; /* trailing null */
1063 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001064 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001065 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001067 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
1070 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001071 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1073 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1074 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001075 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001076 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001077 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 cifs_buf_release(pSMB);
1080 if (rc == -EAGAIN)
1081 goto RmDirRetry;
1082 return rc;
1083}
1084
1085int
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001086CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
1087 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 int rc = 0;
1090 CREATE_DIRECTORY_REQ *pSMB = NULL;
1091 CREATE_DIRECTORY_RSP *pSMBr = NULL;
1092 int bytes_returned;
1093 int name_len;
Steve French2baa2682014-09-27 02:19:01 -05001094 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Joe Perchesf96637b2013-05-04 22:12:25 -05001096 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097MkDirRetry:
1098 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
1099 (void **) &pSMBr);
1100 if (rc)
1101 return rc;
1102
1103 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06001104 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001105 PATH_MAX, cifs_sb->local_nls,
1106 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 name_len++; /* trailing null */
1108 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -07001109 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 name_len = strnlen(name, PATH_MAX);
1111 name_len++; /* trailing null */
1112 strncpy(pSMB->DirName, name, name_len);
1113 }
1114
1115 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001116 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 pSMB->ByteCount = cpu_to_le16(name_len + 1);
1118 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1119 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001120 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +00001121 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001122 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 cifs_buf_release(pSMB);
1125 if (rc == -EAGAIN)
1126 goto MkDirRetry;
1127 return rc;
1128}
1129
Steve French2dd29d32007-04-23 22:07:35 +00001130int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001131CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1132 __u32 posix_flags, __u64 mode, __u16 *netfid,
1133 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1134 const char *name, const struct nls_table *nls_codepage,
1135 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001136{
1137 TRANSACTION2_SPI_REQ *pSMB = NULL;
1138 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1139 int name_len;
1140 int rc = 0;
1141 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001142 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001143 OPEN_PSX_REQ *pdata;
1144 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001145
Joe Perchesf96637b2013-05-04 22:12:25 -05001146 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001147PsxCreat:
1148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1149 (void **) &pSMBr);
1150 if (rc)
1151 return rc;
1152
1153 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1154 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001155 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1156 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001157 name_len++; /* trailing null */
1158 name_len *= 2;
1159 } else { /* BB improve the check for buffer overruns BB */
1160 name_len = strnlen(name, PATH_MAX);
1161 name_len++; /* trailing null */
1162 strncpy(pSMB->FileName, name, name_len);
1163 }
1164
1165 params = 6 + name_len;
1166 count = sizeof(OPEN_PSX_REQ);
1167 pSMB->MaxParameterCount = cpu_to_le16(2);
1168 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1169 pSMB->MaxSetupCount = 0;
1170 pSMB->Reserved = 0;
1171 pSMB->Flags = 0;
1172 pSMB->Timeout = 0;
1173 pSMB->Reserved2 = 0;
1174 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001175 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001176 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001177 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001178 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001179 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001180 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001181 pdata->OpenFlags = cpu_to_le32(*pOplock);
1182 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1183 pSMB->DataOffset = cpu_to_le16(offset);
1184 pSMB->SetupCount = 1;
1185 pSMB->Reserved3 = 0;
1186 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1187 byte_count = 3 /* pad */ + params + count;
1188
1189 pSMB->DataCount = cpu_to_le16(count);
1190 pSMB->ParameterCount = cpu_to_le16(params);
1191 pSMB->TotalDataCount = pSMB->DataCount;
1192 pSMB->TotalParameterCount = pSMB->ParameterCount;
1193 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1194 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001195 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001196 pSMB->ByteCount = cpu_to_le16(byte_count);
1197 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1198 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1199 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001200 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001201 goto psx_create_err;
1202 }
1203
Joe Perchesf96637b2013-05-04 22:12:25 -05001204 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001205 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1206
Jeff Layton820a8032011-05-04 08:05:26 -04001207 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001208 rc = -EIO; /* bad smb */
1209 goto psx_create_err;
1210 }
1211
1212 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001213 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001214 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001215
Steve French2dd29d32007-04-23 22:07:35 +00001216 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001217 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001218 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1219 /* Let caller know file was created so we can set the mode. */
1220 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001221 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001222 *pOplock |= CIFS_CREATE_ACTION;
1223 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001224 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1225 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001226 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001227 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001228 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001229 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001230 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001231 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001232 goto psx_create_err;
1233 }
Steve French50c2f752007-07-13 00:33:32 +00001234 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001235 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001236 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001237 }
Steve French2dd29d32007-04-23 22:07:35 +00001238
1239psx_create_err:
1240 cifs_buf_release(pSMB);
1241
Steve French65bc98b2009-07-10 15:27:25 +00001242 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001243 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001244 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001245 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001246
1247 if (rc == -EAGAIN)
1248 goto PsxCreat;
1249
Steve French50c2f752007-07-13 00:33:32 +00001250 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001251}
1252
Steve Frencha9d02ad2005-08-24 23:06:05 -07001253static __u16 convert_disposition(int disposition)
1254{
1255 __u16 ofun = 0;
1256
1257 switch (disposition) {
1258 case FILE_SUPERSEDE:
1259 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1260 break;
1261 case FILE_OPEN:
1262 ofun = SMBOPEN_OAPPEND;
1263 break;
1264 case FILE_CREATE:
1265 ofun = SMBOPEN_OCREATE;
1266 break;
1267 case FILE_OPEN_IF:
1268 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1269 break;
1270 case FILE_OVERWRITE:
1271 ofun = SMBOPEN_OTRUNC;
1272 break;
1273 case FILE_OVERWRITE_IF:
1274 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1275 break;
1276 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001277 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001278 ofun = SMBOPEN_OAPPEND; /* regular open */
1279 }
1280 return ofun;
1281}
1282
Jeff Layton35fc37d2008-05-14 10:22:03 -07001283static int
1284access_flags_to_smbopen_mode(const int access_flags)
1285{
1286 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1287
1288 if (masked_flags == GENERIC_READ)
1289 return SMBOPEN_READ;
1290 else if (masked_flags == GENERIC_WRITE)
1291 return SMBOPEN_WRITE;
1292
1293 /* just go for read/write */
1294 return SMBOPEN_READWRITE;
1295}
1296
Steve Frencha9d02ad2005-08-24 23:06:05 -07001297int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001298SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001299 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001300 const int access_flags, const int create_options, __u16 *netfid,
1301 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001302 const struct nls_table *nls_codepage, int remap)
1303{
1304 int rc = -EACCES;
1305 OPENX_REQ *pSMB = NULL;
1306 OPENX_RSP *pSMBr = NULL;
1307 int bytes_returned;
1308 int name_len;
1309 __u16 count;
1310
1311OldOpenRetry:
1312 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1313 (void **) &pSMBr);
1314 if (rc)
1315 return rc;
1316
1317 pSMB->AndXCommand = 0xFF; /* none */
1318
1319 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1320 count = 1; /* account for one byte pad to word boundary */
1321 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001322 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1323 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001324 name_len++; /* trailing null */
1325 name_len *= 2;
1326 } else { /* BB improve check for buffer overruns BB */
1327 count = 0; /* no pad */
1328 name_len = strnlen(fileName, PATH_MAX);
1329 name_len++; /* trailing null */
1330 strncpy(pSMB->fileName, fileName, name_len);
1331 }
1332 if (*pOplock & REQ_OPLOCK)
1333 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001334 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001335 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001336
Steve Frencha9d02ad2005-08-24 23:06:05 -07001337 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001338 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001339 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1340 /* set file as system file if special file such
1341 as fifo and server expecting SFU style and
1342 no Unix extensions */
1343
Steve French790fe572007-07-07 19:25:05 +00001344 if (create_options & CREATE_OPTION_SPECIAL)
1345 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001346 else /* BB FIXME BB */
1347 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001348
Jeff Layton67750fb2008-05-09 22:28:02 +00001349 if (create_options & CREATE_OPTION_READONLY)
1350 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001351
1352 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001353/* pSMB->CreateOptions = cpu_to_le32(create_options &
1354 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001355 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001356
1357 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001358 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001359 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001360 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001361
1362 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001363 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001364 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001365 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001366 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001367 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001368 } else {
1369 /* BB verify if wct == 15 */
1370
Steve French582d21e2008-05-13 04:54:12 +00001371/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001372
1373 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1374 /* Let caller know file was created so we can set the mode. */
1375 /* Do we care about the CreateAction in any other cases? */
1376 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001377/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001378 *pOplock |= CIFS_CREATE_ACTION; */
1379 /* BB FIXME END */
1380
Steve French790fe572007-07-07 19:25:05 +00001381 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001382 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1383 pfile_info->LastAccessTime = 0; /* BB fixme */
1384 pfile_info->LastWriteTime = 0; /* BB fixme */
1385 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001386 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001387 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001388 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001389 pfile_info->AllocationSize =
1390 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1391 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001392 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001393 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001394 }
1395 }
1396
1397 cifs_buf_release(pSMB);
1398 if (rc == -EAGAIN)
1399 goto OldOpenRetry;
1400 return rc;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001404CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1405 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001408 OPEN_REQ *req = NULL;
1409 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int bytes_returned;
1411 int name_len;
1412 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001413 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1414 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001415 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001416 const struct nls_table *nls = cifs_sb->local_nls;
1417 int create_options = oparms->create_options;
1418 int desired_access = oparms->desired_access;
1419 int disposition = oparms->disposition;
1420 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001423 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1424 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (rc)
1426 return rc;
1427
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001428 /* no commands go after this */
1429 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001431 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1432 /* account for one byte pad to word boundary */
1433 count = 1;
1434 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1435 path, PATH_MAX, nls, remap);
1436 /* trailing null */
1437 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001439 req->NameLength = cpu_to_le16(name_len);
1440 } else {
1441 /* BB improve check for buffer overruns BB */
1442 /* no pad */
1443 count = 0;
1444 name_len = strnlen(path, PATH_MAX);
1445 /* trailing null */
1446 name_len++;
1447 req->NameLength = cpu_to_le16(name_len);
1448 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001450
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001451 if (*oplock & REQ_OPLOCK)
1452 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1453 else if (*oplock & REQ_BATCHOPLOCK)
1454 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1455
1456 req->DesiredAccess = cpu_to_le32(desired_access);
1457 req->AllocationSize = 0;
1458
1459 /*
1460 * Set file as system file if special file such as fifo and server
1461 * expecting SFU style and no Unix extensions.
1462 */
1463 if (create_options & CREATE_OPTION_SPECIAL)
1464 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1465 else
1466 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1467
1468 /*
1469 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1470 * sensitive checks for other servers such as Samba.
1471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001473 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Jeff Layton67750fb2008-05-09 22:28:02 +00001475 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001476 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001477
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001478 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1479 req->CreateDisposition = cpu_to_le32(disposition);
1480 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1481
Steve French09d1db52005-04-28 22:41:08 -07001482 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001483 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1484 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001487 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001489 req->ByteCount = cpu_to_le16(count);
1490 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1491 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001492 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001494 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001495 cifs_buf_release(req);
1496 if (rc == -EAGAIN)
1497 goto openRetry;
1498 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001500
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001501 /* 1 byte no need to le_to_cpu */
1502 *oplock = rsp->OplockLevel;
1503 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001504 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001505
1506 /* Let caller know file was created so we can set the mode. */
1507 /* Do we care about the CreateAction in any other cases? */
1508 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1509 *oplock |= CIFS_CREATE_ACTION;
1510
1511 if (buf) {
1512 /* copy from CreationTime to Attributes */
1513 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1514 /* the file_info buf is endian converted by caller */
1515 buf->AllocationSize = rsp->AllocationSize;
1516 buf->EndOfFile = rsp->EndOfFile;
1517 buf->NumberOfLinks = cpu_to_le32(1);
1518 buf->DeletePending = 0;
1519 }
1520
1521 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return rc;
1523}
1524
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001525/*
1526 * Discard any remaining data in the current SMB. To do this, we borrow the
1527 * current bigbuf.
1528 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001529int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001530cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001531{
Ronnie Sahlberg05432e22018-04-09 18:06:31 +10001532 unsigned int rfclen = server->pdu_size;
1533 int remaining = rfclen + server->vals->header_preamble_size -
1534 server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535
1536 while (remaining > 0) {
1537 int length;
1538
1539 length = cifs_read_from_socket(server, server->bigbuf,
1540 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001541 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001542 if (length < 0)
1543 return length;
1544 server->total_read += length;
1545 remaining -= length;
1546 }
1547
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001548 return 0;
1549}
1550
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001551static int
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001552__cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1553 bool malformed)
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001554{
1555 int length;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001556
Pavel Shilovsky350be252017-04-10 10:31:33 -07001557 length = cifs_discard_remaining_data(server);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001558 dequeue_mid(mid, malformed);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001559 mid->resp_buf = server->smallbuf;
1560 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001561 return length;
1562}
1563
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001564static int
1565cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1566{
1567 struct cifs_readdata *rdata = mid->callback_data;
1568
1569 return __cifs_readv_discard(server, mid, rdata->result);
1570}
1571
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001572int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001573cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1574{
1575 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001576 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001577 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001578 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10001579 unsigned int buflen = server->pdu_size +
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001580 server->vals->header_preamble_size;
Long Li74dcf412017-11-22 17:38:46 -07001581 bool use_rdma_mr = false;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001582
Joe Perchesf96637b2013-05-04 22:12:25 -05001583 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1584 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001585
1586 /*
1587 * read the rest of READ_RSP header (sans Data array), or whatever we
1588 * can if there's not enough data. At this point, we've read down to
1589 * the Mid.
1590 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001591 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001592 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593
Al Viroa6137302016-01-09 19:37:16 -05001594 length = cifs_read_from_socket(server,
1595 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001596 if (length < 0)
1597 return length;
1598 server->total_read += length;
1599
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001600 if (server->ops->is_session_expired &&
1601 server->ops->is_session_expired(buf)) {
1602 cifs_reconnect(server);
1603 wake_up(&server->response_q);
1604 return -1;
1605 }
1606
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001607 if (server->ops->is_status_pending &&
1608 server->ops->is_status_pending(buf, server, 0)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001609 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001610 return -1;
1611 }
1612
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001613 /* set up first two iov for signature check and to get credits */
1614 rdata->iov[0].iov_base = buf;
1615 rdata->iov[0].iov_len = 4;
1616 rdata->iov[1].iov_base = buf + 4;
1617 rdata->iov[1].iov_len = server->total_read - 4;
1618 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1619 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
1620 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
1621 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
1622
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001623 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001624 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001625 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001626 cifs_dbg(FYI, "%s: server returned error %d\n",
1627 __func__, rdata->result);
Pavel Shilovsky8004c782019-01-17 15:29:26 -08001628 /* normal error on read response */
1629 return __cifs_readv_discard(server, mid, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001630 }
1631
1632 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001633 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001634 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1635 __func__, server->total_read,
1636 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001637 rdata->result = -EIO;
1638 return cifs_readv_discard(server, mid);
1639 }
1640
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001641 data_offset = server->ops->read_data_offset(buf) +
1642 server->vals->header_preamble_size;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001643 if (data_offset < server->total_read) {
1644 /*
1645 * win2k8 sometimes sends an offset of 0 when the read
1646 * is beyond the EOF. Treat it as if the data starts just after
1647 * the header.
1648 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001649 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1650 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001651 data_offset = server->total_read;
1652 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1653 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001654 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1655 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001656 rdata->result = -EIO;
1657 return cifs_readv_discard(server, mid);
1658 }
1659
Joe Perchesf96637b2013-05-04 22:12:25 -05001660 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1661 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001662
1663 len = data_offset - server->total_read;
1664 if (len > 0) {
1665 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001666 length = cifs_read_from_socket(server,
1667 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001668 if (length < 0)
1669 return length;
1670 server->total_read += length;
1671 }
1672
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001673 /* how much data is in the response? */
Long Li74dcf412017-11-22 17:38:46 -07001674#ifdef CONFIG_CIFS_SMB_DIRECT
1675 use_rdma_mr = rdata->mr;
1676#endif
1677 data_len = server->ops->read_data_length(buf, use_rdma_mr);
1678 if (!use_rdma_mr && (data_offset + data_len > buflen)) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001679 /* data_len is corrupt -- discard frame */
1680 rdata->result = -EIO;
1681 return cifs_readv_discard(server, mid);
1682 }
1683
Jeff Layton8321fec2012-09-19 06:22:32 -07001684 length = rdata->read_into_pages(server, rdata, data_len);
1685 if (length < 0)
1686 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001687
Jeff Layton8321fec2012-09-19 06:22:32 -07001688 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001689
Joe Perchesf96637b2013-05-04 22:12:25 -05001690 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1691 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001692
1693 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001694 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001695 return cifs_readv_discard(server, mid);
1696
1697 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001698 mid->resp_buf = server->smallbuf;
1699 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001700 return length;
1701}
1702
1703static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001704cifs_readv_callback(struct mid_q_entry *mid)
1705{
1706 struct cifs_readdata *rdata = mid->callback_data;
1707 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1708 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001709 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1710 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001711 .rq_pages = rdata->pages,
Long Li6d3adb22018-09-20 21:18:38 +00001712 .rq_offset = rdata->page_offset,
Jeff Layton8321fec2012-09-19 06:22:32 -07001713 .rq_npages = rdata->nr_pages,
1714 .rq_pagesz = rdata->pagesz,
1715 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001716
Joe Perchesf96637b2013-05-04 22:12:25 -05001717 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1718 __func__, mid->mid, mid->mid_state, rdata->result,
1719 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001720
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001721 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001722 case MID_RESPONSE_RECEIVED:
1723 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001724 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001725 int rc = 0;
1726
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001727 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001728 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001729 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001730 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1731 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001732 }
1733 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001734 task_io_account_read(rdata->got_bytes);
1735 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001736 break;
1737 case MID_REQUEST_SUBMITTED:
1738 case MID_RETRY_NEEDED:
1739 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001740 if (server->sign && rdata->got_bytes)
1741 /* reset bytes number since we can not check a sign */
1742 rdata->got_bytes = 0;
1743 /* FIXME: should this be counted toward the initiating task? */
1744 task_io_account_read(rdata->got_bytes);
1745 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001746 break;
1747 default:
1748 rdata->result = -EIO;
1749 }
1750
Jeff Laytonda472fc2012-03-23 14:40:53 -04001751 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001752 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001753 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001754}
1755
1756/* cifs_async_readv - send an async write, and set up mid to handle result */
1757int
1758cifs_async_readv(struct cifs_readdata *rdata)
1759{
1760 int rc;
1761 READ_REQ *smb = NULL;
1762 int wct;
1763 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001764 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1765 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001766
Joe Perchesf96637b2013-05-04 22:12:25 -05001767 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1768 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001769
1770 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1771 wct = 12;
1772 else {
1773 wct = 10; /* old style read */
1774 if ((rdata->offset >> 32) > 0) {
1775 /* can not handle this big offset for old */
1776 return -EIO;
1777 }
1778 }
1779
1780 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1781 if (rc)
1782 return rc;
1783
1784 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1785 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1786
1787 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001788 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001789 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1790 if (wct == 12)
1791 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1792 smb->Remaining = 0;
1793 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1794 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1795 if (wct == 12)
1796 smb->ByteCount = 0;
1797 else {
1798 /* old style read */
1799 struct smb_com_readx_req *smbr =
1800 (struct smb_com_readx_req *)smb;
1801 smbr->ByteCount = 0;
1802 }
1803
1804 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001805 rdata->iov[0].iov_base = smb;
1806 rdata->iov[0].iov_len = 4;
1807 rdata->iov[1].iov_base = (char *)smb + 4;
1808 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001809
Jeff Layton6993f742012-05-16 07:13:17 -04001810 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001811 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08001812 cifs_readv_callback, NULL, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001813
1814 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001815 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001816 else
1817 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001818
1819 cifs_small_buf_release(smb);
1820 return rc;
1821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001824CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1825 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 int rc = -EACCES;
1828 READ_REQ *pSMB = NULL;
1829 READ_RSP *pSMBr = NULL;
1830 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001831 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001832 int resp_buf_type = 0;
1833 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001834 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001835 __u32 pid = io_parms->pid;
1836 __u16 netfid = io_parms->netfid;
1837 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001838 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001839 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Joe Perchesf96637b2013-05-04 22:12:25 -05001841 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001842 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001843 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001844 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001845 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001846 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001847 /* can not handle this big offset for old */
1848 return -EIO;
1849 }
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001853 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (rc)
1855 return rc;
1856
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001857 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1858 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* tcon and ses pointer are checked in smb_init */
1861 if (tcon->ses->server == NULL)
1862 return -ECONNABORTED;
1863
Steve Frenchec637e32005-12-12 20:53:18 -08001864 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001866 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001867 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001868 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 pSMB->Remaining = 0;
1871 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1872 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001873 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001874 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1875 else {
1876 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001877 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001878 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001879 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001880 }
Steve Frenchec637e32005-12-12 20:53:18 -08001881
1882 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001883 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001884 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1885 CIFS_LOG_ERROR, &rsp_iov);
1886 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001887 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001888 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001890 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 } else {
1892 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1893 data_length = data_length << 16;
1894 data_length += le16_to_cpu(pSMBr->DataLength);
1895 *nbytes = data_length;
1896
1897 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001898 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001900 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001901 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 rc = -EIO;
1903 *nbytes = 0;
1904 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001905 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001906 le16_to_cpu(pSMBr->DataOffset);
1907/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001908 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001909 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001910 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001911 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001912 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Steve French790fe572007-07-07 19:25:05 +00001916 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001917 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001918 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001919 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001920 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001921 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001922 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001923 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001924 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001925 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001926
1927 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 since file handle passed in no longer valid */
1929 return rc;
1930}
1931
Steve Frenchec637e32005-12-12 20:53:18 -08001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001934CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001935 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 int rc = -EACCES;
1938 WRITE_REQ *pSMB = NULL;
1939 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001940 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 __u32 bytes_sent;
1942 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001943 __u32 pid = io_parms->pid;
1944 __u16 netfid = io_parms->netfid;
1945 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001946 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001947 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Steve Frencha24e2d72010-04-03 17:20:21 +00001949 *nbytes = 0;
1950
Joe Perchesf96637b2013-05-04 22:12:25 -05001951 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001952 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001953 return -ECONNABORTED;
1954
Steve French790fe572007-07-07 19:25:05 +00001955 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001956 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001957 else {
Steve French1c955182005-08-30 20:58:07 -07001958 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001959 if ((offset >> 32) > 0) {
1960 /* can not handle big offset for old srv */
1961 return -EIO;
1962 }
1963 }
Steve French1c955182005-08-30 20:58:07 -07001964
1965 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 (void **) &pSMBr);
1967 if (rc)
1968 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001969
1970 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1971 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /* tcon and ses pointer are checked in smb_init */
1974 if (tcon->ses->server == NULL)
1975 return -ECONNABORTED;
1976
1977 pSMB->AndXCommand = 0xFF; /* none */
1978 pSMB->Fid = netfid;
1979 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001980 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001981 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 pSMB->Reserved = 0xFFFFFFFF;
1984 pSMB->WriteMode = 0;
1985 pSMB->Remaining = 0;
1986
Steve French50c2f752007-07-13 00:33:32 +00001987 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 can send more if LARGE_WRITE_X capability returned by the server and if
1989 our buffer is big enough or if we convert to iovecs on socket writes
1990 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001991 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1993 } else {
1994 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1995 & ~0xFF;
1996 }
1997
1998 if (bytes_sent > count)
1999 bytes_sent = count;
2000 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002001 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00002002 if (buf)
Steve French61e74802008-12-03 00:57:54 +00002003 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04002004 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /* No buffer */
2006 cifs_buf_release(pSMB);
2007 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07002008 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00002009 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07002010 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00002011 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07002012 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
2015 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002016 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07002017
Steve French790fe572007-07-07 19:25:05 +00002018 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07002019 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00002020 else { /* old style write has byte count 4 bytes earlier
2021 so 4 bytes pad */
2022 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07002023 (struct smb_com_writex_req *)pSMB;
2024 pSMBW->ByteCount = cpu_to_le16(byte_count);
2025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04002028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002029 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002031 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else {
2033 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2034 *nbytes = (*nbytes) << 16;
2035 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302036
2037 /*
2038 * Mask off high 16 bits when bytes written as returned by the
2039 * server is greater than bytes requested by the client. Some
2040 * OS/2 servers are known to set incorrect CountHigh values.
2041 */
2042 if (*nbytes > count)
2043 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
2046 cifs_buf_release(pSMB);
2047
Steve French50c2f752007-07-13 00:33:32 +00002048 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 since file handle passed in no longer valid */
2050
2051 return rc;
2052}
2053
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002054void
2055cifs_writedata_release(struct kref *refcount)
2056{
2057 struct cifs_writedata *wdata = container_of(refcount,
2058 struct cifs_writedata, refcount);
Long Lidb223a52017-11-22 17:38:45 -07002059#ifdef CONFIG_CIFS_SMB_DIRECT
2060 if (wdata->mr) {
2061 smbd_deregister_mr(wdata->mr);
2062 wdata->mr = NULL;
2063 }
2064#endif
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002065
2066 if (wdata->cfile)
2067 cifsFileInfo_put(wdata->cfile);
2068
Long Li8e7360f2018-05-30 12:47:56 -07002069 kvfree(wdata->pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002070 kfree(wdata);
2071}
2072
2073/*
2074 * Write failed with a retryable error. Resend the write request. It's also
2075 * possible that the page was redirtied so re-clean the page.
2076 */
2077static void
2078cifs_writev_requeue(struct cifs_writedata *wdata)
2079{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002080 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00002081 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07002082 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002083 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002084
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002085 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2086 i = 0;
2087 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002088 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002089 struct cifs_writedata *wdata2;
2090 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002091
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002092 wsize = server->ops->wp_retry_size(inode);
2093 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002094 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002095 if (!nr_pages) {
2096 rc = -ENOTSUPP;
2097 break;
2098 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002099 cur_len = nr_pages * PAGE_SIZE;
2100 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002101 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002102 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002103 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002104 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06002105 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002106
2107 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2108 if (!wdata2) {
2109 rc = -ENOMEM;
2110 break;
2111 }
2112
2113 for (j = 0; j < nr_pages; j++) {
2114 wdata2->pages[j] = wdata->pages[i + j];
2115 lock_page(wdata2->pages[j]);
2116 clear_page_dirty_for_io(wdata2->pages[j]);
2117 }
2118
2119 wdata2->sync_mode = wdata->sync_mode;
2120 wdata2->nr_pages = nr_pages;
2121 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002122 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002123 wdata2->tailsz = tailsz;
2124 wdata2->bytes = cur_len;
2125
2126 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
2127 if (!wdata2->cfile) {
2128 cifs_dbg(VFS, "No writable handles for inode\n");
2129 rc = -EBADF;
2130 break;
2131 }
2132 wdata2->pid = wdata2->cfile->pid;
2133 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
2134
2135 for (j = 0; j < nr_pages; j++) {
2136 unlock_page(wdata2->pages[j]);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002137 if (rc != 0 && !is_retryable_error(rc)) {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002138 SetPageError(wdata2->pages[j]);
2139 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002141 }
2142 }
2143
2144 if (rc) {
2145 kref_put(&wdata2->refcount, cifs_writedata_release);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002146 if (is_retryable_error(rc))
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002147 continue;
2148 break;
2149 }
2150
2151 rest_len -= cur_len;
2152 i += nr_pages;
2153 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002154
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002155 if (rc != 0 && !is_retryable_error(rc))
2156 mapping_set_error(inode->i_mapping, rc);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002157 kref_put(&wdata->refcount, cifs_writedata_release);
2158}
2159
Jeff Laytonc2e87642012-03-23 14:40:55 -04002160void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002161cifs_writev_complete(struct work_struct *work)
2162{
2163 struct cifs_writedata *wdata = container_of(work,
2164 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002165 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002166 int i = 0;
2167
2168 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002169 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002170 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002171 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002172 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2173 wdata->bytes);
2174 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2175 return cifs_writev_requeue(wdata);
2176
2177 for (i = 0; i < wdata->nr_pages; i++) {
2178 struct page *page = wdata->pages[i];
2179 if (wdata->result == -EAGAIN)
2180 __set_page_dirty_nobuffers(page);
2181 else if (wdata->result < 0)
2182 SetPageError(page);
2183 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002184 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002185 }
2186 if (wdata->result != -EAGAIN)
2187 mapping_set_error(inode->i_mapping, wdata->result);
2188 kref_put(&wdata->refcount, cifs_writedata_release);
2189}
2190
2191struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002192cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002193{
Long Li8e7360f2018-05-30 12:47:56 -07002194 struct page **pages =
Kees Cook6396bb22018-06-12 14:03:40 -07002195 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Long Li8e7360f2018-05-30 12:47:56 -07002196 if (pages)
2197 return cifs_writedata_direct_alloc(pages, complete);
2198
2199 return NULL;
2200}
2201
2202struct cifs_writedata *
2203cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2204{
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002205 struct cifs_writedata *wdata;
2206
Long Li8e7360f2018-05-30 12:47:56 -07002207 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002208 if (wdata != NULL) {
Long Li8e7360f2018-05-30 12:47:56 -07002209 wdata->pages = pages;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002210 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002211 INIT_LIST_HEAD(&wdata->list);
2212 init_completion(&wdata->done);
2213 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002214 }
2215 return wdata;
2216}
2217
2218/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002219 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002220 * workqueue completion task.
2221 */
2222static void
2223cifs_writev_callback(struct mid_q_entry *mid)
2224{
2225 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002226 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002227 unsigned int written;
2228 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2229
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002230 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002231 case MID_RESPONSE_RECEIVED:
2232 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2233 if (wdata->result != 0)
2234 break;
2235
2236 written = le16_to_cpu(smb->CountHigh);
2237 written <<= 16;
2238 written += le16_to_cpu(smb->Count);
2239 /*
2240 * Mask off high 16 bits when bytes written as returned
2241 * by the server is greater than bytes requested by the
2242 * client. OS/2 servers are known to set incorrect
2243 * CountHigh values.
2244 */
2245 if (written > wdata->bytes)
2246 written &= 0xFFFF;
2247
2248 if (written < wdata->bytes)
2249 wdata->result = -ENOSPC;
2250 else
2251 wdata->bytes = written;
2252 break;
2253 case MID_REQUEST_SUBMITTED:
2254 case MID_RETRY_NEEDED:
2255 wdata->result = -EAGAIN;
2256 break;
2257 default:
2258 wdata->result = -EIO;
2259 break;
2260 }
2261
Jeff Laytonda472fc2012-03-23 14:40:53 -04002262 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002263 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002264 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002265}
2266
2267/* cifs_async_writev - send an async write, and set up mid to handle result */
2268int
Steve French4a5c80d2014-02-07 20:45:12 -06002269cifs_async_writev(struct cifs_writedata *wdata,
2270 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002271{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002272 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002273 WRITE_REQ *smb = NULL;
2274 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002275 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002276 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002277 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002278
2279 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2280 wct = 14;
2281 } else {
2282 wct = 12;
2283 if (wdata->offset >> 32 > 0) {
2284 /* can not handle big offset for old srv */
2285 return -EIO;
2286 }
2287 }
2288
2289 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2290 if (rc)
2291 goto async_writev_out;
2292
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002293 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2294 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002295
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002296 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002297 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002298 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2299 if (wct == 14)
2300 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2301 smb->Reserved = 0xFFFFFFFF;
2302 smb->WriteMode = 0;
2303 smb->Remaining = 0;
2304
2305 smb->DataOffset =
2306 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2307
2308 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002309 iov[0].iov_len = 4;
2310 iov[0].iov_base = smb;
2311 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2312 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002313
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002314 rqst.rq_iov = iov;
2315 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002316 rqst.rq_pages = wdata->pages;
Long Li6d3adb22018-09-20 21:18:38 +00002317 rqst.rq_offset = wdata->page_offset;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002318 rqst.rq_npages = wdata->nr_pages;
2319 rqst.rq_pagesz = wdata->pagesz;
2320 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002321
Joe Perchesf96637b2013-05-04 22:12:25 -05002322 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2323 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002324
2325 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2326 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2327
2328 if (wct == 14) {
2329 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2330 put_bcc(wdata->bytes + 1, &smb->hdr);
2331 } else {
2332 /* wct == 12 */
2333 struct smb_com_writex_req *smbw =
2334 (struct smb_com_writex_req *)smb;
2335 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2336 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002337 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002338 }
2339
2340 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002341 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002342 cifs_writev_callback, NULL, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002343
2344 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002345 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002346 else
Steve French4a5c80d2014-02-07 20:45:12 -06002347 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002348
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002349async_writev_out:
2350 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002351 return rc;
2352}
2353
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002354int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002355CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002356 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 int rc = -EACCES;
2359 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002360 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002361 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002362 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002363 __u32 pid = io_parms->pid;
2364 __u16 netfid = io_parms->netfid;
2365 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002366 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002367 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002368 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002370 *nbytes = 0;
2371
Joe Perchesf96637b2013-05-04 22:12:25 -05002372 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002373
Steve French4c3130e2008-12-09 00:28:16 +00002374 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002375 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002376 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002377 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002378 if ((offset >> 32) > 0) {
2379 /* can not handle big offset for old srv */
2380 return -EIO;
2381 }
2382 }
Steve French8cc64c62005-10-03 13:49:43 -07002383 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (rc)
2385 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002386
2387 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2388 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 /* tcon and ses pointer are checked in smb_init */
2391 if (tcon->ses->server == NULL)
2392 return -ECONNABORTED;
2393
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002394 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 pSMB->Fid = netfid;
2396 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002397 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002398 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 pSMB->Reserved = 0xFFFFFFFF;
2400 pSMB->WriteMode = 0;
2401 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002404 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Steve French3e844692005-10-03 13:37:24 -07002406 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2407 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002408 /* header + 1 byte pad */
2409 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002410 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002411 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002412 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002413 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002414 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002415 pSMB->ByteCount = cpu_to_le16(count + 1);
2416 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002417 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002418 (struct smb_com_writex_req *)pSMB;
2419 pSMBW->ByteCount = cpu_to_le16(count + 5);
2420 }
Steve French3e844692005-10-03 13:37:24 -07002421 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002422 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002423 iov[0].iov_len = smb_hdr_len + 4;
2424 else /* wct == 12 pad bigger by four bytes */
2425 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002426
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002427 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2428 &rsp_iov);
2429 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002430 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002432 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002433 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002434 /* presumably this can not happen, but best to be safe */
2435 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002436 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002437 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002438 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2439 *nbytes = (*nbytes) << 16;
2440 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302441
2442 /*
2443 * Mask off high 16 bits when bytes written as returned by the
2444 * server is greater than bytes requested by the client. OS/2
2445 * servers are known to set incorrect CountHigh values.
2446 */
2447 if (*nbytes > count)
2448 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002451 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Steve French50c2f752007-07-13 00:33:32 +00002453 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 since file handle passed in no longer valid */
2455
2456 return rc;
2457}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002458
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002459int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2460 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002461 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2462{
2463 int rc = 0;
2464 LOCK_REQ *pSMB = NULL;
2465 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002466 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002467 int resp_buf_type;
2468 __u16 count;
2469
Joe Perchesf96637b2013-05-04 22:12:25 -05002470 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2471 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002472
2473 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2474 if (rc)
2475 return rc;
2476
2477 pSMB->Timeout = 0;
2478 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2479 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2480 pSMB->LockType = lock_type;
2481 pSMB->AndXCommand = 0xFF; /* none */
2482 pSMB->Fid = netfid; /* netfid stays le */
2483
2484 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2485 inc_rfc1001_len(pSMB, count);
2486 pSMB->ByteCount = cpu_to_le16(count);
2487
2488 iov[0].iov_base = (char *)pSMB;
2489 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2490 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2491 iov[1].iov_base = (char *)buf;
2492 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2493
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002494 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002495 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2496 &rsp_iov);
2497 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002498 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002499 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002500
2501 return rc;
2502}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002505CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002506 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002508 const __u32 numLock, const __u8 lockType,
2509 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 int rc = 0;
2512 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002513/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002515 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 __u16 count;
2517
Joe Perchesf96637b2013-05-04 22:12:25 -05002518 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2519 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002520 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (rc)
2523 return rc;
2524
Steve French790fe572007-07-07 19:25:05 +00002525 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002526 /* no response expected */
2527 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002529 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002530 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2532 } else {
2533 pSMB->Timeout = 0;
2534 }
2535
2536 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2537 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2538 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002539 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 pSMB->AndXCommand = 0xFF; /* none */
2541 pSMB->Fid = smb_file_id; /* netfid stays le */
2542
Steve French790fe572007-07-07 19:25:05 +00002543 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002544 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /* BB where to store pid high? */
2546 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2547 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2548 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2549 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2550 count = sizeof(LOCKING_ANDX_RANGE);
2551 } else {
2552 /* oplock break */
2553 count = 0;
2554 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002555 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 pSMB->ByteCount = cpu_to_le16(count);
2557
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002558 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002559 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002560 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002561 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002562 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002563 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002564 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002565 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002566 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Steve French50c2f752007-07-13 00:33:32 +00002568 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 since file handle passed in no longer valid */
2570 return rc;
2571}
2572
2573int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002574CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002575 const __u16 smb_file_id, const __u32 netpid,
2576 const loff_t start_offset, const __u64 len,
2577 struct file_lock *pLockData, const __u16 lock_type,
2578 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002579{
2580 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2581 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002582 struct cifs_posix_lock *parm_data;
2583 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002584 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002585 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002586 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002587 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002588 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002589 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002590
Joe Perchesf96637b2013-05-04 22:12:25 -05002591 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002592
Steve French08547b02006-02-28 22:39:25 +00002593 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2594
2595 if (rc)
2596 return rc;
2597
2598 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2599
Steve French50c2f752007-07-13 00:33:32 +00002600 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002601 pSMB->MaxSetupCount = 0;
2602 pSMB->Reserved = 0;
2603 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002604 pSMB->Reserved2 = 0;
2605 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2606 offset = param_offset + params;
2607
Steve French08547b02006-02-28 22:39:25 +00002608 count = sizeof(struct cifs_posix_lock);
2609 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002610 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002611 pSMB->SetupCount = 1;
2612 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002613 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002614 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2615 else
2616 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2617 byte_count = 3 /* pad */ + params + count;
2618 pSMB->DataCount = cpu_to_le16(count);
2619 pSMB->ParameterCount = cpu_to_le16(params);
2620 pSMB->TotalDataCount = pSMB->DataCount;
2621 pSMB->TotalParameterCount = pSMB->ParameterCount;
2622 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002623 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002624 (((char *) &pSMB->hdr.Protocol) + offset);
2625
2626 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002627 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002628 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002629 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002630 pSMB->Timeout = cpu_to_le32(-1);
2631 } else
2632 pSMB->Timeout = 0;
2633
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002634 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002635 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002636 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002637
2638 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002639 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002640 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2641 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002642 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002643 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002644 if (waitFlag) {
2645 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2646 (struct smb_hdr *) pSMBr, &bytes_returned);
2647 } else {
Steve French133672e2007-11-13 22:41:37 +00002648 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002649 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002650 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002651 &resp_buf_type, timeout, &rsp_iov);
2652 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002653 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002654 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002655
Steve French08547b02006-02-28 22:39:25 +00002656 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002657 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002658 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002659 /* lock structure can be returned on get */
2660 __u16 data_offset;
2661 __u16 data_count;
2662 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002663
Jeff Layton820a8032011-05-04 08:05:26 -04002664 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002665 rc = -EIO; /* bad smb */
2666 goto plk_err_exit;
2667 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002668 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2669 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002670 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002671 rc = -EIO;
2672 goto plk_err_exit;
2673 }
2674 parm_data = (struct cifs_posix_lock *)
2675 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002676 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002677 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002678 else {
2679 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002680 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002681 pLockData->fl_type = F_RDLCK;
2682 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002683 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002684 pLockData->fl_type = F_WRLCK;
2685
Steve French5443d132011-03-13 05:08:25 +00002686 pLockData->fl_start = le64_to_cpu(parm_data->start);
2687 pLockData->fl_end = pLockData->fl_start +
2688 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002689 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002690 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002691 }
Steve French50c2f752007-07-13 00:33:32 +00002692
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002693plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002694 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002695
Steve French08547b02006-02-28 22:39:25 +00002696 /* Note: On -EAGAIN error only caller can retry on handle based calls
2697 since file handle passed in no longer valid */
2698
2699 return rc;
2700}
2701
2702
2703int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002704CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
2706 int rc = 0;
2707 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002708 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710/* do not retry on dead session on close */
2711 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002712 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return 0;
2714 if (rc)
2715 return rc;
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002718 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002720 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002721 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002722 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002724 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002726 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
2728 }
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002731 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 rc = 0;
2733
2734 return rc;
2735}
2736
2737int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002738CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002739{
2740 int rc = 0;
2741 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002742 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002743
2744 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2745 if (rc)
2746 return rc;
2747
2748 pSMB->FileID = (__u16) smb_file_id;
2749 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002750 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002751 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002752 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002753 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002754 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002755
2756 return rc;
2757}
2758
2759int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002760CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002761 const char *from_name, const char *to_name,
2762 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 int rc = 0;
2765 RENAME_REQ *pSMB = NULL;
2766 RENAME_RSP *pSMBr = NULL;
2767 int bytes_returned;
2768 int name_len, name_len2;
2769 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002770 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Joe Perchesf96637b2013-05-04 22:12:25 -05002772 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773renameRetry:
2774 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2775 (void **) &pSMBr);
2776 if (rc)
2777 return rc;
2778
2779 pSMB->BufferFormat = 0x04;
2780 pSMB->SearchAttributes =
2781 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2782 ATTR_DIRECTORY);
2783
2784 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002785 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2786 from_name, PATH_MAX,
2787 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 name_len++; /* trailing null */
2789 name_len *= 2;
2790 pSMB->OldFileName[name_len] = 0x04; /* pad */
2791 /* protocol requires ASCII signature byte on Unicode string */
2792 pSMB->OldFileName[name_len + 1] = 0x00;
2793 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002794 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002795 to_name, PATH_MAX, cifs_sb->local_nls,
2796 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2798 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002799 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002800 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002802 strncpy(pSMB->OldFileName, from_name, name_len);
2803 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 name_len2++; /* trailing null */
2805 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002806 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 name_len2++; /* trailing null */
2808 name_len2++; /* signature byte */
2809 }
2810
2811 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002812 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 pSMB->ByteCount = cpu_to_le16(count);
2814
2815 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2816 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002817 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002818 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002819 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 cifs_buf_release(pSMB);
2822
2823 if (rc == -EAGAIN)
2824 goto renameRetry;
2825
2826 return rc;
2827}
2828
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002829int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002830 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002831 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2834 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002835 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 char *data_offset;
2837 char dummy_string[30];
2838 int rc = 0;
2839 int bytes_returned = 0;
2840 int len_of_str;
2841 __u16 params, param_offset, offset, count, byte_count;
2842
Joe Perchesf96637b2013-05-04 22:12:25 -05002843 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2845 (void **) &pSMBr);
2846 if (rc)
2847 return rc;
2848
2849 params = 6;
2850 pSMB->MaxSetupCount = 0;
2851 pSMB->Reserved = 0;
2852 pSMB->Flags = 0;
2853 pSMB->Timeout = 0;
2854 pSMB->Reserved2 = 0;
2855 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2856 offset = param_offset + params;
2857
2858 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2859 rename_info = (struct set_file_rename *) data_offset;
2860 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002861 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 pSMB->SetupCount = 1;
2863 pSMB->Reserved3 = 0;
2864 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2865 byte_count = 3 /* pad */ + params;
2866 pSMB->ParameterCount = cpu_to_le16(params);
2867 pSMB->TotalParameterCount = pSMB->ParameterCount;
2868 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2869 pSMB->DataOffset = cpu_to_le16(offset);
2870 /* construct random name ".cifs_tmp<inodenum><mid>" */
2871 rename_info->overwrite = cpu_to_le32(1);
2872 rename_info->root_fid = 0;
2873 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002874 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002875 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002876 len_of_str =
2877 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002878 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002880 len_of_str =
2881 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002882 target_name, PATH_MAX, nls_codepage,
2883 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
2885 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002886 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 byte_count += count;
2888 pSMB->DataCount = cpu_to_le16(count);
2889 pSMB->TotalDataCount = pSMB->DataCount;
2890 pSMB->Fid = netfid;
2891 pSMB->InformationLevel =
2892 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2893 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002894 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 pSMB->ByteCount = cpu_to_le16(byte_count);
2896 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002897 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002898 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002899 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002900 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2901 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 cifs_buf_release(pSMB);
2904
2905 /* Note: On -EAGAIN error only caller can retry on handle based calls
2906 since file handle passed in no longer valid */
2907
2908 return rc;
2909}
2910
2911int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002912CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2913 const char *fromName, const __u16 target_tid, const char *toName,
2914 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 int rc = 0;
2917 COPY_REQ *pSMB = NULL;
2918 COPY_RSP *pSMBr = NULL;
2919 int bytes_returned;
2920 int name_len, name_len2;
2921 __u16 count;
2922
Joe Perchesf96637b2013-05-04 22:12:25 -05002923 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924copyRetry:
2925 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2926 (void **) &pSMBr);
2927 if (rc)
2928 return rc;
2929
2930 pSMB->BufferFormat = 0x04;
2931 pSMB->Tid2 = target_tid;
2932
2933 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2934
2935 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002936 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2937 fromName, PATH_MAX, nls_codepage,
2938 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 name_len++; /* trailing null */
2940 name_len *= 2;
2941 pSMB->OldFileName[name_len] = 0x04; /* pad */
2942 /* protocol requires ASCII signature byte on Unicode string */
2943 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002944 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002945 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2946 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2948 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002949 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 name_len = strnlen(fromName, PATH_MAX);
2951 name_len++; /* trailing null */
2952 strncpy(pSMB->OldFileName, fromName, name_len);
2953 name_len2 = strnlen(toName, PATH_MAX);
2954 name_len2++; /* trailing null */
2955 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2956 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2957 name_len2++; /* trailing null */
2958 name_len2++; /* signature byte */
2959 }
2960
2961 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002962 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 pSMB->ByteCount = cpu_to_le16(count);
2964
2965 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2966 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2967 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002968 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2969 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Steve French0d817bc2008-05-22 02:02:03 +00002971 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 if (rc == -EAGAIN)
2974 goto copyRetry;
2975
2976 return rc;
2977}
2978
2979int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002980CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002982 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 TRANSACTION2_SPI_REQ *pSMB = NULL;
2985 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2986 char *data_offset;
2987 int name_len;
2988 int name_len_target;
2989 int rc = 0;
2990 int bytes_returned = 0;
2991 __u16 params, param_offset, offset, byte_count;
2992
Joe Perchesf96637b2013-05-04 22:12:25 -05002993 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994createSymLinkRetry:
2995 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2996 (void **) &pSMBr);
2997 if (rc)
2998 return rc;
2999
3000 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3001 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003002 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
3003 /* find define for this maxpathcomponent */
3004 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 name_len++; /* trailing null */
3006 name_len *= 2;
3007
Steve French50c2f752007-07-13 00:33:32 +00003008 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 name_len = strnlen(fromName, PATH_MAX);
3010 name_len++; /* trailing null */
3011 strncpy(pSMB->FileName, fromName, name_len);
3012 }
3013 params = 6 + name_len;
3014 pSMB->MaxSetupCount = 0;
3015 pSMB->Reserved = 0;
3016 pSMB->Flags = 0;
3017 pSMB->Timeout = 0;
3018 pSMB->Reserved2 = 0;
3019 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003020 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 offset = param_offset + params;
3022
3023 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3024 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3025 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003026 cifsConvertToUTF16((__le16 *) data_offset, toName,
3027 /* find define for this maxpathcomponent */
3028 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 name_len_target++; /* trailing null */
3030 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003031 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 name_len_target = strnlen(toName, PATH_MAX);
3033 name_len_target++; /* trailing null */
3034 strncpy(data_offset, toName, name_len_target);
3035 }
3036
3037 pSMB->MaxParameterCount = cpu_to_le16(2);
3038 /* BB find exact max on data count below from sess */
3039 pSMB->MaxDataCount = cpu_to_le16(1000);
3040 pSMB->SetupCount = 1;
3041 pSMB->Reserved3 = 0;
3042 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3043 byte_count = 3 /* pad */ + params + name_len_target;
3044 pSMB->DataCount = cpu_to_le16(name_len_target);
3045 pSMB->ParameterCount = cpu_to_le16(params);
3046 pSMB->TotalDataCount = pSMB->DataCount;
3047 pSMB->TotalParameterCount = pSMB->ParameterCount;
3048 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3049 pSMB->DataOffset = cpu_to_le16(offset);
3050 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
3051 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003052 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 pSMB->ByteCount = cpu_to_le16(byte_count);
3054 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3055 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003056 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003057 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003058 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
3059 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Steve French0d817bc2008-05-22 02:02:03 +00003061 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 if (rc == -EAGAIN)
3064 goto createSymLinkRetry;
3065
3066 return rc;
3067}
3068
3069int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003070CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07003072 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 TRANSACTION2_SPI_REQ *pSMB = NULL;
3075 TRANSACTION2_SPI_RSP *pSMBr = NULL;
3076 char *data_offset;
3077 int name_len;
3078 int name_len_target;
3079 int rc = 0;
3080 int bytes_returned = 0;
3081 __u16 params, param_offset, offset, byte_count;
3082
Joe Perchesf96637b2013-05-04 22:12:25 -05003083 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084createHardLinkRetry:
3085 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3086 (void **) &pSMBr);
3087 if (rc)
3088 return rc;
3089
3090 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06003091 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
3092 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 name_len++; /* trailing null */
3094 name_len *= 2;
3095
Steve French50c2f752007-07-13 00:33:32 +00003096 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 name_len = strnlen(toName, PATH_MAX);
3098 name_len++; /* trailing null */
3099 strncpy(pSMB->FileName, toName, name_len);
3100 }
3101 params = 6 + name_len;
3102 pSMB->MaxSetupCount = 0;
3103 pSMB->Reserved = 0;
3104 pSMB->Flags = 0;
3105 pSMB->Timeout = 0;
3106 pSMB->Reserved2 = 0;
3107 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003108 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 offset = param_offset + params;
3110
3111 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
3112 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3113 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06003114 cifsConvertToUTF16((__le16 *) data_offset, fromName,
3115 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 name_len_target++; /* trailing null */
3117 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003118 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 name_len_target = strnlen(fromName, PATH_MAX);
3120 name_len_target++; /* trailing null */
3121 strncpy(data_offset, fromName, name_len_target);
3122 }
3123
3124 pSMB->MaxParameterCount = cpu_to_le16(2);
3125 /* BB find exact max on data count below from sess*/
3126 pSMB->MaxDataCount = cpu_to_le16(1000);
3127 pSMB->SetupCount = 1;
3128 pSMB->Reserved3 = 0;
3129 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3130 byte_count = 3 /* pad */ + params + name_len_target;
3131 pSMB->ParameterCount = cpu_to_le16(params);
3132 pSMB->TotalParameterCount = pSMB->ParameterCount;
3133 pSMB->DataCount = cpu_to_le16(name_len_target);
3134 pSMB->TotalDataCount = pSMB->DataCount;
3135 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3136 pSMB->DataOffset = cpu_to_le16(offset);
3137 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
3138 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003139 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 pSMB->ByteCount = cpu_to_le16(byte_count);
3141 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3142 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003143 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003144 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003145 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
3146 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 cifs_buf_release(pSMB);
3149 if (rc == -EAGAIN)
3150 goto createHardLinkRetry;
3151
3152 return rc;
3153}
3154
3155int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003156CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07003157 const char *from_name, const char *to_name,
3158 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
3160 int rc = 0;
3161 NT_RENAME_REQ *pSMB = NULL;
3162 RENAME_RSP *pSMBr = NULL;
3163 int bytes_returned;
3164 int name_len, name_len2;
3165 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003166 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Joe Perchesf96637b2013-05-04 22:12:25 -05003168 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169winCreateHardLinkRetry:
3170
3171 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3172 (void **) &pSMBr);
3173 if (rc)
3174 return rc;
3175
3176 pSMB->SearchAttributes =
3177 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3178 ATTR_DIRECTORY);
3179 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3180 pSMB->ClusterCount = 0;
3181
3182 pSMB->BufferFormat = 0x04;
3183
3184 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3185 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003186 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3187 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 name_len++; /* trailing null */
3189 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003190
3191 /* protocol specifies ASCII buffer format (0x04) for unicode */
3192 pSMB->OldFileName[name_len] = 0x04;
3193 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003195 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003196 to_name, PATH_MAX, cifs_sb->local_nls,
3197 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3199 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003200 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003201 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003203 strncpy(pSMB->OldFileName, from_name, name_len);
3204 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 name_len2++; /* trailing null */
3206 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003207 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 name_len2++; /* trailing null */
3209 name_len2++; /* signature byte */
3210 }
3211
3212 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003213 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 pSMB->ByteCount = cpu_to_le16(count);
3215
3216 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3217 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003218 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003219 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003220 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 cifs_buf_release(pSMB);
3223 if (rc == -EAGAIN)
3224 goto winCreateHardLinkRetry;
3225
3226 return rc;
3227}
3228
3229int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003230CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003231 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003232 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234/* SMB_QUERY_FILE_UNIX_LINK */
3235 TRANSACTION2_QPI_REQ *pSMB = NULL;
3236 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3237 int rc = 0;
3238 int bytes_returned;
3239 int name_len;
3240 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003241 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Joe Perchesf96637b2013-05-04 22:12:25 -05003243 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245querySymLinkRetry:
3246 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3247 (void **) &pSMBr);
3248 if (rc)
3249 return rc;
3250
3251 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3252 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003253 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3254 searchName, PATH_MAX, nls_codepage,
3255 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 name_len++; /* trailing null */
3257 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003258 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 name_len = strnlen(searchName, PATH_MAX);
3260 name_len++; /* trailing null */
3261 strncpy(pSMB->FileName, searchName, name_len);
3262 }
3263
3264 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3265 pSMB->TotalDataCount = 0;
3266 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003267 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 pSMB->MaxSetupCount = 0;
3269 pSMB->Reserved = 0;
3270 pSMB->Flags = 0;
3271 pSMB->Timeout = 0;
3272 pSMB->Reserved2 = 0;
3273 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003274 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 pSMB->DataCount = 0;
3276 pSMB->DataOffset = 0;
3277 pSMB->SetupCount = 1;
3278 pSMB->Reserved3 = 0;
3279 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3280 byte_count = params + 1 /* pad */ ;
3281 pSMB->TotalParameterCount = cpu_to_le16(params);
3282 pSMB->ParameterCount = pSMB->TotalParameterCount;
3283 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3284 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003285 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 pSMB->ByteCount = cpu_to_le16(byte_count);
3287
3288 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3289 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3290 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003291 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 } else {
3293 /* decode response */
3294
3295 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003297 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003298 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003300 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003301 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Jeff Layton460b9692009-04-30 07:17:56 -04003303 data_start = ((char *) &pSMBr->hdr.Protocol) +
3304 le16_to_cpu(pSMBr->t2.DataOffset);
3305
Steve French0e0d2cf2009-05-01 05:27:32 +00003306 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3307 is_unicode = true;
3308 else
3309 is_unicode = false;
3310
Steve French737b7582005-04-28 22:41:06 -07003311 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003312 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3313 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003314 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003315 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
3317 }
3318 cifs_buf_release(pSMB);
3319 if (rc == -EAGAIN)
3320 goto querySymLinkRetry;
3321 return rc;
3322}
3323
Steve Frenchc52a95542011-02-24 06:16:22 +00003324/*
3325 * Recent Windows versions now create symlinks more frequently
3326 * and they use the "reparse point" mechanism below. We can of course
3327 * do symlinks nicely to Samba and other servers which support the
3328 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3329 * "MF" symlinks optionally, but for recent Windows we really need to
3330 * reenable the code below and fix the cifs_symlink callers to handle this.
3331 * In the interim this code has been moved to its own config option so
3332 * it is not compiled in by default until callers fixed up and more tested.
3333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003335CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3336 __u16 fid, char **symlinkinfo,
3337 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 int rc = 0;
3340 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003341 struct smb_com_transaction_ioctl_req *pSMB;
3342 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003343 bool is_unicode;
3344 unsigned int sub_len;
3345 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003346 struct reparse_symlink_data *reparse_buf;
3347 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003348 __u32 data_offset, data_count;
3349 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003351 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3353 (void **) &pSMBr);
3354 if (rc)
3355 return rc;
3356
3357 pSMB->TotalParameterCount = 0 ;
3358 pSMB->TotalDataCount = 0;
3359 pSMB->MaxParameterCount = cpu_to_le32(2);
3360 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003361 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 pSMB->MaxSetupCount = 4;
3363 pSMB->Reserved = 0;
3364 pSMB->ParameterOffset = 0;
3365 pSMB->DataCount = 0;
3366 pSMB->DataOffset = 0;
3367 pSMB->SetupCount = 4;
3368 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3369 pSMB->ParameterCount = pSMB->TotalParameterCount;
3370 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3371 pSMB->IsFsctl = 1; /* FSCTL */
3372 pSMB->IsRootFlag = 0;
3373 pSMB->Fid = fid; /* file handle always le */
3374 pSMB->ByteCount = 0;
3375
3376 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3377 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3378 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003379 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003380 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
Steve French989c7e52009-05-02 05:32:20 +00003382
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003383 data_offset = le32_to_cpu(pSMBr->DataOffset);
3384 data_count = le32_to_cpu(pSMBr->DataCount);
3385 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3386 /* BB also check enough total bytes returned */
3387 rc = -EIO; /* bad smb */
3388 goto qreparse_out;
3389 }
3390 if (!data_count || (data_count > 2048)) {
3391 rc = -EIO;
3392 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3393 goto qreparse_out;
3394 }
3395 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003396 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003397 ((char *)&pSMBr->hdr.Protocol + data_offset);
3398 if ((char *)reparse_buf >= end_of_smb) {
3399 rc = -EIO;
3400 goto qreparse_out;
3401 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003402 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3403 cifs_dbg(FYI, "NFS style reparse tag\n");
3404 posix_buf = (struct reparse_posix_data *)reparse_buf;
3405
3406 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3407 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3408 le64_to_cpu(posix_buf->InodeType));
3409 rc = -EOPNOTSUPP;
3410 goto qreparse_out;
3411 }
3412 is_unicode = true;
3413 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3414 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3415 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3416 rc = -EIO;
3417 goto qreparse_out;
3418 }
3419 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3420 sub_len, is_unicode, nls_codepage);
3421 goto qreparse_out;
3422 } else if (reparse_buf->ReparseTag !=
3423 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3424 rc = -EOPNOTSUPP;
3425 goto qreparse_out;
3426 }
3427
3428 /* Reparse tag is NTFS symlink */
3429 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3430 reparse_buf->PathBuffer;
3431 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3432 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003433 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3434 rc = -EIO;
3435 goto qreparse_out;
3436 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003437 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3438 is_unicode = true;
3439 else
3440 is_unicode = false;
3441
3442 /* BB FIXME investigate remapping reserved chars here */
3443 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3444 nls_codepage);
3445 if (!*symlinkinfo)
3446 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003448 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003450 /*
3451 * Note: On -EAGAIN error only caller can retry on handle based calls
3452 * since file handle passed in no longer valid.
3453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 return rc;
3455}
3456
Steve Frenchc7f508a2013-10-14 15:27:32 -05003457int
3458CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3459 __u16 fid)
3460{
3461 int rc = 0;
3462 int bytes_returned;
3463 struct smb_com_transaction_compr_ioctl_req *pSMB;
3464 struct smb_com_transaction_ioctl_rsp *pSMBr;
3465
3466 cifs_dbg(FYI, "Set compression for %u\n", fid);
3467 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3468 (void **) &pSMBr);
3469 if (rc)
3470 return rc;
3471
3472 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3473
3474 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003475 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003476 pSMB->MaxParameterCount = 0;
3477 pSMB->MaxDataCount = 0;
3478 pSMB->MaxSetupCount = 4;
3479 pSMB->Reserved = 0;
3480 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003481 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003482 pSMB->DataOffset =
3483 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3484 compression_state) - 4); /* 84 */
3485 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003486 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003487 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003488 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003489 pSMB->IsFsctl = 1; /* FSCTL */
3490 pSMB->IsRootFlag = 0;
3491 pSMB->Fid = fid; /* file handle always le */
3492 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003493 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003494 inc_rfc1001_len(pSMB, 5);
3495
3496 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3497 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3498 if (rc)
3499 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3500
3501 cifs_buf_release(pSMB);
3502
3503 /*
3504 * Note: On -EAGAIN error only caller can retry on handle based calls
3505 * since file handle passed in no longer valid.
3506 */
3507 return rc;
3508}
3509
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511#ifdef CONFIG_CIFS_POSIX
3512
3513/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003514static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003515 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
3517 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003518 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3519 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3520 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003521/*
3522 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3523 ace->e_perm, ace->e_tag, ace->e_id);
3524*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 return;
3527}
3528
3529/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003530static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3531 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 int size = 0;
3534 int i;
3535 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003536 struct cifs_posix_ace *pACE;
3537 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003538 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3541 return -EOPNOTSUPP;
3542
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003543 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 count = le16_to_cpu(cifs_acl->access_entry_count);
3545 pACE = &cifs_acl->ace_array[0];
3546 size = sizeof(struct cifs_posix_acl);
3547 size += sizeof(struct cifs_posix_ace) * count;
3548 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003549 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003550 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3551 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return -EINVAL;
3553 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003554 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 count = le16_to_cpu(cifs_acl->access_entry_count);
3556 size = sizeof(struct cifs_posix_acl);
3557 size += sizeof(struct cifs_posix_ace) * count;
3558/* skip past access ACEs to get to default ACEs */
3559 pACE = &cifs_acl->ace_array[count];
3560 count = le16_to_cpu(cifs_acl->default_entry_count);
3561 size += sizeof(struct cifs_posix_ace) * count;
3562 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003563 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return -EINVAL;
3565 } else {
3566 /* illegal type */
3567 return -EINVAL;
3568 }
3569
3570 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003571 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003572 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003573 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return -ERANGE;
3575 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003576 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3577
Steve Frenchff7feac2005-11-15 16:45:16 -08003578 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003579 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003580 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003581 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
3583 }
3584 return size;
3585}
3586
Steve French50c2f752007-07-13 00:33:32 +00003587static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003588 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 __u16 rc = 0; /* 0 = ACL converted ok */
3591
Steve Frenchff7feac2005-11-15 16:45:16 -08003592 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3593 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003595 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 /* Probably no need to le convert -1 on any arch but can not hurt */
3597 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003598 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003599 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003600/*
3601 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3602 ace->e_perm, ace->e_tag, ace->e_id);
3603*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return rc;
3605}
3606
3607/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003608static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3609 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003612 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003613 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003614 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 int count;
3616 int i;
3617
Steve French790fe572007-07-07 19:25:05 +00003618 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return 0;
3620
3621 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003622 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3623 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003624 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003625 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3626 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return 0;
3628 }
3629 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003630 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003631 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003632 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003633 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003634 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003635 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003636 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003637 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return 0;
3639 }
Steve French50c2f752007-07-13 00:33:32 +00003640 for (i = 0; i < count; i++) {
Eryu Guanae9ebe72016-10-24 20:46:40 +08003641 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003642 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 /* ACE not converted */
3644 break;
3645 }
3646 }
Steve French790fe572007-07-07 19:25:05 +00003647 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3649 rc += sizeof(struct cifs_posix_acl);
3650 /* BB add check to make sure ACL does not overflow SMB */
3651 }
3652 return rc;
3653}
3654
3655int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003656CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003657 const unsigned char *searchName,
3658 char *acl_inf, const int buflen, const int acl_type,
3659 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
3661/* SMB_QUERY_POSIX_ACL */
3662 TRANSACTION2_QPI_REQ *pSMB = NULL;
3663 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3664 int rc = 0;
3665 int bytes_returned;
3666 int name_len;
3667 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003668
Joe Perchesf96637b2013-05-04 22:12:25 -05003669 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671queryAclRetry:
3672 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3673 (void **) &pSMBr);
3674 if (rc)
3675 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3678 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003679 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3680 searchName, PATH_MAX, nls_codepage,
3681 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 name_len++; /* trailing null */
3683 name_len *= 2;
3684 pSMB->FileName[name_len] = 0;
3685 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003686 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 name_len = strnlen(searchName, PATH_MAX);
3688 name_len++; /* trailing null */
3689 strncpy(pSMB->FileName, searchName, name_len);
3690 }
3691
3692 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3693 pSMB->TotalDataCount = 0;
3694 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003695 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 pSMB->MaxDataCount = cpu_to_le16(4000);
3697 pSMB->MaxSetupCount = 0;
3698 pSMB->Reserved = 0;
3699 pSMB->Flags = 0;
3700 pSMB->Timeout = 0;
3701 pSMB->Reserved2 = 0;
3702 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003703 offsetof(struct smb_com_transaction2_qpi_req,
3704 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 pSMB->DataCount = 0;
3706 pSMB->DataOffset = 0;
3707 pSMB->SetupCount = 1;
3708 pSMB->Reserved3 = 0;
3709 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3710 byte_count = params + 1 /* pad */ ;
3711 pSMB->TotalParameterCount = cpu_to_le16(params);
3712 pSMB->ParameterCount = pSMB->TotalParameterCount;
3713 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3714 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003715 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 pSMB->ByteCount = cpu_to_le16(byte_count);
3717
3718 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3719 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003720 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003722 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 } else {
3724 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003728 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 rc = -EIO; /* bad smb */
3730 else {
3731 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3732 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3733 rc = cifs_copy_posix_acl(acl_inf,
3734 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003735 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 }
3737 }
3738 cifs_buf_release(pSMB);
3739 if (rc == -EAGAIN)
3740 goto queryAclRetry;
3741 return rc;
3742}
3743
3744int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003745CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003746 const unsigned char *fileName,
3747 const char *local_acl, const int buflen,
3748 const int acl_type,
3749 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct smb_com_transaction2_spi_req *pSMB = NULL;
3752 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3753 char *parm_data;
3754 int name_len;
3755 int rc = 0;
3756 int bytes_returned = 0;
3757 __u16 params, byte_count, data_count, param_offset, offset;
3758
Joe Perchesf96637b2013-05-04 22:12:25 -05003759 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760setAclRetry:
3761 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003762 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (rc)
3764 return rc;
3765 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3766 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003767 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3768 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 name_len++; /* trailing null */
3770 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003771 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 name_len = strnlen(fileName, PATH_MAX);
3773 name_len++; /* trailing null */
3774 strncpy(pSMB->FileName, fileName, name_len);
3775 }
3776 params = 6 + name_len;
3777 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003778 /* BB find max SMB size from sess */
3779 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 pSMB->MaxSetupCount = 0;
3781 pSMB->Reserved = 0;
3782 pSMB->Flags = 0;
3783 pSMB->Timeout = 0;
3784 pSMB->Reserved2 = 0;
3785 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003786 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 offset = param_offset + params;
3788 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3789 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3790
3791 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003792 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Steve French790fe572007-07-07 19:25:05 +00003794 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 rc = -EOPNOTSUPP;
3796 goto setACLerrorExit;
3797 }
3798 pSMB->DataOffset = cpu_to_le16(offset);
3799 pSMB->SetupCount = 1;
3800 pSMB->Reserved3 = 0;
3801 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3802 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3803 byte_count = 3 /* pad */ + params + data_count;
3804 pSMB->DataCount = cpu_to_le16(data_count);
3805 pSMB->TotalDataCount = pSMB->DataCount;
3806 pSMB->ParameterCount = cpu_to_le16(params);
3807 pSMB->TotalParameterCount = pSMB->ParameterCount;
3808 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003809 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 pSMB->ByteCount = cpu_to_le16(byte_count);
3811 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003812 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003813 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003814 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816setACLerrorExit:
3817 cifs_buf_release(pSMB);
3818 if (rc == -EAGAIN)
3819 goto setAclRetry;
3820 return rc;
3821}
3822
Steve Frenchf654bac2005-04-28 22:41:04 -07003823/* BB fix tabs in this function FIXME BB */
3824int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003825CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003826 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003827{
Steve French50c2f752007-07-13 00:33:32 +00003828 int rc = 0;
3829 struct smb_t2_qfi_req *pSMB = NULL;
3830 struct smb_t2_qfi_rsp *pSMBr = NULL;
3831 int bytes_returned;
3832 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003833
Joe Perchesf96637b2013-05-04 22:12:25 -05003834 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003835 if (tcon == NULL)
3836 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003837
3838GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003839 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3840 (void **) &pSMBr);
3841 if (rc)
3842 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003843
Steve Frenchad7a2922008-02-07 23:25:02 +00003844 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003845 pSMB->t2.TotalDataCount = 0;
3846 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3847 /* BB find exact max data count below from sess structure BB */
3848 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3849 pSMB->t2.MaxSetupCount = 0;
3850 pSMB->t2.Reserved = 0;
3851 pSMB->t2.Flags = 0;
3852 pSMB->t2.Timeout = 0;
3853 pSMB->t2.Reserved2 = 0;
3854 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3855 Fid) - 4);
3856 pSMB->t2.DataCount = 0;
3857 pSMB->t2.DataOffset = 0;
3858 pSMB->t2.SetupCount = 1;
3859 pSMB->t2.Reserved3 = 0;
3860 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3861 byte_count = params + 1 /* pad */ ;
3862 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3863 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3864 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3865 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003866 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003867 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003868 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003869
Steve French790fe572007-07-07 19:25:05 +00003870 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3871 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3872 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003873 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003874 } else {
3875 /* decode response */
3876 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003877 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003878 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003879 /* If rc should we check for EOPNOSUPP and
3880 disable the srvino flag? or in caller? */
3881 rc = -EIO; /* bad smb */
3882 else {
3883 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3884 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3885 struct file_chattr_info *pfinfo;
3886 /* BB Do we need a cast or hash here ? */
3887 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003888 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003889 rc = -EIO;
3890 goto GetExtAttrOut;
3891 }
3892 pfinfo = (struct file_chattr_info *)
3893 (data_offset + (char *) &pSMBr->hdr.Protocol);
3894 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003895 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003896 }
3897 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003898GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003899 cifs_buf_release(pSMB);
3900 if (rc == -EAGAIN)
3901 goto GetExtAttrRetry;
3902 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003903}
3904
Steve Frenchf654bac2005-04-28 22:41:04 -07003905#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Jeff Layton79df1ba2010-12-06 12:52:08 -05003907#ifdef CONFIG_CIFS_ACL
3908/*
3909 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3910 * all NT TRANSACTS that we init here have total parm and data under about 400
3911 * bytes (to fit in small cifs buffer size), which is the case so far, it
3912 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3913 * returned setup area) and MaxParameterCount (returned parms size) must be set
3914 * by caller
3915 */
3916static int
3917smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003918 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003919 void **ret_buf)
3920{
3921 int rc;
3922 __u32 temp_offset;
3923 struct smb_com_ntransact_req *pSMB;
3924
3925 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3926 (void **)&pSMB);
3927 if (rc)
3928 return rc;
3929 *ret_buf = (void *)pSMB;
3930 pSMB->Reserved = 0;
3931 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3932 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003933 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003934 pSMB->ParameterCount = pSMB->TotalParameterCount;
3935 pSMB->DataCount = pSMB->TotalDataCount;
3936 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3937 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3938 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3939 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3940 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3941 pSMB->SubCommand = cpu_to_le16(sub_command);
3942 return 0;
3943}
3944
3945static int
3946validate_ntransact(char *buf, char **ppparm, char **ppdata,
3947 __u32 *pparmlen, __u32 *pdatalen)
3948{
3949 char *end_of_smb;
3950 __u32 data_count, data_offset, parm_count, parm_offset;
3951 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003952 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003953
3954 *pdatalen = 0;
3955 *pparmlen = 0;
3956
3957 if (buf == NULL)
3958 return -EINVAL;
3959
3960 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3961
Jeff Layton820a8032011-05-04 08:05:26 -04003962 bcc = get_bcc(&pSMBr->hdr);
3963 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003964 (char *)&pSMBr->ByteCount;
3965
3966 data_offset = le32_to_cpu(pSMBr->DataOffset);
3967 data_count = le32_to_cpu(pSMBr->DataCount);
3968 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3969 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3970
3971 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3972 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3973
3974 /* should we also check that parm and data areas do not overlap? */
3975 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003976 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003977 return -EINVAL;
3978 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003979 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003980 return -EINVAL;
3981 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003982 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003983 return -EINVAL;
3984 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003985 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3986 *ppdata, data_count, (data_count + *ppdata),
3987 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003988 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003989 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003990 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003991 return -EINVAL;
3992 }
3993 *pdatalen = data_count;
3994 *pparmlen = parm_count;
3995 return 0;
3996}
3997
Steve French0a4b92c2006-01-12 15:44:21 -08003998/* Get Security Descriptor (by handle) from remote server for a file or dir */
3999int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004000CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00004001 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08004002{
4003 int rc = 0;
4004 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00004005 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08004006 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004007 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08004008
Joe Perchesf96637b2013-05-04 22:12:25 -05004009 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08004010
Steve French630f3f0c2007-10-25 21:17:17 +00004011 *pbuflen = 0;
4012 *acl_inf = NULL;
4013
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00004014 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08004015 8 /* parm len */, tcon, (void **) &pSMB);
4016 if (rc)
4017 return rc;
4018
4019 pSMB->MaxParameterCount = cpu_to_le32(4);
4020 /* BB TEST with big acls that might need to be e.g. larger than 16K */
4021 pSMB->MaxSetupCount = 0;
4022 pSMB->Fid = fid; /* file handle always le */
4023 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
4024 CIFS_ACL_DACL);
4025 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004026 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08004027 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004028 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08004029
Steve Frencha761ac52007-10-18 21:45:27 +00004030 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004031 0, &rsp_iov);
4032 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004033 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08004034 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004035 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08004036 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00004037 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00004038 __u32 parm_len;
4039 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00004040 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00004041 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08004042
4043/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004044 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00004045 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00004046 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08004047 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004048 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08004049
Joe Perchesf96637b2013-05-04 22:12:25 -05004050 cifs_dbg(FYI, "smb %p parm %p data %p\n",
4051 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08004052
4053 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
4054 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00004055 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08004056 goto qsec_out;
4057 }
4058
4059/* BB check that data area is minimum length and as big as acl_len */
4060
Steve Frenchaf6f4612007-10-16 18:40:37 +00004061 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00004062 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004063 cifs_dbg(VFS, "acl length %d does not match %d\n",
4064 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004065 if (*pbuflen > acl_len)
4066 *pbuflen = acl_len;
4067 }
Steve French0a4b92c2006-01-12 15:44:21 -08004068
Steve French630f3f0c2007-10-25 21:17:17 +00004069 /* check if buffer is big enough for the acl
4070 header followed by the smallest SID */
4071 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
4072 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004073 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00004074 rc = -EINVAL;
4075 *pbuflen = 0;
4076 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02004077 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00004078 if (*acl_inf == NULL) {
4079 *pbuflen = 0;
4080 rc = -ENOMEM;
4081 }
Steve French630f3f0c2007-10-25 21:17:17 +00004082 }
Steve French0a4b92c2006-01-12 15:44:21 -08004083 }
4084qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004085 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08004086 return rc;
4087}
Steve French97837582007-12-31 07:47:21 +00004088
4089int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004090CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004091 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00004092{
4093 __u16 byte_count, param_count, data_count, param_offset, data_offset;
4094 int rc = 0;
4095 int bytes_returned = 0;
4096 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004097 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00004098
4099setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004100 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00004101 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004102 return rc;
Steve French97837582007-12-31 07:47:21 +00004103
4104 pSMB->MaxSetupCount = 0;
4105 pSMB->Reserved = 0;
4106
4107 param_count = 8;
4108 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
4109 data_count = acllen;
4110 data_offset = param_offset + param_count;
4111 byte_count = 3 /* pad */ + param_count;
4112
4113 pSMB->DataCount = cpu_to_le32(data_count);
4114 pSMB->TotalDataCount = pSMB->DataCount;
4115 pSMB->MaxParameterCount = cpu_to_le32(4);
4116 pSMB->MaxDataCount = cpu_to_le32(16384);
4117 pSMB->ParameterCount = cpu_to_le32(param_count);
4118 pSMB->ParameterOffset = cpu_to_le32(param_offset);
4119 pSMB->TotalParameterCount = pSMB->ParameterCount;
4120 pSMB->DataOffset = cpu_to_le32(data_offset);
4121 pSMB->SetupCount = 0;
4122 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
4123 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
4124
4125 pSMB->Fid = fid; /* file handle always le */
4126 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05004127 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00004128
4129 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04004130 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
4131 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004132 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00004133 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004134 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00004135
4136 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4137 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4138
Joe Perchesf96637b2013-05-04 22:12:25 -05004139 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
4140 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00004141 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004142 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00004143 cifs_buf_release(pSMB);
4144
4145 if (rc == -EAGAIN)
4146 goto setCifsAclRetry;
4147
4148 return (rc);
4149}
4150
Jeff Layton79df1ba2010-12-06 12:52:08 -05004151#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08004152
Steve French6b8edfe2005-08-23 20:26:03 -07004153/* Legacy Query Path Information call for lookup to old servers such
4154 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004155int
4156SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
4157 const char *search_name, FILE_ALL_INFO *data,
4158 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07004159{
Steve Frenchad7a2922008-02-07 23:25:02 +00004160 QUERY_INFORMATION_REQ *pSMB;
4161 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07004162 int rc = 0;
4163 int bytes_returned;
4164 int name_len;
4165
Joe Perchesf96637b2013-05-04 22:12:25 -05004166 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004167QInfRetry:
4168 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004169 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004170 if (rc)
4171 return rc;
4172
4173 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4174 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004175 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004176 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004177 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004178 name_len++; /* trailing null */
4179 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004180 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004181 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004182 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004183 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004184 }
4185 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004186 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004187 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004188 pSMB->ByteCount = cpu_to_le16(name_len);
4189
4190 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004191 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004192 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004193 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004194 } else if (data) {
Arnd Bergmann95390202018-06-19 17:27:58 +02004195 struct timespec64 ts;
Steve French1bd5bbc2006-09-28 03:35:57 +00004196 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004197
4198 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004199 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004200 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004201 ts.tv_nsec = 0;
4202 ts.tv_sec = time;
4203 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004204 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4205 data->LastWriteTime = data->ChangeTime;
4206 data->LastAccessTime = 0;
4207 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004208 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004209 data->EndOfFile = data->AllocationSize;
4210 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004211 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004212 } else
4213 rc = -EIO; /* bad buffer passed in */
4214
4215 cifs_buf_release(pSMB);
4216
4217 if (rc == -EAGAIN)
4218 goto QInfRetry;
4219
4220 return rc;
4221}
4222
Jeff Laytonbcd53572010-02-12 07:44:16 -05004223int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004224CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004225 u16 netfid, FILE_ALL_INFO *pFindData)
4226{
4227 struct smb_t2_qfi_req *pSMB = NULL;
4228 struct smb_t2_qfi_rsp *pSMBr = NULL;
4229 int rc = 0;
4230 int bytes_returned;
4231 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004232
Jeff Laytonbcd53572010-02-12 07:44:16 -05004233QFileInfoRetry:
4234 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4235 (void **) &pSMBr);
4236 if (rc)
4237 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004238
Jeff Laytonbcd53572010-02-12 07:44:16 -05004239 params = 2 /* level */ + 2 /* fid */;
4240 pSMB->t2.TotalDataCount = 0;
4241 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4242 /* BB find exact max data count below from sess structure BB */
4243 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4244 pSMB->t2.MaxSetupCount = 0;
4245 pSMB->t2.Reserved = 0;
4246 pSMB->t2.Flags = 0;
4247 pSMB->t2.Timeout = 0;
4248 pSMB->t2.Reserved2 = 0;
4249 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4250 Fid) - 4);
4251 pSMB->t2.DataCount = 0;
4252 pSMB->t2.DataOffset = 0;
4253 pSMB->t2.SetupCount = 1;
4254 pSMB->t2.Reserved3 = 0;
4255 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4256 byte_count = params + 1 /* pad */ ;
4257 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4258 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4259 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4260 pSMB->Pad = 0;
4261 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004262 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004263 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004264
4265 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4266 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4267 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004268 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004269 } else { /* decode response */
4270 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4271
4272 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4273 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004274 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004275 rc = -EIO; /* bad smb */
4276 else if (pFindData) {
4277 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4278 memcpy((char *) pFindData,
4279 (char *) &pSMBr->hdr.Protocol +
4280 data_offset, sizeof(FILE_ALL_INFO));
4281 } else
4282 rc = -ENOMEM;
4283 }
4284 cifs_buf_release(pSMB);
4285 if (rc == -EAGAIN)
4286 goto QFileInfoRetry;
4287
4288 return rc;
4289}
Steve French6b8edfe2005-08-23 20:26:03 -07004290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004292CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004293 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004294 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004295 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004297 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 TRANSACTION2_QPI_REQ *pSMB = NULL;
4299 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4300 int rc = 0;
4301 int bytes_returned;
4302 int name_len;
4303 __u16 params, byte_count;
4304
Joe Perchesf96637b2013-05-04 22:12:25 -05004305 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306QPathInfoRetry:
4307 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4308 (void **) &pSMBr);
4309 if (rc)
4310 return rc;
4311
4312 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4313 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004314 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004315 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 name_len++; /* trailing null */
4317 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004318 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004319 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004321 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323
Steve French50c2f752007-07-13 00:33:32 +00004324 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 pSMB->TotalDataCount = 0;
4326 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004327 /* BB find exact max SMB PDU from sess structure BB */
4328 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 pSMB->MaxSetupCount = 0;
4330 pSMB->Reserved = 0;
4331 pSMB->Flags = 0;
4332 pSMB->Timeout = 0;
4333 pSMB->Reserved2 = 0;
4334 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004335 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 pSMB->DataCount = 0;
4337 pSMB->DataOffset = 0;
4338 pSMB->SetupCount = 1;
4339 pSMB->Reserved3 = 0;
4340 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4341 byte_count = params + 1 /* pad */ ;
4342 pSMB->TotalParameterCount = cpu_to_le16(params);
4343 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004344 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004345 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4346 else
4347 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004349 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 pSMB->ByteCount = cpu_to_le16(byte_count);
4351
4352 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4353 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4354 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004355 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 } else { /* decode response */
4357 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4358
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004359 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4360 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004361 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004363 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004364 rc = -EIO; /* 24 or 26 expected but we do not read
4365 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004366 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004367 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004369
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004370 /*
4371 * On legacy responses we do not read the last field,
4372 * EAsize, fortunately since it varies by subdialect and
4373 * also note it differs on Set vs Get, ie two bytes or 4
4374 * bytes depending but we don't care here.
4375 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004376 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004377 size = sizeof(FILE_INFO_STANDARD);
4378 else
4379 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004380 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004381 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 } else
4383 rc = -ENOMEM;
4384 }
4385 cifs_buf_release(pSMB);
4386 if (rc == -EAGAIN)
4387 goto QPathInfoRetry;
4388
4389 return rc;
4390}
4391
4392int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004393CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004394 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4395{
4396 struct smb_t2_qfi_req *pSMB = NULL;
4397 struct smb_t2_qfi_rsp *pSMBr = NULL;
4398 int rc = 0;
4399 int bytes_returned;
4400 __u16 params, byte_count;
4401
4402UnixQFileInfoRetry:
4403 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4404 (void **) &pSMBr);
4405 if (rc)
4406 return rc;
4407
4408 params = 2 /* level */ + 2 /* fid */;
4409 pSMB->t2.TotalDataCount = 0;
4410 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4411 /* BB find exact max data count below from sess structure BB */
4412 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4413 pSMB->t2.MaxSetupCount = 0;
4414 pSMB->t2.Reserved = 0;
4415 pSMB->t2.Flags = 0;
4416 pSMB->t2.Timeout = 0;
4417 pSMB->t2.Reserved2 = 0;
4418 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4419 Fid) - 4);
4420 pSMB->t2.DataCount = 0;
4421 pSMB->t2.DataOffset = 0;
4422 pSMB->t2.SetupCount = 1;
4423 pSMB->t2.Reserved3 = 0;
4424 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4425 byte_count = params + 1 /* pad */ ;
4426 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4427 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4428 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4429 pSMB->Pad = 0;
4430 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004431 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004432 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004433
4434 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4435 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4436 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004437 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004438 } else { /* decode response */
4439 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4440
Jeff Layton820a8032011-05-04 08:05:26 -04004441 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004442 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 -05004443 rc = -EIO; /* bad smb */
4444 } else {
4445 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4446 memcpy((char *) pFindData,
4447 (char *) &pSMBr->hdr.Protocol +
4448 data_offset,
4449 sizeof(FILE_UNIX_BASIC_INFO));
4450 }
4451 }
4452
4453 cifs_buf_release(pSMB);
4454 if (rc == -EAGAIN)
4455 goto UnixQFileInfoRetry;
4456
4457 return rc;
4458}
4459
4460int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004461CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004463 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004464 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
4466/* SMB_QUERY_FILE_UNIX_BASIC */
4467 TRANSACTION2_QPI_REQ *pSMB = NULL;
4468 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4469 int rc = 0;
4470 int bytes_returned = 0;
4471 int name_len;
4472 __u16 params, byte_count;
4473
Joe Perchesf96637b2013-05-04 22:12:25 -05004474 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475UnixQPathInfoRetry:
4476 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4477 (void **) &pSMBr);
4478 if (rc)
4479 return rc;
4480
4481 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4482 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004483 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4484 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 name_len++; /* trailing null */
4486 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004487 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 name_len = strnlen(searchName, PATH_MAX);
4489 name_len++; /* trailing null */
4490 strncpy(pSMB->FileName, searchName, name_len);
4491 }
4492
Steve French50c2f752007-07-13 00:33:32 +00004493 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 pSMB->TotalDataCount = 0;
4495 pSMB->MaxParameterCount = cpu_to_le16(2);
4496 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004497 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 pSMB->MaxSetupCount = 0;
4499 pSMB->Reserved = 0;
4500 pSMB->Flags = 0;
4501 pSMB->Timeout = 0;
4502 pSMB->Reserved2 = 0;
4503 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004504 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 pSMB->DataCount = 0;
4506 pSMB->DataOffset = 0;
4507 pSMB->SetupCount = 1;
4508 pSMB->Reserved3 = 0;
4509 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4510 byte_count = params + 1 /* pad */ ;
4511 pSMB->TotalParameterCount = cpu_to_le16(params);
4512 pSMB->ParameterCount = pSMB->TotalParameterCount;
4513 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4514 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004515 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 pSMB->ByteCount = cpu_to_le16(byte_count);
4517
4518 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4519 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4520 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004521 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 } else { /* decode response */
4523 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4524
Jeff Layton820a8032011-05-04 08:05:26 -04004525 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004526 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 -07004527 rc = -EIO; /* bad smb */
4528 } else {
4529 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4530 memcpy((char *) pFindData,
4531 (char *) &pSMBr->hdr.Protocol +
4532 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004533 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
4535 }
4536 cifs_buf_release(pSMB);
4537 if (rc == -EAGAIN)
4538 goto UnixQPathInfoRetry;
4539
4540 return rc;
4541}
4542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543/* xid, tcon, searchName and codepage are input parms, rest are returned */
4544int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004545CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004546 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004547 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004548 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
4550/* level 257 SMB_ */
4551 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4552 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004553 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 int rc = 0;
4555 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004556 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004558 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Joe Perchesf96637b2013-05-04 22:12:25 -05004560 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562findFirstRetry:
4563 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4564 (void **) &pSMBr);
4565 if (rc)
4566 return rc;
4567
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004568 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004569 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4572 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004573 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4574 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004575 /* We can not add the asterik earlier in case
4576 it got remapped to 0xF03A as if it were part of the
4577 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004579 if (msearch) {
4580 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4581 pSMB->FileName[name_len+1] = 0;
4582 pSMB->FileName[name_len+2] = '*';
4583 pSMB->FileName[name_len+3] = 0;
4584 name_len += 4; /* now the trailing null */
4585 /* null terminate just in case */
4586 pSMB->FileName[name_len] = 0;
4587 pSMB->FileName[name_len+1] = 0;
4588 name_len += 2;
4589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 } else { /* BB add check for overrun of SMB buf BB */
4591 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004593 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 free buffer exit; BB */
4595 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004596 if (msearch) {
4597 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4598 pSMB->FileName[name_len+1] = '*';
4599 pSMB->FileName[name_len+2] = 0;
4600 name_len += 3;
4601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 }
4603
4604 params = 12 + name_len /* includes null */ ;
4605 pSMB->TotalDataCount = 0; /* no EAs */
4606 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004607 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 pSMB->MaxSetupCount = 0;
4609 pSMB->Reserved = 0;
4610 pSMB->Flags = 0;
4611 pSMB->Timeout = 0;
4612 pSMB->Reserved2 = 0;
4613 byte_count = params + 1 /* pad */ ;
4614 pSMB->TotalParameterCount = cpu_to_le16(params);
4615 pSMB->ParameterCount = pSMB->TotalParameterCount;
4616 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004617 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4618 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 pSMB->DataCount = 0;
4620 pSMB->DataOffset = 0;
4621 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4622 pSMB->Reserved3 = 0;
4623 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4624 pSMB->SearchAttributes =
4625 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4626 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004627 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004628 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4630
4631 /* BB what should we set StorageType to? Does it matter? BB */
4632 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004633 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 pSMB->ByteCount = cpu_to_le16(byte_count);
4635
4636 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4637 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004638 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Steve French88274812006-03-09 22:21:45 +00004640 if (rc) {/* BB add logic to retry regular search if Unix search
4641 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004643 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004644
Steve French88274812006-03-09 22:21:45 +00004645 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647 /* BB eventually could optimize out free and realloc of buf */
4648 /* for this case */
4649 if (rc == -EAGAIN)
4650 goto findFirstRetry;
4651 } else { /* decode response */
4652 /* BB remember to free buffer if error BB */
4653 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004654 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004655 unsigned int lnoff;
4656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004658 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 else
Steve French4b18f2a2008-04-29 00:06:05 +00004660 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004663 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004664 psrch_inf->srch_entries_start =
4665 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4668 le16_to_cpu(pSMBr->t2.ParameterOffset));
4669
Steve French790fe572007-07-07 19:25:05 +00004670 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004671 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 else
Steve French4b18f2a2008-04-29 00:06:05 +00004673 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Steve French50c2f752007-07-13 00:33:32 +00004675 psrch_inf->entries_in_buffer =
4676 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004677 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004679 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004680 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004681 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004682 psrch_inf->last_entry = NULL;
4683 return rc;
4684 }
4685
Steve French0752f152008-10-07 20:03:33 +00004686 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004687 lnoff;
4688
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004689 if (pnetfid)
4690 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 } else {
4692 cifs_buf_release(pSMB);
4693 }
4694 }
4695
4696 return rc;
4697}
4698
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004699int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4700 __u16 searchHandle, __u16 search_flags,
4701 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
4703 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4704 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004705 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 char *response_data;
4707 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004708 int bytes_returned;
4709 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 __u16 params, byte_count;
4711
Joe Perchesf96637b2013-05-04 22:12:25 -05004712 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Steve French4b18f2a2008-04-29 00:06:05 +00004714 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return -ENOENT;
4716
4717 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4718 (void **) &pSMBr);
4719 if (rc)
4720 return rc;
4721
Steve French50c2f752007-07-13 00:33:32 +00004722 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 byte_count = 0;
4724 pSMB->TotalDataCount = 0; /* no EAs */
4725 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004726 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 pSMB->MaxSetupCount = 0;
4728 pSMB->Reserved = 0;
4729 pSMB->Flags = 0;
4730 pSMB->Timeout = 0;
4731 pSMB->Reserved2 = 0;
4732 pSMB->ParameterOffset = cpu_to_le16(
4733 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4734 pSMB->DataCount = 0;
4735 pSMB->DataOffset = 0;
4736 pSMB->SetupCount = 1;
4737 pSMB->Reserved3 = 0;
4738 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4739 pSMB->SearchHandle = searchHandle; /* always kept as le */
4740 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004741 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4743 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004744 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746 name_len = psrch_inf->resume_name_len;
4747 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004748 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4750 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004751 /* 14 byte parm len above enough for 2 byte null terminator */
4752 pSMB->ResumeFileName[name_len] = 0;
4753 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 } else {
4755 rc = -EINVAL;
4756 goto FNext2_err_exit;
4757 }
4758 byte_count = params + 1 /* pad */ ;
4759 pSMB->TotalParameterCount = cpu_to_le16(params);
4760 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004761 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4765 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004766 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 if (rc) {
4768 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004769 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004770 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004771 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004773 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 } else { /* decode response */
4775 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004776
Steve French790fe572007-07-07 19:25:05 +00004777 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004778 unsigned int lnoff;
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 /* BB fixme add lock for file (srch_info) struct here */
4781 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004782 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 else
Steve French4b18f2a2008-04-29 00:06:05 +00004784 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 response_data = (char *) &pSMBr->hdr.Protocol +
4786 le16_to_cpu(pSMBr->t2.ParameterOffset);
4787 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4788 response_data = (char *)&pSMBr->hdr.Protocol +
4789 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004790 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004791 cifs_small_buf_release(
4792 psrch_inf->ntwrk_buf_start);
4793 else
4794 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 psrch_inf->srch_entries_start = response_data;
4796 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004797 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004798 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004799 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 else
Steve French4b18f2a2008-04-29 00:06:05 +00004801 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004802 psrch_inf->entries_in_buffer =
4803 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 psrch_inf->index_of_last_entry +=
4805 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004806 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004807 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004808 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004809 psrch_inf->last_entry = NULL;
4810 return rc;
4811 } else
4812 psrch_inf->last_entry =
4813 psrch_inf->srch_entries_start + lnoff;
4814
Joe Perchesf96637b2013-05-04 22:12:25 -05004815/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4816 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
4818 /* BB fixme add unlock here */
4819 }
4820
4821 }
4822
4823 /* BB On error, should we leave previous search buf (and count and
4824 last entry fields) intact or free the previous one? */
4825
4826 /* Note: On -EAGAIN error only caller can retry on handle based calls
4827 since file handle passed in no longer valid */
4828FNext2_err_exit:
4829 if (rc != 0)
4830 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 return rc;
4832}
4833
4834int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004835CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004836 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
4838 int rc = 0;
4839 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Joe Perchesf96637b2013-05-04 22:12:25 -05004841 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4843
4844 /* no sense returning error if session restarted
4845 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004846 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return 0;
4848 if (rc)
4849 return rc;
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 pSMB->FileID = searchHandle;
4852 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004853 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004854 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004855 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004856 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004857
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004858 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
4860 /* Since session is dead, search handle closed on server already */
4861 if (rc == -EAGAIN)
4862 rc = 0;
4863
4864 return rc;
4865}
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004868CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004869 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004870 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871{
4872 int rc = 0;
4873 TRANSACTION2_QPI_REQ *pSMB = NULL;
4874 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4875 int name_len, bytes_returned;
4876 __u16 params, byte_count;
4877
Joe Perchesf96637b2013-05-04 22:12:25 -05004878 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004879 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004880 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882GetInodeNumberRetry:
4883 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004884 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 if (rc)
4886 return rc;
4887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4889 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004890 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004891 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004892 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 name_len++; /* trailing null */
4894 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004895 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004896 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004898 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 }
4900
4901 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4902 pSMB->TotalDataCount = 0;
4903 pSMB->MaxParameterCount = cpu_to_le16(2);
4904 /* BB find exact max data count below from sess structure BB */
4905 pSMB->MaxDataCount = cpu_to_le16(4000);
4906 pSMB->MaxSetupCount = 0;
4907 pSMB->Reserved = 0;
4908 pSMB->Flags = 0;
4909 pSMB->Timeout = 0;
4910 pSMB->Reserved2 = 0;
4911 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004912 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 pSMB->DataCount = 0;
4914 pSMB->DataOffset = 0;
4915 pSMB->SetupCount = 1;
4916 pSMB->Reserved3 = 0;
4917 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4918 byte_count = params + 1 /* pad */ ;
4919 pSMB->TotalParameterCount = cpu_to_le16(params);
4920 pSMB->ParameterCount = pSMB->TotalParameterCount;
4921 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4922 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004923 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 pSMB->ByteCount = cpu_to_le16(byte_count);
4925
4926 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4927 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4928 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004929 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 } else {
4931 /* decode response */
4932 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004934 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 /* If rc should we check for EOPNOSUPP and
4936 disable the srvino flag? or in caller? */
4937 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004938 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4940 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004941 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004943 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004944 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 rc = -EIO;
4946 goto GetInodeNumOut;
4947 }
4948 pfinfo = (struct file_internal_info *)
4949 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004950 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
4952 }
4953GetInodeNumOut:
4954 cifs_buf_release(pSMB);
4955 if (rc == -EAGAIN)
4956 goto GetInodeNumberRetry;
4957 return rc;
4958}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004961CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004962 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004963 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004964 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
4966/* TRANS2_GET_DFS_REFERRAL */
4967 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4968 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 int rc = 0;
4970 int bytes_returned;
4971 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004973 *num_of_nodes = 0;
4974 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Joe Perchesf96637b2013-05-04 22:12:25 -05004976 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004977 if (ses == NULL || ses->tcon_ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return -ENODEV;
Aurelien Aptelb327a712018-01-24 13:46:10 +01004979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980getDFSRetry:
Aurelien Aptelb327a712018-01-24 13:46:10 +01004981 rc = smb_init(SMB_COM_TRANSACTION2, 15, ses->tcon_ipc, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 (void **) &pSMBr);
4983 if (rc)
4984 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004985
4986 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004987 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004988 pSMB->hdr.Mid = get_next_mid(ses->server);
Aurelien Aptelb327a712018-01-24 13:46:10 +01004989 pSMB->hdr.Tid = ses->tcon_ipc->tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004991 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004993 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
4996 if (ses->capabilities & CAP_UNICODE) {
4997 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4998 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004999 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04005000 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06005001 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 name_len++; /* trailing null */
5003 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005004 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04005005 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04005007 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
5009
Dan Carpenter65c3b202015-04-30 17:30:24 +03005010 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04005011 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00005012
Steve French50c2f752007-07-13 00:33:32 +00005013 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00005014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 params = 2 /* level */ + name_len /*includes null */ ;
5016 pSMB->TotalDataCount = 0;
5017 pSMB->DataCount = 0;
5018 pSMB->DataOffset = 0;
5019 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00005020 /* BB find exact max SMB PDU from sess structure BB */
5021 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 pSMB->MaxSetupCount = 0;
5023 pSMB->Reserved = 0;
5024 pSMB->Flags = 0;
5025 pSMB->Timeout = 0;
5026 pSMB->Reserved2 = 0;
5027 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005028 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 pSMB->SetupCount = 1;
5030 pSMB->Reserved3 = 0;
5031 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
5032 byte_count = params + 3 /* pad */ ;
5033 pSMB->ParameterCount = cpu_to_le16(params);
5034 pSMB->TotalParameterCount = pSMB->ParameterCount;
5035 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005036 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 pSMB->ByteCount = cpu_to_le16(byte_count);
5038
5039 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
5040 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5041 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005042 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005043 goto GetDFSRefExit;
5044 }
5045 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005047 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04005048 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00005049 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04005050 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 }
Igor Mammedovfec45852008-05-16 13:06:30 +04005052
Joe Perchesf96637b2013-05-04 22:12:25 -05005053 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
5054 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04005055
5056 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01005057 rc = parse_dfs_referrals(&pSMBr->dfs_data,
5058 le16_to_cpu(pSMBr->t2.DataCount),
5059 num_of_nodes, target_nodes, nls_codepage,
5060 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06005061 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04005062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00005064 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066 if (rc == -EAGAIN)
5067 goto getDFSRetry;
5068
5069 return rc;
5070}
5071
Steve French20962432005-09-21 22:05:57 -07005072/* Query File System Info such as free space to old servers such as Win 9x */
5073int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005074SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5075 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005076{
5077/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5078 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5079 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5080 FILE_SYSTEM_ALLOC_INFO *response_data;
5081 int rc = 0;
5082 int bytes_returned = 0;
5083 __u16 params, byte_count;
5084
Joe Perchesf96637b2013-05-04 22:12:25 -05005085 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005086oldQFSInfoRetry:
5087 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5088 (void **) &pSMBr);
5089 if (rc)
5090 return rc;
Steve French20962432005-09-21 22:05:57 -07005091
5092 params = 2; /* level */
5093 pSMB->TotalDataCount = 0;
5094 pSMB->MaxParameterCount = cpu_to_le16(2);
5095 pSMB->MaxDataCount = cpu_to_le16(1000);
5096 pSMB->MaxSetupCount = 0;
5097 pSMB->Reserved = 0;
5098 pSMB->Flags = 0;
5099 pSMB->Timeout = 0;
5100 pSMB->Reserved2 = 0;
5101 byte_count = params + 1 /* pad */ ;
5102 pSMB->TotalParameterCount = cpu_to_le16(params);
5103 pSMB->ParameterCount = pSMB->TotalParameterCount;
5104 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5105 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5106 pSMB->DataCount = 0;
5107 pSMB->DataOffset = 0;
5108 pSMB->SetupCount = 1;
5109 pSMB->Reserved3 = 0;
5110 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5111 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005112 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005113 pSMB->ByteCount = cpu_to_le16(byte_count);
5114
5115 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5116 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5117 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005118 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005119 } else { /* decode response */
5120 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5121
Jeff Layton820a8032011-05-04 08:05:26 -04005122 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005123 rc = -EIO; /* bad smb */
5124 else {
5125 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005126 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005127 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005128
Steve French50c2f752007-07-13 00:33:32 +00005129 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005130 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5131 FSData->f_bsize =
5132 le16_to_cpu(response_data->BytesPerSector) *
5133 le32_to_cpu(response_data->
5134 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005135 /*
5136 * much prefer larger but if server doesn't report
5137 * a valid size than 4K is a reasonable minimum
5138 */
5139 if (FSData->f_bsize < 512)
5140 FSData->f_bsize = 4096;
5141
Steve French20962432005-09-21 22:05:57 -07005142 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005143 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005144 FSData->f_bfree = FSData->f_bavail =
5145 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005146 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5147 (unsigned long long)FSData->f_blocks,
5148 (unsigned long long)FSData->f_bfree,
5149 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005150 }
5151 }
5152 cifs_buf_release(pSMB);
5153
5154 if (rc == -EAGAIN)
5155 goto oldQFSInfoRetry;
5156
5157 return rc;
5158}
5159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005161CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5162 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
5164/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5165 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5166 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5167 FILE_SYSTEM_INFO *response_data;
5168 int rc = 0;
5169 int bytes_returned = 0;
5170 __u16 params, byte_count;
5171
Joe Perchesf96637b2013-05-04 22:12:25 -05005172 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173QFSInfoRetry:
5174 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5175 (void **) &pSMBr);
5176 if (rc)
5177 return rc;
5178
5179 params = 2; /* level */
5180 pSMB->TotalDataCount = 0;
5181 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005182 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 pSMB->MaxSetupCount = 0;
5184 pSMB->Reserved = 0;
5185 pSMB->Flags = 0;
5186 pSMB->Timeout = 0;
5187 pSMB->Reserved2 = 0;
5188 byte_count = params + 1 /* pad */ ;
5189 pSMB->TotalParameterCount = cpu_to_le16(params);
5190 pSMB->ParameterCount = pSMB->TotalParameterCount;
5191 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005192 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 pSMB->DataCount = 0;
5194 pSMB->DataOffset = 0;
5195 pSMB->SetupCount = 1;
5196 pSMB->Reserved3 = 0;
5197 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5198 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005199 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 pSMB->ByteCount = cpu_to_le16(byte_count);
5201
5202 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5203 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5204 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005205 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005207 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
Jeff Layton820a8032011-05-04 08:05:26 -04005209 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 rc = -EIO; /* bad smb */
5211 else {
5212 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
5214 response_data =
5215 (FILE_SYSTEM_INFO
5216 *) (((char *) &pSMBr->hdr.Protocol) +
5217 data_offset);
5218 FSData->f_bsize =
5219 le32_to_cpu(response_data->BytesPerSector) *
5220 le32_to_cpu(response_data->
5221 SectorsPerAllocationUnit);
Steve French5a519be2018-09-15 14:07:09 -05005222 /*
5223 * much prefer larger but if server doesn't report
5224 * a valid size than 4K is a reasonable minimum
5225 */
5226 if (FSData->f_bsize < 512)
5227 FSData->f_bsize = 4096;
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 FSData->f_blocks =
5230 le64_to_cpu(response_data->TotalAllocationUnits);
5231 FSData->f_bfree = FSData->f_bavail =
5232 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005233 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5234 (unsigned long long)FSData->f_blocks,
5235 (unsigned long long)FSData->f_bfree,
5236 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 }
5238 }
5239 cifs_buf_release(pSMB);
5240
5241 if (rc == -EAGAIN)
5242 goto QFSInfoRetry;
5243
5244 return rc;
5245}
5246
5247int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005248CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
5250/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5251 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5252 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5253 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5254 int rc = 0;
5255 int bytes_returned = 0;
5256 __u16 params, byte_count;
5257
Joe Perchesf96637b2013-05-04 22:12:25 -05005258 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259QFSAttributeRetry:
5260 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5261 (void **) &pSMBr);
5262 if (rc)
5263 return rc;
5264
5265 params = 2; /* level */
5266 pSMB->TotalDataCount = 0;
5267 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005268 /* BB find exact max SMB PDU from sess structure BB */
5269 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 pSMB->MaxSetupCount = 0;
5271 pSMB->Reserved = 0;
5272 pSMB->Flags = 0;
5273 pSMB->Timeout = 0;
5274 pSMB->Reserved2 = 0;
5275 byte_count = params + 1 /* pad */ ;
5276 pSMB->TotalParameterCount = cpu_to_le16(params);
5277 pSMB->ParameterCount = pSMB->TotalParameterCount;
5278 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005279 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 pSMB->DataCount = 0;
5281 pSMB->DataOffset = 0;
5282 pSMB->SetupCount = 1;
5283 pSMB->Reserved3 = 0;
5284 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5285 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005286 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 pSMB->ByteCount = cpu_to_le16(byte_count);
5288
5289 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5290 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5291 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005292 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 } else { /* decode response */
5294 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5295
Jeff Layton820a8032011-05-04 08:05:26 -04005296 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005297 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 rc = -EIO; /* bad smb */
5299 } else {
5300 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5301 response_data =
5302 (FILE_SYSTEM_ATTRIBUTE_INFO
5303 *) (((char *) &pSMBr->hdr.Protocol) +
5304 data_offset);
5305 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005306 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 }
5308 }
5309 cifs_buf_release(pSMB);
5310
5311 if (rc == -EAGAIN)
5312 goto QFSAttributeRetry;
5313
5314 return rc;
5315}
5316
5317int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005318CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319{
5320/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5321 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5322 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5323 FILE_SYSTEM_DEVICE_INFO *response_data;
5324 int rc = 0;
5325 int bytes_returned = 0;
5326 __u16 params, byte_count;
5327
Joe Perchesf96637b2013-05-04 22:12:25 -05005328 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329QFSDeviceRetry:
5330 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5331 (void **) &pSMBr);
5332 if (rc)
5333 return rc;
5334
5335 params = 2; /* level */
5336 pSMB->TotalDataCount = 0;
5337 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005338 /* BB find exact max SMB PDU from sess structure BB */
5339 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 pSMB->MaxSetupCount = 0;
5341 pSMB->Reserved = 0;
5342 pSMB->Flags = 0;
5343 pSMB->Timeout = 0;
5344 pSMB->Reserved2 = 0;
5345 byte_count = params + 1 /* pad */ ;
5346 pSMB->TotalParameterCount = cpu_to_le16(params);
5347 pSMB->ParameterCount = pSMB->TotalParameterCount;
5348 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005349 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 pSMB->DataCount = 0;
5352 pSMB->DataOffset = 0;
5353 pSMB->SetupCount = 1;
5354 pSMB->Reserved3 = 0;
5355 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5356 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005357 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 pSMB->ByteCount = cpu_to_le16(byte_count);
5359
5360 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5361 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5362 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005363 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 } else { /* decode response */
5365 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5366
Jeff Layton820a8032011-05-04 08:05:26 -04005367 if (rc || get_bcc(&pSMBr->hdr) <
5368 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 rc = -EIO; /* bad smb */
5370 else {
5371 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5372 response_data =
Steve French737b7582005-04-28 22:41:06 -07005373 (FILE_SYSTEM_DEVICE_INFO *)
5374 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 data_offset);
5376 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005377 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 }
5379 }
5380 cifs_buf_release(pSMB);
5381
5382 if (rc == -EAGAIN)
5383 goto QFSDeviceRetry;
5384
5385 return rc;
5386}
5387
5388int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005389CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390{
5391/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5392 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5393 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5394 FILE_SYSTEM_UNIX_INFO *response_data;
5395 int rc = 0;
5396 int bytes_returned = 0;
5397 __u16 params, byte_count;
5398
Joe Perchesf96637b2013-05-04 22:12:25 -05005399 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005401 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5402 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 if (rc)
5404 return rc;
5405
5406 params = 2; /* level */
5407 pSMB->TotalDataCount = 0;
5408 pSMB->DataCount = 0;
5409 pSMB->DataOffset = 0;
5410 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005411 /* BB find exact max SMB PDU from sess structure BB */
5412 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 pSMB->MaxSetupCount = 0;
5414 pSMB->Reserved = 0;
5415 pSMB->Flags = 0;
5416 pSMB->Timeout = 0;
5417 pSMB->Reserved2 = 0;
5418 byte_count = params + 1 /* pad */ ;
5419 pSMB->ParameterCount = cpu_to_le16(params);
5420 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005421 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5422 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 pSMB->SetupCount = 1;
5424 pSMB->Reserved3 = 0;
5425 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5426 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005427 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 pSMB->ByteCount = cpu_to_le16(byte_count);
5429
5430 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5431 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5432 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005433 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 } else { /* decode response */
5435 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5436
Jeff Layton820a8032011-05-04 08:05:26 -04005437 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 rc = -EIO; /* bad smb */
5439 } else {
5440 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5441 response_data =
5442 (FILE_SYSTEM_UNIX_INFO
5443 *) (((char *) &pSMBr->hdr.Protocol) +
5444 data_offset);
5445 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005446 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448 }
5449 cifs_buf_release(pSMB);
5450
5451 if (rc == -EAGAIN)
5452 goto QFSUnixRetry;
5453
5454
5455 return rc;
5456}
5457
Jeremy Allisonac670552005-06-22 17:26:35 -07005458int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005459CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005460{
5461/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5462 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5463 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5464 int rc = 0;
5465 int bytes_returned = 0;
5466 __u16 params, param_offset, offset, byte_count;
5467
Joe Perchesf96637b2013-05-04 22:12:25 -05005468 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005469SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005470 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005471 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5472 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005473 if (rc)
5474 return rc;
5475
5476 params = 4; /* 2 bytes zero followed by info level. */
5477 pSMB->MaxSetupCount = 0;
5478 pSMB->Reserved = 0;
5479 pSMB->Flags = 0;
5480 pSMB->Timeout = 0;
5481 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005482 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5483 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005484 offset = param_offset + params;
5485
5486 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005487 /* BB find exact max SMB PDU from sess structure BB */
5488 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005489 pSMB->SetupCount = 1;
5490 pSMB->Reserved3 = 0;
5491 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5492 byte_count = 1 /* pad */ + params + 12;
5493
5494 pSMB->DataCount = cpu_to_le16(12);
5495 pSMB->ParameterCount = cpu_to_le16(params);
5496 pSMB->TotalDataCount = pSMB->DataCount;
5497 pSMB->TotalParameterCount = pSMB->ParameterCount;
5498 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5499 pSMB->DataOffset = cpu_to_le16(offset);
5500
5501 /* Params. */
5502 pSMB->FileNum = 0;
5503 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5504
5505 /* Data. */
5506 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5507 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5508 pSMB->ClientUnixCap = cpu_to_le64(cap);
5509
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005510 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005511 pSMB->ByteCount = cpu_to_le16(byte_count);
5512
5513 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5514 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5515 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005516 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005517 } else { /* decode response */
5518 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005519 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005520 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005521 }
5522 cifs_buf_release(pSMB);
5523
5524 if (rc == -EAGAIN)
5525 goto SETFSUnixRetry;
5526
5527 return rc;
5528}
5529
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005533CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005534 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535{
5536/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5537 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5538 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5539 FILE_SYSTEM_POSIX_INFO *response_data;
5540 int rc = 0;
5541 int bytes_returned = 0;
5542 __u16 params, byte_count;
5543
Joe Perchesf96637b2013-05-04 22:12:25 -05005544 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545QFSPosixRetry:
5546 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5547 (void **) &pSMBr);
5548 if (rc)
5549 return rc;
5550
5551 params = 2; /* level */
5552 pSMB->TotalDataCount = 0;
5553 pSMB->DataCount = 0;
5554 pSMB->DataOffset = 0;
5555 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005556 /* BB find exact max SMB PDU from sess structure BB */
5557 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 pSMB->MaxSetupCount = 0;
5559 pSMB->Reserved = 0;
5560 pSMB->Flags = 0;
5561 pSMB->Timeout = 0;
5562 pSMB->Reserved2 = 0;
5563 byte_count = params + 1 /* pad */ ;
5564 pSMB->ParameterCount = cpu_to_le16(params);
5565 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005566 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5567 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 pSMB->SetupCount = 1;
5569 pSMB->Reserved3 = 0;
5570 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5571 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005572 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 pSMB->ByteCount = cpu_to_le16(byte_count);
5574
5575 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5576 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5577 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005578 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 } else { /* decode response */
5580 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5581
Jeff Layton820a8032011-05-04 08:05:26 -04005582 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 rc = -EIO; /* bad smb */
5584 } else {
5585 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5586 response_data =
5587 (FILE_SYSTEM_POSIX_INFO
5588 *) (((char *) &pSMBr->hdr.Protocol) +
5589 data_offset);
5590 FSData->f_bsize =
5591 le32_to_cpu(response_data->BlockSize);
Steve French5a519be2018-09-15 14:07:09 -05005592 /*
5593 * much prefer larger but if server doesn't report
5594 * a valid size than 4K is a reasonable minimum
5595 */
5596 if (FSData->f_bsize < 512)
5597 FSData->f_bsize = 4096;
5598
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 FSData->f_blocks =
5600 le64_to_cpu(response_data->TotalBlocks);
5601 FSData->f_bfree =
5602 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005603 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 FSData->f_bavail = FSData->f_bfree;
5605 } else {
5606 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005607 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 }
Steve French790fe572007-07-07 19:25:05 +00005609 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005611 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005612 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005614 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
5616 }
5617 cifs_buf_release(pSMB);
5618
5619 if (rc == -EAGAIN)
5620 goto QFSPosixRetry;
5621
5622 return rc;
5623}
5624
5625
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005626/*
5627 * We can not use write of zero bytes trick to set file size due to need for
5628 * large file support. Also note that this SetPathInfo is preferred to
5629 * SetFileInfo based method in next routine which is only needed to work around
5630 * a sharing violation bugin Samba which this routine can run into.
5631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005633CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005634 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5635 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
5637 struct smb_com_transaction2_spi_req *pSMB = NULL;
5638 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5639 struct file_end_of_file_info *parm_data;
5640 int name_len;
5641 int rc = 0;
5642 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005643 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 __u16 params, byte_count, data_count, param_offset, offset;
5646
Joe Perchesf96637b2013-05-04 22:12:25 -05005647 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648SetEOFRetry:
5649 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5650 (void **) &pSMBr);
5651 if (rc)
5652 return rc;
5653
5654 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5655 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005656 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5657 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 name_len++; /* trailing null */
5659 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005660 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005661 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005663 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 }
5665 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005666 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005668 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 pSMB->MaxSetupCount = 0;
5670 pSMB->Reserved = 0;
5671 pSMB->Flags = 0;
5672 pSMB->Timeout = 0;
5673 pSMB->Reserved2 = 0;
5674 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005675 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005677 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005678 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5679 pSMB->InformationLevel =
5680 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5681 else
5682 pSMB->InformationLevel =
5683 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5684 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5686 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005687 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 else
5689 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005690 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 }
5692
5693 parm_data =
5694 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5695 offset);
5696 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5697 pSMB->DataOffset = cpu_to_le16(offset);
5698 pSMB->SetupCount = 1;
5699 pSMB->Reserved3 = 0;
5700 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5701 byte_count = 3 /* pad */ + params + data_count;
5702 pSMB->DataCount = cpu_to_le16(data_count);
5703 pSMB->TotalDataCount = pSMB->DataCount;
5704 pSMB->ParameterCount = cpu_to_le16(params);
5705 pSMB->TotalParameterCount = pSMB->ParameterCount;
5706 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005707 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 parm_data->FileSize = cpu_to_le64(size);
5709 pSMB->ByteCount = cpu_to_le16(byte_count);
5710 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5711 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005712 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005713 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
5715 cifs_buf_release(pSMB);
5716
5717 if (rc == -EAGAIN)
5718 goto SetEOFRetry;
5719
5720 return rc;
5721}
5722
5723int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005724CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5725 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
5727 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 struct file_end_of_file_info *parm_data;
5729 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 __u16 params, param_offset, offset, byte_count, count;
5731
Joe Perchesf96637b2013-05-04 22:12:25 -05005732 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5733 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005734 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5735
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 if (rc)
5737 return rc;
5738
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005739 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5740 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 params = 6;
5743 pSMB->MaxSetupCount = 0;
5744 pSMB->Reserved = 0;
5745 pSMB->Flags = 0;
5746 pSMB->Timeout = 0;
5747 pSMB->Reserved2 = 0;
5748 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5749 offset = param_offset + params;
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 count = sizeof(struct file_end_of_file_info);
5752 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005753 /* BB find exact max SMB PDU from sess structure BB */
5754 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 pSMB->SetupCount = 1;
5756 pSMB->Reserved3 = 0;
5757 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5758 byte_count = 3 /* pad */ + params + count;
5759 pSMB->DataCount = cpu_to_le16(count);
5760 pSMB->ParameterCount = cpu_to_le16(params);
5761 pSMB->TotalDataCount = pSMB->DataCount;
5762 pSMB->TotalParameterCount = pSMB->ParameterCount;
5763 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5764 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005765 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5766 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 pSMB->DataOffset = cpu_to_le16(offset);
5768 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005769 pSMB->Fid = cfile->fid.netfid;
5770 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5772 pSMB->InformationLevel =
5773 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5774 else
5775 pSMB->InformationLevel =
5776 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005777 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5779 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005780 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 else
5782 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005783 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 }
5785 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005786 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005788 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005789 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005791 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5792 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 }
5794
Steve French50c2f752007-07-13 00:33:32 +00005795 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 since file handle passed in no longer valid */
5797
5798 return rc;
5799}
5800
Steve French50c2f752007-07-13 00:33:32 +00005801/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 an open handle, rather than by pathname - this is awkward due to
5803 potential access conflicts on the open, but it is unavoidable for these
5804 old servers since the only other choice is to go from 100 nanosecond DCE
5805 time and resort to the original setpathinfo level which takes the ancient
5806 DOS time format with 2 second granularity */
5807int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005808CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005809 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
5811 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 char *data_offset;
5813 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 __u16 params, param_offset, offset, byte_count, count;
5815
Joe Perchesf96637b2013-05-04 22:12:25 -05005816 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005817 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 if (rc)
5820 return rc;
5821
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005822 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5823 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 params = 6;
5826 pSMB->MaxSetupCount = 0;
5827 pSMB->Reserved = 0;
5828 pSMB->Flags = 0;
5829 pSMB->Timeout = 0;
5830 pSMB->Reserved2 = 0;
5831 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5832 offset = param_offset + params;
5833
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005834 data_offset = (char *)pSMB +
5835 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Steve French26f57362007-08-30 22:09:15 +00005837 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005839 /* BB find max SMB PDU from sess */
5840 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 pSMB->SetupCount = 1;
5842 pSMB->Reserved3 = 0;
5843 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5844 byte_count = 3 /* pad */ + params + count;
5845 pSMB->DataCount = cpu_to_le16(count);
5846 pSMB->ParameterCount = cpu_to_le16(params);
5847 pSMB->TotalDataCount = pSMB->DataCount;
5848 pSMB->TotalParameterCount = pSMB->ParameterCount;
5849 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5850 pSMB->DataOffset = cpu_to_le16(offset);
5851 pSMB->Fid = fid;
5852 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5853 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5854 else
5855 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5856 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005857 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005859 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005860 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005861 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005862 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005863 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5864 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
Steve French50c2f752007-07-13 00:33:32 +00005866 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 since file handle passed in no longer valid */
5868
5869 return rc;
5870}
5871
Jeff Layton6d22f092008-09-23 11:48:35 -04005872int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005873CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005874 bool delete_file, __u16 fid, __u32 pid_of_opener)
5875{
5876 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5877 char *data_offset;
5878 int rc = 0;
5879 __u16 params, param_offset, offset, byte_count, count;
5880
Joe Perchesf96637b2013-05-04 22:12:25 -05005881 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005882 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5883
5884 if (rc)
5885 return rc;
5886
5887 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5888 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5889
5890 params = 6;
5891 pSMB->MaxSetupCount = 0;
5892 pSMB->Reserved = 0;
5893 pSMB->Flags = 0;
5894 pSMB->Timeout = 0;
5895 pSMB->Reserved2 = 0;
5896 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5897 offset = param_offset + params;
5898
5899 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5900
5901 count = 1;
5902 pSMB->MaxParameterCount = cpu_to_le16(2);
5903 /* BB find max SMB PDU from sess */
5904 pSMB->MaxDataCount = cpu_to_le16(1000);
5905 pSMB->SetupCount = 1;
5906 pSMB->Reserved3 = 0;
5907 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5908 byte_count = 3 /* pad */ + params + count;
5909 pSMB->DataCount = cpu_to_le16(count);
5910 pSMB->ParameterCount = cpu_to_le16(params);
5911 pSMB->TotalDataCount = pSMB->DataCount;
5912 pSMB->TotalParameterCount = pSMB->ParameterCount;
5913 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5914 pSMB->DataOffset = cpu_to_le16(offset);
5915 pSMB->Fid = fid;
5916 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5917 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005918 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005919 pSMB->ByteCount = cpu_to_le16(byte_count);
5920 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005921 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005922 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005923 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005924 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005925
5926 return rc;
5927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005930CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005931 const char *fileName, const FILE_BASIC_INFO *data,
5932 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933{
5934 TRANSACTION2_SPI_REQ *pSMB = NULL;
5935 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5936 int name_len;
5937 int rc = 0;
5938 int bytes_returned = 0;
5939 char *data_offset;
5940 __u16 params, param_offset, offset, byte_count, count;
5941
Joe Perchesf96637b2013-05-04 22:12:25 -05005942 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944SetTimesRetry:
5945 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5946 (void **) &pSMBr);
5947 if (rc)
5948 return rc;
5949
5950 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5951 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005952 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5953 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 name_len++; /* trailing null */
5955 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005956 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 name_len = strnlen(fileName, PATH_MAX);
5958 name_len++; /* trailing null */
5959 strncpy(pSMB->FileName, fileName, name_len);
5960 }
5961
5962 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005963 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005965 /* BB find max SMB PDU from sess structure BB */
5966 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 pSMB->MaxSetupCount = 0;
5968 pSMB->Reserved = 0;
5969 pSMB->Flags = 0;
5970 pSMB->Timeout = 0;
5971 pSMB->Reserved2 = 0;
5972 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005973 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 offset = param_offset + params;
5975 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5976 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5977 pSMB->DataOffset = cpu_to_le16(offset);
5978 pSMB->SetupCount = 1;
5979 pSMB->Reserved3 = 0;
5980 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5981 byte_count = 3 /* pad */ + params + count;
5982
5983 pSMB->DataCount = cpu_to_le16(count);
5984 pSMB->ParameterCount = cpu_to_le16(params);
5985 pSMB->TotalDataCount = pSMB->DataCount;
5986 pSMB->TotalParameterCount = pSMB->ParameterCount;
5987 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5988 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5989 else
5990 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5991 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005992 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005993 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 pSMB->ByteCount = cpu_to_le16(byte_count);
5995 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5996 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005997 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005998 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 cifs_buf_release(pSMB);
6001
6002 if (rc == -EAGAIN)
6003 goto SetTimesRetry;
6004
6005 return rc;
6006}
6007
6008/* Can not be used to set time stamps yet (due to old DOS time format) */
6009/* Can be used to set attributes */
6010#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
6011 handling it anyway and NT4 was what we thought it would be needed for
6012 Do not delete it until we prove whether needed for Win9x though */
6013int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006014CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 __u16 dos_attrs, const struct nls_table *nls_codepage)
6016{
6017 SETATTR_REQ *pSMB = NULL;
6018 SETATTR_RSP *pSMBr = NULL;
6019 int rc = 0;
6020 int bytes_returned;
6021 int name_len;
6022
Joe Perchesf96637b2013-05-04 22:12:25 -05006023 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025SetAttrLgcyRetry:
6026 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
6027 (void **) &pSMBr);
6028 if (rc)
6029 return rc;
6030
6031 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6032 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006033 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
6034 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 name_len++; /* trailing null */
6036 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006037 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 name_len = strnlen(fileName, PATH_MAX);
6039 name_len++; /* trailing null */
6040 strncpy(pSMB->fileName, fileName, name_len);
6041 }
6042 pSMB->attr = cpu_to_le16(dos_attrs);
6043 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006044 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 pSMB->ByteCount = cpu_to_le16(name_len + 1);
6046 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6047 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006048 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006049 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
6051 cifs_buf_release(pSMB);
6052
6053 if (rc == -EAGAIN)
6054 goto SetAttrLgcyRetry;
6055
6056 return rc;
6057}
6058#endif /* temporarily unneeded SetAttr legacy function */
6059
Jeff Layton654cf142009-07-09 20:02:49 -04006060static void
6061cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
6062 const struct cifs_unix_set_info_args *args)
6063{
Eric W. Biederman49418b22013-02-06 00:57:56 -08006064 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04006065 u64 mode = args->mode;
6066
Eric W. Biederman49418b22013-02-06 00:57:56 -08006067 if (uid_valid(args->uid))
6068 uid = from_kuid(&init_user_ns, args->uid);
6069 if (gid_valid(args->gid))
6070 gid = from_kgid(&init_user_ns, args->gid);
6071
Jeff Layton654cf142009-07-09 20:02:49 -04006072 /*
6073 * Samba server ignores set of file size to zero due to bugs in some
6074 * older clients, but we should be precise - we use SetFileSize to
6075 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006076 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04006077 * zero instead of -1 here
6078 */
6079 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
6080 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
6081 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
6082 data_offset->LastAccessTime = cpu_to_le64(args->atime);
6083 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08006084 data_offset->Uid = cpu_to_le64(uid);
6085 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04006086 /* better to leave device as zero when it is */
6087 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6088 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6089 data_offset->Permissions = cpu_to_le64(mode);
6090
6091 if (S_ISREG(mode))
6092 data_offset->Type = cpu_to_le32(UNIX_FILE);
6093 else if (S_ISDIR(mode))
6094 data_offset->Type = cpu_to_le32(UNIX_DIR);
6095 else if (S_ISLNK(mode))
6096 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6097 else if (S_ISCHR(mode))
6098 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6099 else if (S_ISBLK(mode))
6100 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6101 else if (S_ISFIFO(mode))
6102 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6103 else if (S_ISSOCK(mode))
6104 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6105}
6106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006108CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006109 const struct cifs_unix_set_info_args *args,
6110 u16 fid, u32 pid_of_opener)
6111{
6112 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006113 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006114 int rc = 0;
6115 u16 params, param_offset, offset, byte_count, count;
6116
Joe Perchesf96637b2013-05-04 22:12:25 -05006117 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006118 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6119
6120 if (rc)
6121 return rc;
6122
6123 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6124 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6125
6126 params = 6;
6127 pSMB->MaxSetupCount = 0;
6128 pSMB->Reserved = 0;
6129 pSMB->Flags = 0;
6130 pSMB->Timeout = 0;
6131 pSMB->Reserved2 = 0;
6132 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6133 offset = param_offset + params;
6134
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006135 data_offset = (char *)pSMB +
6136 offsetof(struct smb_hdr, Protocol) + offset;
6137
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006138 count = sizeof(FILE_UNIX_BASIC_INFO);
6139
6140 pSMB->MaxParameterCount = cpu_to_le16(2);
6141 /* BB find max SMB PDU from sess */
6142 pSMB->MaxDataCount = cpu_to_le16(1000);
6143 pSMB->SetupCount = 1;
6144 pSMB->Reserved3 = 0;
6145 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6146 byte_count = 3 /* pad */ + params + count;
6147 pSMB->DataCount = cpu_to_le16(count);
6148 pSMB->ParameterCount = cpu_to_le16(params);
6149 pSMB->TotalDataCount = pSMB->DataCount;
6150 pSMB->TotalParameterCount = pSMB->ParameterCount;
6151 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6152 pSMB->DataOffset = cpu_to_le16(offset);
6153 pSMB->Fid = fid;
6154 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6155 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006156 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006157 pSMB->ByteCount = cpu_to_le16(byte_count);
6158
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006159 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006160
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006161 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006162 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006163 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006164 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6165 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006166
6167 /* Note: On -EAGAIN error only caller can retry on handle based calls
6168 since file handle passed in no longer valid */
6169
6170 return rc;
6171}
6172
6173int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006174CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006175 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006176 const struct cifs_unix_set_info_args *args,
6177 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178{
6179 TRANSACTION2_SPI_REQ *pSMB = NULL;
6180 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6181 int name_len;
6182 int rc = 0;
6183 int bytes_returned = 0;
6184 FILE_UNIX_BASIC_INFO *data_offset;
6185 __u16 params, param_offset, offset, count, byte_count;
6186
Joe Perchesf96637b2013-05-04 22:12:25 -05006187 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188setPermsRetry:
6189 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6190 (void **) &pSMBr);
6191 if (rc)
6192 return rc;
6193
6194 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6195 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006196 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006197 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 name_len++; /* trailing null */
6199 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006200 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006201 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006203 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 }
6205
6206 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006207 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006209 /* BB find max SMB PDU from sess structure BB */
6210 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 pSMB->MaxSetupCount = 0;
6212 pSMB->Reserved = 0;
6213 pSMB->Flags = 0;
6214 pSMB->Timeout = 0;
6215 pSMB->Reserved2 = 0;
6216 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006217 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 offset = param_offset + params;
6219 data_offset =
6220 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6221 offset);
6222 memset(data_offset, 0, count);
6223 pSMB->DataOffset = cpu_to_le16(offset);
6224 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6225 pSMB->SetupCount = 1;
6226 pSMB->Reserved3 = 0;
6227 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6228 byte_count = 3 /* pad */ + params + count;
6229 pSMB->ParameterCount = cpu_to_le16(params);
6230 pSMB->DataCount = cpu_to_le16(count);
6231 pSMB->TotalParameterCount = pSMB->ParameterCount;
6232 pSMB->TotalDataCount = pSMB->DataCount;
6233 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6234 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006235 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006236
Jeff Layton654cf142009-07-09 20:02:49 -04006237 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
6239 pSMB->ByteCount = cpu_to_le16(byte_count);
6240 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6241 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006242 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006243 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
Steve French0d817bc2008-05-22 02:02:03 +00006245 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 if (rc == -EAGAIN)
6247 goto setPermsRetry;
6248 return rc;
6249}
6250
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006252/*
6253 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6254 * function used by listxattr and getxattr type calls. When ea_name is set,
6255 * it looks for that attribute name and stuffs that value into the EAData
6256 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6257 * buffer. In both cases, the return value is either the length of the
6258 * resulting data or a negative error code. If EAData is a NULL pointer then
6259 * the data isn't copied to it, but the length is returned.
6260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006262CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006263 const unsigned char *searchName, const unsigned char *ea_name,
6264 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006265 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266{
6267 /* BB assumes one setup word */
6268 TRANSACTION2_QPI_REQ *pSMB = NULL;
6269 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006270 int remap = cifs_remap(cifs_sb);
6271 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 int rc = 0;
6273 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006274 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006275 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006276 struct fea *temp_fea;
6277 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006278 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006279 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006280 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Joe Perchesf96637b2013-05-04 22:12:25 -05006282 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283QAllEAsRetry:
6284 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6285 (void **) &pSMBr);
6286 if (rc)
6287 return rc;
6288
6289 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006290 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006291 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6292 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006293 list_len++; /* trailing null */
6294 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006296 list_len = strnlen(searchName, PATH_MAX);
6297 list_len++; /* trailing null */
6298 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 }
6300
Jeff Layton6e462b92010-02-10 16:18:26 -05006301 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 pSMB->TotalDataCount = 0;
6303 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006304 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006305 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 pSMB->MaxSetupCount = 0;
6307 pSMB->Reserved = 0;
6308 pSMB->Flags = 0;
6309 pSMB->Timeout = 0;
6310 pSMB->Reserved2 = 0;
6311 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006312 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 pSMB->DataCount = 0;
6314 pSMB->DataOffset = 0;
6315 pSMB->SetupCount = 1;
6316 pSMB->Reserved3 = 0;
6317 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6318 byte_count = params + 1 /* pad */ ;
6319 pSMB->TotalParameterCount = cpu_to_le16(params);
6320 pSMB->ParameterCount = pSMB->TotalParameterCount;
6321 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6322 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006323 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 pSMB->ByteCount = cpu_to_le16(byte_count);
6325
6326 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6327 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6328 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006329 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006330 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006332
6333
6334 /* BB also check enough total bytes returned */
6335 /* BB we need to improve the validity checking
6336 of these trans2 responses */
6337
6338 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006339 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006340 rc = -EIO; /* bad smb */
6341 goto QAllEAsOut;
6342 }
6343
6344 /* check that length of list is not more than bcc */
6345 /* check that each entry does not go beyond length
6346 of list */
6347 /* check that each element of each entry does not
6348 go beyond end of list */
6349 /* validate_trans2_offsets() */
6350 /* BB check if start of smb + data_offset > &bcc+ bcc */
6351
6352 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6353 ea_response_data = (struct fealist *)
6354 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6355
Jeff Layton6e462b92010-02-10 16:18:26 -05006356 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006357 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006358 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006359 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006360 /* didn't find the named attribute */
6361 if (ea_name)
6362 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006363 goto QAllEAsOut;
6364 }
6365
Jeff Layton0cd126b2010-02-10 16:18:26 -05006366 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006367 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006368 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006369 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006370 rc = -EIO;
6371 goto QAllEAsOut;
6372 }
6373
Jeff Laytonf0d38682010-02-10 16:18:26 -05006374 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006375 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006376 temp_fea = ea_response_data->list;
6377 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006378 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006379 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006380 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006381
Jeff Layton6e462b92010-02-10 16:18:26 -05006382 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006383 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006384 /* make sure we can read name_len and value_len */
6385 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006386 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006387 rc = -EIO;
6388 goto QAllEAsOut;
6389 }
6390
6391 name_len = temp_fea->name_len;
6392 value_len = le16_to_cpu(temp_fea->value_len);
6393 list_len -= name_len + 1 + value_len;
6394 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006395 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006396 rc = -EIO;
6397 goto QAllEAsOut;
6398 }
6399
Jeff Layton31c05192010-02-10 16:18:26 -05006400 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006401 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006402 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006403 temp_ptr += name_len + 1;
6404 rc = value_len;
6405 if (buf_size == 0)
6406 goto QAllEAsOut;
6407 if ((size_t)value_len > buf_size) {
6408 rc = -ERANGE;
6409 goto QAllEAsOut;
6410 }
6411 memcpy(EAData, temp_ptr, value_len);
6412 goto QAllEAsOut;
6413 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006414 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006415 /* account for prefix user. and trailing null */
6416 rc += (5 + 1 + name_len);
6417 if (rc < (int) buf_size) {
6418 memcpy(EAData, "user.", 5);
6419 EAData += 5;
6420 memcpy(EAData, temp_ptr, name_len);
6421 EAData += name_len;
6422 /* null terminate name */
6423 *EAData = 0;
6424 ++EAData;
6425 } else if (buf_size == 0) {
6426 /* skip copy - calc size only */
6427 } else {
6428 /* stop before overrun buffer */
6429 rc = -ERANGE;
6430 break;
6431 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006432 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006433 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006434 temp_fea = (struct fea *)temp_ptr;
6435 }
6436
Jeff Layton31c05192010-02-10 16:18:26 -05006437 /* didn't find the named attribute */
6438 if (ea_name)
6439 rc = -ENODATA;
6440
Jeff Laytonf0d38682010-02-10 16:18:26 -05006441QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006442 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 if (rc == -EAGAIN)
6444 goto QAllEAsRetry;
6445
6446 return (ssize_t)rc;
6447}
6448
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006450CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6451 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006452 const __u16 ea_value_len, const struct nls_table *nls_codepage,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006453 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454{
6455 struct smb_com_transaction2_spi_req *pSMB = NULL;
6456 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6457 struct fealist *parm_data;
6458 int name_len;
6459 int rc = 0;
6460 int bytes_returned = 0;
6461 __u16 params, param_offset, byte_count, offset, count;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10006462 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
Joe Perchesf96637b2013-05-04 22:12:25 -05006464 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465SetEARetry:
6466 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6467 (void **) &pSMBr);
6468 if (rc)
6469 return rc;
6470
6471 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6472 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006473 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6474 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 name_len++; /* trailing null */
6476 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006477 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 name_len = strnlen(fileName, PATH_MAX);
6479 name_len++; /* trailing null */
6480 strncpy(pSMB->FileName, fileName, name_len);
6481 }
6482
6483 params = 6 + name_len;
6484
6485 /* done calculating parms using name_len of file name,
6486 now use name_len to calculate length of ea name
6487 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006488 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 name_len = 0;
6490 else
Steve French50c2f752007-07-13 00:33:32 +00006491 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006493 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006495 /* BB find max SMB PDU from sess */
6496 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 pSMB->MaxSetupCount = 0;
6498 pSMB->Reserved = 0;
6499 pSMB->Flags = 0;
6500 pSMB->Timeout = 0;
6501 pSMB->Reserved2 = 0;
6502 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006503 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 offset = param_offset + params;
6505 pSMB->InformationLevel =
6506 cpu_to_le16(SMB_SET_FILE_EA);
6507
Arnd Bergmannade7db92018-02-02 16:48:47 +01006508 parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6510 pSMB->DataOffset = cpu_to_le16(offset);
6511 pSMB->SetupCount = 1;
6512 pSMB->Reserved3 = 0;
6513 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6514 byte_count = 3 /* pad */ + params + count;
6515 pSMB->DataCount = cpu_to_le16(count);
6516 parm_data->list_len = cpu_to_le32(count);
6517 parm_data->list[0].EA_flags = 0;
6518 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006519 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006521 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006522 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 parm_data->list[0].name[name_len] = 0;
6524 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6525 /* caller ensures that ea_value_len is less than 64K but
6526 we need to ensure that it fits within the smb */
6527
Steve French50c2f752007-07-13 00:33:32 +00006528 /*BB add length check to see if it would fit in
6529 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006530 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6531 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006532 memcpy(parm_data->list[0].name+name_len+1,
6533 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
6535 pSMB->TotalDataCount = pSMB->DataCount;
6536 pSMB->ParameterCount = cpu_to_le16(params);
6537 pSMB->TotalParameterCount = pSMB->ParameterCount;
6538 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006539 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 pSMB->ByteCount = cpu_to_le16(byte_count);
6541 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6542 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006543 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006544 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
6546 cifs_buf_release(pSMB);
6547
6548 if (rc == -EAGAIN)
6549 goto SetEARetry;
6550
6551 return rc;
6552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553#endif
Steve French0eff0e22011-02-24 05:39:23 +00006554
6555#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6556/*
6557 * Years ago the kernel added a "dnotify" function for Samba server,
6558 * to allow network clients (such as Windows) to display updated
6559 * lists of files in directory listings automatically when
6560 * files are added by one user when another user has the
6561 * same directory open on their desktop. The Linux cifs kernel
6562 * client hooked into the kernel side of this interface for
6563 * the same reason, but ironically when the VFS moved from
6564 * "dnotify" to "inotify" it became harder to plug in Linux
6565 * network file system clients (the most obvious use case
6566 * for notify interfaces is when multiple users can update
6567 * the contents of the same directory - exactly what network
6568 * file systems can do) although the server (Samba) could
6569 * still use it. For the short term we leave the worker
6570 * function ifdeffed out (below) until inotify is fixed
6571 * in the VFS to make it easier to plug in network file
6572 * system clients. If inotify turns out to be permanently
6573 * incompatible for network fs clients, we could instead simply
6574 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6575 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006576int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006577 const int notify_subdirs, const __u16 netfid,
6578 __u32 filter, struct file *pfile, int multishot,
6579 const struct nls_table *nls_codepage)
6580{
6581 int rc = 0;
6582 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6583 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6584 struct dir_notify_req *dnotify_req;
6585 int bytes_returned;
6586
Joe Perchesf96637b2013-05-04 22:12:25 -05006587 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006588 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6589 (void **) &pSMBr);
6590 if (rc)
6591 return rc;
6592
6593 pSMB->TotalParameterCount = 0 ;
6594 pSMB->TotalDataCount = 0;
6595 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006596 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006597 pSMB->MaxSetupCount = 4;
6598 pSMB->Reserved = 0;
6599 pSMB->ParameterOffset = 0;
6600 pSMB->DataCount = 0;
6601 pSMB->DataOffset = 0;
6602 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6603 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6604 pSMB->ParameterCount = pSMB->TotalParameterCount;
6605 if (notify_subdirs)
6606 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6607 pSMB->Reserved2 = 0;
6608 pSMB->CompletionFilter = cpu_to_le32(filter);
6609 pSMB->Fid = netfid; /* file handle always le */
6610 pSMB->ByteCount = 0;
6611
6612 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6613 (struct smb_hdr *)pSMBr, &bytes_returned,
6614 CIFS_ASYNC_OP);
6615 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006616 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006617 } else {
6618 /* Add file to outstanding requests */
6619 /* BB change to kmem cache alloc */
6620 dnotify_req = kmalloc(
6621 sizeof(struct dir_notify_req),
6622 GFP_KERNEL);
6623 if (dnotify_req) {
6624 dnotify_req->Pid = pSMB->hdr.Pid;
6625 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6626 dnotify_req->Mid = pSMB->hdr.Mid;
6627 dnotify_req->Tid = pSMB->hdr.Tid;
6628 dnotify_req->Uid = pSMB->hdr.Uid;
6629 dnotify_req->netfid = netfid;
6630 dnotify_req->pfile = pfile;
6631 dnotify_req->filter = filter;
6632 dnotify_req->multishot = multishot;
6633 spin_lock(&GlobalMid_Lock);
6634 list_add_tail(&dnotify_req->lhead,
6635 &GlobalDnotifyReqList);
6636 spin_unlock(&GlobalMid_Lock);
6637 } else
6638 rc = -ENOMEM;
6639 }
6640 cifs_buf_release(pSMB);
6641 return rc;
6642}
6643#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */