blob: 118a63e7e221fa54630a6149d3e664d688a805ba [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef CONFIG_CIFS_POSIX
48static struct {
49 int index;
50 char *name;
51} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000052#ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000054 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000055#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000056 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000057 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 {BAD_PROT, "\2"}
59};
60#else
61static struct {
62 int index;
63 char *name;
64} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000065#ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000067 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000068#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000069 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {BAD_PROT, "\2"}
71};
72#endif
73
Steve French39798772006-05-31 22:40:51 +000074/* define the number of elements in the cifs dialect array */
75#ifdef CONFIG_CIFS_POSIX
76#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000077#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000078#else
79#define CIFS_NUM_PROT 2
80#endif /* CIFS_WEAK_PW_HASH */
81#else /* not posix */
82#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000083#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000084#else
85#define CIFS_NUM_PROT 1
86#endif /* CONFIG_CIFS_WEAK_PW_HASH */
87#endif /* CIFS_POSIX */
88
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +040089/*
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
92 */
93void
94cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000097 struct list_head *tmp;
98 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400100 /* list all files open on tree connection and mark them invalid */
Steve French3afca262016-09-22 18:58:16 -0500101 spin_lock(&tcon->open_file_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000104 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400105 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Steve French3afca262016-09-22 18:58:16 -0500107 spin_unlock(&tcon->open_file_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400108 /*
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
110 * to this tcon.
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jeff Layton9162ab22009-09-03 12:07:17 -0400114/* reconnect the socket, tcon, and smb session if needed */
115static int
Steve French96daf2b2011-05-27 04:34:02 +0000116cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400117{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400118 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000119 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
122
123 /*
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
126 * calling routine
127 */
128 if (!tcon)
129 return 0;
130
131 ses = tcon->ses;
132 server = ses->server;
133
134 /*
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
137 */
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
143 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400144 return -ENODEV;
145 }
146 }
147
Jeff Layton9162ab22009-09-03 12:07:17 -0400148 /*
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
151 */
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400155
Steve Frenchfd88ce92011-04-12 01:01:14 +0000156 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400157 if (server->tcpStatus != CifsNeedReconnect)
158 break;
159
160 /*
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
163 * back on-line
164 */
Jeff Laytond4025392011-02-07 08:54:35 -0500165 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400167 return -EHOSTDOWN;
168 }
169 }
170
171 if (!ses->need_reconnect && !tcon->need_reconnect)
172 return 0;
173
174 nls_codepage = load_nls_default();
175
176 /*
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
179 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000180 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400183 rc = cifs_setup_session(0, ses, nls_codepage);
184
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000187 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400188 goto out;
189 }
190
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400191 cifs_mark_open_files_invalid(tcon);
Jeff Layton9162ab22009-09-03 12:07:17 -0400192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000193 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400195
196 if (rc)
197 goto out;
198
Jeff Layton9162ab22009-09-03 12:07:17 -0400199 atomic_inc(&tconInfoReconnectCount);
200
201 /* tell server Unix caps we support */
202 if (ses->capabilities & CAP_UNIX)
203 reset_cifs_unix_caps(0, tcon, NULL, NULL);
204
205 /*
206 * Removed call to reopen open files here. It is safer (and faster) to
207 * reopen files one at a time as needed in read and write.
208 *
209 * FIXME: what about file locks? don't we need to reclaim them ASAP?
210 */
211
212out:
213 /*
214 * Check if handle based operation so we know whether we can continue
215 * or not without returning to caller to reset file handle
216 */
217 switch (smb_command) {
218 case SMB_COM_READ_ANDX:
219 case SMB_COM_WRITE_ANDX:
220 case SMB_COM_CLOSE:
221 case SMB_COM_FIND_CLOSE2:
222 case SMB_COM_LOCKING_ANDX:
223 rc = -EAGAIN;
224 }
225
226 unload_nls(nls_codepage);
227 return rc;
228}
229
Steve Frenchad7a2922008-02-07 23:25:02 +0000230/* Allocate and return pointer to an SMB request buffer, and set basic
231 SMB information in the SMB header. If the return code is zero, this
232 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int
Steve French96daf2b2011-05-27 04:34:02 +0000234small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000235 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jeff Laytonf5695992010-09-29 15:27:08 -0400237 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jeff Layton9162ab22009-09-03 12:07:17 -0400239 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000240 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return rc;
242
243 *request_buf = cifs_small_buf_get();
244 if (*request_buf == NULL) {
245 /* BB should we add a retry in here if not a writepage? */
246 return -ENOMEM;
247 }
248
Steve French63135e02007-07-17 17:34:02 +0000249 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000250 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Steve French790fe572007-07-07 19:25:05 +0000252 if (tcon != NULL)
253 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700254
Jeff Laytonf5695992010-09-29 15:27:08 -0400255 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000256}
257
Steve French12b3b8f2006-02-09 21:12:47 +0000258int
Steve French50c2f752007-07-13 00:33:32 +0000259small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000260 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000261{
262 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000263 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000264
Steve French5815449d2006-02-14 01:36:20 +0000265 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000266 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000267 return rc;
268
Steve French04fdabe2006-02-10 05:52:50 +0000269 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400270 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000271 if (ses->capabilities & CAP_UNICODE)
272 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000273 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000274 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
275
276 /* uid, tid can stay at zero as set in header assemble */
277
Steve French50c2f752007-07-13 00:33:32 +0000278 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000279 this function is used after 1st of session setup requests */
280
281 return rc;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* If the return code is zero, this function must fill in request_buf pointer */
285static int
Steve French96daf2b2011-05-27 04:34:02 +0000286__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400287 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *request_buf = cifs_buf_get();
290 if (*request_buf == NULL) {
291 /* BB should we add a retry in here if not a writepage? */
292 return -ENOMEM;
293 }
294 /* Although the original thought was we needed the response buf for */
295 /* potential retries of smb operations it turns out we can determine */
296 /* from the mid flags when the request buffer can be resent without */
297 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000298 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000299 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000302 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Steve French790fe572007-07-07 19:25:05 +0000304 if (tcon != NULL)
305 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700306
Jeff Laytonf5695992010-09-29 15:27:08 -0400307 return 0;
308}
309
310/* If the return code is zero, this function must fill in request_buf pointer */
311static int
Steve French96daf2b2011-05-27 04:34:02 +0000312smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400313 void **request_buf, void **response_buf)
314{
315 int rc;
316
317 rc = cifs_reconnect_tcon(tcon, smb_command);
318 if (rc)
319 return rc;
320
321 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
322}
323
324static int
Steve French96daf2b2011-05-27 04:34:02 +0000325smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400326 void **request_buf, void **response_buf)
327{
328 if (tcon->ses->need_reconnect || tcon->need_reconnect)
329 return -EHOSTDOWN;
330
331 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Steve French50c2f752007-07-13 00:33:32 +0000334static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jeff Layton12df83c2011-01-20 13:36:51 -0500336 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Jeff Layton12df83c2011-01-20 13:36:51 -0500338 /* check for plausible wct */
339 if (pSMB->hdr.WordCount < 10)
340 goto vt2_err;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500343 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
344 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
345 goto vt2_err;
346
Jeff Layton12df83c2011-01-20 13:36:51 -0500347 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
348 if (total_size >= 512)
349 goto vt2_err;
350
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400351 /* check that bcc is at least as big as parms + data, and that it is
352 * less than negotiated smb buffer
353 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500354 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
355 if (total_size > get_bcc(&pSMB->hdr) ||
356 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
357 goto vt2_err;
358
359 return 0;
360vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000361 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Jeff Layton690c5222011-01-20 13:36:51 -0500365
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400366static int
Jeff Layton3f618222013-06-12 19:52:14 -0500367decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400368{
369 int rc = 0;
370 u16 count;
371 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500372 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400373
374 count = get_bcc(&pSMBr->hdr);
375 if (count < SMB1_CLIENT_GUID_SIZE)
376 return -EIO;
377
378 spin_lock(&cifs_tcp_ses_lock);
379 if (server->srv_count > 1) {
380 spin_unlock(&cifs_tcp_ses_lock);
381 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
382 cifs_dbg(FYI, "server UID changed\n");
383 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
384 }
385 } else {
386 spin_unlock(&cifs_tcp_ses_lock);
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
388 }
389
390 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500391 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400392 } else {
393 count -= SMB1_CLIENT_GUID_SIZE;
394 rc = decode_negTokenInit(
395 pSMBr->u.extended_response.SecurityBlob, count, server);
396 if (rc != 1)
397 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400398 }
399
400 return 0;
401}
402
Jeff Layton9ddec562013-05-26 07:00:58 -0400403int
Jeff Layton38d77c52013-05-26 07:01:00 -0400404cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400405{
Jeff Layton50285882013-06-27 12:45:00 -0400406 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
407 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400408 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
409
410 /*
411 * Is signing required by mnt options? If not then check
412 * global_secflags to see if it is there.
413 */
414 if (!mnt_sign_required)
415 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
416 CIFSSEC_MUST_SIGN);
417
418 /*
419 * If signing is required then it's automatically enabled too,
420 * otherwise, check to see if the secflags allow it.
421 */
422 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
423 (global_secflags & CIFSSEC_MAY_SIGN);
424
425 /* If server requires signing, does client allow it? */
426 if (srv_sign_required) {
427 if (!mnt_sign_enabled) {
428 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
429 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400430 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400431 server->sign = true;
432 }
433
434 /* If client requires signing, does server allow it? */
435 if (mnt_sign_required) {
436 if (!srv_sign_enabled) {
437 cifs_dbg(VFS, "Server does not support signing!");
438 return -ENOTSUPP;
439 }
440 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400441 }
442
443 return 0;
444}
445
Jeff Layton2190eca2013-05-26 07:00:57 -0400446#ifdef CONFIG_CIFS_WEAK_PW_HASH
447static int
Jeff Layton3f618222013-06-12 19:52:14 -0500448decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400449{
450 __s16 tmp;
451 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
452
453 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
454 return -EOPNOTSUPP;
455
Jeff Layton2190eca2013-05-26 07:00:57 -0400456 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
457 server->maxReq = min_t(unsigned int,
458 le16_to_cpu(rsp->MaxMpxCount),
459 cifs_max_pending);
460 set_credits(server, server->maxReq);
461 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Jeff Layton2190eca2013-05-26 07:00:57 -0400462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
465 server->max_rw = 0xFF00;
466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
467 } else {
468 server->max_rw = 0;/* do not need to use raw anyway */
469 server->capabilities = CAP_MPX_MODE;
470 }
471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
472 if (tmp == -1) {
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
478 * this requirement.
479 */
480 int val, seconds, remain, result;
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700481 struct timespec ts;
482 unsigned long utc = ktime_get_real_seconds();
Jeff Layton2190eca2013-05-26 07:00:57 -0400483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
485 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700486 (int)ts.tv_sec, (int)utc,
487 (int)(utc - ts.tv_sec));
488 val = (int)(utc - ts.tv_sec);
Jeff Layton2190eca2013-05-26 07:00:57 -0400489 seconds = abs(val);
490 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
491 remain = seconds % MIN_TZ_ADJ;
492 if (remain >= (MIN_TZ_ADJ / 2))
493 result += MIN_TZ_ADJ;
494 if (val < 0)
495 result = -result;
496 server->timeAdj = result;
497 } else {
498 server->timeAdj = (int)tmp;
499 server->timeAdj *= 60; /* also in seconds */
500 }
501 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
502
503
504 /* BB get server time for time conversions and add
505 code to use it and timezone since this is not UTC */
506
507 if (rsp->EncryptionKeyLength ==
508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
509 memcpy(server->cryptkey, rsp->EncryptionKey,
510 CIFS_CRYPTO_KEY_SIZE);
511 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
512 return -EIO; /* need cryptkey unless plain text */
513 }
514
515 cifs_dbg(FYI, "LANMAN negotiated\n");
516 return 0;
517}
518#else
519static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500520decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400521{
522 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
523 return -EOPNOTSUPP;
524}
525#endif
526
Jeff Layton91934002013-05-26 07:00:58 -0400527static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500528should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400529{
Jeff Layton3f618222013-06-12 19:52:14 -0500530 switch (sectype) {
531 case RawNTLMSSP:
532 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400533 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500534 case Unspecified:
535 if (global_secflags &
536 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
537 return true;
538 /* Fallthrough */
539 default:
540 return false;
541 }
Jeff Layton91934002013-05-26 07:00:58 -0400542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400545CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 NEGOTIATE_REQ *pSMB;
548 NEGOTIATE_RSP *pSMBr;
549 int rc = 0;
550 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000551 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400552 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 u16 count;
554
Jeff Layton3534b852013-05-24 07:41:01 -0400555 if (!server) {
556 WARN(1, "%s: server is NULL!\n", __func__);
557 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Jeff Layton3534b852013-05-24 07:41:01 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
561 (void **) &pSMB, (void **) &pSMBr);
562 if (rc)
563 return rc;
Steve French750d1152006-06-27 06:28:30 +0000564
Pavel Shilovsky88257362012-05-23 14:01:59 +0400565 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000566 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000567
Jeff Layton3f618222013-06-12 19:52:14 -0500568 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400569 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000570 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
571 }
Steve French50c2f752007-07-13 00:33:32 +0000572
Steve French39798772006-05-31 22:40:51 +0000573 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000574 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000575 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
576 count += strlen(protocols[i].name) + 1;
577 /* null at end of source and target buffers anyway */
578 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000579 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pSMB->ByteCount = cpu_to_le16(count);
581
582 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
583 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000584 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000585 goto neg_err_exit;
586
Jeff Layton9bf67e52010-04-24 07:57:46 -0400587 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500588 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000589 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400590 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000591 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000592 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000593 could not negotiate a common dialect */
594 rc = -EOPNOTSUPP;
595 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000596 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400597 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500598 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400599 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000600 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000601 /* unknown wct */
602 rc = -EOPNOTSUPP;
603 goto neg_err_exit;
604 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400605 /* else wct == 17, NTLM or better */
606
Steve French96daf2b2011-05-27 04:34:02 +0000607 server->sec_mode = pSMBr->SecurityMode;
608 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500609 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000610
Steve French254e55e2006-06-04 05:53:15 +0000611 /* one byte, so no need to convert this or EncryptionKeyLen from
612 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300613 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
614 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400615 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000616 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400617 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000618 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500619 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000620 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000621 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
622 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400623
Jeff Laytone598d1d82013-05-26 07:00:59 -0400624 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
625 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500626 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000627 CIFS_CRYPTO_KEY_SIZE);
Noel Powerf2910952015-05-27 09:22:10 +0100628 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
629 server->capabilities & CAP_EXTENDED_SECURITY) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400630 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500631 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400632 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000633 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400634 } else {
635 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000636 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400637 }
Steve French254e55e2006-06-04 05:53:15 +0000638
639signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400640 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400641 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000642neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700643 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000644
Joe Perchesf96637b2013-05-04 22:12:25 -0500645 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return rc;
647}
648
649int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400650CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Joe Perchesf96637b2013-05-04 22:12:25 -0500655 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500656
657 /* BB: do we need to check this? These should never be NULL. */
658 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
659 return -EIO;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500662 * No need to return error on this operation if tid invalidated and
663 * closed on server already e.g. due to tcp session crashing. Also,
664 * the tcon is no longer on the list, so no need to take lock before
665 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Steve French268875b2009-06-25 00:29:21 +0000667 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Steve French50c2f752007-07-13 00:33:32 +0000670 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700671 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500672 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return rc;
Steve French133672e2007-11-13 22:41:37 +0000674
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700676 cifs_small_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500678 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Steve French50c2f752007-07-13 00:33:32 +0000680 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500681 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (rc == -EAGAIN)
683 rc = 0;
684
685 return rc;
686}
687
Jeff Layton766fdbb2011-01-11 07:24:21 -0500688/*
689 * This is a no-op for now. We're not really interested in the reply, but
690 * rather in the fact that the server sent one and that server->lstrp
691 * gets updated.
692 *
693 * FIXME: maybe we should consider checking that the reply matches request?
694 */
695static void
696cifs_echo_callback(struct mid_q_entry *mid)
697{
698 struct TCP_Server_Info *server = mid->callback_data;
699
700 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400701 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500702}
703
704int
705CIFSSMBEcho(struct TCP_Server_Info *server)
706{
707 ECHO_REQ *smb;
708 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800709 struct kvec iov[2];
710 struct smb_rqst rqst = { .rq_iov = iov,
711 .rq_nvec = 2 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500712
Joe Perchesf96637b2013-05-04 22:12:25 -0500713 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500714
715 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
716 if (rc)
717 return rc;
718
Steve French26c9cb62017-05-02 13:35:20 -0500719 if (server->capabilities & CAP_UNICODE)
720 smb->hdr.Flags2 |= SMBFLG2_UNICODE;
721
Jeff Layton766fdbb2011-01-11 07:24:21 -0500722 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000723 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500724 smb->hdr.WordCount = 1;
725 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400726 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500727 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000728 inc_rfc1001_len(smb, 3);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800729
730 iov[0].iov_len = 4;
731 iov[0].iov_base = smb;
732 iov[1].iov_len = get_rfc1002_length(smb);
733 iov[1].iov_base = (char *)smb + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500734
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800735 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400736 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500737 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500738 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500739
740 cifs_small_buf_release(smb);
741
742 return rc;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400746CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 LOGOFF_ANDX_REQ *pSMB;
749 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Joe Perchesf96637b2013-05-04 22:12:25 -0500751 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500752
753 /*
754 * BB: do we need to check validity of ses and server? They should
755 * always be valid since we have an active reference. If not, that
756 * should probably be a BUG()
757 */
758 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EIO;
760
Steve Frenchd7b619c2010-02-25 05:36:46 +0000761 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000762 if (ses->need_reconnect)
763 goto session_already_dead; /* no need to send SMBlogoff if uid
764 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
766 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000767 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return rc;
769 }
770
Pavel Shilovsky88257362012-05-23 14:01:59 +0400771 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700772
Jeff Layton38d77c52013-05-26 07:01:00 -0400773 if (ses->server->sign)
774 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 pSMB->hdr.Uid = ses->Suid;
777
778 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400779 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700780 cifs_small_buf_release(pSMB);
Steve French3b795212008-11-13 19:45:32 +0000781session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000782 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000785 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 error */
787 if (rc == -EAGAIN)
788 rc = 0;
789 return rc;
790}
791
792int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400793CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
794 const char *fileName, __u16 type,
795 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000796{
797 TRANSACTION2_SPI_REQ *pSMB = NULL;
798 TRANSACTION2_SPI_RSP *pSMBr = NULL;
799 struct unlink_psx_rq *pRqD;
800 int name_len;
801 int rc = 0;
802 int bytes_returned = 0;
803 __u16 params, param_offset, offset, byte_count;
804
Joe Perchesf96637b2013-05-04 22:12:25 -0500805 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000806PsxDelete:
807 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
808 (void **) &pSMBr);
809 if (rc)
810 return rc;
811
812 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
813 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600814 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
815 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000816 name_len++; /* trailing null */
817 name_len *= 2;
818 } else { /* BB add path length overrun check */
819 name_len = strnlen(fileName, PATH_MAX);
820 name_len++; /* trailing null */
821 strncpy(pSMB->FileName, fileName, name_len);
822 }
823
824 params = 6 + name_len;
825 pSMB->MaxParameterCount = cpu_to_le16(2);
826 pSMB->MaxDataCount = 0; /* BB double check this with jra */
827 pSMB->MaxSetupCount = 0;
828 pSMB->Reserved = 0;
829 pSMB->Flags = 0;
830 pSMB->Timeout = 0;
831 pSMB->Reserved2 = 0;
832 param_offset = offsetof(struct smb_com_transaction2_spi_req,
833 InformationLevel) - 4;
834 offset = param_offset + params;
835
836 /* Setup pointer to Request Data (inode type) */
837 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
838 pRqD->type = cpu_to_le16(type);
839 pSMB->ParameterOffset = cpu_to_le16(param_offset);
840 pSMB->DataOffset = cpu_to_le16(offset);
841 pSMB->SetupCount = 1;
842 pSMB->Reserved3 = 0;
843 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
844 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
845
846 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
847 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
848 pSMB->ParameterCount = cpu_to_le16(params);
849 pSMB->TotalParameterCount = pSMB->ParameterCount;
850 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
851 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000852 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000853 pSMB->ByteCount = cpu_to_le16(byte_count);
854 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
855 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000856 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500857 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000858 cifs_buf_release(pSMB);
859
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400860 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000861
862 if (rc == -EAGAIN)
863 goto PsxDelete;
864
865 return rc;
866}
867
868int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700869CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
870 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 DELETE_FILE_REQ *pSMB = NULL;
873 DELETE_FILE_RSP *pSMBr = NULL;
874 int rc = 0;
875 int bytes_returned;
876 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500877 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879DelFileRetry:
880 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
881 (void **) &pSMBr);
882 if (rc)
883 return rc;
884
885 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700886 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
887 PATH_MAX, cifs_sb->local_nls,
888 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 name_len++; /* trailing null */
890 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700891 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700892 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700894 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 pSMB->SearchAttributes =
897 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
898 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000899 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 pSMB->ByteCount = cpu_to_le16(name_len + 1);
901 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
902 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400903 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000904 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500905 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 cifs_buf_release(pSMB);
908 if (rc == -EAGAIN)
909 goto DelFileRetry;
910
911 return rc;
912}
913
914int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400915CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
916 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 DELETE_DIRECTORY_REQ *pSMB = NULL;
919 DELETE_DIRECTORY_RSP *pSMBr = NULL;
920 int rc = 0;
921 int bytes_returned;
922 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500923 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Joe Perchesf96637b2013-05-04 22:12:25 -0500925 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926RmDirRetry:
927 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
928 (void **) &pSMBr);
929 if (rc)
930 return rc;
931
932 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400933 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
934 PATH_MAX, cifs_sb->local_nls,
935 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 name_len++; /* trailing null */
937 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700938 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400939 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400941 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
944 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000945 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 pSMB->ByteCount = cpu_to_le16(name_len + 1);
947 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
948 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400949 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000950 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500951 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 cifs_buf_release(pSMB);
954 if (rc == -EAGAIN)
955 goto RmDirRetry;
956 return rc;
957}
958
959int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300960CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
961 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 int rc = 0;
964 CREATE_DIRECTORY_REQ *pSMB = NULL;
965 CREATE_DIRECTORY_RSP *pSMBr = NULL;
966 int bytes_returned;
967 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500968 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Joe Perchesf96637b2013-05-04 22:12:25 -0500970 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971MkDirRetry:
972 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
973 (void **) &pSMBr);
974 if (rc)
975 return rc;
976
977 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600978 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300979 PATH_MAX, cifs_sb->local_nls,
980 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 name_len++; /* trailing null */
982 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700983 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 name_len = strnlen(name, PATH_MAX);
985 name_len++; /* trailing null */
986 strncpy(pSMB->DirName, name, name_len);
987 }
988
989 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000990 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 pSMB->ByteCount = cpu_to_le16(name_len + 1);
992 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
993 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400994 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000995 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500996 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 cifs_buf_release(pSMB);
999 if (rc == -EAGAIN)
1000 goto MkDirRetry;
1001 return rc;
1002}
1003
Steve French2dd29d32007-04-23 22:07:35 +00001004int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001005CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1006 __u32 posix_flags, __u64 mode, __u16 *netfid,
1007 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1008 const char *name, const struct nls_table *nls_codepage,
1009 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001010{
1011 TRANSACTION2_SPI_REQ *pSMB = NULL;
1012 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1013 int name_len;
1014 int rc = 0;
1015 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001016 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001017 OPEN_PSX_REQ *pdata;
1018 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001019
Joe Perchesf96637b2013-05-04 22:12:25 -05001020 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001021PsxCreat:
1022 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1023 (void **) &pSMBr);
1024 if (rc)
1025 return rc;
1026
1027 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1028 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001029 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1030 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001031 name_len++; /* trailing null */
1032 name_len *= 2;
1033 } else { /* BB improve the check for buffer overruns BB */
1034 name_len = strnlen(name, PATH_MAX);
1035 name_len++; /* trailing null */
1036 strncpy(pSMB->FileName, name, name_len);
1037 }
1038
1039 params = 6 + name_len;
1040 count = sizeof(OPEN_PSX_REQ);
1041 pSMB->MaxParameterCount = cpu_to_le16(2);
1042 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1043 pSMB->MaxSetupCount = 0;
1044 pSMB->Reserved = 0;
1045 pSMB->Flags = 0;
1046 pSMB->Timeout = 0;
1047 pSMB->Reserved2 = 0;
1048 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001049 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001050 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001051 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001052 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001053 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001054 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001055 pdata->OpenFlags = cpu_to_le32(*pOplock);
1056 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1057 pSMB->DataOffset = cpu_to_le16(offset);
1058 pSMB->SetupCount = 1;
1059 pSMB->Reserved3 = 0;
1060 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1061 byte_count = 3 /* pad */ + params + count;
1062
1063 pSMB->DataCount = cpu_to_le16(count);
1064 pSMB->ParameterCount = cpu_to_le16(params);
1065 pSMB->TotalDataCount = pSMB->DataCount;
1066 pSMB->TotalParameterCount = pSMB->ParameterCount;
1067 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1068 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001069 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001070 pSMB->ByteCount = cpu_to_le16(byte_count);
1071 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1072 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1073 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001074 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001075 goto psx_create_err;
1076 }
1077
Joe Perchesf96637b2013-05-04 22:12:25 -05001078 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001079 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1080
Jeff Layton820a8032011-05-04 08:05:26 -04001081 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001082 rc = -EIO; /* bad smb */
1083 goto psx_create_err;
1084 }
1085
1086 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001087 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001088 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001089
Steve French2dd29d32007-04-23 22:07:35 +00001090 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001091 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001092 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1093 /* Let caller know file was created so we can set the mode. */
1094 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001095 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001096 *pOplock |= CIFS_CREATE_ACTION;
1097 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001098 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1099 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001100 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001101 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001102 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001103 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001104 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001105 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001106 goto psx_create_err;
1107 }
Steve French50c2f752007-07-13 00:33:32 +00001108 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001109 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001110 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001111 }
Steve French2dd29d32007-04-23 22:07:35 +00001112
1113psx_create_err:
1114 cifs_buf_release(pSMB);
1115
Steve French65bc98b2009-07-10 15:27:25 +00001116 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001117 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001118 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001119 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001120
1121 if (rc == -EAGAIN)
1122 goto PsxCreat;
1123
Steve French50c2f752007-07-13 00:33:32 +00001124 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001125}
1126
Steve Frencha9d02ad2005-08-24 23:06:05 -07001127static __u16 convert_disposition(int disposition)
1128{
1129 __u16 ofun = 0;
1130
1131 switch (disposition) {
1132 case FILE_SUPERSEDE:
1133 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1134 break;
1135 case FILE_OPEN:
1136 ofun = SMBOPEN_OAPPEND;
1137 break;
1138 case FILE_CREATE:
1139 ofun = SMBOPEN_OCREATE;
1140 break;
1141 case FILE_OPEN_IF:
1142 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1143 break;
1144 case FILE_OVERWRITE:
1145 ofun = SMBOPEN_OTRUNC;
1146 break;
1147 case FILE_OVERWRITE_IF:
1148 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1149 break;
1150 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001151 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001152 ofun = SMBOPEN_OAPPEND; /* regular open */
1153 }
1154 return ofun;
1155}
1156
Jeff Layton35fc37d2008-05-14 10:22:03 -07001157static int
1158access_flags_to_smbopen_mode(const int access_flags)
1159{
1160 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1161
1162 if (masked_flags == GENERIC_READ)
1163 return SMBOPEN_READ;
1164 else if (masked_flags == GENERIC_WRITE)
1165 return SMBOPEN_WRITE;
1166
1167 /* just go for read/write */
1168 return SMBOPEN_READWRITE;
1169}
1170
Steve Frencha9d02ad2005-08-24 23:06:05 -07001171int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001172SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001173 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001174 const int access_flags, const int create_options, __u16 *netfid,
1175 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001176 const struct nls_table *nls_codepage, int remap)
1177{
1178 int rc = -EACCES;
1179 OPENX_REQ *pSMB = NULL;
1180 OPENX_RSP *pSMBr = NULL;
1181 int bytes_returned;
1182 int name_len;
1183 __u16 count;
1184
1185OldOpenRetry:
1186 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1187 (void **) &pSMBr);
1188 if (rc)
1189 return rc;
1190
1191 pSMB->AndXCommand = 0xFF; /* none */
1192
1193 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1194 count = 1; /* account for one byte pad to word boundary */
1195 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001196 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1197 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001198 name_len++; /* trailing null */
1199 name_len *= 2;
1200 } else { /* BB improve check for buffer overruns BB */
1201 count = 0; /* no pad */
1202 name_len = strnlen(fileName, PATH_MAX);
1203 name_len++; /* trailing null */
1204 strncpy(pSMB->fileName, fileName, name_len);
1205 }
1206 if (*pOplock & REQ_OPLOCK)
1207 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001208 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001209 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001210
Steve Frencha9d02ad2005-08-24 23:06:05 -07001211 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001212 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001213 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1214 /* set file as system file if special file such
1215 as fifo and server expecting SFU style and
1216 no Unix extensions */
1217
Steve French790fe572007-07-07 19:25:05 +00001218 if (create_options & CREATE_OPTION_SPECIAL)
1219 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001220 else /* BB FIXME BB */
1221 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001222
Jeff Layton67750fb2008-05-09 22:28:02 +00001223 if (create_options & CREATE_OPTION_READONLY)
1224 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001225
1226 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001227/* pSMB->CreateOptions = cpu_to_le32(create_options &
1228 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001229 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001230
1231 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001232 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001233 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001234 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001235
1236 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001237 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001238 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001239 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001240 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001241 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001242 } else {
1243 /* BB verify if wct == 15 */
1244
Steve French582d21e2008-05-13 04:54:12 +00001245/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001246
1247 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1248 /* Let caller know file was created so we can set the mode. */
1249 /* Do we care about the CreateAction in any other cases? */
1250 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001251/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001252 *pOplock |= CIFS_CREATE_ACTION; */
1253 /* BB FIXME END */
1254
Steve French790fe572007-07-07 19:25:05 +00001255 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001256 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1257 pfile_info->LastAccessTime = 0; /* BB fixme */
1258 pfile_info->LastWriteTime = 0; /* BB fixme */
1259 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001260 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001261 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001262 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001263 pfile_info->AllocationSize =
1264 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1265 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001266 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001267 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001268 }
1269 }
1270
1271 cifs_buf_release(pSMB);
1272 if (rc == -EAGAIN)
1273 goto OldOpenRetry;
1274 return rc;
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001278CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1279 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001282 OPEN_REQ *req = NULL;
1283 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int bytes_returned;
1285 int name_len;
1286 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001287 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1288 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001289 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001290 const struct nls_table *nls = cifs_sb->local_nls;
1291 int create_options = oparms->create_options;
1292 int desired_access = oparms->desired_access;
1293 int disposition = oparms->disposition;
1294 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001297 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1298 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (rc)
1300 return rc;
1301
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001302 /* no commands go after this */
1303 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001305 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1306 /* account for one byte pad to word boundary */
1307 count = 1;
1308 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1309 path, PATH_MAX, nls, remap);
1310 /* trailing null */
1311 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001313 req->NameLength = cpu_to_le16(name_len);
1314 } else {
1315 /* BB improve check for buffer overruns BB */
1316 /* no pad */
1317 count = 0;
1318 name_len = strnlen(path, PATH_MAX);
1319 /* trailing null */
1320 name_len++;
1321 req->NameLength = cpu_to_le16(name_len);
1322 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001324
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001325 if (*oplock & REQ_OPLOCK)
1326 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1327 else if (*oplock & REQ_BATCHOPLOCK)
1328 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1329
1330 req->DesiredAccess = cpu_to_le32(desired_access);
1331 req->AllocationSize = 0;
1332
1333 /*
1334 * Set file as system file if special file such as fifo and server
1335 * expecting SFU style and no Unix extensions.
1336 */
1337 if (create_options & CREATE_OPTION_SPECIAL)
1338 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1339 else
1340 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1341
1342 /*
1343 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1344 * sensitive checks for other servers such as Samba.
1345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001347 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Jeff Layton67750fb2008-05-09 22:28:02 +00001349 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001350 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001351
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001352 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1353 req->CreateDisposition = cpu_to_le32(disposition);
1354 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1355
Steve French09d1db52005-04-28 22:41:08 -07001356 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001357 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1358 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001361 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001363 req->ByteCount = cpu_to_le16(count);
1364 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1365 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001366 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001368 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001369 cifs_buf_release(req);
1370 if (rc == -EAGAIN)
1371 goto openRetry;
1372 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001374
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001375 /* 1 byte no need to le_to_cpu */
1376 *oplock = rsp->OplockLevel;
1377 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001378 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001379
1380 /* Let caller know file was created so we can set the mode. */
1381 /* Do we care about the CreateAction in any other cases? */
1382 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1383 *oplock |= CIFS_CREATE_ACTION;
1384
1385 if (buf) {
1386 /* copy from CreationTime to Attributes */
1387 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1388 /* the file_info buf is endian converted by caller */
1389 buf->AllocationSize = rsp->AllocationSize;
1390 buf->EndOfFile = rsp->EndOfFile;
1391 buf->NumberOfLinks = cpu_to_le32(1);
1392 buf->DeletePending = 0;
1393 }
1394
1395 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return rc;
1397}
1398
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001399/*
1400 * Discard any remaining data in the current SMB. To do this, we borrow the
1401 * current bigbuf.
1402 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001403int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001404cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001405{
Pavel Shilovsky350be252017-04-10 10:31:33 -07001406 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001407 int remaining = rfclen + 4 - server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001408
1409 while (remaining > 0) {
1410 int length;
1411
1412 length = cifs_read_from_socket(server, server->bigbuf,
1413 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001414 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001415 if (length < 0)
1416 return length;
1417 server->total_read += length;
1418 remaining -= length;
1419 }
1420
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001421 return 0;
1422}
1423
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001424static int
1425cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1426{
1427 int length;
1428 struct cifs_readdata *rdata = mid->callback_data;
1429
Pavel Shilovsky350be252017-04-10 10:31:33 -07001430 length = cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001431 dequeue_mid(mid, rdata->result);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001432 mid->resp_buf = server->smallbuf;
1433 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001434 return length;
1435}
1436
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001437int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001438cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1439{
1440 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001441 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001442 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001443 char *buf = server->smallbuf;
1444 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001445
Joe Perchesf96637b2013-05-04 22:12:25 -05001446 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1447 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001448
1449 /*
1450 * read the rest of READ_RSP header (sans Data array), or whatever we
1451 * can if there's not enough data. At this point, we've read down to
1452 * the Mid.
1453 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001454 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001455 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001456
Al Viroa6137302016-01-09 19:37:16 -05001457 length = cifs_read_from_socket(server,
1458 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001459 if (length < 0)
1460 return length;
1461 server->total_read += length;
1462
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001463 if (server->ops->is_session_expired &&
1464 server->ops->is_session_expired(buf)) {
1465 cifs_reconnect(server);
1466 wake_up(&server->response_q);
1467 return -1;
1468 }
1469
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001470 if (server->ops->is_status_pending &&
1471 server->ops->is_status_pending(buf, server, 0)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001472 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001473 return -1;
1474 }
1475
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001476 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001477 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001478 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001479 cifs_dbg(FYI, "%s: server returned error %d\n",
1480 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001481 return cifs_readv_discard(server, mid);
1482 }
1483
1484 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001485 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001486 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1487 __func__, server->total_read,
1488 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001489 rdata->result = -EIO;
1490 return cifs_readv_discard(server, mid);
1491 }
1492
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001493 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001494 if (data_offset < server->total_read) {
1495 /*
1496 * win2k8 sometimes sends an offset of 0 when the read
1497 * is beyond the EOF. Treat it as if the data starts just after
1498 * the header.
1499 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001500 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1501 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001502 data_offset = server->total_read;
1503 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1504 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001505 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1506 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001507 rdata->result = -EIO;
1508 return cifs_readv_discard(server, mid);
1509 }
1510
Joe Perchesf96637b2013-05-04 22:12:25 -05001511 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1512 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001513
1514 len = data_offset - server->total_read;
1515 if (len > 0) {
1516 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001517 length = cifs_read_from_socket(server,
1518 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001519 if (length < 0)
1520 return length;
1521 server->total_read += length;
1522 }
1523
1524 /* set up first iov for signature check */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001525 rdata->iov[0].iov_base = buf;
1526 rdata->iov[0].iov_len = 4;
1527 rdata->iov[1].iov_base = buf + 4;
1528 rdata->iov[1].iov_len = server->total_read - 4;
1529 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1530 rdata->iov[0].iov_base, server->total_read);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001531
1532 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001533 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001534 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535 /* data_len is corrupt -- discard frame */
1536 rdata->result = -EIO;
1537 return cifs_readv_discard(server, mid);
1538 }
1539
Jeff Layton8321fec2012-09-19 06:22:32 -07001540 length = rdata->read_into_pages(server, rdata, data_len);
1541 if (length < 0)
1542 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001543
Jeff Layton8321fec2012-09-19 06:22:32 -07001544 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545
Joe Perchesf96637b2013-05-04 22:12:25 -05001546 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1547 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001548
1549 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001550 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001551 return cifs_readv_discard(server, mid);
1552
1553 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001554 mid->resp_buf = server->smallbuf;
1555 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001556 return length;
1557}
1558
1559static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001560cifs_readv_callback(struct mid_q_entry *mid)
1561{
1562 struct cifs_readdata *rdata = mid->callback_data;
1563 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1564 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001565 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1566 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001567 .rq_pages = rdata->pages,
1568 .rq_npages = rdata->nr_pages,
1569 .rq_pagesz = rdata->pagesz,
1570 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001571
Joe Perchesf96637b2013-05-04 22:12:25 -05001572 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1573 __func__, mid->mid, mid->mid_state, rdata->result,
1574 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001575
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001576 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001577 case MID_RESPONSE_RECEIVED:
1578 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001579 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001580 int rc = 0;
1581
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001582 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001583 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001584 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001585 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1586 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001587 }
1588 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001589 task_io_account_read(rdata->got_bytes);
1590 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001591 break;
1592 case MID_REQUEST_SUBMITTED:
1593 case MID_RETRY_NEEDED:
1594 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001595 if (server->sign && rdata->got_bytes)
1596 /* reset bytes number since we can not check a sign */
1597 rdata->got_bytes = 0;
1598 /* FIXME: should this be counted toward the initiating task? */
1599 task_io_account_read(rdata->got_bytes);
1600 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001601 break;
1602 default:
1603 rdata->result = -EIO;
1604 }
1605
Jeff Laytonda472fc2012-03-23 14:40:53 -04001606 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001607 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001608 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001609}
1610
1611/* cifs_async_readv - send an async write, and set up mid to handle result */
1612int
1613cifs_async_readv(struct cifs_readdata *rdata)
1614{
1615 int rc;
1616 READ_REQ *smb = NULL;
1617 int wct;
1618 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001619 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1620 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001621
Joe Perchesf96637b2013-05-04 22:12:25 -05001622 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1623 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001624
1625 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1626 wct = 12;
1627 else {
1628 wct = 10; /* old style read */
1629 if ((rdata->offset >> 32) > 0) {
1630 /* can not handle this big offset for old */
1631 return -EIO;
1632 }
1633 }
1634
1635 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1636 if (rc)
1637 return rc;
1638
1639 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1640 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1641
1642 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001643 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001644 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1645 if (wct == 12)
1646 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1647 smb->Remaining = 0;
1648 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1649 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1650 if (wct == 12)
1651 smb->ByteCount = 0;
1652 else {
1653 /* old style read */
1654 struct smb_com_readx_req *smbr =
1655 (struct smb_com_readx_req *)smb;
1656 smbr->ByteCount = 0;
1657 }
1658
1659 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001660 rdata->iov[0].iov_base = smb;
1661 rdata->iov[0].iov_len = 4;
1662 rdata->iov[1].iov_base = (char *)smb + 4;
1663 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001664
Jeff Layton6993f742012-05-16 07:13:17 -04001665 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001666 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08001667 cifs_readv_callback, NULL, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001668
1669 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001670 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001671 else
1672 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001673
1674 cifs_small_buf_release(smb);
1675 return rc;
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001679CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1680 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int rc = -EACCES;
1683 READ_REQ *pSMB = NULL;
1684 READ_RSP *pSMBr = NULL;
1685 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001686 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001687 int resp_buf_type = 0;
1688 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001689 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001690 __u32 pid = io_parms->pid;
1691 __u16 netfid = io_parms->netfid;
1692 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001693 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001694 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Joe Perchesf96637b2013-05-04 22:12:25 -05001696 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001697 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001698 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001699 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001700 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001701 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001702 /* can not handle this big offset for old */
1703 return -EIO;
1704 }
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001708 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (rc)
1710 return rc;
1711
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001712 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1713 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /* tcon and ses pointer are checked in smb_init */
1716 if (tcon->ses->server == NULL)
1717 return -ECONNABORTED;
1718
Steve Frenchec637e32005-12-12 20:53:18 -08001719 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001721 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001722 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001723 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 pSMB->Remaining = 0;
1726 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1727 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001728 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001729 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1730 else {
1731 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001732 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001733 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001734 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001735 }
Steve Frenchec637e32005-12-12 20:53:18 -08001736
1737 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001738 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001739 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1740 CIFS_LOG_ERROR, &rsp_iov);
1741 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001742 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001743 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001745 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 } else {
1747 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1748 data_length = data_length << 16;
1749 data_length += le16_to_cpu(pSMBr->DataLength);
1750 *nbytes = data_length;
1751
1752 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001753 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001755 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001756 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 rc = -EIO;
1758 *nbytes = 0;
1759 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001760 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001761 le16_to_cpu(pSMBr->DataOffset);
1762/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001763 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001764 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001765 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001766 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001767 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Steve French790fe572007-07-07 19:25:05 +00001771 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001772 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001773 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001774 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001775 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001776 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001777 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001778 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001779 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001780 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001781
1782 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 since file handle passed in no longer valid */
1784 return rc;
1785}
1786
Steve Frenchec637e32005-12-12 20:53:18 -08001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001789CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001790 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int rc = -EACCES;
1793 WRITE_REQ *pSMB = NULL;
1794 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001795 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 __u32 bytes_sent;
1797 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001798 __u32 pid = io_parms->pid;
1799 __u16 netfid = io_parms->netfid;
1800 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001801 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001802 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Steve Frencha24e2d72010-04-03 17:20:21 +00001804 *nbytes = 0;
1805
Joe Perchesf96637b2013-05-04 22:12:25 -05001806 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001807 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001808 return -ECONNABORTED;
1809
Steve French790fe572007-07-07 19:25:05 +00001810 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001811 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001812 else {
Steve French1c955182005-08-30 20:58:07 -07001813 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001814 if ((offset >> 32) > 0) {
1815 /* can not handle big offset for old srv */
1816 return -EIO;
1817 }
1818 }
Steve French1c955182005-08-30 20:58:07 -07001819
1820 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 (void **) &pSMBr);
1822 if (rc)
1823 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001824
1825 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1826 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* tcon and ses pointer are checked in smb_init */
1829 if (tcon->ses->server == NULL)
1830 return -ECONNABORTED;
1831
1832 pSMB->AndXCommand = 0xFF; /* none */
1833 pSMB->Fid = netfid;
1834 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001835 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001836 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 pSMB->Reserved = 0xFFFFFFFF;
1839 pSMB->WriteMode = 0;
1840 pSMB->Remaining = 0;
1841
Steve French50c2f752007-07-13 00:33:32 +00001842 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 can send more if LARGE_WRITE_X capability returned by the server and if
1844 our buffer is big enough or if we convert to iovecs on socket writes
1845 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001846 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1848 } else {
1849 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1850 & ~0xFF;
1851 }
1852
1853 if (bytes_sent > count)
1854 bytes_sent = count;
1855 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001856 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001857 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001858 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04001859 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* No buffer */
1861 cifs_buf_release(pSMB);
1862 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001863 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001864 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001865 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001866 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001867 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1870 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001871 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001872
Steve French790fe572007-07-07 19:25:05 +00001873 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001874 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001875 else { /* old style write has byte count 4 bytes earlier
1876 so 4 bytes pad */
1877 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001878 (struct smb_com_writex_req *)pSMB;
1879 pSMBW->ByteCount = cpu_to_le16(byte_count);
1880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04001883 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001884 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001886 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 } else {
1888 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1889 *nbytes = (*nbytes) << 16;
1890 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301891
1892 /*
1893 * Mask off high 16 bits when bytes written as returned by the
1894 * server is greater than bytes requested by the client. Some
1895 * OS/2 servers are known to set incorrect CountHigh values.
1896 */
1897 if (*nbytes > count)
1898 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
1901 cifs_buf_release(pSMB);
1902
Steve French50c2f752007-07-13 00:33:32 +00001903 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 since file handle passed in no longer valid */
1905
1906 return rc;
1907}
1908
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001909void
1910cifs_writedata_release(struct kref *refcount)
1911{
1912 struct cifs_writedata *wdata = container_of(refcount,
1913 struct cifs_writedata, refcount);
1914
1915 if (wdata->cfile)
1916 cifsFileInfo_put(wdata->cfile);
1917
1918 kfree(wdata);
1919}
1920
1921/*
1922 * Write failed with a retryable error. Resend the write request. It's also
1923 * possible that the page was redirtied so re-clean the page.
1924 */
1925static void
1926cifs_writev_requeue(struct cifs_writedata *wdata)
1927{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001928 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001929 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001930 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001931 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001932
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001933 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1934 i = 0;
1935 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001936 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001937 struct cifs_writedata *wdata2;
1938 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001939
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001940 wsize = server->ops->wp_retry_size(inode);
1941 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001942 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001943 if (!nr_pages) {
1944 rc = -ENOTSUPP;
1945 break;
1946 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001947 cur_len = nr_pages * PAGE_SIZE;
1948 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001949 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001950 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001951 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001952 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001953 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001954
1955 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1956 if (!wdata2) {
1957 rc = -ENOMEM;
1958 break;
1959 }
1960
1961 for (j = 0; j < nr_pages; j++) {
1962 wdata2->pages[j] = wdata->pages[i + j];
1963 lock_page(wdata2->pages[j]);
1964 clear_page_dirty_for_io(wdata2->pages[j]);
1965 }
1966
1967 wdata2->sync_mode = wdata->sync_mode;
1968 wdata2->nr_pages = nr_pages;
1969 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001970 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001971 wdata2->tailsz = tailsz;
1972 wdata2->bytes = cur_len;
1973
1974 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1975 if (!wdata2->cfile) {
1976 cifs_dbg(VFS, "No writable handles for inode\n");
1977 rc = -EBADF;
1978 break;
1979 }
1980 wdata2->pid = wdata2->cfile->pid;
1981 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1982
1983 for (j = 0; j < nr_pages; j++) {
1984 unlock_page(wdata2->pages[j]);
1985 if (rc != 0 && rc != -EAGAIN) {
1986 SetPageError(wdata2->pages[j]);
1987 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001988 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001989 }
1990 }
1991
1992 if (rc) {
1993 kref_put(&wdata2->refcount, cifs_writedata_release);
1994 if (rc == -EAGAIN)
1995 continue;
1996 break;
1997 }
1998
1999 rest_len -= cur_len;
2000 i += nr_pages;
2001 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002002
2003 mapping_set_error(inode->i_mapping, rc);
2004 kref_put(&wdata->refcount, cifs_writedata_release);
2005}
2006
Jeff Laytonc2e87642012-03-23 14:40:55 -04002007void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002008cifs_writev_complete(struct work_struct *work)
2009{
2010 struct cifs_writedata *wdata = container_of(work,
2011 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002012 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002013 int i = 0;
2014
2015 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002016 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002017 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002018 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002019 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2020 wdata->bytes);
2021 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2022 return cifs_writev_requeue(wdata);
2023
2024 for (i = 0; i < wdata->nr_pages; i++) {
2025 struct page *page = wdata->pages[i];
2026 if (wdata->result == -EAGAIN)
2027 __set_page_dirty_nobuffers(page);
2028 else if (wdata->result < 0)
2029 SetPageError(page);
2030 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002031 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002032 }
2033 if (wdata->result != -EAGAIN)
2034 mapping_set_error(inode->i_mapping, wdata->result);
2035 kref_put(&wdata->refcount, cifs_writedata_release);
2036}
2037
2038struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002039cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040{
2041 struct cifs_writedata *wdata;
2042
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002043 /* writedata + number of page pointers */
2044 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002045 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002046 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002047 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002048 INIT_LIST_HEAD(&wdata->list);
2049 init_completion(&wdata->done);
2050 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002051 }
2052 return wdata;
2053}
2054
2055/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002056 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002057 * workqueue completion task.
2058 */
2059static void
2060cifs_writev_callback(struct mid_q_entry *mid)
2061{
2062 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002063 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002064 unsigned int written;
2065 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2066
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002067 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002068 case MID_RESPONSE_RECEIVED:
2069 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2070 if (wdata->result != 0)
2071 break;
2072
2073 written = le16_to_cpu(smb->CountHigh);
2074 written <<= 16;
2075 written += le16_to_cpu(smb->Count);
2076 /*
2077 * Mask off high 16 bits when bytes written as returned
2078 * by the server is greater than bytes requested by the
2079 * client. OS/2 servers are known to set incorrect
2080 * CountHigh values.
2081 */
2082 if (written > wdata->bytes)
2083 written &= 0xFFFF;
2084
2085 if (written < wdata->bytes)
2086 wdata->result = -ENOSPC;
2087 else
2088 wdata->bytes = written;
2089 break;
2090 case MID_REQUEST_SUBMITTED:
2091 case MID_RETRY_NEEDED:
2092 wdata->result = -EAGAIN;
2093 break;
2094 default:
2095 wdata->result = -EIO;
2096 break;
2097 }
2098
Jeff Laytonda472fc2012-03-23 14:40:53 -04002099 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002100 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002101 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002102}
2103
2104/* cifs_async_writev - send an async write, and set up mid to handle result */
2105int
Steve French4a5c80d2014-02-07 20:45:12 -06002106cifs_async_writev(struct cifs_writedata *wdata,
2107 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002108{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002109 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002110 WRITE_REQ *smb = NULL;
2111 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002112 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002113 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002114 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002115
2116 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2117 wct = 14;
2118 } else {
2119 wct = 12;
2120 if (wdata->offset >> 32 > 0) {
2121 /* can not handle big offset for old srv */
2122 return -EIO;
2123 }
2124 }
2125
2126 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2127 if (rc)
2128 goto async_writev_out;
2129
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002130 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2131 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002132
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002133 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002134 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002135 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2136 if (wct == 14)
2137 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2138 smb->Reserved = 0xFFFFFFFF;
2139 smb->WriteMode = 0;
2140 smb->Remaining = 0;
2141
2142 smb->DataOffset =
2143 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2144
2145 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002146 iov[0].iov_len = 4;
2147 iov[0].iov_base = smb;
2148 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2149 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002150
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002151 rqst.rq_iov = iov;
2152 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002153 rqst.rq_pages = wdata->pages;
2154 rqst.rq_npages = wdata->nr_pages;
2155 rqst.rq_pagesz = wdata->pagesz;
2156 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002157
Joe Perchesf96637b2013-05-04 22:12:25 -05002158 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2159 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002160
2161 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2162 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2163
2164 if (wct == 14) {
2165 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2166 put_bcc(wdata->bytes + 1, &smb->hdr);
2167 } else {
2168 /* wct == 12 */
2169 struct smb_com_writex_req *smbw =
2170 (struct smb_com_writex_req *)smb;
2171 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2172 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002173 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002174 }
2175
2176 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002177 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002178 cifs_writev_callback, NULL, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002179
2180 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002181 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002182 else
Steve French4a5c80d2014-02-07 20:45:12 -06002183 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002184
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002185async_writev_out:
2186 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002187 return rc;
2188}
2189
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002190int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002191CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002192 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 int rc = -EACCES;
2195 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002196 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002197 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002198 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002199 __u32 pid = io_parms->pid;
2200 __u16 netfid = io_parms->netfid;
2201 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002202 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002203 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002204 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002206 *nbytes = 0;
2207
Joe Perchesf96637b2013-05-04 22:12:25 -05002208 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002209
Steve French4c3130e2008-12-09 00:28:16 +00002210 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002211 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002212 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002213 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002214 if ((offset >> 32) > 0) {
2215 /* can not handle big offset for old srv */
2216 return -EIO;
2217 }
2218 }
Steve French8cc64c62005-10-03 13:49:43 -07002219 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (rc)
2221 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002222
2223 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2224 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* tcon and ses pointer are checked in smb_init */
2227 if (tcon->ses->server == NULL)
2228 return -ECONNABORTED;
2229
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002230 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 pSMB->Fid = netfid;
2232 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002233 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002234 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 pSMB->Reserved = 0xFFFFFFFF;
2236 pSMB->WriteMode = 0;
2237 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002240 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Steve French3e844692005-10-03 13:37:24 -07002242 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2243 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002244 /* header + 1 byte pad */
2245 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002246 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002247 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002248 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002249 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002250 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002251 pSMB->ByteCount = cpu_to_le16(count + 1);
2252 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002253 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002254 (struct smb_com_writex_req *)pSMB;
2255 pSMBW->ByteCount = cpu_to_le16(count + 5);
2256 }
Steve French3e844692005-10-03 13:37:24 -07002257 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002258 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002259 iov[0].iov_len = smb_hdr_len + 4;
2260 else /* wct == 12 pad bigger by four bytes */
2261 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002262
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002263 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2264 &rsp_iov);
2265 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002266 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002268 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002269 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002270 /* presumably this can not happen, but best to be safe */
2271 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002272 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002273 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002274 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2275 *nbytes = (*nbytes) << 16;
2276 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302277
2278 /*
2279 * Mask off high 16 bits when bytes written as returned by the
2280 * server is greater than bytes requested by the client. OS/2
2281 * servers are known to set incorrect CountHigh values.
2282 */
2283 if (*nbytes > count)
2284 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002287 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Steve French50c2f752007-07-13 00:33:32 +00002289 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 since file handle passed in no longer valid */
2291
2292 return rc;
2293}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002294
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002295int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2296 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002297 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2298{
2299 int rc = 0;
2300 LOCK_REQ *pSMB = NULL;
2301 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002302 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002303 int resp_buf_type;
2304 __u16 count;
2305
Joe Perchesf96637b2013-05-04 22:12:25 -05002306 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2307 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002308
2309 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2310 if (rc)
2311 return rc;
2312
2313 pSMB->Timeout = 0;
2314 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2315 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2316 pSMB->LockType = lock_type;
2317 pSMB->AndXCommand = 0xFF; /* none */
2318 pSMB->Fid = netfid; /* netfid stays le */
2319
2320 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2321 inc_rfc1001_len(pSMB, count);
2322 pSMB->ByteCount = cpu_to_le16(count);
2323
2324 iov[0].iov_base = (char *)pSMB;
2325 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2326 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2327 iov[1].iov_base = (char *)buf;
2328 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2329
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002330 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002331 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2332 &rsp_iov);
2333 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002334 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002335 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002336
2337 return rc;
2338}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002341CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002342 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002344 const __u32 numLock, const __u8 lockType,
2345 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int rc = 0;
2348 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002349/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002351 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 __u16 count;
2353
Joe Perchesf96637b2013-05-04 22:12:25 -05002354 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2355 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002356 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (rc)
2359 return rc;
2360
Steve French790fe572007-07-07 19:25:05 +00002361 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002362 /* no response expected */
2363 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002365 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002366 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2368 } else {
2369 pSMB->Timeout = 0;
2370 }
2371
2372 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2373 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2374 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002375 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 pSMB->AndXCommand = 0xFF; /* none */
2377 pSMB->Fid = smb_file_id; /* netfid stays le */
2378
Steve French790fe572007-07-07 19:25:05 +00002379 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002380 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* BB where to store pid high? */
2382 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2383 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2384 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2385 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2386 count = sizeof(LOCKING_ANDX_RANGE);
2387 } else {
2388 /* oplock break */
2389 count = 0;
2390 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002391 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 pSMB->ByteCount = cpu_to_le16(count);
2393
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002394 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002395 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002396 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002397 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002398 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002399 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002400 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002401 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002402 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Steve French50c2f752007-07-13 00:33:32 +00002404 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 since file handle passed in no longer valid */
2406 return rc;
2407}
2408
2409int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002410CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002411 const __u16 smb_file_id, const __u32 netpid,
2412 const loff_t start_offset, const __u64 len,
2413 struct file_lock *pLockData, const __u16 lock_type,
2414 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002415{
2416 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2417 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002418 struct cifs_posix_lock *parm_data;
2419 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002420 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002421 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002422 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002423 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002424 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002425 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002426
Joe Perchesf96637b2013-05-04 22:12:25 -05002427 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002428
Steve French08547b02006-02-28 22:39:25 +00002429 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2430
2431 if (rc)
2432 return rc;
2433
2434 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2435
Steve French50c2f752007-07-13 00:33:32 +00002436 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002437 pSMB->MaxSetupCount = 0;
2438 pSMB->Reserved = 0;
2439 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002440 pSMB->Reserved2 = 0;
2441 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2442 offset = param_offset + params;
2443
Steve French08547b02006-02-28 22:39:25 +00002444 count = sizeof(struct cifs_posix_lock);
2445 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002446 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002447 pSMB->SetupCount = 1;
2448 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002449 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002450 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2451 else
2452 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2453 byte_count = 3 /* pad */ + params + count;
2454 pSMB->DataCount = cpu_to_le16(count);
2455 pSMB->ParameterCount = cpu_to_le16(params);
2456 pSMB->TotalDataCount = pSMB->DataCount;
2457 pSMB->TotalParameterCount = pSMB->ParameterCount;
2458 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002459 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002460 (((char *) &pSMB->hdr.Protocol) + offset);
2461
2462 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002463 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002464 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002465 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002466 pSMB->Timeout = cpu_to_le32(-1);
2467 } else
2468 pSMB->Timeout = 0;
2469
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002470 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002471 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002472 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002473
2474 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002475 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002476 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2477 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002478 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002479 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002480 if (waitFlag) {
2481 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2482 (struct smb_hdr *) pSMBr, &bytes_returned);
2483 } else {
Steve French133672e2007-11-13 22:41:37 +00002484 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002485 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002486 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002487 &resp_buf_type, timeout, &rsp_iov);
2488 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002489 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002490 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002491
Steve French08547b02006-02-28 22:39:25 +00002492 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002493 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002494 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002495 /* lock structure can be returned on get */
2496 __u16 data_offset;
2497 __u16 data_count;
2498 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002499
Jeff Layton820a8032011-05-04 08:05:26 -04002500 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002501 rc = -EIO; /* bad smb */
2502 goto plk_err_exit;
2503 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002504 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2505 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002506 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002507 rc = -EIO;
2508 goto plk_err_exit;
2509 }
2510 parm_data = (struct cifs_posix_lock *)
2511 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002512 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002513 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002514 else {
2515 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002516 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002517 pLockData->fl_type = F_RDLCK;
2518 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002519 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002520 pLockData->fl_type = F_WRLCK;
2521
Steve French5443d132011-03-13 05:08:25 +00002522 pLockData->fl_start = le64_to_cpu(parm_data->start);
2523 pLockData->fl_end = pLockData->fl_start +
2524 le64_to_cpu(parm_data->length) - 1;
Benjamin Coddington9d5b86a2017-07-16 10:28:22 -04002525 pLockData->fl_pid = -le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002526 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002527 }
Steve French50c2f752007-07-13 00:33:32 +00002528
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002529plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002530 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002531
Steve French08547b02006-02-28 22:39:25 +00002532 /* Note: On -EAGAIN error only caller can retry on handle based calls
2533 since file handle passed in no longer valid */
2534
2535 return rc;
2536}
2537
2538
2539int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002540CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 int rc = 0;
2543 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002544 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546/* do not retry on dead session on close */
2547 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002548 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return 0;
2550 if (rc)
2551 return rc;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002554 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002556 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002557 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002558 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002560 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002562 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 }
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002567 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 rc = 0;
2569
2570 return rc;
2571}
2572
2573int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002574CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002575{
2576 int rc = 0;
2577 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002578 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002579
2580 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2581 if (rc)
2582 return rc;
2583
2584 pSMB->FileID = (__u16) smb_file_id;
2585 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002586 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002587 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002588 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002589 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002590 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002591
2592 return rc;
2593}
2594
2595int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002596CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002597 const char *from_name, const char *to_name,
2598 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 int rc = 0;
2601 RENAME_REQ *pSMB = NULL;
2602 RENAME_RSP *pSMBr = NULL;
2603 int bytes_returned;
2604 int name_len, name_len2;
2605 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002606 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Joe Perchesf96637b2013-05-04 22:12:25 -05002608 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609renameRetry:
2610 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2611 (void **) &pSMBr);
2612 if (rc)
2613 return rc;
2614
2615 pSMB->BufferFormat = 0x04;
2616 pSMB->SearchAttributes =
2617 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2618 ATTR_DIRECTORY);
2619
2620 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002621 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2622 from_name, PATH_MAX,
2623 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 name_len++; /* trailing null */
2625 name_len *= 2;
2626 pSMB->OldFileName[name_len] = 0x04; /* pad */
2627 /* protocol requires ASCII signature byte on Unicode string */
2628 pSMB->OldFileName[name_len + 1] = 0x00;
2629 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002630 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002631 to_name, PATH_MAX, cifs_sb->local_nls,
2632 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2634 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002635 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002636 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002638 strncpy(pSMB->OldFileName, from_name, name_len);
2639 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 name_len2++; /* trailing null */
2641 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002642 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 name_len2++; /* trailing null */
2644 name_len2++; /* signature byte */
2645 }
2646
2647 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002648 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 pSMB->ByteCount = cpu_to_le16(count);
2650
2651 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2652 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002653 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002654 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002655 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 cifs_buf_release(pSMB);
2658
2659 if (rc == -EAGAIN)
2660 goto renameRetry;
2661
2662 return rc;
2663}
2664
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002665int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002666 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002667 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2670 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002671 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 char *data_offset;
2673 char dummy_string[30];
2674 int rc = 0;
2675 int bytes_returned = 0;
2676 int len_of_str;
2677 __u16 params, param_offset, offset, count, byte_count;
2678
Joe Perchesf96637b2013-05-04 22:12:25 -05002679 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2681 (void **) &pSMBr);
2682 if (rc)
2683 return rc;
2684
2685 params = 6;
2686 pSMB->MaxSetupCount = 0;
2687 pSMB->Reserved = 0;
2688 pSMB->Flags = 0;
2689 pSMB->Timeout = 0;
2690 pSMB->Reserved2 = 0;
2691 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2692 offset = param_offset + params;
2693
2694 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2695 rename_info = (struct set_file_rename *) data_offset;
2696 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002697 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 pSMB->SetupCount = 1;
2699 pSMB->Reserved3 = 0;
2700 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2701 byte_count = 3 /* pad */ + params;
2702 pSMB->ParameterCount = cpu_to_le16(params);
2703 pSMB->TotalParameterCount = pSMB->ParameterCount;
2704 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2705 pSMB->DataOffset = cpu_to_le16(offset);
2706 /* construct random name ".cifs_tmp<inodenum><mid>" */
2707 rename_info->overwrite = cpu_to_le32(1);
2708 rename_info->root_fid = 0;
2709 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002710 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002711 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002712 len_of_str =
2713 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002714 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002716 len_of_str =
2717 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002718 target_name, PATH_MAX, nls_codepage,
2719 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002722 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 byte_count += count;
2724 pSMB->DataCount = cpu_to_le16(count);
2725 pSMB->TotalDataCount = pSMB->DataCount;
2726 pSMB->Fid = netfid;
2727 pSMB->InformationLevel =
2728 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2729 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002730 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 pSMB->ByteCount = cpu_to_le16(byte_count);
2732 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002733 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002734 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002735 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002736 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2737 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 cifs_buf_release(pSMB);
2740
2741 /* Note: On -EAGAIN error only caller can retry on handle based calls
2742 since file handle passed in no longer valid */
2743
2744 return rc;
2745}
2746
2747int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002748CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2749 const char *fromName, const __u16 target_tid, const char *toName,
2750 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 int rc = 0;
2753 COPY_REQ *pSMB = NULL;
2754 COPY_RSP *pSMBr = NULL;
2755 int bytes_returned;
2756 int name_len, name_len2;
2757 __u16 count;
2758
Joe Perchesf96637b2013-05-04 22:12:25 -05002759 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760copyRetry:
2761 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2762 (void **) &pSMBr);
2763 if (rc)
2764 return rc;
2765
2766 pSMB->BufferFormat = 0x04;
2767 pSMB->Tid2 = target_tid;
2768
2769 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2770
2771 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002772 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2773 fromName, PATH_MAX, nls_codepage,
2774 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 name_len++; /* trailing null */
2776 name_len *= 2;
2777 pSMB->OldFileName[name_len] = 0x04; /* pad */
2778 /* protocol requires ASCII signature byte on Unicode string */
2779 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002780 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002781 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2782 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2784 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002785 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 name_len = strnlen(fromName, PATH_MAX);
2787 name_len++; /* trailing null */
2788 strncpy(pSMB->OldFileName, fromName, name_len);
2789 name_len2 = strnlen(toName, PATH_MAX);
2790 name_len2++; /* trailing null */
2791 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2792 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2793 name_len2++; /* trailing null */
2794 name_len2++; /* signature byte */
2795 }
2796
2797 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002798 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 pSMB->ByteCount = cpu_to_le16(count);
2800
2801 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2802 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2803 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002804 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2805 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
Steve French0d817bc2008-05-22 02:02:03 +00002807 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 if (rc == -EAGAIN)
2810 goto copyRetry;
2811
2812 return rc;
2813}
2814
2815int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002816CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002818 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
2820 TRANSACTION2_SPI_REQ *pSMB = NULL;
2821 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2822 char *data_offset;
2823 int name_len;
2824 int name_len_target;
2825 int rc = 0;
2826 int bytes_returned = 0;
2827 __u16 params, param_offset, offset, byte_count;
2828
Joe Perchesf96637b2013-05-04 22:12:25 -05002829 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830createSymLinkRetry:
2831 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2832 (void **) &pSMBr);
2833 if (rc)
2834 return rc;
2835
2836 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2837 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002838 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2839 /* find define for this maxpathcomponent */
2840 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 name_len++; /* trailing null */
2842 name_len *= 2;
2843
Steve French50c2f752007-07-13 00:33:32 +00002844 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 name_len = strnlen(fromName, PATH_MAX);
2846 name_len++; /* trailing null */
2847 strncpy(pSMB->FileName, fromName, name_len);
2848 }
2849 params = 6 + name_len;
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_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002856 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 offset = param_offset + params;
2858
2859 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2860 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2861 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002862 cifsConvertToUTF16((__le16 *) data_offset, toName,
2863 /* find define for this maxpathcomponent */
2864 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 name_len_target++; /* trailing null */
2866 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002867 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 name_len_target = strnlen(toName, PATH_MAX);
2869 name_len_target++; /* trailing null */
2870 strncpy(data_offset, toName, name_len_target);
2871 }
2872
2873 pSMB->MaxParameterCount = cpu_to_le16(2);
2874 /* BB find exact max on data count below from sess */
2875 pSMB->MaxDataCount = cpu_to_le16(1000);
2876 pSMB->SetupCount = 1;
2877 pSMB->Reserved3 = 0;
2878 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2879 byte_count = 3 /* pad */ + params + name_len_target;
2880 pSMB->DataCount = cpu_to_le16(name_len_target);
2881 pSMB->ParameterCount = cpu_to_le16(params);
2882 pSMB->TotalDataCount = pSMB->DataCount;
2883 pSMB->TotalParameterCount = pSMB->ParameterCount;
2884 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2885 pSMB->DataOffset = cpu_to_le16(offset);
2886 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2887 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002888 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 pSMB->ByteCount = cpu_to_le16(byte_count);
2890 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2891 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002892 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002893 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002894 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2895 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Steve French0d817bc2008-05-22 02:02:03 +00002897 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 if (rc == -EAGAIN)
2900 goto createSymLinkRetry;
2901
2902 return rc;
2903}
2904
2905int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002906CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002908 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 TRANSACTION2_SPI_REQ *pSMB = NULL;
2911 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2912 char *data_offset;
2913 int name_len;
2914 int name_len_target;
2915 int rc = 0;
2916 int bytes_returned = 0;
2917 __u16 params, param_offset, offset, byte_count;
2918
Joe Perchesf96637b2013-05-04 22:12:25 -05002919 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920createHardLinkRetry:
2921 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2922 (void **) &pSMBr);
2923 if (rc)
2924 return rc;
2925
2926 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002927 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2928 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 name_len++; /* trailing null */
2930 name_len *= 2;
2931
Steve French50c2f752007-07-13 00:33:32 +00002932 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 name_len = strnlen(toName, PATH_MAX);
2934 name_len++; /* trailing null */
2935 strncpy(pSMB->FileName, toName, name_len);
2936 }
2937 params = 6 + name_len;
2938 pSMB->MaxSetupCount = 0;
2939 pSMB->Reserved = 0;
2940 pSMB->Flags = 0;
2941 pSMB->Timeout = 0;
2942 pSMB->Reserved2 = 0;
2943 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002944 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 offset = param_offset + params;
2946
2947 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2948 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2949 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002950 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2951 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 name_len_target++; /* trailing null */
2953 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002954 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 name_len_target = strnlen(fromName, PATH_MAX);
2956 name_len_target++; /* trailing null */
2957 strncpy(data_offset, fromName, name_len_target);
2958 }
2959
2960 pSMB->MaxParameterCount = cpu_to_le16(2);
2961 /* BB find exact max on data count below from sess*/
2962 pSMB->MaxDataCount = cpu_to_le16(1000);
2963 pSMB->SetupCount = 1;
2964 pSMB->Reserved3 = 0;
2965 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2966 byte_count = 3 /* pad */ + params + name_len_target;
2967 pSMB->ParameterCount = cpu_to_le16(params);
2968 pSMB->TotalParameterCount = pSMB->ParameterCount;
2969 pSMB->DataCount = cpu_to_le16(name_len_target);
2970 pSMB->TotalDataCount = pSMB->DataCount;
2971 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2972 pSMB->DataOffset = cpu_to_le16(offset);
2973 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2974 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002975 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 pSMB->ByteCount = cpu_to_le16(byte_count);
2977 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2978 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002979 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002980 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002981 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2982 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 cifs_buf_release(pSMB);
2985 if (rc == -EAGAIN)
2986 goto createHardLinkRetry;
2987
2988 return rc;
2989}
2990
2991int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002992CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002993 const char *from_name, const char *to_name,
2994 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 int rc = 0;
2997 NT_RENAME_REQ *pSMB = NULL;
2998 RENAME_RSP *pSMBr = NULL;
2999 int bytes_returned;
3000 int name_len, name_len2;
3001 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05003002 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Joe Perchesf96637b2013-05-04 22:12:25 -05003004 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005winCreateHardLinkRetry:
3006
3007 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3008 (void **) &pSMBr);
3009 if (rc)
3010 return rc;
3011
3012 pSMB->SearchAttributes =
3013 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3014 ATTR_DIRECTORY);
3015 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3016 pSMB->ClusterCount = 0;
3017
3018 pSMB->BufferFormat = 0x04;
3019
3020 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3021 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003022 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3023 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 name_len++; /* trailing null */
3025 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003026
3027 /* protocol specifies ASCII buffer format (0x04) for unicode */
3028 pSMB->OldFileName[name_len] = 0x04;
3029 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003031 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003032 to_name, PATH_MAX, cifs_sb->local_nls,
3033 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3035 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003036 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003037 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003039 strncpy(pSMB->OldFileName, from_name, name_len);
3040 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 name_len2++; /* trailing null */
3042 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003043 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 name_len2++; /* trailing null */
3045 name_len2++; /* signature byte */
3046 }
3047
3048 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003049 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 pSMB->ByteCount = cpu_to_le16(count);
3051
3052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003054 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003055 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003056 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 cifs_buf_release(pSMB);
3059 if (rc == -EAGAIN)
3060 goto winCreateHardLinkRetry;
3061
3062 return rc;
3063}
3064
3065int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003066CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003067 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003068 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070/* SMB_QUERY_FILE_UNIX_LINK */
3071 TRANSACTION2_QPI_REQ *pSMB = NULL;
3072 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3073 int rc = 0;
3074 int bytes_returned;
3075 int name_len;
3076 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003077 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Joe Perchesf96637b2013-05-04 22:12:25 -05003079 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081querySymLinkRetry:
3082 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3083 (void **) &pSMBr);
3084 if (rc)
3085 return rc;
3086
3087 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3088 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003089 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3090 searchName, PATH_MAX, nls_codepage,
3091 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 name_len++; /* trailing null */
3093 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003094 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 name_len = strnlen(searchName, PATH_MAX);
3096 name_len++; /* trailing null */
3097 strncpy(pSMB->FileName, searchName, name_len);
3098 }
3099
3100 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3101 pSMB->TotalDataCount = 0;
3102 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003103 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 pSMB->MaxSetupCount = 0;
3105 pSMB->Reserved = 0;
3106 pSMB->Flags = 0;
3107 pSMB->Timeout = 0;
3108 pSMB->Reserved2 = 0;
3109 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003110 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 pSMB->DataCount = 0;
3112 pSMB->DataOffset = 0;
3113 pSMB->SetupCount = 1;
3114 pSMB->Reserved3 = 0;
3115 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3116 byte_count = params + 1 /* pad */ ;
3117 pSMB->TotalParameterCount = cpu_to_le16(params);
3118 pSMB->ParameterCount = pSMB->TotalParameterCount;
3119 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3120 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003121 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 pSMB->ByteCount = cpu_to_le16(byte_count);
3123
3124 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3125 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3126 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003127 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 } else {
3129 /* decode response */
3130
3131 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003133 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003134 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003136 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003137 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Jeff Layton460b9692009-04-30 07:17:56 -04003139 data_start = ((char *) &pSMBr->hdr.Protocol) +
3140 le16_to_cpu(pSMBr->t2.DataOffset);
3141
Steve French0e0d2cf2009-05-01 05:27:32 +00003142 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3143 is_unicode = true;
3144 else
3145 is_unicode = false;
3146
Steve French737b7582005-04-28 22:41:06 -07003147 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003148 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3149 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003150 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003151 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153 }
3154 cifs_buf_release(pSMB);
3155 if (rc == -EAGAIN)
3156 goto querySymLinkRetry;
3157 return rc;
3158}
3159
Steve Frenchc52a9552011-02-24 06:16:22 +00003160/*
3161 * Recent Windows versions now create symlinks more frequently
3162 * and they use the "reparse point" mechanism below. We can of course
3163 * do symlinks nicely to Samba and other servers which support the
3164 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3165 * "MF" symlinks optionally, but for recent Windows we really need to
3166 * reenable the code below and fix the cifs_symlink callers to handle this.
3167 * In the interim this code has been moved to its own config option so
3168 * it is not compiled in by default until callers fixed up and more tested.
3169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003171CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3172 __u16 fid, char **symlinkinfo,
3173 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
3175 int rc = 0;
3176 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003177 struct smb_com_transaction_ioctl_req *pSMB;
3178 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003179 bool is_unicode;
3180 unsigned int sub_len;
3181 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003182 struct reparse_symlink_data *reparse_buf;
3183 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003184 __u32 data_offset, data_count;
3185 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003187 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3189 (void **) &pSMBr);
3190 if (rc)
3191 return rc;
3192
3193 pSMB->TotalParameterCount = 0 ;
3194 pSMB->TotalDataCount = 0;
3195 pSMB->MaxParameterCount = cpu_to_le32(2);
3196 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003197 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 pSMB->MaxSetupCount = 4;
3199 pSMB->Reserved = 0;
3200 pSMB->ParameterOffset = 0;
3201 pSMB->DataCount = 0;
3202 pSMB->DataOffset = 0;
3203 pSMB->SetupCount = 4;
3204 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3205 pSMB->ParameterCount = pSMB->TotalParameterCount;
3206 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3207 pSMB->IsFsctl = 1; /* FSCTL */
3208 pSMB->IsRootFlag = 0;
3209 pSMB->Fid = fid; /* file handle always le */
3210 pSMB->ByteCount = 0;
3211
3212 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3213 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3214 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003215 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003216 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
Steve French989c7e52009-05-02 05:32:20 +00003218
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003219 data_offset = le32_to_cpu(pSMBr->DataOffset);
3220 data_count = le32_to_cpu(pSMBr->DataCount);
3221 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3222 /* BB also check enough total bytes returned */
3223 rc = -EIO; /* bad smb */
3224 goto qreparse_out;
3225 }
3226 if (!data_count || (data_count > 2048)) {
3227 rc = -EIO;
3228 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3229 goto qreparse_out;
3230 }
3231 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003232 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003233 ((char *)&pSMBr->hdr.Protocol + data_offset);
3234 if ((char *)reparse_buf >= end_of_smb) {
3235 rc = -EIO;
3236 goto qreparse_out;
3237 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003238 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3239 cifs_dbg(FYI, "NFS style reparse tag\n");
3240 posix_buf = (struct reparse_posix_data *)reparse_buf;
3241
3242 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3243 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3244 le64_to_cpu(posix_buf->InodeType));
3245 rc = -EOPNOTSUPP;
3246 goto qreparse_out;
3247 }
3248 is_unicode = true;
3249 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3250 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3251 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3252 rc = -EIO;
3253 goto qreparse_out;
3254 }
3255 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3256 sub_len, is_unicode, nls_codepage);
3257 goto qreparse_out;
3258 } else if (reparse_buf->ReparseTag !=
3259 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3260 rc = -EOPNOTSUPP;
3261 goto qreparse_out;
3262 }
3263
3264 /* Reparse tag is NTFS symlink */
3265 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3266 reparse_buf->PathBuffer;
3267 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3268 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003269 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3270 rc = -EIO;
3271 goto qreparse_out;
3272 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003273 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3274 is_unicode = true;
3275 else
3276 is_unicode = false;
3277
3278 /* BB FIXME investigate remapping reserved chars here */
3279 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3280 nls_codepage);
3281 if (!*symlinkinfo)
3282 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003284 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003286 /*
3287 * Note: On -EAGAIN error only caller can retry on handle based calls
3288 * since file handle passed in no longer valid.
3289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return rc;
3291}
3292
Steve Frenchc7f508a2013-10-14 15:27:32 -05003293int
3294CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3295 __u16 fid)
3296{
3297 int rc = 0;
3298 int bytes_returned;
3299 struct smb_com_transaction_compr_ioctl_req *pSMB;
3300 struct smb_com_transaction_ioctl_rsp *pSMBr;
3301
3302 cifs_dbg(FYI, "Set compression for %u\n", fid);
3303 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3304 (void **) &pSMBr);
3305 if (rc)
3306 return rc;
3307
3308 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3309
3310 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003311 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003312 pSMB->MaxParameterCount = 0;
3313 pSMB->MaxDataCount = 0;
3314 pSMB->MaxSetupCount = 4;
3315 pSMB->Reserved = 0;
3316 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003317 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003318 pSMB->DataOffset =
3319 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3320 compression_state) - 4); /* 84 */
3321 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003322 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003323 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003324 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003325 pSMB->IsFsctl = 1; /* FSCTL */
3326 pSMB->IsRootFlag = 0;
3327 pSMB->Fid = fid; /* file handle always le */
3328 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003329 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003330 inc_rfc1001_len(pSMB, 5);
3331
3332 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3333 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3334 if (rc)
3335 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3336
3337 cifs_buf_release(pSMB);
3338
3339 /*
3340 * Note: On -EAGAIN error only caller can retry on handle based calls
3341 * since file handle passed in no longer valid.
3342 */
3343 return rc;
3344}
3345
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347#ifdef CONFIG_CIFS_POSIX
3348
3349/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003350static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003351 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
3353 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003354 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3355 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3356 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003357/*
3358 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3359 ace->e_perm, ace->e_tag, ace->e_id);
3360*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 return;
3363}
3364
3365/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003366static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3367 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
3369 int size = 0;
3370 int i;
3371 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003372 struct cifs_posix_ace *pACE;
3373 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003374 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3377 return -EOPNOTSUPP;
3378
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003379 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 count = le16_to_cpu(cifs_acl->access_entry_count);
3381 pACE = &cifs_acl->ace_array[0];
3382 size = sizeof(struct cifs_posix_acl);
3383 size += sizeof(struct cifs_posix_ace) * count;
3384 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003385 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003386 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3387 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return -EINVAL;
3389 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003390 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 count = le16_to_cpu(cifs_acl->access_entry_count);
3392 size = sizeof(struct cifs_posix_acl);
3393 size += sizeof(struct cifs_posix_ace) * count;
3394/* skip past access ACEs to get to default ACEs */
3395 pACE = &cifs_acl->ace_array[count];
3396 count = le16_to_cpu(cifs_acl->default_entry_count);
3397 size += sizeof(struct cifs_posix_ace) * count;
3398 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003399 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return -EINVAL;
3401 } else {
3402 /* illegal type */
3403 return -EINVAL;
3404 }
3405
3406 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003407 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003408 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003409 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return -ERANGE;
3411 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003412 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3413
Steve Frenchff7feac2005-11-15 16:45:16 -08003414 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003415 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003416 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003417 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419 }
3420 return size;
3421}
3422
Steve French50c2f752007-07-13 00:33:32 +00003423static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003424 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
3426 __u16 rc = 0; /* 0 = ACL converted ok */
3427
Steve Frenchff7feac2005-11-15 16:45:16 -08003428 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3429 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003431 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 /* Probably no need to le convert -1 on any arch but can not hurt */
3433 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003434 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003435 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003436/*
3437 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3438 ace->e_perm, ace->e_tag, ace->e_id);
3439*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return rc;
3441}
3442
3443/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003444static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3445 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
3447 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003448 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003449 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003450 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 int count;
3452 int i;
3453
Steve French790fe572007-07-07 19:25:05 +00003454 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 return 0;
3456
3457 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003458 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3459 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003460 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003461 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3462 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return 0;
3464 }
3465 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003466 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003467 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003468 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003469 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003470 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003471 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003472 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003473 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return 0;
3475 }
Steve French50c2f752007-07-13 00:33:32 +00003476 for (i = 0; i < count; i++) {
Eryu Guanae9ebe72016-10-24 20:46:40 +08003477 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003478 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 /* ACE not converted */
3480 break;
3481 }
3482 }
Steve French790fe572007-07-07 19:25:05 +00003483 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3485 rc += sizeof(struct cifs_posix_acl);
3486 /* BB add check to make sure ACL does not overflow SMB */
3487 }
3488 return rc;
3489}
3490
3491int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003492CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003493 const unsigned char *searchName,
3494 char *acl_inf, const int buflen, const int acl_type,
3495 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497/* SMB_QUERY_POSIX_ACL */
3498 TRANSACTION2_QPI_REQ *pSMB = NULL;
3499 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3500 int rc = 0;
3501 int bytes_returned;
3502 int name_len;
3503 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003504
Joe Perchesf96637b2013-05-04 22:12:25 -05003505 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
3507queryAclRetry:
3508 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3509 (void **) &pSMBr);
3510 if (rc)
3511 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3514 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003515 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3516 searchName, PATH_MAX, nls_codepage,
3517 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 name_len++; /* trailing null */
3519 name_len *= 2;
3520 pSMB->FileName[name_len] = 0;
3521 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003522 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 name_len = strnlen(searchName, PATH_MAX);
3524 name_len++; /* trailing null */
3525 strncpy(pSMB->FileName, searchName, name_len);
3526 }
3527
3528 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3529 pSMB->TotalDataCount = 0;
3530 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003531 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 pSMB->MaxDataCount = cpu_to_le16(4000);
3533 pSMB->MaxSetupCount = 0;
3534 pSMB->Reserved = 0;
3535 pSMB->Flags = 0;
3536 pSMB->Timeout = 0;
3537 pSMB->Reserved2 = 0;
3538 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003539 offsetof(struct smb_com_transaction2_qpi_req,
3540 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 pSMB->DataCount = 0;
3542 pSMB->DataOffset = 0;
3543 pSMB->SetupCount = 1;
3544 pSMB->Reserved3 = 0;
3545 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3546 byte_count = params + 1 /* pad */ ;
3547 pSMB->TotalParameterCount = cpu_to_le16(params);
3548 pSMB->ParameterCount = pSMB->TotalParameterCount;
3549 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3550 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003551 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 pSMB->ByteCount = cpu_to_le16(byte_count);
3553
3554 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3555 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003556 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003558 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 } else {
3560 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003564 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 rc = -EIO; /* bad smb */
3566 else {
3567 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3568 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3569 rc = cifs_copy_posix_acl(acl_inf,
3570 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003571 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
3573 }
3574 cifs_buf_release(pSMB);
3575 if (rc == -EAGAIN)
3576 goto queryAclRetry;
3577 return rc;
3578}
3579
3580int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003581CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003582 const unsigned char *fileName,
3583 const char *local_acl, const int buflen,
3584 const int acl_type,
3585 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 struct smb_com_transaction2_spi_req *pSMB = NULL;
3588 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3589 char *parm_data;
3590 int name_len;
3591 int rc = 0;
3592 int bytes_returned = 0;
3593 __u16 params, byte_count, data_count, param_offset, offset;
3594
Joe Perchesf96637b2013-05-04 22:12:25 -05003595 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596setAclRetry:
3597 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003598 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 if (rc)
3600 return rc;
3601 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3602 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003603 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3604 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 name_len++; /* trailing null */
3606 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003607 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 name_len = strnlen(fileName, PATH_MAX);
3609 name_len++; /* trailing null */
3610 strncpy(pSMB->FileName, fileName, name_len);
3611 }
3612 params = 6 + name_len;
3613 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003614 /* BB find max SMB size from sess */
3615 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 pSMB->MaxSetupCount = 0;
3617 pSMB->Reserved = 0;
3618 pSMB->Flags = 0;
3619 pSMB->Timeout = 0;
3620 pSMB->Reserved2 = 0;
3621 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003622 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 offset = param_offset + params;
3624 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3625 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3626
3627 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003628 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Steve French790fe572007-07-07 19:25:05 +00003630 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 rc = -EOPNOTSUPP;
3632 goto setACLerrorExit;
3633 }
3634 pSMB->DataOffset = cpu_to_le16(offset);
3635 pSMB->SetupCount = 1;
3636 pSMB->Reserved3 = 0;
3637 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3638 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3639 byte_count = 3 /* pad */ + params + data_count;
3640 pSMB->DataCount = cpu_to_le16(data_count);
3641 pSMB->TotalDataCount = pSMB->DataCount;
3642 pSMB->ParameterCount = cpu_to_le16(params);
3643 pSMB->TotalParameterCount = pSMB->ParameterCount;
3644 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003645 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 pSMB->ByteCount = cpu_to_le16(byte_count);
3647 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003648 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003649 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003650 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
3652setACLerrorExit:
3653 cifs_buf_release(pSMB);
3654 if (rc == -EAGAIN)
3655 goto setAclRetry;
3656 return rc;
3657}
3658
Steve Frenchf654bac2005-04-28 22:41:04 -07003659/* BB fix tabs in this function FIXME BB */
3660int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003661CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003662 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003663{
Steve French50c2f752007-07-13 00:33:32 +00003664 int rc = 0;
3665 struct smb_t2_qfi_req *pSMB = NULL;
3666 struct smb_t2_qfi_rsp *pSMBr = NULL;
3667 int bytes_returned;
3668 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003669
Joe Perchesf96637b2013-05-04 22:12:25 -05003670 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003671 if (tcon == NULL)
3672 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003673
3674GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003675 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3676 (void **) &pSMBr);
3677 if (rc)
3678 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003679
Steve Frenchad7a2922008-02-07 23:25:02 +00003680 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003681 pSMB->t2.TotalDataCount = 0;
3682 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3683 /* BB find exact max data count below from sess structure BB */
3684 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3685 pSMB->t2.MaxSetupCount = 0;
3686 pSMB->t2.Reserved = 0;
3687 pSMB->t2.Flags = 0;
3688 pSMB->t2.Timeout = 0;
3689 pSMB->t2.Reserved2 = 0;
3690 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3691 Fid) - 4);
3692 pSMB->t2.DataCount = 0;
3693 pSMB->t2.DataOffset = 0;
3694 pSMB->t2.SetupCount = 1;
3695 pSMB->t2.Reserved3 = 0;
3696 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3697 byte_count = params + 1 /* pad */ ;
3698 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3699 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3700 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3701 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003702 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003703 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003704 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003705
Steve French790fe572007-07-07 19:25:05 +00003706 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3707 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3708 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003709 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003710 } else {
3711 /* decode response */
3712 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003713 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003714 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003715 /* If rc should we check for EOPNOSUPP and
3716 disable the srvino flag? or in caller? */
3717 rc = -EIO; /* bad smb */
3718 else {
3719 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3720 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3721 struct file_chattr_info *pfinfo;
3722 /* BB Do we need a cast or hash here ? */
3723 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003724 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003725 rc = -EIO;
3726 goto GetExtAttrOut;
3727 }
3728 pfinfo = (struct file_chattr_info *)
3729 (data_offset + (char *) &pSMBr->hdr.Protocol);
3730 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003731 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003732 }
3733 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003734GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003735 cifs_buf_release(pSMB);
3736 if (rc == -EAGAIN)
3737 goto GetExtAttrRetry;
3738 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003739}
3740
Steve Frenchf654bac2005-04-28 22:41:04 -07003741#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Jeff Layton79df1ba2010-12-06 12:52:08 -05003743#ifdef CONFIG_CIFS_ACL
3744/*
3745 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3746 * all NT TRANSACTS that we init here have total parm and data under about 400
3747 * bytes (to fit in small cifs buffer size), which is the case so far, it
3748 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3749 * returned setup area) and MaxParameterCount (returned parms size) must be set
3750 * by caller
3751 */
3752static int
3753smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003754 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003755 void **ret_buf)
3756{
3757 int rc;
3758 __u32 temp_offset;
3759 struct smb_com_ntransact_req *pSMB;
3760
3761 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3762 (void **)&pSMB);
3763 if (rc)
3764 return rc;
3765 *ret_buf = (void *)pSMB;
3766 pSMB->Reserved = 0;
3767 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3768 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003769 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003770 pSMB->ParameterCount = pSMB->TotalParameterCount;
3771 pSMB->DataCount = pSMB->TotalDataCount;
3772 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3773 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3774 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3775 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3776 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3777 pSMB->SubCommand = cpu_to_le16(sub_command);
3778 return 0;
3779}
3780
3781static int
3782validate_ntransact(char *buf, char **ppparm, char **ppdata,
3783 __u32 *pparmlen, __u32 *pdatalen)
3784{
3785 char *end_of_smb;
3786 __u32 data_count, data_offset, parm_count, parm_offset;
3787 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003788 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003789
3790 *pdatalen = 0;
3791 *pparmlen = 0;
3792
3793 if (buf == NULL)
3794 return -EINVAL;
3795
3796 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3797
Jeff Layton820a8032011-05-04 08:05:26 -04003798 bcc = get_bcc(&pSMBr->hdr);
3799 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003800 (char *)&pSMBr->ByteCount;
3801
3802 data_offset = le32_to_cpu(pSMBr->DataOffset);
3803 data_count = le32_to_cpu(pSMBr->DataCount);
3804 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3805 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3806
3807 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3808 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3809
3810 /* should we also check that parm and data areas do not overlap? */
3811 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003812 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003813 return -EINVAL;
3814 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003815 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003816 return -EINVAL;
3817 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003818 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003819 return -EINVAL;
3820 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003821 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3822 *ppdata, data_count, (data_count + *ppdata),
3823 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003824 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003825 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003826 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003827 return -EINVAL;
3828 }
3829 *pdatalen = data_count;
3830 *pparmlen = parm_count;
3831 return 0;
3832}
3833
Steve French0a4b92c2006-01-12 15:44:21 -08003834/* Get Security Descriptor (by handle) from remote server for a file or dir */
3835int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003836CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003837 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003838{
3839 int rc = 0;
3840 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003841 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003842 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003843 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003844
Joe Perchesf96637b2013-05-04 22:12:25 -05003845 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003846
Steve French630f3f0c2007-10-25 21:17:17 +00003847 *pbuflen = 0;
3848 *acl_inf = NULL;
3849
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003850 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003851 8 /* parm len */, tcon, (void **) &pSMB);
3852 if (rc)
3853 return rc;
3854
3855 pSMB->MaxParameterCount = cpu_to_le32(4);
3856 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3857 pSMB->MaxSetupCount = 0;
3858 pSMB->Fid = fid; /* file handle always le */
3859 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3860 CIFS_ACL_DACL);
3861 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003862 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003863 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003864 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003865
Steve Frencha761ac52007-10-18 21:45:27 +00003866 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003867 0, &rsp_iov);
3868 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003869 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003870 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003871 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003872 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003873 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003874 __u32 parm_len;
3875 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003876 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003877 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003878
3879/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003880 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003881 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003882 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003883 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003884 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08003885
Joe Perchesf96637b2013-05-04 22:12:25 -05003886 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3887 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003888
3889 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3890 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003891 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003892 goto qsec_out;
3893 }
3894
3895/* BB check that data area is minimum length and as big as acl_len */
3896
Steve Frenchaf6f4612007-10-16 18:40:37 +00003897 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003898 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003899 cifs_dbg(VFS, "acl length %d does not match %d\n",
3900 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003901 if (*pbuflen > acl_len)
3902 *pbuflen = acl_len;
3903 }
Steve French0a4b92c2006-01-12 15:44:21 -08003904
Steve French630f3f0c2007-10-25 21:17:17 +00003905 /* check if buffer is big enough for the acl
3906 header followed by the smallest SID */
3907 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3908 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003909 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003910 rc = -EINVAL;
3911 *pbuflen = 0;
3912 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003913 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003914 if (*acl_inf == NULL) {
3915 *pbuflen = 0;
3916 rc = -ENOMEM;
3917 }
Steve French630f3f0c2007-10-25 21:17:17 +00003918 }
Steve French0a4b92c2006-01-12 15:44:21 -08003919 }
3920qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003921 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08003922 return rc;
3923}
Steve French97837582007-12-31 07:47:21 +00003924
3925int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003926CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003927 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003928{
3929 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3930 int rc = 0;
3931 int bytes_returned = 0;
3932 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003933 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003934
3935setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003936 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003937 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003938 return rc;
Steve French97837582007-12-31 07:47:21 +00003939
3940 pSMB->MaxSetupCount = 0;
3941 pSMB->Reserved = 0;
3942
3943 param_count = 8;
3944 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3945 data_count = acllen;
3946 data_offset = param_offset + param_count;
3947 byte_count = 3 /* pad */ + param_count;
3948
3949 pSMB->DataCount = cpu_to_le32(data_count);
3950 pSMB->TotalDataCount = pSMB->DataCount;
3951 pSMB->MaxParameterCount = cpu_to_le32(4);
3952 pSMB->MaxDataCount = cpu_to_le32(16384);
3953 pSMB->ParameterCount = cpu_to_le32(param_count);
3954 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3955 pSMB->TotalParameterCount = pSMB->ParameterCount;
3956 pSMB->DataOffset = cpu_to_le32(data_offset);
3957 pSMB->SetupCount = 0;
3958 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3959 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3960
3961 pSMB->Fid = fid; /* file handle always le */
3962 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003963 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003964
3965 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003966 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3967 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003968 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003969 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003970 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003971
3972 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3973 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3974
Joe Perchesf96637b2013-05-04 22:12:25 -05003975 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3976 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003977 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003978 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003979 cifs_buf_release(pSMB);
3980
3981 if (rc == -EAGAIN)
3982 goto setCifsAclRetry;
3983
3984 return (rc);
3985}
3986
Jeff Layton79df1ba2010-12-06 12:52:08 -05003987#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003988
Steve French6b8edfe2005-08-23 20:26:03 -07003989/* Legacy Query Path Information call for lookup to old servers such
3990 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003991int
3992SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3993 const char *search_name, FILE_ALL_INFO *data,
3994 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003995{
Steve Frenchad7a2922008-02-07 23:25:02 +00003996 QUERY_INFORMATION_REQ *pSMB;
3997 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003998 int rc = 0;
3999 int bytes_returned;
4000 int name_len;
4001
Joe Perchesf96637b2013-05-04 22:12:25 -05004002 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004003QInfRetry:
4004 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004005 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004006 if (rc)
4007 return rc;
4008
4009 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4010 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004011 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004012 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004013 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004014 name_len++; /* trailing null */
4015 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004016 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004017 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004018 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004019 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004020 }
4021 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004022 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004023 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004024 pSMB->ByteCount = cpu_to_le16(name_len);
4025
4026 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004027 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004028 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004029 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004030 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004031 struct timespec ts;
4032 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004033
4034 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004035 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004036 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004037 ts.tv_nsec = 0;
4038 ts.tv_sec = time;
4039 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004040 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4041 data->LastWriteTime = data->ChangeTime;
4042 data->LastAccessTime = 0;
4043 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004044 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004045 data->EndOfFile = data->AllocationSize;
4046 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004047 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004048 } else
4049 rc = -EIO; /* bad buffer passed in */
4050
4051 cifs_buf_release(pSMB);
4052
4053 if (rc == -EAGAIN)
4054 goto QInfRetry;
4055
4056 return rc;
4057}
4058
Jeff Laytonbcd53572010-02-12 07:44:16 -05004059int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004060CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004061 u16 netfid, FILE_ALL_INFO *pFindData)
4062{
4063 struct smb_t2_qfi_req *pSMB = NULL;
4064 struct smb_t2_qfi_rsp *pSMBr = NULL;
4065 int rc = 0;
4066 int bytes_returned;
4067 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004068
Jeff Laytonbcd53572010-02-12 07:44:16 -05004069QFileInfoRetry:
4070 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4071 (void **) &pSMBr);
4072 if (rc)
4073 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004074
Jeff Laytonbcd53572010-02-12 07:44:16 -05004075 params = 2 /* level */ + 2 /* fid */;
4076 pSMB->t2.TotalDataCount = 0;
4077 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4078 /* BB find exact max data count below from sess structure BB */
4079 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4080 pSMB->t2.MaxSetupCount = 0;
4081 pSMB->t2.Reserved = 0;
4082 pSMB->t2.Flags = 0;
4083 pSMB->t2.Timeout = 0;
4084 pSMB->t2.Reserved2 = 0;
4085 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4086 Fid) - 4);
4087 pSMB->t2.DataCount = 0;
4088 pSMB->t2.DataOffset = 0;
4089 pSMB->t2.SetupCount = 1;
4090 pSMB->t2.Reserved3 = 0;
4091 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4092 byte_count = params + 1 /* pad */ ;
4093 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4094 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4095 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4096 pSMB->Pad = 0;
4097 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004098 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004099 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004100
4101 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4102 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4103 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004104 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004105 } else { /* decode response */
4106 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4107
4108 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4109 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004110 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004111 rc = -EIO; /* bad smb */
4112 else if (pFindData) {
4113 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4114 memcpy((char *) pFindData,
4115 (char *) &pSMBr->hdr.Protocol +
4116 data_offset, sizeof(FILE_ALL_INFO));
4117 } else
4118 rc = -ENOMEM;
4119 }
4120 cifs_buf_release(pSMB);
4121 if (rc == -EAGAIN)
4122 goto QFileInfoRetry;
4123
4124 return rc;
4125}
Steve French6b8edfe2005-08-23 20:26:03 -07004126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004128CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004129 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004130 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004131 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004133 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 TRANSACTION2_QPI_REQ *pSMB = NULL;
4135 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4136 int rc = 0;
4137 int bytes_returned;
4138 int name_len;
4139 __u16 params, byte_count;
4140
Joe Perchesf96637b2013-05-04 22:12:25 -05004141 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142QPathInfoRetry:
4143 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4144 (void **) &pSMBr);
4145 if (rc)
4146 return rc;
4147
4148 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4149 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004150 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004151 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 name_len++; /* trailing null */
4153 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004154 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004155 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004157 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
4159
Steve French50c2f752007-07-13 00:33:32 +00004160 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 pSMB->TotalDataCount = 0;
4162 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004163 /* BB find exact max SMB PDU from sess structure BB */
4164 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 pSMB->MaxSetupCount = 0;
4166 pSMB->Reserved = 0;
4167 pSMB->Flags = 0;
4168 pSMB->Timeout = 0;
4169 pSMB->Reserved2 = 0;
4170 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004171 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 pSMB->DataCount = 0;
4173 pSMB->DataOffset = 0;
4174 pSMB->SetupCount = 1;
4175 pSMB->Reserved3 = 0;
4176 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4177 byte_count = params + 1 /* pad */ ;
4178 pSMB->TotalParameterCount = cpu_to_le16(params);
4179 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004180 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004181 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4182 else
4183 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004185 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 pSMB->ByteCount = cpu_to_le16(byte_count);
4187
4188 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4189 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4190 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004191 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 } else { /* decode response */
4193 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4194
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004195 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4196 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004197 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004199 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004200 rc = -EIO; /* 24 or 26 expected but we do not read
4201 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004202 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004203 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004205
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004206 /*
4207 * On legacy responses we do not read the last field,
4208 * EAsize, fortunately since it varies by subdialect and
4209 * also note it differs on Set vs Get, ie two bytes or 4
4210 * bytes depending but we don't care here.
4211 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004212 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004213 size = sizeof(FILE_INFO_STANDARD);
4214 else
4215 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004216 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004217 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 } else
4219 rc = -ENOMEM;
4220 }
4221 cifs_buf_release(pSMB);
4222 if (rc == -EAGAIN)
4223 goto QPathInfoRetry;
4224
4225 return rc;
4226}
4227
4228int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004229CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004230 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4231{
4232 struct smb_t2_qfi_req *pSMB = NULL;
4233 struct smb_t2_qfi_rsp *pSMBr = NULL;
4234 int rc = 0;
4235 int bytes_returned;
4236 __u16 params, byte_count;
4237
4238UnixQFileInfoRetry:
4239 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4240 (void **) &pSMBr);
4241 if (rc)
4242 return rc;
4243
4244 params = 2 /* level */ + 2 /* fid */;
4245 pSMB->t2.TotalDataCount = 0;
4246 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4247 /* BB find exact max data count below from sess structure BB */
4248 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4249 pSMB->t2.MaxSetupCount = 0;
4250 pSMB->t2.Reserved = 0;
4251 pSMB->t2.Flags = 0;
4252 pSMB->t2.Timeout = 0;
4253 pSMB->t2.Reserved2 = 0;
4254 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4255 Fid) - 4);
4256 pSMB->t2.DataCount = 0;
4257 pSMB->t2.DataOffset = 0;
4258 pSMB->t2.SetupCount = 1;
4259 pSMB->t2.Reserved3 = 0;
4260 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4261 byte_count = params + 1 /* pad */ ;
4262 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4263 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4264 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4265 pSMB->Pad = 0;
4266 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004267 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004268 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004269
4270 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4271 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4272 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004273 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004274 } else { /* decode response */
4275 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4276
Jeff Layton820a8032011-05-04 08:05:26 -04004277 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004278 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 -05004279 rc = -EIO; /* bad smb */
4280 } else {
4281 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4282 memcpy((char *) pFindData,
4283 (char *) &pSMBr->hdr.Protocol +
4284 data_offset,
4285 sizeof(FILE_UNIX_BASIC_INFO));
4286 }
4287 }
4288
4289 cifs_buf_release(pSMB);
4290 if (rc == -EAGAIN)
4291 goto UnixQFileInfoRetry;
4292
4293 return rc;
4294}
4295
4296int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004297CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004299 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004300 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
4302/* SMB_QUERY_FILE_UNIX_BASIC */
4303 TRANSACTION2_QPI_REQ *pSMB = NULL;
4304 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4305 int rc = 0;
4306 int bytes_returned = 0;
4307 int name_len;
4308 __u16 params, byte_count;
4309
Joe Perchesf96637b2013-05-04 22:12:25 -05004310 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311UnixQPathInfoRetry:
4312 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4313 (void **) &pSMBr);
4314 if (rc)
4315 return rc;
4316
4317 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4318 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004319 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4320 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 name_len++; /* trailing null */
4322 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004323 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 name_len = strnlen(searchName, PATH_MAX);
4325 name_len++; /* trailing null */
4326 strncpy(pSMB->FileName, searchName, name_len);
4327 }
4328
Steve French50c2f752007-07-13 00:33:32 +00004329 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 pSMB->TotalDataCount = 0;
4331 pSMB->MaxParameterCount = cpu_to_le16(2);
4332 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004333 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 pSMB->MaxSetupCount = 0;
4335 pSMB->Reserved = 0;
4336 pSMB->Flags = 0;
4337 pSMB->Timeout = 0;
4338 pSMB->Reserved2 = 0;
4339 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004340 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 pSMB->DataCount = 0;
4342 pSMB->DataOffset = 0;
4343 pSMB->SetupCount = 1;
4344 pSMB->Reserved3 = 0;
4345 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4346 byte_count = params + 1 /* pad */ ;
4347 pSMB->TotalParameterCount = cpu_to_le16(params);
4348 pSMB->ParameterCount = pSMB->TotalParameterCount;
4349 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4350 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004351 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 pSMB->ByteCount = cpu_to_le16(byte_count);
4353
4354 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4355 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4356 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004357 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 } else { /* decode response */
4359 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4360
Jeff Layton820a8032011-05-04 08:05:26 -04004361 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004362 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 -07004363 rc = -EIO; /* bad smb */
4364 } else {
4365 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4366 memcpy((char *) pFindData,
4367 (char *) &pSMBr->hdr.Protocol +
4368 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004369 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
4371 }
4372 cifs_buf_release(pSMB);
4373 if (rc == -EAGAIN)
4374 goto UnixQPathInfoRetry;
4375
4376 return rc;
4377}
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379/* xid, tcon, searchName and codepage are input parms, rest are returned */
4380int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004381CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004382 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004383 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004384 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
4386/* level 257 SMB_ */
4387 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4388 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004389 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 int rc = 0;
4391 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004392 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004394 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Joe Perchesf96637b2013-05-04 22:12:25 -05004396 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
4398findFirstRetry:
4399 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4400 (void **) &pSMBr);
4401 if (rc)
4402 return rc;
4403
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004404 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004405 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4408 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004409 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4410 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004411 /* We can not add the asterik earlier in case
4412 it got remapped to 0xF03A as if it were part of the
4413 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004415 if (msearch) {
4416 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4417 pSMB->FileName[name_len+1] = 0;
4418 pSMB->FileName[name_len+2] = '*';
4419 pSMB->FileName[name_len+3] = 0;
4420 name_len += 4; /* now the trailing null */
4421 /* null terminate just in case */
4422 pSMB->FileName[name_len] = 0;
4423 pSMB->FileName[name_len+1] = 0;
4424 name_len += 2;
4425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 } else { /* BB add check for overrun of SMB buf BB */
4427 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004429 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 free buffer exit; BB */
4431 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004432 if (msearch) {
4433 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4434 pSMB->FileName[name_len+1] = '*';
4435 pSMB->FileName[name_len+2] = 0;
4436 name_len += 3;
4437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 }
4439
4440 params = 12 + name_len /* includes null */ ;
4441 pSMB->TotalDataCount = 0; /* no EAs */
4442 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004443 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 pSMB->MaxSetupCount = 0;
4445 pSMB->Reserved = 0;
4446 pSMB->Flags = 0;
4447 pSMB->Timeout = 0;
4448 pSMB->Reserved2 = 0;
4449 byte_count = params + 1 /* pad */ ;
4450 pSMB->TotalParameterCount = cpu_to_le16(params);
4451 pSMB->ParameterCount = pSMB->TotalParameterCount;
4452 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004453 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4454 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 pSMB->DataCount = 0;
4456 pSMB->DataOffset = 0;
4457 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4458 pSMB->Reserved3 = 0;
4459 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4460 pSMB->SearchAttributes =
4461 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4462 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004463 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004464 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4466
4467 /* BB what should we set StorageType to? Does it matter? BB */
4468 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004469 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 pSMB->ByteCount = cpu_to_le16(byte_count);
4471
4472 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4473 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004474 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Steve French88274812006-03-09 22:21:45 +00004476 if (rc) {/* BB add logic to retry regular search if Unix search
4477 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004479 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004480
Steve French88274812006-03-09 22:21:45 +00004481 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 /* BB eventually could optimize out free and realloc of buf */
4484 /* for this case */
4485 if (rc == -EAGAIN)
4486 goto findFirstRetry;
4487 } else { /* decode response */
4488 /* BB remember to free buffer if error BB */
4489 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004490 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004491 unsigned int lnoff;
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004494 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 else
Steve French4b18f2a2008-04-29 00:06:05 +00004496 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004499 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004500 psrch_inf->srch_entries_start =
4501 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4504 le16_to_cpu(pSMBr->t2.ParameterOffset));
4505
Steve French790fe572007-07-07 19:25:05 +00004506 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004507 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 else
Steve French4b18f2a2008-04-29 00:06:05 +00004509 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Steve French50c2f752007-07-13 00:33:32 +00004511 psrch_inf->entries_in_buffer =
4512 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004513 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004515 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004516 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004517 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004518 psrch_inf->last_entry = NULL;
4519 return rc;
4520 }
4521
Steve French0752f152008-10-07 20:03:33 +00004522 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004523 lnoff;
4524
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004525 if (pnetfid)
4526 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 } else {
4528 cifs_buf_release(pSMB);
4529 }
4530 }
4531
4532 return rc;
4533}
4534
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004535int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4536 __u16 searchHandle, __u16 search_flags,
4537 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
4539 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4540 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004541 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 char *response_data;
4543 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004544 int bytes_returned;
4545 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 __u16 params, byte_count;
4547
Joe Perchesf96637b2013-05-04 22:12:25 -05004548 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Steve French4b18f2a2008-04-29 00:06:05 +00004550 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return -ENOENT;
4552
4553 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4554 (void **) &pSMBr);
4555 if (rc)
4556 return rc;
4557
Steve French50c2f752007-07-13 00:33:32 +00004558 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 byte_count = 0;
4560 pSMB->TotalDataCount = 0; /* no EAs */
4561 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004562 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 pSMB->MaxSetupCount = 0;
4564 pSMB->Reserved = 0;
4565 pSMB->Flags = 0;
4566 pSMB->Timeout = 0;
4567 pSMB->Reserved2 = 0;
4568 pSMB->ParameterOffset = cpu_to_le16(
4569 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4570 pSMB->DataCount = 0;
4571 pSMB->DataOffset = 0;
4572 pSMB->SetupCount = 1;
4573 pSMB->Reserved3 = 0;
4574 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4575 pSMB->SearchHandle = searchHandle; /* always kept as le */
4576 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004577 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4579 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004580 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 name_len = psrch_inf->resume_name_len;
4583 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004584 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4586 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004587 /* 14 byte parm len above enough for 2 byte null terminator */
4588 pSMB->ResumeFileName[name_len] = 0;
4589 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 } else {
4591 rc = -EINVAL;
4592 goto FNext2_err_exit;
4593 }
4594 byte_count = params + 1 /* pad */ ;
4595 pSMB->TotalParameterCount = cpu_to_le16(params);
4596 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004597 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4601 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004602 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 if (rc) {
4604 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004605 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004606 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004607 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004609 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 } else { /* decode response */
4611 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004612
Steve French790fe572007-07-07 19:25:05 +00004613 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004614 unsigned int lnoff;
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 /* BB fixme add lock for file (srch_info) struct here */
4617 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004618 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 else
Steve French4b18f2a2008-04-29 00:06:05 +00004620 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 response_data = (char *) &pSMBr->hdr.Protocol +
4622 le16_to_cpu(pSMBr->t2.ParameterOffset);
4623 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4624 response_data = (char *)&pSMBr->hdr.Protocol +
4625 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004626 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004627 cifs_small_buf_release(
4628 psrch_inf->ntwrk_buf_start);
4629 else
4630 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 psrch_inf->srch_entries_start = response_data;
4632 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004633 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004634 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004635 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 else
Steve French4b18f2a2008-04-29 00:06:05 +00004637 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004638 psrch_inf->entries_in_buffer =
4639 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 psrch_inf->index_of_last_entry +=
4641 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004642 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004643 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004644 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004645 psrch_inf->last_entry = NULL;
4646 return rc;
4647 } else
4648 psrch_inf->last_entry =
4649 psrch_inf->srch_entries_start + lnoff;
4650
Joe Perchesf96637b2013-05-04 22:12:25 -05004651/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4652 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 /* BB fixme add unlock here */
4655 }
4656
4657 }
4658
4659 /* BB On error, should we leave previous search buf (and count and
4660 last entry fields) intact or free the previous one? */
4661
4662 /* Note: On -EAGAIN error only caller can retry on handle based calls
4663 since file handle passed in no longer valid */
4664FNext2_err_exit:
4665 if (rc != 0)
4666 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 return rc;
4668}
4669
4670int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004671CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004672 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
4674 int rc = 0;
4675 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Joe Perchesf96637b2013-05-04 22:12:25 -05004677 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4679
4680 /* no sense returning error if session restarted
4681 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004682 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return 0;
4684 if (rc)
4685 return rc;
4686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 pSMB->FileID = searchHandle;
4688 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004689 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004690 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004691 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004692 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004693
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004694 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 /* Since session is dead, search handle closed on server already */
4697 if (rc == -EAGAIN)
4698 rc = 0;
4699
4700 return rc;
4701}
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004704CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004705 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004706 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
4708 int rc = 0;
4709 TRANSACTION2_QPI_REQ *pSMB = NULL;
4710 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4711 int name_len, bytes_returned;
4712 __u16 params, byte_count;
4713
Joe Perchesf96637b2013-05-04 22:12:25 -05004714 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004715 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004716 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
4718GetInodeNumberRetry:
4719 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004720 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (rc)
4722 return rc;
4723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4725 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004726 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004727 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004728 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 name_len++; /* trailing null */
4730 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004731 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004732 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004734 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736
4737 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4738 pSMB->TotalDataCount = 0;
4739 pSMB->MaxParameterCount = cpu_to_le16(2);
4740 /* BB find exact max data count below from sess structure BB */
4741 pSMB->MaxDataCount = cpu_to_le16(4000);
4742 pSMB->MaxSetupCount = 0;
4743 pSMB->Reserved = 0;
4744 pSMB->Flags = 0;
4745 pSMB->Timeout = 0;
4746 pSMB->Reserved2 = 0;
4747 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004748 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 pSMB->DataCount = 0;
4750 pSMB->DataOffset = 0;
4751 pSMB->SetupCount = 1;
4752 pSMB->Reserved3 = 0;
4753 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4754 byte_count = params + 1 /* pad */ ;
4755 pSMB->TotalParameterCount = cpu_to_le16(params);
4756 pSMB->ParameterCount = pSMB->TotalParameterCount;
4757 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4758 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004759 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 pSMB->ByteCount = cpu_to_le16(byte_count);
4761
4762 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4763 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4764 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004765 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 } else {
4767 /* decode response */
4768 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004770 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /* If rc should we check for EOPNOSUPP and
4772 disable the srvino flag? or in caller? */
4773 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004774 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4776 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004777 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004779 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004780 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 rc = -EIO;
4782 goto GetInodeNumOut;
4783 }
4784 pfinfo = (struct file_internal_info *)
4785 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004786 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
4788 }
4789GetInodeNumOut:
4790 cifs_buf_release(pSMB);
4791 if (rc == -EAGAIN)
4792 goto GetInodeNumberRetry;
4793 return rc;
4794}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004797CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004798 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004799 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004800 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
4802/* TRANS2_GET_DFS_REFERRAL */
4803 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4804 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 int rc = 0;
4806 int bytes_returned;
4807 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004809 *num_of_nodes = 0;
4810 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Joe Perchesf96637b2013-05-04 22:12:25 -05004812 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (ses == NULL)
4814 return -ENODEV;
4815getDFSRetry:
4816 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4817 (void **) &pSMBr);
4818 if (rc)
4819 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004820
4821 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004822 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004823 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 pSMB->hdr.Tid = ses->ipc_tid;
4825 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004826 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004828 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 if (ses->capabilities & CAP_UNICODE) {
4832 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4833 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004834 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004835 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004836 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 name_len++; /* trailing null */
4838 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004839 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004840 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004842 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
4844
Dan Carpenter65c3b202015-04-30 17:30:24 +03004845 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004846 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004847
Steve French50c2f752007-07-13 00:33:32 +00004848 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 params = 2 /* level */ + name_len /*includes null */ ;
4851 pSMB->TotalDataCount = 0;
4852 pSMB->DataCount = 0;
4853 pSMB->DataOffset = 0;
4854 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004855 /* BB find exact max SMB PDU from sess structure BB */
4856 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 pSMB->MaxSetupCount = 0;
4858 pSMB->Reserved = 0;
4859 pSMB->Flags = 0;
4860 pSMB->Timeout = 0;
4861 pSMB->Reserved2 = 0;
4862 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004863 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 pSMB->SetupCount = 1;
4865 pSMB->Reserved3 = 0;
4866 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4867 byte_count = params + 3 /* pad */ ;
4868 pSMB->ParameterCount = cpu_to_le16(params);
4869 pSMB->TotalParameterCount = pSMB->ParameterCount;
4870 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004871 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 pSMB->ByteCount = cpu_to_le16(byte_count);
4873
4874 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4875 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4876 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004877 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004878 goto GetDFSRefExit;
4879 }
4880 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004882 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004883 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004884 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004885 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004887
Joe Perchesf96637b2013-05-04 22:12:25 -05004888 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4889 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004890
4891 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01004892 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4893 le16_to_cpu(pSMBr->t2.DataCount),
4894 num_of_nodes, target_nodes, nls_codepage,
4895 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06004896 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04004897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004899 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 if (rc == -EAGAIN)
4902 goto getDFSRetry;
4903
4904 return rc;
4905}
4906
Steve French20962432005-09-21 22:05:57 -07004907/* Query File System Info such as free space to old servers such as Win 9x */
4908int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004909SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4910 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004911{
4912/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4913 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4914 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4915 FILE_SYSTEM_ALLOC_INFO *response_data;
4916 int rc = 0;
4917 int bytes_returned = 0;
4918 __u16 params, byte_count;
4919
Joe Perchesf96637b2013-05-04 22:12:25 -05004920 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004921oldQFSInfoRetry:
4922 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4923 (void **) &pSMBr);
4924 if (rc)
4925 return rc;
Steve French20962432005-09-21 22:05:57 -07004926
4927 params = 2; /* level */
4928 pSMB->TotalDataCount = 0;
4929 pSMB->MaxParameterCount = cpu_to_le16(2);
4930 pSMB->MaxDataCount = cpu_to_le16(1000);
4931 pSMB->MaxSetupCount = 0;
4932 pSMB->Reserved = 0;
4933 pSMB->Flags = 0;
4934 pSMB->Timeout = 0;
4935 pSMB->Reserved2 = 0;
4936 byte_count = params + 1 /* pad */ ;
4937 pSMB->TotalParameterCount = cpu_to_le16(params);
4938 pSMB->ParameterCount = pSMB->TotalParameterCount;
4939 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4940 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4941 pSMB->DataCount = 0;
4942 pSMB->DataOffset = 0;
4943 pSMB->SetupCount = 1;
4944 pSMB->Reserved3 = 0;
4945 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4946 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004947 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004948 pSMB->ByteCount = cpu_to_le16(byte_count);
4949
4950 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4951 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4952 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004953 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004954 } else { /* decode response */
4955 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4956
Jeff Layton820a8032011-05-04 08:05:26 -04004957 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004958 rc = -EIO; /* bad smb */
4959 else {
4960 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004961 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004962 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004963
Steve French50c2f752007-07-13 00:33:32 +00004964 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004965 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4966 FSData->f_bsize =
4967 le16_to_cpu(response_data->BytesPerSector) *
4968 le32_to_cpu(response_data->
4969 SectorsPerAllocationUnit);
4970 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004971 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004972 FSData->f_bfree = FSData->f_bavail =
4973 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05004974 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4975 (unsigned long long)FSData->f_blocks,
4976 (unsigned long long)FSData->f_bfree,
4977 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004978 }
4979 }
4980 cifs_buf_release(pSMB);
4981
4982 if (rc == -EAGAIN)
4983 goto oldQFSInfoRetry;
4984
4985 return rc;
4986}
4987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004989CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4990 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
4992/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4993 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4994 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4995 FILE_SYSTEM_INFO *response_data;
4996 int rc = 0;
4997 int bytes_returned = 0;
4998 __u16 params, byte_count;
4999
Joe Perchesf96637b2013-05-04 22:12:25 -05005000 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001QFSInfoRetry:
5002 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5003 (void **) &pSMBr);
5004 if (rc)
5005 return rc;
5006
5007 params = 2; /* level */
5008 pSMB->TotalDataCount = 0;
5009 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005010 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 pSMB->MaxSetupCount = 0;
5012 pSMB->Reserved = 0;
5013 pSMB->Flags = 0;
5014 pSMB->Timeout = 0;
5015 pSMB->Reserved2 = 0;
5016 byte_count = params + 1 /* pad */ ;
5017 pSMB->TotalParameterCount = cpu_to_le16(params);
5018 pSMB->ParameterCount = pSMB->TotalParameterCount;
5019 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005020 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 pSMB->DataCount = 0;
5022 pSMB->DataOffset = 0;
5023 pSMB->SetupCount = 1;
5024 pSMB->Reserved3 = 0;
5025 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5026 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005027 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 pSMB->ByteCount = cpu_to_le16(byte_count);
5029
5030 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5031 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5032 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005033 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005035 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Jeff Layton820a8032011-05-04 08:05:26 -04005037 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 rc = -EIO; /* bad smb */
5039 else {
5040 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042 response_data =
5043 (FILE_SYSTEM_INFO
5044 *) (((char *) &pSMBr->hdr.Protocol) +
5045 data_offset);
5046 FSData->f_bsize =
5047 le32_to_cpu(response_data->BytesPerSector) *
5048 le32_to_cpu(response_data->
5049 SectorsPerAllocationUnit);
5050 FSData->f_blocks =
5051 le64_to_cpu(response_data->TotalAllocationUnits);
5052 FSData->f_bfree = FSData->f_bavail =
5053 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005054 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5055 (unsigned long long)FSData->f_blocks,
5056 (unsigned long long)FSData->f_bfree,
5057 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
5059 }
5060 cifs_buf_release(pSMB);
5061
5062 if (rc == -EAGAIN)
5063 goto QFSInfoRetry;
5064
5065 return rc;
5066}
5067
5068int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005069CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070{
5071/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5072 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5073 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5074 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5075 int rc = 0;
5076 int bytes_returned = 0;
5077 __u16 params, byte_count;
5078
Joe Perchesf96637b2013-05-04 22:12:25 -05005079 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080QFSAttributeRetry:
5081 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5082 (void **) &pSMBr);
5083 if (rc)
5084 return rc;
5085
5086 params = 2; /* level */
5087 pSMB->TotalDataCount = 0;
5088 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005089 /* BB find exact max SMB PDU from sess structure BB */
5090 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 pSMB->MaxSetupCount = 0;
5092 pSMB->Reserved = 0;
5093 pSMB->Flags = 0;
5094 pSMB->Timeout = 0;
5095 pSMB->Reserved2 = 0;
5096 byte_count = params + 1 /* pad */ ;
5097 pSMB->TotalParameterCount = cpu_to_le16(params);
5098 pSMB->ParameterCount = pSMB->TotalParameterCount;
5099 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005100 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 pSMB->DataCount = 0;
5102 pSMB->DataOffset = 0;
5103 pSMB->SetupCount = 1;
5104 pSMB->Reserved3 = 0;
5105 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5106 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005107 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 pSMB->ByteCount = cpu_to_le16(byte_count);
5109
5110 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5111 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5112 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005113 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 } else { /* decode response */
5115 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5116
Jeff Layton820a8032011-05-04 08:05:26 -04005117 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005118 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 rc = -EIO; /* bad smb */
5120 } else {
5121 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5122 response_data =
5123 (FILE_SYSTEM_ATTRIBUTE_INFO
5124 *) (((char *) &pSMBr->hdr.Protocol) +
5125 data_offset);
5126 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005127 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 }
5129 }
5130 cifs_buf_release(pSMB);
5131
5132 if (rc == -EAGAIN)
5133 goto QFSAttributeRetry;
5134
5135 return rc;
5136}
5137
5138int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005139CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
5141/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5142 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5143 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5144 FILE_SYSTEM_DEVICE_INFO *response_data;
5145 int rc = 0;
5146 int bytes_returned = 0;
5147 __u16 params, byte_count;
5148
Joe Perchesf96637b2013-05-04 22:12:25 -05005149 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150QFSDeviceRetry:
5151 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5152 (void **) &pSMBr);
5153 if (rc)
5154 return rc;
5155
5156 params = 2; /* level */
5157 pSMB->TotalDataCount = 0;
5158 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005159 /* BB find exact max SMB PDU from sess structure BB */
5160 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 pSMB->MaxSetupCount = 0;
5162 pSMB->Reserved = 0;
5163 pSMB->Flags = 0;
5164 pSMB->Timeout = 0;
5165 pSMB->Reserved2 = 0;
5166 byte_count = params + 1 /* pad */ ;
5167 pSMB->TotalParameterCount = cpu_to_le16(params);
5168 pSMB->ParameterCount = pSMB->TotalParameterCount;
5169 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005170 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
5172 pSMB->DataCount = 0;
5173 pSMB->DataOffset = 0;
5174 pSMB->SetupCount = 1;
5175 pSMB->Reserved3 = 0;
5176 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5177 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005178 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 pSMB->ByteCount = cpu_to_le16(byte_count);
5180
5181 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5182 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5183 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005184 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 } else { /* decode response */
5186 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5187
Jeff Layton820a8032011-05-04 08:05:26 -04005188 if (rc || get_bcc(&pSMBr->hdr) <
5189 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 rc = -EIO; /* bad smb */
5191 else {
5192 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5193 response_data =
Steve French737b7582005-04-28 22:41:06 -07005194 (FILE_SYSTEM_DEVICE_INFO *)
5195 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 data_offset);
5197 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005198 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 }
5200 }
5201 cifs_buf_release(pSMB);
5202
5203 if (rc == -EAGAIN)
5204 goto QFSDeviceRetry;
5205
5206 return rc;
5207}
5208
5209int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005210CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211{
5212/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5213 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5214 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5215 FILE_SYSTEM_UNIX_INFO *response_data;
5216 int rc = 0;
5217 int bytes_returned = 0;
5218 __u16 params, byte_count;
5219
Joe Perchesf96637b2013-05-04 22:12:25 -05005220 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005222 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5223 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (rc)
5225 return rc;
5226
5227 params = 2; /* level */
5228 pSMB->TotalDataCount = 0;
5229 pSMB->DataCount = 0;
5230 pSMB->DataOffset = 0;
5231 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005232 /* BB find exact max SMB PDU from sess structure BB */
5233 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 pSMB->MaxSetupCount = 0;
5235 pSMB->Reserved = 0;
5236 pSMB->Flags = 0;
5237 pSMB->Timeout = 0;
5238 pSMB->Reserved2 = 0;
5239 byte_count = params + 1 /* pad */ ;
5240 pSMB->ParameterCount = cpu_to_le16(params);
5241 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005242 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5243 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 pSMB->SetupCount = 1;
5245 pSMB->Reserved3 = 0;
5246 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5247 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005248 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 pSMB->ByteCount = cpu_to_le16(byte_count);
5250
5251 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5252 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5253 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005254 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 } else { /* decode response */
5256 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5257
Jeff Layton820a8032011-05-04 08:05:26 -04005258 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 rc = -EIO; /* bad smb */
5260 } else {
5261 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5262 response_data =
5263 (FILE_SYSTEM_UNIX_INFO
5264 *) (((char *) &pSMBr->hdr.Protocol) +
5265 data_offset);
5266 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005267 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
5269 }
5270 cifs_buf_release(pSMB);
5271
5272 if (rc == -EAGAIN)
5273 goto QFSUnixRetry;
5274
5275
5276 return rc;
5277}
5278
Jeremy Allisonac670552005-06-22 17:26:35 -07005279int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005280CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005281{
5282/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5283 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5284 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5285 int rc = 0;
5286 int bytes_returned = 0;
5287 __u16 params, param_offset, offset, byte_count;
5288
Joe Perchesf96637b2013-05-04 22:12:25 -05005289 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005290SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005291 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005292 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5293 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005294 if (rc)
5295 return rc;
5296
5297 params = 4; /* 2 bytes zero followed by info level. */
5298 pSMB->MaxSetupCount = 0;
5299 pSMB->Reserved = 0;
5300 pSMB->Flags = 0;
5301 pSMB->Timeout = 0;
5302 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005303 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5304 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005305 offset = param_offset + params;
5306
5307 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005308 /* BB find exact max SMB PDU from sess structure BB */
5309 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005310 pSMB->SetupCount = 1;
5311 pSMB->Reserved3 = 0;
5312 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5313 byte_count = 1 /* pad */ + params + 12;
5314
5315 pSMB->DataCount = cpu_to_le16(12);
5316 pSMB->ParameterCount = cpu_to_le16(params);
5317 pSMB->TotalDataCount = pSMB->DataCount;
5318 pSMB->TotalParameterCount = pSMB->ParameterCount;
5319 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5320 pSMB->DataOffset = cpu_to_le16(offset);
5321
5322 /* Params. */
5323 pSMB->FileNum = 0;
5324 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5325
5326 /* Data. */
5327 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5328 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5329 pSMB->ClientUnixCap = cpu_to_le64(cap);
5330
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005331 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005332 pSMB->ByteCount = cpu_to_le16(byte_count);
5333
5334 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5335 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5336 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005337 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005338 } else { /* decode response */
5339 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005340 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005341 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005342 }
5343 cifs_buf_release(pSMB);
5344
5345 if (rc == -EAGAIN)
5346 goto SETFSUnixRetry;
5347
5348 return rc;
5349}
5350
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005354CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005355 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
5357/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5358 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5359 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5360 FILE_SYSTEM_POSIX_INFO *response_data;
5361 int rc = 0;
5362 int bytes_returned = 0;
5363 __u16 params, byte_count;
5364
Joe Perchesf96637b2013-05-04 22:12:25 -05005365 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366QFSPosixRetry:
5367 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5368 (void **) &pSMBr);
5369 if (rc)
5370 return rc;
5371
5372 params = 2; /* level */
5373 pSMB->TotalDataCount = 0;
5374 pSMB->DataCount = 0;
5375 pSMB->DataOffset = 0;
5376 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005377 /* BB find exact max SMB PDU from sess structure BB */
5378 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 pSMB->MaxSetupCount = 0;
5380 pSMB->Reserved = 0;
5381 pSMB->Flags = 0;
5382 pSMB->Timeout = 0;
5383 pSMB->Reserved2 = 0;
5384 byte_count = params + 1 /* pad */ ;
5385 pSMB->ParameterCount = cpu_to_le16(params);
5386 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005387 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5388 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 pSMB->SetupCount = 1;
5390 pSMB->Reserved3 = 0;
5391 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5392 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005393 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 pSMB->ByteCount = cpu_to_le16(byte_count);
5395
5396 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5397 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5398 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005399 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 } else { /* decode response */
5401 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5402
Jeff Layton820a8032011-05-04 08:05:26 -04005403 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 rc = -EIO; /* bad smb */
5405 } else {
5406 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5407 response_data =
5408 (FILE_SYSTEM_POSIX_INFO
5409 *) (((char *) &pSMBr->hdr.Protocol) +
5410 data_offset);
5411 FSData->f_bsize =
5412 le32_to_cpu(response_data->BlockSize);
5413 FSData->f_blocks =
5414 le64_to_cpu(response_data->TotalBlocks);
5415 FSData->f_bfree =
5416 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005417 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 FSData->f_bavail = FSData->f_bfree;
5419 } else {
5420 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005421 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 }
Steve French790fe572007-07-07 19:25:05 +00005423 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005425 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005426 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005428 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 }
5430 }
5431 cifs_buf_release(pSMB);
5432
5433 if (rc == -EAGAIN)
5434 goto QFSPosixRetry;
5435
5436 return rc;
5437}
5438
5439
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005440/*
5441 * We can not use write of zero bytes trick to set file size due to need for
5442 * large file support. Also note that this SetPathInfo is preferred to
5443 * SetFileInfo based method in next routine which is only needed to work around
5444 * a sharing violation bugin Samba which this routine can run into.
5445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005447CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005448 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5449 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
5451 struct smb_com_transaction2_spi_req *pSMB = NULL;
5452 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5453 struct file_end_of_file_info *parm_data;
5454 int name_len;
5455 int rc = 0;
5456 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005457 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 __u16 params, byte_count, data_count, param_offset, offset;
5460
Joe Perchesf96637b2013-05-04 22:12:25 -05005461 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462SetEOFRetry:
5463 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5464 (void **) &pSMBr);
5465 if (rc)
5466 return rc;
5467
5468 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5469 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005470 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5471 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 name_len++; /* trailing null */
5473 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005474 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005475 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005477 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005480 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005482 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 pSMB->MaxSetupCount = 0;
5484 pSMB->Reserved = 0;
5485 pSMB->Flags = 0;
5486 pSMB->Timeout = 0;
5487 pSMB->Reserved2 = 0;
5488 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005489 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005491 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005492 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5493 pSMB->InformationLevel =
5494 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5495 else
5496 pSMB->InformationLevel =
5497 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5498 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5500 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005501 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 else
5503 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005504 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 }
5506
5507 parm_data =
5508 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5509 offset);
5510 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5511 pSMB->DataOffset = cpu_to_le16(offset);
5512 pSMB->SetupCount = 1;
5513 pSMB->Reserved3 = 0;
5514 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5515 byte_count = 3 /* pad */ + params + data_count;
5516 pSMB->DataCount = cpu_to_le16(data_count);
5517 pSMB->TotalDataCount = pSMB->DataCount;
5518 pSMB->ParameterCount = cpu_to_le16(params);
5519 pSMB->TotalParameterCount = pSMB->ParameterCount;
5520 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005521 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 parm_data->FileSize = cpu_to_le64(size);
5523 pSMB->ByteCount = cpu_to_le16(byte_count);
5524 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5525 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005526 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005527 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
5529 cifs_buf_release(pSMB);
5530
5531 if (rc == -EAGAIN)
5532 goto SetEOFRetry;
5533
5534 return rc;
5535}
5536
5537int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005538CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5539 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
5541 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 struct file_end_of_file_info *parm_data;
5543 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 __u16 params, param_offset, offset, byte_count, count;
5545
Joe Perchesf96637b2013-05-04 22:12:25 -05005546 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5547 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005548 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if (rc)
5551 return rc;
5552
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005553 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5554 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 params = 6;
5557 pSMB->MaxSetupCount = 0;
5558 pSMB->Reserved = 0;
5559 pSMB->Flags = 0;
5560 pSMB->Timeout = 0;
5561 pSMB->Reserved2 = 0;
5562 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5563 offset = param_offset + params;
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 count = sizeof(struct file_end_of_file_info);
5566 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005567 /* BB find exact max SMB PDU from sess structure BB */
5568 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 pSMB->SetupCount = 1;
5570 pSMB->Reserved3 = 0;
5571 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5572 byte_count = 3 /* pad */ + params + count;
5573 pSMB->DataCount = cpu_to_le16(count);
5574 pSMB->ParameterCount = cpu_to_le16(params);
5575 pSMB->TotalDataCount = pSMB->DataCount;
5576 pSMB->TotalParameterCount = pSMB->ParameterCount;
5577 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5578 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005579 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5580 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 pSMB->DataOffset = cpu_to_le16(offset);
5582 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005583 pSMB->Fid = cfile->fid.netfid;
5584 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5586 pSMB->InformationLevel =
5587 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5588 else
5589 pSMB->InformationLevel =
5590 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005591 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5593 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005594 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 else
5596 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005597 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 }
5599 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005600 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005602 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005603 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005605 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5606 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
5608
Steve French50c2f752007-07-13 00:33:32 +00005609 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 since file handle passed in no longer valid */
5611
5612 return rc;
5613}
5614
Steve French50c2f752007-07-13 00:33:32 +00005615/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 an open handle, rather than by pathname - this is awkward due to
5617 potential access conflicts on the open, but it is unavoidable for these
5618 old servers since the only other choice is to go from 100 nanosecond DCE
5619 time and resort to the original setpathinfo level which takes the ancient
5620 DOS time format with 2 second granularity */
5621int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005622CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005623 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624{
5625 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 char *data_offset;
5627 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 __u16 params, param_offset, offset, byte_count, count;
5629
Joe Perchesf96637b2013-05-04 22:12:25 -05005630 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005631 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 if (rc)
5634 return rc;
5635
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005636 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5637 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 params = 6;
5640 pSMB->MaxSetupCount = 0;
5641 pSMB->Reserved = 0;
5642 pSMB->Flags = 0;
5643 pSMB->Timeout = 0;
5644 pSMB->Reserved2 = 0;
5645 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5646 offset = param_offset + params;
5647
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005648 data_offset = (char *)pSMB +
5649 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Steve French26f57362007-08-30 22:09:15 +00005651 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005653 /* BB find max SMB PDU from sess */
5654 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 pSMB->SetupCount = 1;
5656 pSMB->Reserved3 = 0;
5657 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5658 byte_count = 3 /* pad */ + params + count;
5659 pSMB->DataCount = cpu_to_le16(count);
5660 pSMB->ParameterCount = cpu_to_le16(params);
5661 pSMB->TotalDataCount = pSMB->DataCount;
5662 pSMB->TotalParameterCount = pSMB->ParameterCount;
5663 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5664 pSMB->DataOffset = cpu_to_le16(offset);
5665 pSMB->Fid = fid;
5666 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5667 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5668 else
5669 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5670 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005671 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005673 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005674 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005675 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005676 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005677 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5678 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
Steve French50c2f752007-07-13 00:33:32 +00005680 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 since file handle passed in no longer valid */
5682
5683 return rc;
5684}
5685
Jeff Layton6d22f092008-09-23 11:48:35 -04005686int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005687CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005688 bool delete_file, __u16 fid, __u32 pid_of_opener)
5689{
5690 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5691 char *data_offset;
5692 int rc = 0;
5693 __u16 params, param_offset, offset, byte_count, count;
5694
Joe Perchesf96637b2013-05-04 22:12:25 -05005695 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005696 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5697
5698 if (rc)
5699 return rc;
5700
5701 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5702 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5703
5704 params = 6;
5705 pSMB->MaxSetupCount = 0;
5706 pSMB->Reserved = 0;
5707 pSMB->Flags = 0;
5708 pSMB->Timeout = 0;
5709 pSMB->Reserved2 = 0;
5710 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5711 offset = param_offset + params;
5712
5713 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5714
5715 count = 1;
5716 pSMB->MaxParameterCount = cpu_to_le16(2);
5717 /* BB find max SMB PDU from sess */
5718 pSMB->MaxDataCount = cpu_to_le16(1000);
5719 pSMB->SetupCount = 1;
5720 pSMB->Reserved3 = 0;
5721 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5722 byte_count = 3 /* pad */ + params + count;
5723 pSMB->DataCount = cpu_to_le16(count);
5724 pSMB->ParameterCount = cpu_to_le16(params);
5725 pSMB->TotalDataCount = pSMB->DataCount;
5726 pSMB->TotalParameterCount = pSMB->ParameterCount;
5727 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5728 pSMB->DataOffset = cpu_to_le16(offset);
5729 pSMB->Fid = fid;
5730 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5731 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005732 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005733 pSMB->ByteCount = cpu_to_le16(byte_count);
5734 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005735 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005736 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005737 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005738 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005739
5740 return rc;
5741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
5743int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005744CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005745 const char *fileName, const FILE_BASIC_INFO *data,
5746 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747{
5748 TRANSACTION2_SPI_REQ *pSMB = NULL;
5749 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5750 int name_len;
5751 int rc = 0;
5752 int bytes_returned = 0;
5753 char *data_offset;
5754 __u16 params, param_offset, offset, byte_count, count;
5755
Joe Perchesf96637b2013-05-04 22:12:25 -05005756 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758SetTimesRetry:
5759 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5760 (void **) &pSMBr);
5761 if (rc)
5762 return rc;
5763
5764 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5765 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005766 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5767 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 name_len++; /* trailing null */
5769 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005770 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 name_len = strnlen(fileName, PATH_MAX);
5772 name_len++; /* trailing null */
5773 strncpy(pSMB->FileName, fileName, name_len);
5774 }
5775
5776 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005777 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005779 /* BB find max SMB PDU from sess structure BB */
5780 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 pSMB->MaxSetupCount = 0;
5782 pSMB->Reserved = 0;
5783 pSMB->Flags = 0;
5784 pSMB->Timeout = 0;
5785 pSMB->Reserved2 = 0;
5786 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005787 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 offset = param_offset + params;
5789 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5790 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5791 pSMB->DataOffset = cpu_to_le16(offset);
5792 pSMB->SetupCount = 1;
5793 pSMB->Reserved3 = 0;
5794 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5795 byte_count = 3 /* pad */ + params + count;
5796
5797 pSMB->DataCount = cpu_to_le16(count);
5798 pSMB->ParameterCount = cpu_to_le16(params);
5799 pSMB->TotalDataCount = pSMB->DataCount;
5800 pSMB->TotalParameterCount = pSMB->ParameterCount;
5801 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5802 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5803 else
5804 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5805 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005806 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005807 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 pSMB->ByteCount = cpu_to_le16(byte_count);
5809 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5810 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005811 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005812 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814 cifs_buf_release(pSMB);
5815
5816 if (rc == -EAGAIN)
5817 goto SetTimesRetry;
5818
5819 return rc;
5820}
5821
5822/* Can not be used to set time stamps yet (due to old DOS time format) */
5823/* Can be used to set attributes */
5824#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5825 handling it anyway and NT4 was what we thought it would be needed for
5826 Do not delete it until we prove whether needed for Win9x though */
5827int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005828CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 __u16 dos_attrs, const struct nls_table *nls_codepage)
5830{
5831 SETATTR_REQ *pSMB = NULL;
5832 SETATTR_RSP *pSMBr = NULL;
5833 int rc = 0;
5834 int bytes_returned;
5835 int name_len;
5836
Joe Perchesf96637b2013-05-04 22:12:25 -05005837 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
5839SetAttrLgcyRetry:
5840 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5841 (void **) &pSMBr);
5842 if (rc)
5843 return rc;
5844
5845 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5846 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005847 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5848 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 name_len++; /* trailing null */
5850 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005851 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 name_len = strnlen(fileName, PATH_MAX);
5853 name_len++; /* trailing null */
5854 strncpy(pSMB->fileName, fileName, name_len);
5855 }
5856 pSMB->attr = cpu_to_le16(dos_attrs);
5857 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005858 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5860 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5861 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005862 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005863 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865 cifs_buf_release(pSMB);
5866
5867 if (rc == -EAGAIN)
5868 goto SetAttrLgcyRetry;
5869
5870 return rc;
5871}
5872#endif /* temporarily unneeded SetAttr legacy function */
5873
Jeff Layton654cf142009-07-09 20:02:49 -04005874static void
5875cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5876 const struct cifs_unix_set_info_args *args)
5877{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005878 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005879 u64 mode = args->mode;
5880
Eric W. Biederman49418b22013-02-06 00:57:56 -08005881 if (uid_valid(args->uid))
5882 uid = from_kuid(&init_user_ns, args->uid);
5883 if (gid_valid(args->gid))
5884 gid = from_kgid(&init_user_ns, args->gid);
5885
Jeff Layton654cf142009-07-09 20:02:49 -04005886 /*
5887 * Samba server ignores set of file size to zero due to bugs in some
5888 * older clients, but we should be precise - we use SetFileSize to
5889 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005890 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005891 * zero instead of -1 here
5892 */
5893 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5894 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5895 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5896 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5897 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005898 data_offset->Uid = cpu_to_le64(uid);
5899 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005900 /* better to leave device as zero when it is */
5901 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5902 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5903 data_offset->Permissions = cpu_to_le64(mode);
5904
5905 if (S_ISREG(mode))
5906 data_offset->Type = cpu_to_le32(UNIX_FILE);
5907 else if (S_ISDIR(mode))
5908 data_offset->Type = cpu_to_le32(UNIX_DIR);
5909 else if (S_ISLNK(mode))
5910 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5911 else if (S_ISCHR(mode))
5912 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5913 else if (S_ISBLK(mode))
5914 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5915 else if (S_ISFIFO(mode))
5916 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5917 else if (S_ISSOCK(mode))
5918 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5919}
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005922CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005923 const struct cifs_unix_set_info_args *args,
5924 u16 fid, u32 pid_of_opener)
5925{
5926 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005927 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005928 int rc = 0;
5929 u16 params, param_offset, offset, byte_count, count;
5930
Joe Perchesf96637b2013-05-04 22:12:25 -05005931 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005932 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5933
5934 if (rc)
5935 return rc;
5936
5937 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5938 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5939
5940 params = 6;
5941 pSMB->MaxSetupCount = 0;
5942 pSMB->Reserved = 0;
5943 pSMB->Flags = 0;
5944 pSMB->Timeout = 0;
5945 pSMB->Reserved2 = 0;
5946 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5947 offset = param_offset + params;
5948
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005949 data_offset = (char *)pSMB +
5950 offsetof(struct smb_hdr, Protocol) + offset;
5951
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005952 count = sizeof(FILE_UNIX_BASIC_INFO);
5953
5954 pSMB->MaxParameterCount = cpu_to_le16(2);
5955 /* BB find max SMB PDU from sess */
5956 pSMB->MaxDataCount = cpu_to_le16(1000);
5957 pSMB->SetupCount = 1;
5958 pSMB->Reserved3 = 0;
5959 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5960 byte_count = 3 /* pad */ + params + count;
5961 pSMB->DataCount = cpu_to_le16(count);
5962 pSMB->ParameterCount = cpu_to_le16(params);
5963 pSMB->TotalDataCount = pSMB->DataCount;
5964 pSMB->TotalParameterCount = pSMB->ParameterCount;
5965 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5966 pSMB->DataOffset = cpu_to_le16(offset);
5967 pSMB->Fid = fid;
5968 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5969 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005970 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005971 pSMB->ByteCount = cpu_to_le16(byte_count);
5972
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005973 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005974
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005975 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005976 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005977 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005978 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5979 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005980
5981 /* Note: On -EAGAIN error only caller can retry on handle based calls
5982 since file handle passed in no longer valid */
5983
5984 return rc;
5985}
5986
5987int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005988CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005989 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005990 const struct cifs_unix_set_info_args *args,
5991 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992{
5993 TRANSACTION2_SPI_REQ *pSMB = NULL;
5994 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5995 int name_len;
5996 int rc = 0;
5997 int bytes_returned = 0;
5998 FILE_UNIX_BASIC_INFO *data_offset;
5999 __u16 params, param_offset, offset, count, byte_count;
6000
Joe Perchesf96637b2013-05-04 22:12:25 -05006001 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002setPermsRetry:
6003 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6004 (void **) &pSMBr);
6005 if (rc)
6006 return rc;
6007
6008 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6009 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006010 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006011 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 name_len++; /* trailing null */
6013 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006014 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006015 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006017 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 }
6019
6020 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006021 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006023 /* BB find max SMB PDU from sess structure BB */
6024 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 pSMB->MaxSetupCount = 0;
6026 pSMB->Reserved = 0;
6027 pSMB->Flags = 0;
6028 pSMB->Timeout = 0;
6029 pSMB->Reserved2 = 0;
6030 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006031 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 offset = param_offset + params;
6033 data_offset =
6034 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6035 offset);
6036 memset(data_offset, 0, count);
6037 pSMB->DataOffset = cpu_to_le16(offset);
6038 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6039 pSMB->SetupCount = 1;
6040 pSMB->Reserved3 = 0;
6041 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6042 byte_count = 3 /* pad */ + params + count;
6043 pSMB->ParameterCount = cpu_to_le16(params);
6044 pSMB->DataCount = cpu_to_le16(count);
6045 pSMB->TotalParameterCount = pSMB->ParameterCount;
6046 pSMB->TotalDataCount = pSMB->DataCount;
6047 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6048 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006049 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006050
Jeff Layton654cf142009-07-09 20:02:49 -04006051 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
6053 pSMB->ByteCount = cpu_to_le16(byte_count);
6054 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6055 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006056 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006057 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Steve French0d817bc2008-05-22 02:02:03 +00006059 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 if (rc == -EAGAIN)
6061 goto setPermsRetry;
6062 return rc;
6063}
6064
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006066/*
6067 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6068 * function used by listxattr and getxattr type calls. When ea_name is set,
6069 * it looks for that attribute name and stuffs that value into the EAData
6070 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6071 * buffer. In both cases, the return value is either the length of the
6072 * resulting data or a negative error code. If EAData is a NULL pointer then
6073 * the data isn't copied to it, but the length is returned.
6074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006076CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006077 const unsigned char *searchName, const unsigned char *ea_name,
6078 char *EAData, size_t buf_size,
Steve French67b4c882017-05-12 20:59:10 -05006079 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
6081 /* BB assumes one setup word */
6082 TRANSACTION2_QPI_REQ *pSMB = NULL;
6083 TRANSACTION2_QPI_RSP *pSMBr = NULL;
Steve French67b4c882017-05-12 20:59:10 -05006084 int remap = cifs_remap(cifs_sb);
6085 struct nls_table *nls_codepage = cifs_sb->local_nls;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 int rc = 0;
6087 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006088 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006089 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006090 struct fea *temp_fea;
6091 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006092 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006093 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006094 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Joe Perchesf96637b2013-05-04 22:12:25 -05006096 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097QAllEAsRetry:
6098 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6099 (void **) &pSMBr);
6100 if (rc)
6101 return rc;
6102
6103 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006104 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006105 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6106 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006107 list_len++; /* trailing null */
6108 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006110 list_len = strnlen(searchName, PATH_MAX);
6111 list_len++; /* trailing null */
6112 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 }
6114
Jeff Layton6e462b92010-02-10 16:18:26 -05006115 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 pSMB->TotalDataCount = 0;
6117 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006118 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006119 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 pSMB->MaxSetupCount = 0;
6121 pSMB->Reserved = 0;
6122 pSMB->Flags = 0;
6123 pSMB->Timeout = 0;
6124 pSMB->Reserved2 = 0;
6125 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006126 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 pSMB->DataCount = 0;
6128 pSMB->DataOffset = 0;
6129 pSMB->SetupCount = 1;
6130 pSMB->Reserved3 = 0;
6131 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6132 byte_count = params + 1 /* pad */ ;
6133 pSMB->TotalParameterCount = cpu_to_le16(params);
6134 pSMB->ParameterCount = pSMB->TotalParameterCount;
6135 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6136 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006137 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 pSMB->ByteCount = cpu_to_le16(byte_count);
6139
6140 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6141 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6142 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006143 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006144 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006146
6147
6148 /* BB also check enough total bytes returned */
6149 /* BB we need to improve the validity checking
6150 of these trans2 responses */
6151
6152 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006153 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006154 rc = -EIO; /* bad smb */
6155 goto QAllEAsOut;
6156 }
6157
6158 /* check that length of list is not more than bcc */
6159 /* check that each entry does not go beyond length
6160 of list */
6161 /* check that each element of each entry does not
6162 go beyond end of list */
6163 /* validate_trans2_offsets() */
6164 /* BB check if start of smb + data_offset > &bcc+ bcc */
6165
6166 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6167 ea_response_data = (struct fealist *)
6168 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6169
Jeff Layton6e462b92010-02-10 16:18:26 -05006170 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006171 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006172 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006173 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006174 /* didn't find the named attribute */
6175 if (ea_name)
6176 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006177 goto QAllEAsOut;
6178 }
6179
Jeff Layton0cd126b2010-02-10 16:18:26 -05006180 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006181 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006182 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006183 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006184 rc = -EIO;
6185 goto QAllEAsOut;
6186 }
6187
Jeff Laytonf0d38682010-02-10 16:18:26 -05006188 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006189 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006190 temp_fea = ea_response_data->list;
6191 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006192 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006193 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006194 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006195
Jeff Layton6e462b92010-02-10 16:18:26 -05006196 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006197 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006198 /* make sure we can read name_len and value_len */
6199 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006200 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006201 rc = -EIO;
6202 goto QAllEAsOut;
6203 }
6204
6205 name_len = temp_fea->name_len;
6206 value_len = le16_to_cpu(temp_fea->value_len);
6207 list_len -= name_len + 1 + value_len;
6208 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006209 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006210 rc = -EIO;
6211 goto QAllEAsOut;
6212 }
6213
Jeff Layton31c05192010-02-10 16:18:26 -05006214 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006215 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006216 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006217 temp_ptr += name_len + 1;
6218 rc = value_len;
6219 if (buf_size == 0)
6220 goto QAllEAsOut;
6221 if ((size_t)value_len > buf_size) {
6222 rc = -ERANGE;
6223 goto QAllEAsOut;
6224 }
6225 memcpy(EAData, temp_ptr, value_len);
6226 goto QAllEAsOut;
6227 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006228 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006229 /* account for prefix user. and trailing null */
6230 rc += (5 + 1 + name_len);
6231 if (rc < (int) buf_size) {
6232 memcpy(EAData, "user.", 5);
6233 EAData += 5;
6234 memcpy(EAData, temp_ptr, name_len);
6235 EAData += name_len;
6236 /* null terminate name */
6237 *EAData = 0;
6238 ++EAData;
6239 } else if (buf_size == 0) {
6240 /* skip copy - calc size only */
6241 } else {
6242 /* stop before overrun buffer */
6243 rc = -ERANGE;
6244 break;
6245 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006246 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006247 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006248 temp_fea = (struct fea *)temp_ptr;
6249 }
6250
Jeff Layton31c05192010-02-10 16:18:26 -05006251 /* didn't find the named attribute */
6252 if (ea_name)
6253 rc = -ENODATA;
6254
Jeff Laytonf0d38682010-02-10 16:18:26 -05006255QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006256 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 if (rc == -EAGAIN)
6258 goto QAllEAsRetry;
6259
6260 return (ssize_t)rc;
6261}
6262
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006264CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6265 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006266 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6267 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268{
6269 struct smb_com_transaction2_spi_req *pSMB = NULL;
6270 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6271 struct fealist *parm_data;
6272 int name_len;
6273 int rc = 0;
6274 int bytes_returned = 0;
6275 __u16 params, param_offset, byte_count, offset, count;
6276
Joe Perchesf96637b2013-05-04 22:12:25 -05006277 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278SetEARetry:
6279 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6280 (void **) &pSMBr);
6281 if (rc)
6282 return rc;
6283
6284 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6285 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006286 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6287 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 name_len++; /* trailing null */
6289 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006290 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 name_len = strnlen(fileName, PATH_MAX);
6292 name_len++; /* trailing null */
6293 strncpy(pSMB->FileName, fileName, name_len);
6294 }
6295
6296 params = 6 + name_len;
6297
6298 /* done calculating parms using name_len of file name,
6299 now use name_len to calculate length of ea name
6300 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006301 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 name_len = 0;
6303 else
Steve French50c2f752007-07-13 00:33:32 +00006304 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006306 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006308 /* BB find max SMB PDU from sess */
6309 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 pSMB->MaxSetupCount = 0;
6311 pSMB->Reserved = 0;
6312 pSMB->Flags = 0;
6313 pSMB->Timeout = 0;
6314 pSMB->Reserved2 = 0;
6315 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006316 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 offset = param_offset + params;
6318 pSMB->InformationLevel =
6319 cpu_to_le16(SMB_SET_FILE_EA);
6320
6321 parm_data =
6322 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6323 offset);
6324 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6325 pSMB->DataOffset = cpu_to_le16(offset);
6326 pSMB->SetupCount = 1;
6327 pSMB->Reserved3 = 0;
6328 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6329 byte_count = 3 /* pad */ + params + count;
6330 pSMB->DataCount = cpu_to_le16(count);
6331 parm_data->list_len = cpu_to_le32(count);
6332 parm_data->list[0].EA_flags = 0;
6333 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006334 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006336 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006337 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 parm_data->list[0].name[name_len] = 0;
6339 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6340 /* caller ensures that ea_value_len is less than 64K but
6341 we need to ensure that it fits within the smb */
6342
Steve French50c2f752007-07-13 00:33:32 +00006343 /*BB add length check to see if it would fit in
6344 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006345 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6346 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006347 memcpy(parm_data->list[0].name+name_len+1,
6348 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 pSMB->TotalDataCount = pSMB->DataCount;
6351 pSMB->ParameterCount = cpu_to_le16(params);
6352 pSMB->TotalParameterCount = pSMB->ParameterCount;
6353 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006354 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 pSMB->ByteCount = cpu_to_le16(byte_count);
6356 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6357 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006358 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006359 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
6361 cifs_buf_release(pSMB);
6362
6363 if (rc == -EAGAIN)
6364 goto SetEARetry;
6365
6366 return rc;
6367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368#endif
Steve French0eff0e22011-02-24 05:39:23 +00006369
6370#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6371/*
6372 * Years ago the kernel added a "dnotify" function for Samba server,
6373 * to allow network clients (such as Windows) to display updated
6374 * lists of files in directory listings automatically when
6375 * files are added by one user when another user has the
6376 * same directory open on their desktop. The Linux cifs kernel
6377 * client hooked into the kernel side of this interface for
6378 * the same reason, but ironically when the VFS moved from
6379 * "dnotify" to "inotify" it became harder to plug in Linux
6380 * network file system clients (the most obvious use case
6381 * for notify interfaces is when multiple users can update
6382 * the contents of the same directory - exactly what network
6383 * file systems can do) although the server (Samba) could
6384 * still use it. For the short term we leave the worker
6385 * function ifdeffed out (below) until inotify is fixed
6386 * in the VFS to make it easier to plug in network file
6387 * system clients. If inotify turns out to be permanently
6388 * incompatible for network fs clients, we could instead simply
6389 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6390 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006391int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006392 const int notify_subdirs, const __u16 netfid,
6393 __u32 filter, struct file *pfile, int multishot,
6394 const struct nls_table *nls_codepage)
6395{
6396 int rc = 0;
6397 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6398 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6399 struct dir_notify_req *dnotify_req;
6400 int bytes_returned;
6401
Joe Perchesf96637b2013-05-04 22:12:25 -05006402 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006403 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6404 (void **) &pSMBr);
6405 if (rc)
6406 return rc;
6407
6408 pSMB->TotalParameterCount = 0 ;
6409 pSMB->TotalDataCount = 0;
6410 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006411 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006412 pSMB->MaxSetupCount = 4;
6413 pSMB->Reserved = 0;
6414 pSMB->ParameterOffset = 0;
6415 pSMB->DataCount = 0;
6416 pSMB->DataOffset = 0;
6417 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6418 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6419 pSMB->ParameterCount = pSMB->TotalParameterCount;
6420 if (notify_subdirs)
6421 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6422 pSMB->Reserved2 = 0;
6423 pSMB->CompletionFilter = cpu_to_le32(filter);
6424 pSMB->Fid = netfid; /* file handle always le */
6425 pSMB->ByteCount = 0;
6426
6427 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6428 (struct smb_hdr *)pSMBr, &bytes_returned,
6429 CIFS_ASYNC_OP);
6430 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006431 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006432 } else {
6433 /* Add file to outstanding requests */
6434 /* BB change to kmem cache alloc */
6435 dnotify_req = kmalloc(
6436 sizeof(struct dir_notify_req),
6437 GFP_KERNEL);
6438 if (dnotify_req) {
6439 dnotify_req->Pid = pSMB->hdr.Pid;
6440 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6441 dnotify_req->Mid = pSMB->hdr.Mid;
6442 dnotify_req->Tid = pSMB->hdr.Tid;
6443 dnotify_req->Uid = pSMB->hdr.Uid;
6444 dnotify_req->netfid = netfid;
6445 dnotify_req->pfile = pfile;
6446 dnotify_req->filter = filter;
6447 dnotify_req->multishot = multishot;
6448 spin_lock(&GlobalMid_Lock);
6449 list_add_tail(&dnotify_req->lhead,
6450 &GlobalDnotifyReqList);
6451 spin_unlock(&GlobalMid_Lock);
6452 } else
6453 rc = -ENOMEM;
6454 }
6455 cifs_buf_release(pSMB);
6456 return rc;
6457}
6458#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */