blob: 5d21f00ae341b4ec002eb33dd79c7a16a1e1dfa4 [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 Layton502858822013-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;
481 struct timespec ts, utc;
482 utc = CURRENT_TIME;
483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
485 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
486 (int)ts.tv_sec, (int)utc.tv_sec,
487 (int)(utc.tv_sec - ts.tv_sec));
488 val = (int)(utc.tv_sec - ts.tv_sec);
489 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
Christopher Oo5fb4e282015-06-25 16:10:48 -0700700 mutex_lock(&server->srv_mutex);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500701 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -0700702 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400703 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500704}
705
706int
707CIFSSMBEcho(struct TCP_Server_Info *server)
708{
709 ECHO_REQ *smb;
710 int rc = 0;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800711 struct kvec iov[2];
712 struct smb_rqst rqst = { .rq_iov = iov,
713 .rq_nvec = 2 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500714
Joe Perchesf96637b2013-05-04 22:12:25 -0500715 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500716
717 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
718 if (rc)
719 return rc;
720
721 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000722 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c8f2011-01-20 21:19:25 -0500723 smb->hdr.WordCount = 1;
724 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400725 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500726 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000727 inc_rfc1001_len(smb, 3);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -0800728
729 iov[0].iov_len = 4;
730 iov[0].iov_base = smb;
731 iov[1].iov_len = get_rfc1002_length(smb);
732 iov[1].iov_base = (char *)smb + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500733
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -0800734 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback, NULL,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400735 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500736 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500737 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500738
739 cifs_small_buf_release(smb);
740
741 return rc;
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400745CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 LOGOFF_ANDX_REQ *pSMB;
748 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Joe Perchesf96637b2013-05-04 22:12:25 -0500750 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500751
752 /*
753 * BB: do we need to check validity of ses and server? They should
754 * always be valid since we have an active reference. If not, that
755 * should probably be a BUG()
756 */
757 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return -EIO;
759
Steve Frenchd7b619c2010-02-25 05:36:46 +0000760 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000761 if (ses->need_reconnect)
762 goto session_already_dead; /* no need to send SMBlogoff if uid
763 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
765 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000766 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return rc;
768 }
769
Pavel Shilovsky88257362012-05-23 14:01:59 +0400770 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700771
Jeff Layton38d77c52013-05-26 07:01:00 -0400772 if (ses->server->sign)
773 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 pSMB->hdr.Uid = ses->Suid;
776
777 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400778 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -0700779 cifs_small_buf_release(pSMB);
Steve French3b795212008-11-13 19:45:32 +0000780session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000781 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000784 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 error */
786 if (rc == -EAGAIN)
787 rc = 0;
788 return rc;
789}
790
791int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400792CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
793 const char *fileName, __u16 type,
794 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000795{
796 TRANSACTION2_SPI_REQ *pSMB = NULL;
797 TRANSACTION2_SPI_RSP *pSMBr = NULL;
798 struct unlink_psx_rq *pRqD;
799 int name_len;
800 int rc = 0;
801 int bytes_returned = 0;
802 __u16 params, param_offset, offset, byte_count;
803
Joe Perchesf96637b2013-05-04 22:12:25 -0500804 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000805PsxDelete:
806 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
807 (void **) &pSMBr);
808 if (rc)
809 return rc;
810
811 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
812 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600813 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
814 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000815 name_len++; /* trailing null */
816 name_len *= 2;
817 } else { /* BB add path length overrun check */
818 name_len = strnlen(fileName, PATH_MAX);
819 name_len++; /* trailing null */
820 strncpy(pSMB->FileName, fileName, name_len);
821 }
822
823 params = 6 + name_len;
824 pSMB->MaxParameterCount = cpu_to_le16(2);
825 pSMB->MaxDataCount = 0; /* BB double check this with jra */
826 pSMB->MaxSetupCount = 0;
827 pSMB->Reserved = 0;
828 pSMB->Flags = 0;
829 pSMB->Timeout = 0;
830 pSMB->Reserved2 = 0;
831 param_offset = offsetof(struct smb_com_transaction2_spi_req,
832 InformationLevel) - 4;
833 offset = param_offset + params;
834
835 /* Setup pointer to Request Data (inode type) */
836 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
837 pRqD->type = cpu_to_le16(type);
838 pSMB->ParameterOffset = cpu_to_le16(param_offset);
839 pSMB->DataOffset = cpu_to_le16(offset);
840 pSMB->SetupCount = 1;
841 pSMB->Reserved3 = 0;
842 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
843 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
844
845 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
846 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
847 pSMB->ParameterCount = cpu_to_le16(params);
848 pSMB->TotalParameterCount = pSMB->ParameterCount;
849 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
850 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000851 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000852 pSMB->ByteCount = cpu_to_le16(byte_count);
853 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
854 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000855 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500856 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000857 cifs_buf_release(pSMB);
858
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400859 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000860
861 if (rc == -EAGAIN)
862 goto PsxDelete;
863
864 return rc;
865}
866
867int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700868CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
869 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 DELETE_FILE_REQ *pSMB = NULL;
872 DELETE_FILE_RSP *pSMBr = NULL;
873 int rc = 0;
874 int bytes_returned;
875 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500876 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878DelFileRetry:
879 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
880 (void **) &pSMBr);
881 if (rc)
882 return rc;
883
884 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700885 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
886 PATH_MAX, cifs_sb->local_nls,
887 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 name_len++; /* trailing null */
889 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700890 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700891 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700893 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 pSMB->SearchAttributes =
896 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
897 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000898 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 pSMB->ByteCount = cpu_to_le16(name_len + 1);
900 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
901 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400902 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000903 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500904 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 cifs_buf_release(pSMB);
907 if (rc == -EAGAIN)
908 goto DelFileRetry;
909
910 return rc;
911}
912
913int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400914CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
915 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 DELETE_DIRECTORY_REQ *pSMB = NULL;
918 DELETE_DIRECTORY_RSP *pSMBr = NULL;
919 int rc = 0;
920 int bytes_returned;
921 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500922 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Joe Perchesf96637b2013-05-04 22:12:25 -0500924 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925RmDirRetry:
926 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
927 (void **) &pSMBr);
928 if (rc)
929 return rc;
930
931 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400932 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
933 PATH_MAX, cifs_sb->local_nls,
934 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 name_len++; /* trailing null */
936 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700937 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400938 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400940 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000944 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 pSMB->ByteCount = cpu_to_le16(name_len + 1);
946 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
947 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400948 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000949 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500950 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 cifs_buf_release(pSMB);
953 if (rc == -EAGAIN)
954 goto RmDirRetry;
955 return rc;
956}
957
958int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300959CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
960 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 int rc = 0;
963 CREATE_DIRECTORY_REQ *pSMB = NULL;
964 CREATE_DIRECTORY_RSP *pSMBr = NULL;
965 int bytes_returned;
966 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500967 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Joe Perchesf96637b2013-05-04 22:12:25 -0500969 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970MkDirRetry:
971 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
972 (void **) &pSMBr);
973 if (rc)
974 return rc;
975
976 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600977 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300978 PATH_MAX, cifs_sb->local_nls,
979 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 name_len++; /* trailing null */
981 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700982 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 name_len = strnlen(name, PATH_MAX);
984 name_len++; /* trailing null */
985 strncpy(pSMB->DirName, name, name_len);
986 }
987
988 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000989 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 pSMB->ByteCount = cpu_to_le16(name_len + 1);
991 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
992 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400993 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000994 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500995 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 cifs_buf_release(pSMB);
998 if (rc == -EAGAIN)
999 goto MkDirRetry;
1000 return rc;
1001}
1002
Steve French2dd29d32007-04-23 22:07:35 +00001003int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001004CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1005 __u32 posix_flags, __u64 mode, __u16 *netfid,
1006 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1007 const char *name, const struct nls_table *nls_codepage,
1008 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001009{
1010 TRANSACTION2_SPI_REQ *pSMB = NULL;
1011 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1012 int name_len;
1013 int rc = 0;
1014 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001015 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001016 OPEN_PSX_REQ *pdata;
1017 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001018
Joe Perchesf96637b2013-05-04 22:12:25 -05001019 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001020PsxCreat:
1021 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1022 (void **) &pSMBr);
1023 if (rc)
1024 return rc;
1025
1026 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1027 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001028 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1029 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001030 name_len++; /* trailing null */
1031 name_len *= 2;
1032 } else { /* BB improve the check for buffer overruns BB */
1033 name_len = strnlen(name, PATH_MAX);
1034 name_len++; /* trailing null */
1035 strncpy(pSMB->FileName, name, name_len);
1036 }
1037
1038 params = 6 + name_len;
1039 count = sizeof(OPEN_PSX_REQ);
1040 pSMB->MaxParameterCount = cpu_to_le16(2);
1041 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1042 pSMB->MaxSetupCount = 0;
1043 pSMB->Reserved = 0;
1044 pSMB->Flags = 0;
1045 pSMB->Timeout = 0;
1046 pSMB->Reserved2 = 0;
1047 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001048 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001049 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001050 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001051 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001052 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001053 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001054 pdata->OpenFlags = cpu_to_le32(*pOplock);
1055 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1056 pSMB->DataOffset = cpu_to_le16(offset);
1057 pSMB->SetupCount = 1;
1058 pSMB->Reserved3 = 0;
1059 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1060 byte_count = 3 /* pad */ + params + count;
1061
1062 pSMB->DataCount = cpu_to_le16(count);
1063 pSMB->ParameterCount = cpu_to_le16(params);
1064 pSMB->TotalDataCount = pSMB->DataCount;
1065 pSMB->TotalParameterCount = pSMB->ParameterCount;
1066 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1067 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001068 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001069 pSMB->ByteCount = cpu_to_le16(byte_count);
1070 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1071 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1072 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001073 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001074 goto psx_create_err;
1075 }
1076
Joe Perchesf96637b2013-05-04 22:12:25 -05001077 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001078 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1079
Jeff Layton820a8032011-05-04 08:05:26 -04001080 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001081 rc = -EIO; /* bad smb */
1082 goto psx_create_err;
1083 }
1084
1085 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001086 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001087 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001088
Steve French2dd29d32007-04-23 22:07:35 +00001089 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001090 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001091 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1092 /* Let caller know file was created so we can set the mode. */
1093 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001094 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001095 *pOplock |= CIFS_CREATE_ACTION;
1096 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001097 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1098 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001099 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001100 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001101 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001102 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001103 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001104 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001105 goto psx_create_err;
1106 }
Steve French50c2f752007-07-13 00:33:32 +00001107 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001108 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001109 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001110 }
Steve French2dd29d32007-04-23 22:07:35 +00001111
1112psx_create_err:
1113 cifs_buf_release(pSMB);
1114
Steve French65bc98b2009-07-10 15:27:25 +00001115 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001116 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001117 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001118 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001119
1120 if (rc == -EAGAIN)
1121 goto PsxCreat;
1122
Steve French50c2f752007-07-13 00:33:32 +00001123 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001124}
1125
Steve Frencha9d02ad2005-08-24 23:06:05 -07001126static __u16 convert_disposition(int disposition)
1127{
1128 __u16 ofun = 0;
1129
1130 switch (disposition) {
1131 case FILE_SUPERSEDE:
1132 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1133 break;
1134 case FILE_OPEN:
1135 ofun = SMBOPEN_OAPPEND;
1136 break;
1137 case FILE_CREATE:
1138 ofun = SMBOPEN_OCREATE;
1139 break;
1140 case FILE_OPEN_IF:
1141 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1142 break;
1143 case FILE_OVERWRITE:
1144 ofun = SMBOPEN_OTRUNC;
1145 break;
1146 case FILE_OVERWRITE_IF:
1147 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1148 break;
1149 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001150 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001151 ofun = SMBOPEN_OAPPEND; /* regular open */
1152 }
1153 return ofun;
1154}
1155
Jeff Layton35fc37d2008-05-14 10:22:03 -07001156static int
1157access_flags_to_smbopen_mode(const int access_flags)
1158{
1159 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1160
1161 if (masked_flags == GENERIC_READ)
1162 return SMBOPEN_READ;
1163 else if (masked_flags == GENERIC_WRITE)
1164 return SMBOPEN_WRITE;
1165
1166 /* just go for read/write */
1167 return SMBOPEN_READWRITE;
1168}
1169
Steve Frencha9d02ad2005-08-24 23:06:05 -07001170int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001171SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001172 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001173 const int access_flags, const int create_options, __u16 *netfid,
1174 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001175 const struct nls_table *nls_codepage, int remap)
1176{
1177 int rc = -EACCES;
1178 OPENX_REQ *pSMB = NULL;
1179 OPENX_RSP *pSMBr = NULL;
1180 int bytes_returned;
1181 int name_len;
1182 __u16 count;
1183
1184OldOpenRetry:
1185 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1186 (void **) &pSMBr);
1187 if (rc)
1188 return rc;
1189
1190 pSMB->AndXCommand = 0xFF; /* none */
1191
1192 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1193 count = 1; /* account for one byte pad to word boundary */
1194 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001195 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1196 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001197 name_len++; /* trailing null */
1198 name_len *= 2;
1199 } else { /* BB improve check for buffer overruns BB */
1200 count = 0; /* no pad */
1201 name_len = strnlen(fileName, PATH_MAX);
1202 name_len++; /* trailing null */
1203 strncpy(pSMB->fileName, fileName, name_len);
1204 }
1205 if (*pOplock & REQ_OPLOCK)
1206 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001207 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001208 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001209
Steve Frencha9d02ad2005-08-24 23:06:05 -07001210 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001211 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001212 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1213 /* set file as system file if special file such
1214 as fifo and server expecting SFU style and
1215 no Unix extensions */
1216
Steve French790fe572007-07-07 19:25:05 +00001217 if (create_options & CREATE_OPTION_SPECIAL)
1218 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001219 else /* BB FIXME BB */
1220 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001221
Jeff Layton67750fb2008-05-09 22:28:02 +00001222 if (create_options & CREATE_OPTION_READONLY)
1223 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001224
1225 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001226/* pSMB->CreateOptions = cpu_to_le32(create_options &
1227 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001228 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001229
1230 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001231 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001232 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001233 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001234
1235 pSMB->ByteCount = cpu_to_le16(count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001236 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001237 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001238 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001239 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001240 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241 } else {
1242 /* BB verify if wct == 15 */
1243
Steve French582d21e2008-05-13 04:54:12 +00001244/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001245
1246 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1247 /* Let caller know file was created so we can set the mode. */
1248 /* Do we care about the CreateAction in any other cases? */
1249 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001250/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001251 *pOplock |= CIFS_CREATE_ACTION; */
1252 /* BB FIXME END */
1253
Steve French790fe572007-07-07 19:25:05 +00001254 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001255 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1256 pfile_info->LastAccessTime = 0; /* BB fixme */
1257 pfile_info->LastWriteTime = 0; /* BB fixme */
1258 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001259 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001260 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001261 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001262 pfile_info->AllocationSize =
1263 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1264 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001265 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001266 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001267 }
1268 }
1269
1270 cifs_buf_release(pSMB);
1271 if (rc == -EAGAIN)
1272 goto OldOpenRetry;
1273 return rc;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001277CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1278 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001281 OPEN_REQ *req = NULL;
1282 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int bytes_returned;
1284 int name_len;
1285 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001286 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1287 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001288 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001289 const struct nls_table *nls = cifs_sb->local_nls;
1290 int create_options = oparms->create_options;
1291 int desired_access = oparms->desired_access;
1292 int disposition = oparms->disposition;
1293 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001296 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1297 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (rc)
1299 return rc;
1300
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001301 /* no commands go after this */
1302 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001304 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1305 /* account for one byte pad to word boundary */
1306 count = 1;
1307 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1308 path, PATH_MAX, nls, remap);
1309 /* trailing null */
1310 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001312 req->NameLength = cpu_to_le16(name_len);
1313 } else {
1314 /* BB improve check for buffer overruns BB */
1315 /* no pad */
1316 count = 0;
1317 name_len = strnlen(path, PATH_MAX);
1318 /* trailing null */
1319 name_len++;
1320 req->NameLength = cpu_to_le16(name_len);
1321 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001323
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001324 if (*oplock & REQ_OPLOCK)
1325 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1326 else if (*oplock & REQ_BATCHOPLOCK)
1327 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1328
1329 req->DesiredAccess = cpu_to_le32(desired_access);
1330 req->AllocationSize = 0;
1331
1332 /*
1333 * Set file as system file if special file such as fifo and server
1334 * expecting SFU style and no Unix extensions.
1335 */
1336 if (create_options & CREATE_OPTION_SPECIAL)
1337 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1338 else
1339 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1340
1341 /*
1342 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1343 * sensitive checks for other servers such as Samba.
1344 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001346 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jeff Layton67750fb2008-05-09 22:28:02 +00001348 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001349 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001350
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001351 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1352 req->CreateDisposition = cpu_to_le32(disposition);
1353 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1354
Steve French09d1db52005-04-28 22:41:08 -07001355 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001356 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1357 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001360 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001362 req->ByteCount = cpu_to_le16(count);
1363 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1364 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001365 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001367 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001368 cifs_buf_release(req);
1369 if (rc == -EAGAIN)
1370 goto openRetry;
1371 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001373
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001374 /* 1 byte no need to le_to_cpu */
1375 *oplock = rsp->OplockLevel;
1376 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001377 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001378
1379 /* Let caller know file was created so we can set the mode. */
1380 /* Do we care about the CreateAction in any other cases? */
1381 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1382 *oplock |= CIFS_CREATE_ACTION;
1383
1384 if (buf) {
1385 /* copy from CreationTime to Attributes */
1386 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1387 /* the file_info buf is endian converted by caller */
1388 buf->AllocationSize = rsp->AllocationSize;
1389 buf->EndOfFile = rsp->EndOfFile;
1390 buf->NumberOfLinks = cpu_to_le32(1);
1391 buf->DeletePending = 0;
1392 }
1393
1394 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return rc;
1396}
1397
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001398/*
1399 * Discard any remaining data in the current SMB. To do this, we borrow the
1400 * current bigbuf.
1401 */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001402int
Pavel Shilovsky350be252017-04-10 10:31:33 -07001403cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001404{
Pavel Shilovsky350be252017-04-10 10:31:33 -07001405 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001406 int remaining = rfclen + 4 - server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001407
1408 while (remaining > 0) {
1409 int length;
1410
1411 length = cifs_read_from_socket(server, server->bigbuf,
1412 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001413 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001414 if (length < 0)
1415 return length;
1416 server->total_read += length;
1417 remaining -= length;
1418 }
1419
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001420 return 0;
1421}
1422
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001423static int
1424cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1425{
1426 int length;
1427 struct cifs_readdata *rdata = mid->callback_data;
1428
Pavel Shilovsky350be252017-04-10 10:31:33 -07001429 length = cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001430 dequeue_mid(mid, rdata->result);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001431 mid->resp_buf = server->smallbuf;
1432 server->smallbuf = NULL;
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001433 return length;
1434}
1435
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001436int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001437cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1438{
1439 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001440 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001441 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001442 char *buf = server->smallbuf;
1443 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001444
Joe Perchesf96637b2013-05-04 22:12:25 -05001445 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1446 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001447
1448 /*
1449 * read the rest of READ_RSP header (sans Data array), or whatever we
1450 * can if there's not enough data. At this point, we've read down to
1451 * the Mid.
1452 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001453 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001454 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001455
Al Viroa6137302016-01-09 19:37:16 -05001456 length = cifs_read_from_socket(server,
1457 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001458 if (length < 0)
1459 return length;
1460 server->total_read += length;
1461
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001462 if (server->ops->is_status_pending &&
1463 server->ops->is_status_pending(buf, server, 0)) {
Pavel Shilovsky350be252017-04-10 10:31:33 -07001464 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001465 return -1;
1466 }
1467
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001468 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001469 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001470 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001471 cifs_dbg(FYI, "%s: server returned error %d\n",
1472 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001473 return cifs_readv_discard(server, mid);
1474 }
1475
1476 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001477 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001478 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1479 __func__, server->total_read,
1480 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001481 rdata->result = -EIO;
1482 return cifs_readv_discard(server, mid);
1483 }
1484
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001485 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001486 if (data_offset < server->total_read) {
1487 /*
1488 * win2k8 sometimes sends an offset of 0 when the read
1489 * is beyond the EOF. Treat it as if the data starts just after
1490 * the header.
1491 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001492 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1493 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001494 data_offset = server->total_read;
1495 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1496 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001497 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1498 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001499 rdata->result = -EIO;
1500 return cifs_readv_discard(server, mid);
1501 }
1502
Joe Perchesf96637b2013-05-04 22:12:25 -05001503 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1504 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001505
1506 len = data_offset - server->total_read;
1507 if (len > 0) {
1508 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001509 length = cifs_read_from_socket(server,
1510 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001511 if (length < 0)
1512 return length;
1513 server->total_read += length;
1514 }
1515
1516 /* set up first iov for signature check */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001517 rdata->iov[0].iov_base = buf;
1518 rdata->iov[0].iov_len = 4;
1519 rdata->iov[1].iov_base = buf + 4;
1520 rdata->iov[1].iov_len = server->total_read - 4;
1521 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1522 rdata->iov[0].iov_base, server->total_read);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001523
1524 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001525 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001526 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001527 /* data_len is corrupt -- discard frame */
1528 rdata->result = -EIO;
1529 return cifs_readv_discard(server, mid);
1530 }
1531
Jeff Layton8321fec2012-09-19 06:22:32 -07001532 length = rdata->read_into_pages(server, rdata, data_len);
1533 if (length < 0)
1534 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535
Jeff Layton8321fec2012-09-19 06:22:32 -07001536 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001537
Joe Perchesf96637b2013-05-04 22:12:25 -05001538 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1539 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001540
1541 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001542 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001543 return cifs_readv_discard(server, mid);
1544
1545 dequeue_mid(mid, false);
Pavel Shilovsky350be252017-04-10 10:31:33 -07001546 mid->resp_buf = server->smallbuf;
1547 server->smallbuf = NULL;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001548 return length;
1549}
1550
1551static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001552cifs_readv_callback(struct mid_q_entry *mid)
1553{
1554 struct cifs_readdata *rdata = mid->callback_data;
1555 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1556 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001557 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1558 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001559 .rq_pages = rdata->pages,
1560 .rq_npages = rdata->nr_pages,
1561 .rq_pagesz = rdata->pagesz,
1562 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001563
Joe Perchesf96637b2013-05-04 22:12:25 -05001564 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1565 __func__, mid->mid, mid->mid_state, rdata->result,
1566 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001567
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001568 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001569 case MID_RESPONSE_RECEIVED:
1570 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001571 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001572 int rc = 0;
1573
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001574 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001575 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001576 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001577 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1578 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001579 }
1580 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001581 task_io_account_read(rdata->got_bytes);
1582 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001583 break;
1584 case MID_REQUEST_SUBMITTED:
1585 case MID_RETRY_NEEDED:
1586 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001587 if (server->sign && rdata->got_bytes)
1588 /* reset bytes number since we can not check a sign */
1589 rdata->got_bytes = 0;
1590 /* FIXME: should this be counted toward the initiating task? */
1591 task_io_account_read(rdata->got_bytes);
1592 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593 break;
1594 default:
1595 rdata->result = -EIO;
1596 }
1597
Jeff Laytonda472fc2012-03-23 14:40:53 -04001598 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001599 mutex_lock(&server->srv_mutex);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001600 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001601 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001602 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001603}
1604
1605/* cifs_async_readv - send an async write, and set up mid to handle result */
1606int
1607cifs_async_readv(struct cifs_readdata *rdata)
1608{
1609 int rc;
1610 READ_REQ *smb = NULL;
1611 int wct;
1612 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001613 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1614 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001615
Joe Perchesf96637b2013-05-04 22:12:25 -05001616 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1617 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001618
1619 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1620 wct = 12;
1621 else {
1622 wct = 10; /* old style read */
1623 if ((rdata->offset >> 32) > 0) {
1624 /* can not handle this big offset for old */
1625 return -EIO;
1626 }
1627 }
1628
1629 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1630 if (rc)
1631 return rc;
1632
1633 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1634 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1635
1636 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001637 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001638 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1639 if (wct == 12)
1640 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1641 smb->Remaining = 0;
1642 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1643 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1644 if (wct == 12)
1645 smb->ByteCount = 0;
1646 else {
1647 /* old style read */
1648 struct smb_com_readx_req *smbr =
1649 (struct smb_com_readx_req *)smb;
1650 smbr->ByteCount = 0;
1651 }
1652
1653 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001654 rdata->iov[0].iov_base = smb;
1655 rdata->iov[0].iov_len = 4;
1656 rdata->iov[1].iov_base = (char *)smb + 4;
1657 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001658
Jeff Layton6993f742012-05-16 07:13:17 -04001659 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001660 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08001661 cifs_readv_callback, NULL, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001662
1663 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001664 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001665 else
1666 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001667
1668 cifs_small_buf_release(smb);
1669 return rc;
1670}
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001673CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1674 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 int rc = -EACCES;
1677 READ_REQ *pSMB = NULL;
1678 READ_RSP *pSMBr = NULL;
1679 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001680 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001681 int resp_buf_type = 0;
1682 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001683 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001684 __u32 pid = io_parms->pid;
1685 __u16 netfid = io_parms->netfid;
1686 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001687 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001688 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Joe Perchesf96637b2013-05-04 22:12:25 -05001690 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001691 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001692 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001693 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001694 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001695 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001696 /* can not handle this big offset for old */
1697 return -EIO;
1698 }
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001702 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (rc)
1704 return rc;
1705
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001706 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1707 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* tcon and ses pointer are checked in smb_init */
1710 if (tcon->ses->server == NULL)
1711 return -ECONNABORTED;
1712
Steve Frenchec637e32005-12-12 20:53:18 -08001713 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001715 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001716 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001717 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 pSMB->Remaining = 0;
1720 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1721 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001722 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001723 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1724 else {
1725 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001726 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001727 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001728 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001729 }
Steve Frenchec637e32005-12-12 20:53:18 -08001730
1731 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001732 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001733 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1734 CIFS_LOG_ERROR, &rsp_iov);
1735 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001736 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001737 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001739 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 } else {
1741 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1742 data_length = data_length << 16;
1743 data_length += le16_to_cpu(pSMBr->DataLength);
1744 *nbytes = data_length;
1745
1746 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001747 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001749 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001750 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 rc = -EIO;
1752 *nbytes = 0;
1753 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001754 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001755 le16_to_cpu(pSMBr->DataOffset);
1756/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001757 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001758 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001759 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001760 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001761 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Steve French790fe572007-07-07 19:25:05 +00001765 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001766 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001767 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001768 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001769 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001770 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001771 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001772 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001773 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001774 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001775
1776 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 since file handle passed in no longer valid */
1778 return rc;
1779}
1780
Steve Frenchec637e32005-12-12 20:53:18 -08001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001783CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001784 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 int rc = -EACCES;
1787 WRITE_REQ *pSMB = NULL;
1788 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001789 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 __u32 bytes_sent;
1791 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001792 __u32 pid = io_parms->pid;
1793 __u16 netfid = io_parms->netfid;
1794 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001795 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001796 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Steve Frencha24e2d72010-04-03 17:20:21 +00001798 *nbytes = 0;
1799
Joe Perchesf96637b2013-05-04 22:12:25 -05001800 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001801 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001802 return -ECONNABORTED;
1803
Steve French790fe572007-07-07 19:25:05 +00001804 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001805 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001806 else {
Steve French1c955182005-08-30 20:58:07 -07001807 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001808 if ((offset >> 32) > 0) {
1809 /* can not handle big offset for old srv */
1810 return -EIO;
1811 }
1812 }
Steve French1c955182005-08-30 20:58:07 -07001813
1814 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 (void **) &pSMBr);
1816 if (rc)
1817 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001818
1819 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1820 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* tcon and ses pointer are checked in smb_init */
1823 if (tcon->ses->server == NULL)
1824 return -ECONNABORTED;
1825
1826 pSMB->AndXCommand = 0xFF; /* none */
1827 pSMB->Fid = netfid;
1828 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001829 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001830 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 pSMB->Reserved = 0xFFFFFFFF;
1833 pSMB->WriteMode = 0;
1834 pSMB->Remaining = 0;
1835
Steve French50c2f752007-07-13 00:33:32 +00001836 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 can send more if LARGE_WRITE_X capability returned by the server and if
1838 our buffer is big enough or if we convert to iovecs on socket writes
1839 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001840 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1842 } else {
1843 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1844 & ~0xFF;
1845 }
1846
1847 if (bytes_sent > count)
1848 bytes_sent = count;
1849 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001850 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001851 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001852 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04001853 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /* No buffer */
1855 cifs_buf_release(pSMB);
1856 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001857 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001858 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001859 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001860 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001861 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1864 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001865 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001866
Steve French790fe572007-07-07 19:25:05 +00001867 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001868 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001869 else { /* old style write has byte count 4 bytes earlier
1870 so 4 bytes pad */
1871 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001872 (struct smb_com_writex_req *)pSMB;
1873 pSMBW->ByteCount = cpu_to_le16(byte_count);
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04001877 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001878 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001880 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 } else {
1882 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1883 *nbytes = (*nbytes) << 16;
1884 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301885
1886 /*
1887 * Mask off high 16 bits when bytes written as returned by the
1888 * server is greater than bytes requested by the client. Some
1889 * OS/2 servers are known to set incorrect CountHigh values.
1890 */
1891 if (*nbytes > count)
1892 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
1895 cifs_buf_release(pSMB);
1896
Steve French50c2f752007-07-13 00:33:32 +00001897 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 since file handle passed in no longer valid */
1899
1900 return rc;
1901}
1902
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001903void
1904cifs_writedata_release(struct kref *refcount)
1905{
1906 struct cifs_writedata *wdata = container_of(refcount,
1907 struct cifs_writedata, refcount);
1908
1909 if (wdata->cfile)
1910 cifsFileInfo_put(wdata->cfile);
1911
1912 kfree(wdata);
1913}
1914
1915/*
1916 * Write failed with a retryable error. Resend the write request. It's also
1917 * possible that the page was redirtied so re-clean the page.
1918 */
1919static void
1920cifs_writev_requeue(struct cifs_writedata *wdata)
1921{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001922 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001923 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001924 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001925 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001926
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001927 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1928 i = 0;
1929 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001930 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001931 struct cifs_writedata *wdata2;
1932 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001933
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001934 wsize = server->ops->wp_retry_size(inode);
1935 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001936 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001937 if (!nr_pages) {
1938 rc = -ENOTSUPP;
1939 break;
1940 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001941 cur_len = nr_pages * PAGE_SIZE;
1942 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001943 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001944 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001945 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001946 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001947 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001948
1949 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1950 if (!wdata2) {
1951 rc = -ENOMEM;
1952 break;
1953 }
1954
1955 for (j = 0; j < nr_pages; j++) {
1956 wdata2->pages[j] = wdata->pages[i + j];
1957 lock_page(wdata2->pages[j]);
1958 clear_page_dirty_for_io(wdata2->pages[j]);
1959 }
1960
1961 wdata2->sync_mode = wdata->sync_mode;
1962 wdata2->nr_pages = nr_pages;
1963 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001964 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001965 wdata2->tailsz = tailsz;
1966 wdata2->bytes = cur_len;
1967
1968 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1969 if (!wdata2->cfile) {
1970 cifs_dbg(VFS, "No writable handles for inode\n");
1971 rc = -EBADF;
1972 break;
1973 }
1974 wdata2->pid = wdata2->cfile->pid;
1975 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1976
1977 for (j = 0; j < nr_pages; j++) {
1978 unlock_page(wdata2->pages[j]);
1979 if (rc != 0 && rc != -EAGAIN) {
1980 SetPageError(wdata2->pages[j]);
1981 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001982 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001983 }
1984 }
1985
1986 if (rc) {
1987 kref_put(&wdata2->refcount, cifs_writedata_release);
1988 if (rc == -EAGAIN)
1989 continue;
1990 break;
1991 }
1992
1993 rest_len -= cur_len;
1994 i += nr_pages;
1995 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001996
1997 mapping_set_error(inode->i_mapping, rc);
1998 kref_put(&wdata->refcount, cifs_writedata_release);
1999}
2000
Jeff Laytonc2e87642012-03-23 14:40:55 -04002001void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002002cifs_writev_complete(struct work_struct *work)
2003{
2004 struct cifs_writedata *wdata = container_of(work,
2005 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002006 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002007 int i = 0;
2008
2009 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002010 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002011 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002012 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002013 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2014 wdata->bytes);
2015 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2016 return cifs_writev_requeue(wdata);
2017
2018 for (i = 0; i < wdata->nr_pages; i++) {
2019 struct page *page = wdata->pages[i];
2020 if (wdata->result == -EAGAIN)
2021 __set_page_dirty_nobuffers(page);
2022 else if (wdata->result < 0)
2023 SetPageError(page);
2024 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002025 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002026 }
2027 if (wdata->result != -EAGAIN)
2028 mapping_set_error(inode->i_mapping, wdata->result);
2029 kref_put(&wdata->refcount, cifs_writedata_release);
2030}
2031
2032struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002033cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002034{
2035 struct cifs_writedata *wdata;
2036
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002037 /* writedata + number of page pointers */
2038 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002039 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002041 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002042 INIT_LIST_HEAD(&wdata->list);
2043 init_completion(&wdata->done);
2044 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002045 }
2046 return wdata;
2047}
2048
2049/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002050 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002051 * workqueue completion task.
2052 */
2053static void
2054cifs_writev_callback(struct mid_q_entry *mid)
2055{
2056 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002057 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002058 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002059 unsigned int written;
2060 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2061
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002062 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002063 case MID_RESPONSE_RECEIVED:
2064 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2065 if (wdata->result != 0)
2066 break;
2067
2068 written = le16_to_cpu(smb->CountHigh);
2069 written <<= 16;
2070 written += le16_to_cpu(smb->Count);
2071 /*
2072 * Mask off high 16 bits when bytes written as returned
2073 * by the server is greater than bytes requested by the
2074 * client. OS/2 servers are known to set incorrect
2075 * CountHigh values.
2076 */
2077 if (written > wdata->bytes)
2078 written &= 0xFFFF;
2079
2080 if (written < wdata->bytes)
2081 wdata->result = -ENOSPC;
2082 else
2083 wdata->bytes = written;
2084 break;
2085 case MID_REQUEST_SUBMITTED:
2086 case MID_RETRY_NEEDED:
2087 wdata->result = -EAGAIN;
2088 break;
2089 default:
2090 wdata->result = -EIO;
2091 break;
2092 }
2093
Jeff Laytonda472fc2012-03-23 14:40:53 -04002094 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002095 mutex_lock(&server->srv_mutex);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002096 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002097 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002098 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002099}
2100
2101/* cifs_async_writev - send an async write, and set up mid to handle result */
2102int
Steve French4a5c80d2014-02-07 20:45:12 -06002103cifs_async_writev(struct cifs_writedata *wdata,
2104 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002105{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002106 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002107 WRITE_REQ *smb = NULL;
2108 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002109 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002110 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002111 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002112
2113 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2114 wct = 14;
2115 } else {
2116 wct = 12;
2117 if (wdata->offset >> 32 > 0) {
2118 /* can not handle big offset for old srv */
2119 return -EIO;
2120 }
2121 }
2122
2123 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2124 if (rc)
2125 goto async_writev_out;
2126
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002127 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2128 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002129
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002130 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002131 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002132 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2133 if (wct == 14)
2134 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2135 smb->Reserved = 0xFFFFFFFF;
2136 smb->WriteMode = 0;
2137 smb->Remaining = 0;
2138
2139 smb->DataOffset =
2140 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2141
2142 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002143 iov[0].iov_len = 4;
2144 iov[0].iov_base = smb;
2145 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2146 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002147
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002148 rqst.rq_iov = iov;
2149 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002150 rqst.rq_pages = wdata->pages;
2151 rqst.rq_npages = wdata->nr_pages;
2152 rqst.rq_pagesz = wdata->pagesz;
2153 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002154
Joe Perchesf96637b2013-05-04 22:12:25 -05002155 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2156 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002157
2158 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2159 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2160
2161 if (wct == 14) {
2162 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2163 put_bcc(wdata->bytes + 1, &smb->hdr);
2164 } else {
2165 /* wct == 12 */
2166 struct smb_com_writex_req *smbw =
2167 (struct smb_com_writex_req *)smb;
2168 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2169 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002170 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002171 }
2172
2173 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002174 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002175 cifs_writev_callback, NULL, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002176
2177 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002178 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002179 else
Steve French4a5c80d2014-02-07 20:45:12 -06002180 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002181
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002182async_writev_out:
2183 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002184 return rc;
2185}
2186
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002187int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002188CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002189 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 int rc = -EACCES;
2192 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002193 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002194 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002195 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002196 __u32 pid = io_parms->pid;
2197 __u16 netfid = io_parms->netfid;
2198 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002199 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002200 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002201 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002203 *nbytes = 0;
2204
Joe Perchesf96637b2013-05-04 22:12:25 -05002205 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002206
Steve French4c3130e2008-12-09 00:28:16 +00002207 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002208 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002209 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002210 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002211 if ((offset >> 32) > 0) {
2212 /* can not handle big offset for old srv */
2213 return -EIO;
2214 }
2215 }
Steve French8cc64c62005-10-03 13:49:43 -07002216 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (rc)
2218 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002219
2220 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2221 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* tcon and ses pointer are checked in smb_init */
2224 if (tcon->ses->server == NULL)
2225 return -ECONNABORTED;
2226
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002227 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 pSMB->Fid = netfid;
2229 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002230 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002231 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 pSMB->Reserved = 0xFFFFFFFF;
2233 pSMB->WriteMode = 0;
2234 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002237 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Steve French3e844692005-10-03 13:37:24 -07002239 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2240 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002241 /* header + 1 byte pad */
2242 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002243 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002244 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002245 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002246 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002247 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002248 pSMB->ByteCount = cpu_to_le16(count + 1);
2249 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002250 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002251 (struct smb_com_writex_req *)pSMB;
2252 pSMBW->ByteCount = cpu_to_le16(count + 5);
2253 }
Steve French3e844692005-10-03 13:37:24 -07002254 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002255 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002256 iov[0].iov_len = smb_hdr_len + 4;
2257 else /* wct == 12 pad bigger by four bytes */
2258 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002259
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002260 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2261 &rsp_iov);
2262 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002263 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002265 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002266 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002267 /* presumably this can not happen, but best to be safe */
2268 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002269 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002270 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002271 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2272 *nbytes = (*nbytes) << 16;
2273 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302274
2275 /*
2276 * Mask off high 16 bits when bytes written as returned by the
2277 * server is greater than bytes requested by the client. OS/2
2278 * servers are known to set incorrect CountHigh values.
2279 */
2280 if (*nbytes > count)
2281 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002284 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Steve French50c2f752007-07-13 00:33:32 +00002286 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 since file handle passed in no longer valid */
2288
2289 return rc;
2290}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002291
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002292int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2293 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002294 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2295{
2296 int rc = 0;
2297 LOCK_REQ *pSMB = NULL;
2298 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002299 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002300 int resp_buf_type;
2301 __u16 count;
2302
Joe Perchesf96637b2013-05-04 22:12:25 -05002303 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2304 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002305
2306 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2307 if (rc)
2308 return rc;
2309
2310 pSMB->Timeout = 0;
2311 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2312 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2313 pSMB->LockType = lock_type;
2314 pSMB->AndXCommand = 0xFF; /* none */
2315 pSMB->Fid = netfid; /* netfid stays le */
2316
2317 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2318 inc_rfc1001_len(pSMB, count);
2319 pSMB->ByteCount = cpu_to_le16(count);
2320
2321 iov[0].iov_base = (char *)pSMB;
2322 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2323 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2324 iov[1].iov_base = (char *)buf;
2325 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2326
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002327 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002328 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2329 &rsp_iov);
2330 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002331 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002332 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002333
2334 return rc;
2335}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002338CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002339 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002341 const __u32 numLock, const __u8 lockType,
2342 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 int rc = 0;
2345 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002346/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002348 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 __u16 count;
2350
Joe Perchesf96637b2013-05-04 22:12:25 -05002351 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2352 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002353 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (rc)
2356 return rc;
2357
Steve French790fe572007-07-07 19:25:05 +00002358 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002359 /* no response expected */
2360 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002362 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002363 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2365 } else {
2366 pSMB->Timeout = 0;
2367 }
2368
2369 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2370 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2371 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002372 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 pSMB->AndXCommand = 0xFF; /* none */
2374 pSMB->Fid = smb_file_id; /* netfid stays le */
2375
Steve French790fe572007-07-07 19:25:05 +00002376 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002377 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /* BB where to store pid high? */
2379 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2380 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2381 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2382 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2383 count = sizeof(LOCKING_ANDX_RANGE);
2384 } else {
2385 /* oplock break */
2386 count = 0;
2387 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002388 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 pSMB->ByteCount = cpu_to_le16(count);
2390
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002391 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002392 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002393 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002394 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002395 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002396 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002397 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002398 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002399 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Steve French50c2f752007-07-13 00:33:32 +00002401 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 since file handle passed in no longer valid */
2403 return rc;
2404}
2405
2406int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002407CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002408 const __u16 smb_file_id, const __u32 netpid,
2409 const loff_t start_offset, const __u64 len,
2410 struct file_lock *pLockData, const __u16 lock_type,
2411 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002412{
2413 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2414 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002415 struct cifs_posix_lock *parm_data;
2416 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002417 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002418 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002419 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002420 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002421 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002422 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002423
Joe Perchesf96637b2013-05-04 22:12:25 -05002424 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002425
Steve French08547b02006-02-28 22:39:25 +00002426 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2427
2428 if (rc)
2429 return rc;
2430
2431 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2432
Steve French50c2f752007-07-13 00:33:32 +00002433 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002434 pSMB->MaxSetupCount = 0;
2435 pSMB->Reserved = 0;
2436 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002437 pSMB->Reserved2 = 0;
2438 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2439 offset = param_offset + params;
2440
Steve French08547b02006-02-28 22:39:25 +00002441 count = sizeof(struct cifs_posix_lock);
2442 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002443 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002444 pSMB->SetupCount = 1;
2445 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002446 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002447 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2448 else
2449 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2450 byte_count = 3 /* pad */ + params + count;
2451 pSMB->DataCount = cpu_to_le16(count);
2452 pSMB->ParameterCount = cpu_to_le16(params);
2453 pSMB->TotalDataCount = pSMB->DataCount;
2454 pSMB->TotalParameterCount = pSMB->ParameterCount;
2455 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002456 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002457 (((char *) &pSMB->hdr.Protocol) + offset);
2458
2459 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002460 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002461 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002462 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002463 pSMB->Timeout = cpu_to_le32(-1);
2464 } else
2465 pSMB->Timeout = 0;
2466
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002467 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002468 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002469 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002470
2471 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002472 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002473 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2474 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002475 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002476 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002477 if (waitFlag) {
2478 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2479 (struct smb_hdr *) pSMBr, &bytes_returned);
2480 } else {
Steve French133672e2007-11-13 22:41:37 +00002481 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002482 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002483 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002484 &resp_buf_type, timeout, &rsp_iov);
2485 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002486 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002487 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002488
Steve French08547b02006-02-28 22:39:25 +00002489 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002490 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002491 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002492 /* lock structure can be returned on get */
2493 __u16 data_offset;
2494 __u16 data_count;
2495 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002496
Jeff Layton820a8032011-05-04 08:05:26 -04002497 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002498 rc = -EIO; /* bad smb */
2499 goto plk_err_exit;
2500 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002501 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2502 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002503 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002504 rc = -EIO;
2505 goto plk_err_exit;
2506 }
2507 parm_data = (struct cifs_posix_lock *)
2508 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002509 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002510 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002511 else {
2512 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002513 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002514 pLockData->fl_type = F_RDLCK;
2515 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002516 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002517 pLockData->fl_type = F_WRLCK;
2518
Steve French5443d132011-03-13 05:08:25 +00002519 pLockData->fl_start = le64_to_cpu(parm_data->start);
2520 pLockData->fl_end = pLockData->fl_start +
2521 le64_to_cpu(parm_data->length) - 1;
2522 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002523 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002524 }
Steve French50c2f752007-07-13 00:33:32 +00002525
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002526plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002527 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002528
Steve French08547b02006-02-28 22:39:25 +00002529 /* Note: On -EAGAIN error only caller can retry on handle based calls
2530 since file handle passed in no longer valid */
2531
2532 return rc;
2533}
2534
2535
2536int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002537CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 int rc = 0;
2540 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002541 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543/* do not retry on dead session on close */
2544 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002545 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return 0;
2547 if (rc)
2548 return rc;
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002551 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002553 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002554 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002555 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002557 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002559 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 }
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002564 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 rc = 0;
2566
2567 return rc;
2568}
2569
2570int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002571CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002572{
2573 int rc = 0;
2574 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002575 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002576
2577 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2578 if (rc)
2579 return rc;
2580
2581 pSMB->FileID = (__u16) smb_file_id;
2582 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002583 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002584 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002585 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002586 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002587 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002588
2589 return rc;
2590}
2591
2592int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002593CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002594 const char *from_name, const char *to_name,
2595 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 int rc = 0;
2598 RENAME_REQ *pSMB = NULL;
2599 RENAME_RSP *pSMBr = NULL;
2600 int bytes_returned;
2601 int name_len, name_len2;
2602 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002603 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Joe Perchesf96637b2013-05-04 22:12:25 -05002605 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606renameRetry:
2607 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2608 (void **) &pSMBr);
2609 if (rc)
2610 return rc;
2611
2612 pSMB->BufferFormat = 0x04;
2613 pSMB->SearchAttributes =
2614 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2615 ATTR_DIRECTORY);
2616
2617 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002618 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2619 from_name, PATH_MAX,
2620 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 name_len++; /* trailing null */
2622 name_len *= 2;
2623 pSMB->OldFileName[name_len] = 0x04; /* pad */
2624 /* protocol requires ASCII signature byte on Unicode string */
2625 pSMB->OldFileName[name_len + 1] = 0x00;
2626 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002627 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002628 to_name, PATH_MAX, cifs_sb->local_nls,
2629 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2631 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002632 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002633 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002635 strncpy(pSMB->OldFileName, from_name, name_len);
2636 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 name_len2++; /* trailing null */
2638 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002639 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 name_len2++; /* trailing null */
2641 name_len2++; /* signature byte */
2642 }
2643
2644 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002645 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 pSMB->ByteCount = cpu_to_le16(count);
2647
2648 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2649 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002650 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002651 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002652 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 cifs_buf_release(pSMB);
2655
2656 if (rc == -EAGAIN)
2657 goto renameRetry;
2658
2659 return rc;
2660}
2661
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002662int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002663 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002664 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2667 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002668 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 char *data_offset;
2670 char dummy_string[30];
2671 int rc = 0;
2672 int bytes_returned = 0;
2673 int len_of_str;
2674 __u16 params, param_offset, offset, count, byte_count;
2675
Joe Perchesf96637b2013-05-04 22:12:25 -05002676 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2678 (void **) &pSMBr);
2679 if (rc)
2680 return rc;
2681
2682 params = 6;
2683 pSMB->MaxSetupCount = 0;
2684 pSMB->Reserved = 0;
2685 pSMB->Flags = 0;
2686 pSMB->Timeout = 0;
2687 pSMB->Reserved2 = 0;
2688 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2689 offset = param_offset + params;
2690
2691 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2692 rename_info = (struct set_file_rename *) data_offset;
2693 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002694 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 pSMB->SetupCount = 1;
2696 pSMB->Reserved3 = 0;
2697 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2698 byte_count = 3 /* pad */ + params;
2699 pSMB->ParameterCount = cpu_to_le16(params);
2700 pSMB->TotalParameterCount = pSMB->ParameterCount;
2701 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2702 pSMB->DataOffset = cpu_to_le16(offset);
2703 /* construct random name ".cifs_tmp<inodenum><mid>" */
2704 rename_info->overwrite = cpu_to_le32(1);
2705 rename_info->root_fid = 0;
2706 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002707 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002708 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002709 len_of_str =
2710 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002711 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002713 len_of_str =
2714 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002715 target_name, PATH_MAX, nls_codepage,
2716 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002719 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 byte_count += count;
2721 pSMB->DataCount = cpu_to_le16(count);
2722 pSMB->TotalDataCount = pSMB->DataCount;
2723 pSMB->Fid = netfid;
2724 pSMB->InformationLevel =
2725 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2726 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002727 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 pSMB->ByteCount = cpu_to_le16(byte_count);
2729 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002730 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002731 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002732 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002733 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2734 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 cifs_buf_release(pSMB);
2737
2738 /* Note: On -EAGAIN error only caller can retry on handle based calls
2739 since file handle passed in no longer valid */
2740
2741 return rc;
2742}
2743
2744int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002745CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2746 const char *fromName, const __u16 target_tid, const char *toName,
2747 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 int rc = 0;
2750 COPY_REQ *pSMB = NULL;
2751 COPY_RSP *pSMBr = NULL;
2752 int bytes_returned;
2753 int name_len, name_len2;
2754 __u16 count;
2755
Joe Perchesf96637b2013-05-04 22:12:25 -05002756 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757copyRetry:
2758 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2759 (void **) &pSMBr);
2760 if (rc)
2761 return rc;
2762
2763 pSMB->BufferFormat = 0x04;
2764 pSMB->Tid2 = target_tid;
2765
2766 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2767
2768 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002769 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2770 fromName, PATH_MAX, nls_codepage,
2771 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 name_len++; /* trailing null */
2773 name_len *= 2;
2774 pSMB->OldFileName[name_len] = 0x04; /* pad */
2775 /* protocol requires ASCII signature byte on Unicode string */
2776 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002777 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002778 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2779 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2781 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002782 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 name_len = strnlen(fromName, PATH_MAX);
2784 name_len++; /* trailing null */
2785 strncpy(pSMB->OldFileName, fromName, name_len);
2786 name_len2 = strnlen(toName, PATH_MAX);
2787 name_len2++; /* trailing null */
2788 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2789 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2790 name_len2++; /* trailing null */
2791 name_len2++; /* signature byte */
2792 }
2793
2794 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002795 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 pSMB->ByteCount = cpu_to_le16(count);
2797
2798 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2799 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2800 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002801 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2802 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
Steve French0d817bc2008-05-22 02:02:03 +00002804 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 if (rc == -EAGAIN)
2807 goto copyRetry;
2808
2809 return rc;
2810}
2811
2812int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002813CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002815 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 TRANSACTION2_SPI_REQ *pSMB = NULL;
2818 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2819 char *data_offset;
2820 int name_len;
2821 int name_len_target;
2822 int rc = 0;
2823 int bytes_returned = 0;
2824 __u16 params, param_offset, offset, byte_count;
2825
Joe Perchesf96637b2013-05-04 22:12:25 -05002826 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827createSymLinkRetry:
2828 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2829 (void **) &pSMBr);
2830 if (rc)
2831 return rc;
2832
2833 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2834 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002835 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2836 /* find define for this maxpathcomponent */
2837 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 name_len++; /* trailing null */
2839 name_len *= 2;
2840
Steve French50c2f752007-07-13 00:33:32 +00002841 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 name_len = strnlen(fromName, PATH_MAX);
2843 name_len++; /* trailing null */
2844 strncpy(pSMB->FileName, fromName, name_len);
2845 }
2846 params = 6 + name_len;
2847 pSMB->MaxSetupCount = 0;
2848 pSMB->Reserved = 0;
2849 pSMB->Flags = 0;
2850 pSMB->Timeout = 0;
2851 pSMB->Reserved2 = 0;
2852 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002853 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 offset = param_offset + params;
2855
2856 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2857 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2858 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002859 cifsConvertToUTF16((__le16 *) data_offset, toName,
2860 /* find define for this maxpathcomponent */
2861 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 name_len_target++; /* trailing null */
2863 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002864 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 name_len_target = strnlen(toName, PATH_MAX);
2866 name_len_target++; /* trailing null */
2867 strncpy(data_offset, toName, name_len_target);
2868 }
2869
2870 pSMB->MaxParameterCount = cpu_to_le16(2);
2871 /* BB find exact max on data count below from sess */
2872 pSMB->MaxDataCount = cpu_to_le16(1000);
2873 pSMB->SetupCount = 1;
2874 pSMB->Reserved3 = 0;
2875 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2876 byte_count = 3 /* pad */ + params + name_len_target;
2877 pSMB->DataCount = cpu_to_le16(name_len_target);
2878 pSMB->ParameterCount = cpu_to_le16(params);
2879 pSMB->TotalDataCount = pSMB->DataCount;
2880 pSMB->TotalParameterCount = pSMB->ParameterCount;
2881 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2882 pSMB->DataOffset = cpu_to_le16(offset);
2883 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2884 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002885 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 pSMB->ByteCount = cpu_to_le16(byte_count);
2887 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2888 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002889 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002890 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002891 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2892 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Steve French0d817bc2008-05-22 02:02:03 +00002894 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 if (rc == -EAGAIN)
2897 goto createSymLinkRetry;
2898
2899 return rc;
2900}
2901
2902int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002903CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002905 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 TRANSACTION2_SPI_REQ *pSMB = NULL;
2908 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2909 char *data_offset;
2910 int name_len;
2911 int name_len_target;
2912 int rc = 0;
2913 int bytes_returned = 0;
2914 __u16 params, param_offset, offset, byte_count;
2915
Joe Perchesf96637b2013-05-04 22:12:25 -05002916 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917createHardLinkRetry:
2918 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2919 (void **) &pSMBr);
2920 if (rc)
2921 return rc;
2922
2923 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002924 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2925 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 name_len++; /* trailing null */
2927 name_len *= 2;
2928
Steve French50c2f752007-07-13 00:33:32 +00002929 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 name_len = strnlen(toName, PATH_MAX);
2931 name_len++; /* trailing null */
2932 strncpy(pSMB->FileName, toName, name_len);
2933 }
2934 params = 6 + name_len;
2935 pSMB->MaxSetupCount = 0;
2936 pSMB->Reserved = 0;
2937 pSMB->Flags = 0;
2938 pSMB->Timeout = 0;
2939 pSMB->Reserved2 = 0;
2940 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002941 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 offset = param_offset + params;
2943
2944 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2945 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2946 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002947 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2948 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 name_len_target++; /* trailing null */
2950 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002951 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 name_len_target = strnlen(fromName, PATH_MAX);
2953 name_len_target++; /* trailing null */
2954 strncpy(data_offset, fromName, name_len_target);
2955 }
2956
2957 pSMB->MaxParameterCount = cpu_to_le16(2);
2958 /* BB find exact max on data count below from sess*/
2959 pSMB->MaxDataCount = cpu_to_le16(1000);
2960 pSMB->SetupCount = 1;
2961 pSMB->Reserved3 = 0;
2962 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2963 byte_count = 3 /* pad */ + params + name_len_target;
2964 pSMB->ParameterCount = cpu_to_le16(params);
2965 pSMB->TotalParameterCount = pSMB->ParameterCount;
2966 pSMB->DataCount = cpu_to_le16(name_len_target);
2967 pSMB->TotalDataCount = pSMB->DataCount;
2968 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2969 pSMB->DataOffset = cpu_to_le16(offset);
2970 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2971 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002972 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 pSMB->ByteCount = cpu_to_le16(byte_count);
2974 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2975 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002976 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002977 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002978 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2979 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 cifs_buf_release(pSMB);
2982 if (rc == -EAGAIN)
2983 goto createHardLinkRetry;
2984
2985 return rc;
2986}
2987
2988int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002989CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002990 const char *from_name, const char *to_name,
2991 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 int rc = 0;
2994 NT_RENAME_REQ *pSMB = NULL;
2995 RENAME_RSP *pSMBr = NULL;
2996 int bytes_returned;
2997 int name_len, name_len2;
2998 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002999 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Joe Perchesf96637b2013-05-04 22:12:25 -05003001 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002winCreateHardLinkRetry:
3003
3004 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3005 (void **) &pSMBr);
3006 if (rc)
3007 return rc;
3008
3009 pSMB->SearchAttributes =
3010 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3011 ATTR_DIRECTORY);
3012 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3013 pSMB->ClusterCount = 0;
3014
3015 pSMB->BufferFormat = 0x04;
3016
3017 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3018 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003019 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3020 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 name_len++; /* trailing null */
3022 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003023
3024 /* protocol specifies ASCII buffer format (0x04) for unicode */
3025 pSMB->OldFileName[name_len] = 0x04;
3026 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003028 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003029 to_name, PATH_MAX, cifs_sb->local_nls,
3030 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3032 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003033 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003034 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003036 strncpy(pSMB->OldFileName, from_name, name_len);
3037 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 name_len2++; /* trailing null */
3039 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003040 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 name_len2++; /* trailing null */
3042 name_len2++; /* signature byte */
3043 }
3044
3045 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003046 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 pSMB->ByteCount = cpu_to_le16(count);
3048
3049 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3050 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003051 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003052 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003053 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 cifs_buf_release(pSMB);
3056 if (rc == -EAGAIN)
3057 goto winCreateHardLinkRetry;
3058
3059 return rc;
3060}
3061
3062int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003063CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003064 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003065 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067/* SMB_QUERY_FILE_UNIX_LINK */
3068 TRANSACTION2_QPI_REQ *pSMB = NULL;
3069 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3070 int rc = 0;
3071 int bytes_returned;
3072 int name_len;
3073 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003074 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Joe Perchesf96637b2013-05-04 22:12:25 -05003076 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078querySymLinkRetry:
3079 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3080 (void **) &pSMBr);
3081 if (rc)
3082 return rc;
3083
3084 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3085 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003086 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3087 searchName, PATH_MAX, nls_codepage,
3088 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 name_len++; /* trailing null */
3090 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003091 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 name_len = strnlen(searchName, PATH_MAX);
3093 name_len++; /* trailing null */
3094 strncpy(pSMB->FileName, searchName, name_len);
3095 }
3096
3097 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3098 pSMB->TotalDataCount = 0;
3099 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003100 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 pSMB->MaxSetupCount = 0;
3102 pSMB->Reserved = 0;
3103 pSMB->Flags = 0;
3104 pSMB->Timeout = 0;
3105 pSMB->Reserved2 = 0;
3106 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003107 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 pSMB->DataCount = 0;
3109 pSMB->DataOffset = 0;
3110 pSMB->SetupCount = 1;
3111 pSMB->Reserved3 = 0;
3112 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3113 byte_count = params + 1 /* pad */ ;
3114 pSMB->TotalParameterCount = cpu_to_le16(params);
3115 pSMB->ParameterCount = pSMB->TotalParameterCount;
3116 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3117 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003118 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 pSMB->ByteCount = cpu_to_le16(byte_count);
3120
3121 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3122 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3123 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003124 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 } else {
3126 /* decode response */
3127
3128 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003130 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003131 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003133 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003134 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Jeff Layton460b9692009-04-30 07:17:56 -04003136 data_start = ((char *) &pSMBr->hdr.Protocol) +
3137 le16_to_cpu(pSMBr->t2.DataOffset);
3138
Steve French0e0d2cf2009-05-01 05:27:32 +00003139 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3140 is_unicode = true;
3141 else
3142 is_unicode = false;
3143
Steve French737b7582005-04-28 22:41:06 -07003144 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003145 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3146 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003147 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003148 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
3150 }
3151 cifs_buf_release(pSMB);
3152 if (rc == -EAGAIN)
3153 goto querySymLinkRetry;
3154 return rc;
3155}
3156
Steve Frenchc52a95542011-02-24 06:16:22 +00003157/*
3158 * Recent Windows versions now create symlinks more frequently
3159 * and they use the "reparse point" mechanism below. We can of course
3160 * do symlinks nicely to Samba and other servers which support the
3161 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3162 * "MF" symlinks optionally, but for recent Windows we really need to
3163 * reenable the code below and fix the cifs_symlink callers to handle this.
3164 * In the interim this code has been moved to its own config option so
3165 * it is not compiled in by default until callers fixed up and more tested.
3166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003168CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3169 __u16 fid, char **symlinkinfo,
3170 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 int rc = 0;
3173 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003174 struct smb_com_transaction_ioctl_req *pSMB;
3175 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003176 bool is_unicode;
3177 unsigned int sub_len;
3178 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003179 struct reparse_symlink_data *reparse_buf;
3180 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003181 __u32 data_offset, data_count;
3182 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003184 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3186 (void **) &pSMBr);
3187 if (rc)
3188 return rc;
3189
3190 pSMB->TotalParameterCount = 0 ;
3191 pSMB->TotalDataCount = 0;
3192 pSMB->MaxParameterCount = cpu_to_le32(2);
3193 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003194 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 pSMB->MaxSetupCount = 4;
3196 pSMB->Reserved = 0;
3197 pSMB->ParameterOffset = 0;
3198 pSMB->DataCount = 0;
3199 pSMB->DataOffset = 0;
3200 pSMB->SetupCount = 4;
3201 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3202 pSMB->ParameterCount = pSMB->TotalParameterCount;
3203 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3204 pSMB->IsFsctl = 1; /* FSCTL */
3205 pSMB->IsRootFlag = 0;
3206 pSMB->Fid = fid; /* file handle always le */
3207 pSMB->ByteCount = 0;
3208
3209 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3210 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3211 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003212 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003213 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Steve French989c7e52009-05-02 05:32:20 +00003215
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003216 data_offset = le32_to_cpu(pSMBr->DataOffset);
3217 data_count = le32_to_cpu(pSMBr->DataCount);
3218 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3219 /* BB also check enough total bytes returned */
3220 rc = -EIO; /* bad smb */
3221 goto qreparse_out;
3222 }
3223 if (!data_count || (data_count > 2048)) {
3224 rc = -EIO;
3225 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3226 goto qreparse_out;
3227 }
3228 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003229 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003230 ((char *)&pSMBr->hdr.Protocol + data_offset);
3231 if ((char *)reparse_buf >= end_of_smb) {
3232 rc = -EIO;
3233 goto qreparse_out;
3234 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003235 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3236 cifs_dbg(FYI, "NFS style reparse tag\n");
3237 posix_buf = (struct reparse_posix_data *)reparse_buf;
3238
3239 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3240 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3241 le64_to_cpu(posix_buf->InodeType));
3242 rc = -EOPNOTSUPP;
3243 goto qreparse_out;
3244 }
3245 is_unicode = true;
3246 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3247 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3248 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3249 rc = -EIO;
3250 goto qreparse_out;
3251 }
3252 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3253 sub_len, is_unicode, nls_codepage);
3254 goto qreparse_out;
3255 } else if (reparse_buf->ReparseTag !=
3256 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3257 rc = -EOPNOTSUPP;
3258 goto qreparse_out;
3259 }
3260
3261 /* Reparse tag is NTFS symlink */
3262 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3263 reparse_buf->PathBuffer;
3264 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3265 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003266 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3267 rc = -EIO;
3268 goto qreparse_out;
3269 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003270 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3271 is_unicode = true;
3272 else
3273 is_unicode = false;
3274
3275 /* BB FIXME investigate remapping reserved chars here */
3276 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3277 nls_codepage);
3278 if (!*symlinkinfo)
3279 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003281 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003283 /*
3284 * Note: On -EAGAIN error only caller can retry on handle based calls
3285 * since file handle passed in no longer valid.
3286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return rc;
3288}
3289
Steve Frenchc7f508a2013-10-14 15:27:32 -05003290int
3291CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3292 __u16 fid)
3293{
3294 int rc = 0;
3295 int bytes_returned;
3296 struct smb_com_transaction_compr_ioctl_req *pSMB;
3297 struct smb_com_transaction_ioctl_rsp *pSMBr;
3298
3299 cifs_dbg(FYI, "Set compression for %u\n", fid);
3300 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3301 (void **) &pSMBr);
3302 if (rc)
3303 return rc;
3304
3305 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3306
3307 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003308 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003309 pSMB->MaxParameterCount = 0;
3310 pSMB->MaxDataCount = 0;
3311 pSMB->MaxSetupCount = 4;
3312 pSMB->Reserved = 0;
3313 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003314 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003315 pSMB->DataOffset =
3316 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3317 compression_state) - 4); /* 84 */
3318 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003319 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003320 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003321 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003322 pSMB->IsFsctl = 1; /* FSCTL */
3323 pSMB->IsRootFlag = 0;
3324 pSMB->Fid = fid; /* file handle always le */
3325 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003326 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003327 inc_rfc1001_len(pSMB, 5);
3328
3329 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3330 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3331 if (rc)
3332 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3333
3334 cifs_buf_release(pSMB);
3335
3336 /*
3337 * Note: On -EAGAIN error only caller can retry on handle based calls
3338 * since file handle passed in no longer valid.
3339 */
3340 return rc;
3341}
3342
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344#ifdef CONFIG_CIFS_POSIX
3345
3346/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003347static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003348 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003351 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3352 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3353 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003354/*
3355 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3356 ace->e_perm, ace->e_tag, ace->e_id);
3357*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 return;
3360}
3361
3362/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003363static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3364 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 int size = 0;
3367 int i;
3368 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003369 struct cifs_posix_ace *pACE;
3370 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003371 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3374 return -EOPNOTSUPP;
3375
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003376 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 count = le16_to_cpu(cifs_acl->access_entry_count);
3378 pACE = &cifs_acl->ace_array[0];
3379 size = sizeof(struct cifs_posix_acl);
3380 size += sizeof(struct cifs_posix_ace) * count;
3381 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003382 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003383 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3384 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return -EINVAL;
3386 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003387 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 count = le16_to_cpu(cifs_acl->access_entry_count);
3389 size = sizeof(struct cifs_posix_acl);
3390 size += sizeof(struct cifs_posix_ace) * count;
3391/* skip past access ACEs to get to default ACEs */
3392 pACE = &cifs_acl->ace_array[count];
3393 count = le16_to_cpu(cifs_acl->default_entry_count);
3394 size += sizeof(struct cifs_posix_ace) * count;
3395 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003396 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return -EINVAL;
3398 } else {
3399 /* illegal type */
3400 return -EINVAL;
3401 }
3402
3403 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003404 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003405 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003406 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return -ERANGE;
3408 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003409 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3410
Steve Frenchff7feac2005-11-15 16:45:16 -08003411 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003412 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003413 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003414 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416 }
3417 return size;
3418}
3419
Steve French50c2f752007-07-13 00:33:32 +00003420static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003421 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
3423 __u16 rc = 0; /* 0 = ACL converted ok */
3424
Steve Frenchff7feac2005-11-15 16:45:16 -08003425 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3426 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003428 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 /* Probably no need to le convert -1 on any arch but can not hurt */
3430 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003431 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003432 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003433/*
3434 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3435 ace->e_perm, ace->e_tag, ace->e_id);
3436*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return rc;
3438}
3439
3440/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003441static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3442 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
3444 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003445 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003446 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003447 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 int count;
3449 int i;
3450
Steve French790fe572007-07-07 19:25:05 +00003451 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return 0;
3453
3454 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003455 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3456 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003457 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003458 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3459 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return 0;
3461 }
3462 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003463 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003464 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003465 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003466 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003467 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003468 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003469 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003470 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return 0;
3472 }
Steve French50c2f752007-07-13 00:33:32 +00003473 for (i = 0; i < count; i++) {
Eryu Guanae9ebe72016-10-24 20:46:40 +08003474 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003475 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 /* ACE not converted */
3477 break;
3478 }
3479 }
Steve French790fe572007-07-07 19:25:05 +00003480 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3482 rc += sizeof(struct cifs_posix_acl);
3483 /* BB add check to make sure ACL does not overflow SMB */
3484 }
3485 return rc;
3486}
3487
3488int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003489CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003490 const unsigned char *searchName,
3491 char *acl_inf, const int buflen, const int acl_type,
3492 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494/* SMB_QUERY_POSIX_ACL */
3495 TRANSACTION2_QPI_REQ *pSMB = NULL;
3496 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3497 int rc = 0;
3498 int bytes_returned;
3499 int name_len;
3500 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003501
Joe Perchesf96637b2013-05-04 22:12:25 -05003502 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504queryAclRetry:
3505 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3506 (void **) &pSMBr);
3507 if (rc)
3508 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3511 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003512 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3513 searchName, PATH_MAX, nls_codepage,
3514 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 name_len++; /* trailing null */
3516 name_len *= 2;
3517 pSMB->FileName[name_len] = 0;
3518 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003519 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 name_len = strnlen(searchName, PATH_MAX);
3521 name_len++; /* trailing null */
3522 strncpy(pSMB->FileName, searchName, name_len);
3523 }
3524
3525 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3526 pSMB->TotalDataCount = 0;
3527 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003528 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 pSMB->MaxDataCount = cpu_to_le16(4000);
3530 pSMB->MaxSetupCount = 0;
3531 pSMB->Reserved = 0;
3532 pSMB->Flags = 0;
3533 pSMB->Timeout = 0;
3534 pSMB->Reserved2 = 0;
3535 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003536 offsetof(struct smb_com_transaction2_qpi_req,
3537 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 pSMB->DataCount = 0;
3539 pSMB->DataOffset = 0;
3540 pSMB->SetupCount = 1;
3541 pSMB->Reserved3 = 0;
3542 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3543 byte_count = params + 1 /* pad */ ;
3544 pSMB->TotalParameterCount = cpu_to_le16(params);
3545 pSMB->ParameterCount = pSMB->TotalParameterCount;
3546 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3547 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003548 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 pSMB->ByteCount = cpu_to_le16(byte_count);
3550
3551 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3552 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003553 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003555 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 } else {
3557 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003561 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 rc = -EIO; /* bad smb */
3563 else {
3564 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3565 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3566 rc = cifs_copy_posix_acl(acl_inf,
3567 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003568 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570 }
3571 cifs_buf_release(pSMB);
3572 if (rc == -EAGAIN)
3573 goto queryAclRetry;
3574 return rc;
3575}
3576
3577int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003578CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003579 const unsigned char *fileName,
3580 const char *local_acl, const int buflen,
3581 const int acl_type,
3582 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
3584 struct smb_com_transaction2_spi_req *pSMB = NULL;
3585 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3586 char *parm_data;
3587 int name_len;
3588 int rc = 0;
3589 int bytes_returned = 0;
3590 __u16 params, byte_count, data_count, param_offset, offset;
3591
Joe Perchesf96637b2013-05-04 22:12:25 -05003592 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593setAclRetry:
3594 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003595 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 if (rc)
3597 return rc;
3598 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3599 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003600 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3601 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 name_len++; /* trailing null */
3603 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003604 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 name_len = strnlen(fileName, PATH_MAX);
3606 name_len++; /* trailing null */
3607 strncpy(pSMB->FileName, fileName, name_len);
3608 }
3609 params = 6 + name_len;
3610 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003611 /* BB find max SMB size from sess */
3612 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 pSMB->MaxSetupCount = 0;
3614 pSMB->Reserved = 0;
3615 pSMB->Flags = 0;
3616 pSMB->Timeout = 0;
3617 pSMB->Reserved2 = 0;
3618 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003619 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 offset = param_offset + params;
3621 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3622 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3623
3624 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003625 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Steve French790fe572007-07-07 19:25:05 +00003627 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 rc = -EOPNOTSUPP;
3629 goto setACLerrorExit;
3630 }
3631 pSMB->DataOffset = cpu_to_le16(offset);
3632 pSMB->SetupCount = 1;
3633 pSMB->Reserved3 = 0;
3634 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3635 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3636 byte_count = 3 /* pad */ + params + data_count;
3637 pSMB->DataCount = cpu_to_le16(data_count);
3638 pSMB->TotalDataCount = pSMB->DataCount;
3639 pSMB->ParameterCount = cpu_to_le16(params);
3640 pSMB->TotalParameterCount = pSMB->ParameterCount;
3641 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003642 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 pSMB->ByteCount = cpu_to_le16(byte_count);
3644 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003645 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003646 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003647 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649setACLerrorExit:
3650 cifs_buf_release(pSMB);
3651 if (rc == -EAGAIN)
3652 goto setAclRetry;
3653 return rc;
3654}
3655
Steve Frenchf654bac2005-04-28 22:41:04 -07003656/* BB fix tabs in this function FIXME BB */
3657int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003658CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003659 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003660{
Steve French50c2f752007-07-13 00:33:32 +00003661 int rc = 0;
3662 struct smb_t2_qfi_req *pSMB = NULL;
3663 struct smb_t2_qfi_rsp *pSMBr = NULL;
3664 int bytes_returned;
3665 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003666
Joe Perchesf96637b2013-05-04 22:12:25 -05003667 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003668 if (tcon == NULL)
3669 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003670
3671GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003672 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3673 (void **) &pSMBr);
3674 if (rc)
3675 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003676
Steve Frenchad7a2922008-02-07 23:25:02 +00003677 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003678 pSMB->t2.TotalDataCount = 0;
3679 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3680 /* BB find exact max data count below from sess structure BB */
3681 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3682 pSMB->t2.MaxSetupCount = 0;
3683 pSMB->t2.Reserved = 0;
3684 pSMB->t2.Flags = 0;
3685 pSMB->t2.Timeout = 0;
3686 pSMB->t2.Reserved2 = 0;
3687 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3688 Fid) - 4);
3689 pSMB->t2.DataCount = 0;
3690 pSMB->t2.DataOffset = 0;
3691 pSMB->t2.SetupCount = 1;
3692 pSMB->t2.Reserved3 = 0;
3693 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3694 byte_count = params + 1 /* pad */ ;
3695 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3696 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3697 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3698 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003699 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003700 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003701 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003702
Steve French790fe572007-07-07 19:25:05 +00003703 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3704 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3705 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003706 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003707 } else {
3708 /* decode response */
3709 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003710 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003711 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003712 /* If rc should we check for EOPNOSUPP and
3713 disable the srvino flag? or in caller? */
3714 rc = -EIO; /* bad smb */
3715 else {
3716 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3717 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3718 struct file_chattr_info *pfinfo;
3719 /* BB Do we need a cast or hash here ? */
3720 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003721 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003722 rc = -EIO;
3723 goto GetExtAttrOut;
3724 }
3725 pfinfo = (struct file_chattr_info *)
3726 (data_offset + (char *) &pSMBr->hdr.Protocol);
3727 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003728 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003729 }
3730 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003731GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003732 cifs_buf_release(pSMB);
3733 if (rc == -EAGAIN)
3734 goto GetExtAttrRetry;
3735 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003736}
3737
Steve Frenchf654bac2005-04-28 22:41:04 -07003738#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Jeff Layton79df1ba2010-12-06 12:52:08 -05003740#ifdef CONFIG_CIFS_ACL
3741/*
3742 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3743 * all NT TRANSACTS that we init here have total parm and data under about 400
3744 * bytes (to fit in small cifs buffer size), which is the case so far, it
3745 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3746 * returned setup area) and MaxParameterCount (returned parms size) must be set
3747 * by caller
3748 */
3749static int
3750smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003751 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003752 void **ret_buf)
3753{
3754 int rc;
3755 __u32 temp_offset;
3756 struct smb_com_ntransact_req *pSMB;
3757
3758 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3759 (void **)&pSMB);
3760 if (rc)
3761 return rc;
3762 *ret_buf = (void *)pSMB;
3763 pSMB->Reserved = 0;
3764 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3765 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003766 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003767 pSMB->ParameterCount = pSMB->TotalParameterCount;
3768 pSMB->DataCount = pSMB->TotalDataCount;
3769 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3770 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3771 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3772 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3773 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3774 pSMB->SubCommand = cpu_to_le16(sub_command);
3775 return 0;
3776}
3777
3778static int
3779validate_ntransact(char *buf, char **ppparm, char **ppdata,
3780 __u32 *pparmlen, __u32 *pdatalen)
3781{
3782 char *end_of_smb;
3783 __u32 data_count, data_offset, parm_count, parm_offset;
3784 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003785 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003786
3787 *pdatalen = 0;
3788 *pparmlen = 0;
3789
3790 if (buf == NULL)
3791 return -EINVAL;
3792
3793 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3794
Jeff Layton820a8032011-05-04 08:05:26 -04003795 bcc = get_bcc(&pSMBr->hdr);
3796 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003797 (char *)&pSMBr->ByteCount;
3798
3799 data_offset = le32_to_cpu(pSMBr->DataOffset);
3800 data_count = le32_to_cpu(pSMBr->DataCount);
3801 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3802 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3803
3804 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3805 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3806
3807 /* should we also check that parm and data areas do not overlap? */
3808 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003809 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003810 return -EINVAL;
3811 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003812 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003813 return -EINVAL;
3814 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003815 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003816 return -EINVAL;
3817 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003818 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3819 *ppdata, data_count, (data_count + *ppdata),
3820 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003821 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003822 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003823 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003824 return -EINVAL;
3825 }
3826 *pdatalen = data_count;
3827 *pparmlen = parm_count;
3828 return 0;
3829}
3830
Steve French0a4b92c2006-01-12 15:44:21 -08003831/* Get Security Descriptor (by handle) from remote server for a file or dir */
3832int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003833CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003834 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003835{
3836 int rc = 0;
3837 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003838 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003839 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003840 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003841
Joe Perchesf96637b2013-05-04 22:12:25 -05003842 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003843
Steve French630f3f0c2007-10-25 21:17:17 +00003844 *pbuflen = 0;
3845 *acl_inf = NULL;
3846
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003847 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003848 8 /* parm len */, tcon, (void **) &pSMB);
3849 if (rc)
3850 return rc;
3851
3852 pSMB->MaxParameterCount = cpu_to_le32(4);
3853 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3854 pSMB->MaxSetupCount = 0;
3855 pSMB->Fid = fid; /* file handle always le */
3856 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3857 CIFS_ACL_DACL);
3858 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003859 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003860 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003861 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003862
Steve Frencha761ac52007-10-18 21:45:27 +00003863 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003864 0, &rsp_iov);
3865 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003866 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003867 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003868 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003869 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003870 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003871 __u32 parm_len;
3872 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003873 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003874 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003875
3876/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003877 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003878 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003879 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003880 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003881 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08003882
Joe Perchesf96637b2013-05-04 22:12:25 -05003883 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3884 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003885
3886 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3887 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003888 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003889 goto qsec_out;
3890 }
3891
3892/* BB check that data area is minimum length and as big as acl_len */
3893
Steve Frenchaf6f4612007-10-16 18:40:37 +00003894 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003895 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003896 cifs_dbg(VFS, "acl length %d does not match %d\n",
3897 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003898 if (*pbuflen > acl_len)
3899 *pbuflen = acl_len;
3900 }
Steve French0a4b92c2006-01-12 15:44:21 -08003901
Steve French630f3f0c2007-10-25 21:17:17 +00003902 /* check if buffer is big enough for the acl
3903 header followed by the smallest SID */
3904 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3905 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003906 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003907 rc = -EINVAL;
3908 *pbuflen = 0;
3909 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003910 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003911 if (*acl_inf == NULL) {
3912 *pbuflen = 0;
3913 rc = -ENOMEM;
3914 }
Steve French630f3f0c2007-10-25 21:17:17 +00003915 }
Steve French0a4b92c2006-01-12 15:44:21 -08003916 }
3917qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003918 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08003919 return rc;
3920}
Steve French97837582007-12-31 07:47:21 +00003921
3922int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003923CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003924 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003925{
3926 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3927 int rc = 0;
3928 int bytes_returned = 0;
3929 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003930 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003931
3932setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003933 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003934 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003935 return rc;
Steve French97837582007-12-31 07:47:21 +00003936
3937 pSMB->MaxSetupCount = 0;
3938 pSMB->Reserved = 0;
3939
3940 param_count = 8;
3941 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3942 data_count = acllen;
3943 data_offset = param_offset + param_count;
3944 byte_count = 3 /* pad */ + param_count;
3945
3946 pSMB->DataCount = cpu_to_le32(data_count);
3947 pSMB->TotalDataCount = pSMB->DataCount;
3948 pSMB->MaxParameterCount = cpu_to_le32(4);
3949 pSMB->MaxDataCount = cpu_to_le32(16384);
3950 pSMB->ParameterCount = cpu_to_le32(param_count);
3951 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3952 pSMB->TotalParameterCount = pSMB->ParameterCount;
3953 pSMB->DataOffset = cpu_to_le32(data_offset);
3954 pSMB->SetupCount = 0;
3955 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3956 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3957
3958 pSMB->Fid = fid; /* file handle always le */
3959 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003960 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003961
3962 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003963 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3964 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003965 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003966 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003967 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003968
3969 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3970 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3971
Joe Perchesf96637b2013-05-04 22:12:25 -05003972 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3973 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003974 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003975 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003976 cifs_buf_release(pSMB);
3977
3978 if (rc == -EAGAIN)
3979 goto setCifsAclRetry;
3980
3981 return (rc);
3982}
3983
Jeff Layton79df1ba2010-12-06 12:52:08 -05003984#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003985
Steve French6b8edfe2005-08-23 20:26:03 -07003986/* Legacy Query Path Information call for lookup to old servers such
3987 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003988int
3989SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3990 const char *search_name, FILE_ALL_INFO *data,
3991 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003992{
Steve Frenchad7a2922008-02-07 23:25:02 +00003993 QUERY_INFORMATION_REQ *pSMB;
3994 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003995 int rc = 0;
3996 int bytes_returned;
3997 int name_len;
3998
Joe Perchesf96637b2013-05-04 22:12:25 -05003999 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07004000QInfRetry:
4001 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004002 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07004003 if (rc)
4004 return rc;
4005
4006 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4007 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004008 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004009 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004010 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004011 name_len++; /* trailing null */
4012 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004013 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004014 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004015 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004016 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004017 }
4018 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004019 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004020 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004021 pSMB->ByteCount = cpu_to_le16(name_len);
4022
4023 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004024 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004025 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004026 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004027 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004028 struct timespec ts;
4029 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004030
4031 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004032 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004033 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004034 ts.tv_nsec = 0;
4035 ts.tv_sec = time;
4036 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004037 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4038 data->LastWriteTime = data->ChangeTime;
4039 data->LastAccessTime = 0;
4040 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004041 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004042 data->EndOfFile = data->AllocationSize;
4043 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004044 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004045 } else
4046 rc = -EIO; /* bad buffer passed in */
4047
4048 cifs_buf_release(pSMB);
4049
4050 if (rc == -EAGAIN)
4051 goto QInfRetry;
4052
4053 return rc;
4054}
4055
Jeff Laytonbcd53572010-02-12 07:44:16 -05004056int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004057CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004058 u16 netfid, FILE_ALL_INFO *pFindData)
4059{
4060 struct smb_t2_qfi_req *pSMB = NULL;
4061 struct smb_t2_qfi_rsp *pSMBr = NULL;
4062 int rc = 0;
4063 int bytes_returned;
4064 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004065
Jeff Laytonbcd53572010-02-12 07:44:16 -05004066QFileInfoRetry:
4067 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4068 (void **) &pSMBr);
4069 if (rc)
4070 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004071
Jeff Laytonbcd53572010-02-12 07:44:16 -05004072 params = 2 /* level */ + 2 /* fid */;
4073 pSMB->t2.TotalDataCount = 0;
4074 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4075 /* BB find exact max data count below from sess structure BB */
4076 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4077 pSMB->t2.MaxSetupCount = 0;
4078 pSMB->t2.Reserved = 0;
4079 pSMB->t2.Flags = 0;
4080 pSMB->t2.Timeout = 0;
4081 pSMB->t2.Reserved2 = 0;
4082 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4083 Fid) - 4);
4084 pSMB->t2.DataCount = 0;
4085 pSMB->t2.DataOffset = 0;
4086 pSMB->t2.SetupCount = 1;
4087 pSMB->t2.Reserved3 = 0;
4088 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4089 byte_count = params + 1 /* pad */ ;
4090 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4091 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4092 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4093 pSMB->Pad = 0;
4094 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004095 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004096 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004097
4098 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4099 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4100 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004101 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004102 } else { /* decode response */
4103 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4104
4105 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4106 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004107 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004108 rc = -EIO; /* bad smb */
4109 else if (pFindData) {
4110 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4111 memcpy((char *) pFindData,
4112 (char *) &pSMBr->hdr.Protocol +
4113 data_offset, sizeof(FILE_ALL_INFO));
4114 } else
4115 rc = -ENOMEM;
4116 }
4117 cifs_buf_release(pSMB);
4118 if (rc == -EAGAIN)
4119 goto QFileInfoRetry;
4120
4121 return rc;
4122}
Steve French6b8edfe2005-08-23 20:26:03 -07004123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004125CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004126 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004127 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004128 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004130 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 TRANSACTION2_QPI_REQ *pSMB = NULL;
4132 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4133 int rc = 0;
4134 int bytes_returned;
4135 int name_len;
4136 __u16 params, byte_count;
4137
Joe Perchesf96637b2013-05-04 22:12:25 -05004138 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139QPathInfoRetry:
4140 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4141 (void **) &pSMBr);
4142 if (rc)
4143 return rc;
4144
4145 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4146 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004147 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004148 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 name_len++; /* trailing null */
4150 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004151 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004152 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004154 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 }
4156
Steve French50c2f752007-07-13 00:33:32 +00004157 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 pSMB->TotalDataCount = 0;
4159 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004160 /* BB find exact max SMB PDU from sess structure BB */
4161 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 pSMB->MaxSetupCount = 0;
4163 pSMB->Reserved = 0;
4164 pSMB->Flags = 0;
4165 pSMB->Timeout = 0;
4166 pSMB->Reserved2 = 0;
4167 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004168 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 pSMB->DataCount = 0;
4170 pSMB->DataOffset = 0;
4171 pSMB->SetupCount = 1;
4172 pSMB->Reserved3 = 0;
4173 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4174 byte_count = params + 1 /* pad */ ;
4175 pSMB->TotalParameterCount = cpu_to_le16(params);
4176 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004177 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004178 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4179 else
4180 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004182 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 pSMB->ByteCount = cpu_to_le16(byte_count);
4184
4185 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4186 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4187 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004188 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 } else { /* decode response */
4190 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4191
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004192 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4193 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004194 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004196 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004197 rc = -EIO; /* 24 or 26 expected but we do not read
4198 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004199 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004200 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004202
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004203 /*
4204 * On legacy responses we do not read the last field,
4205 * EAsize, fortunately since it varies by subdialect and
4206 * also note it differs on Set vs Get, ie two bytes or 4
4207 * bytes depending but we don't care here.
4208 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004209 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004210 size = sizeof(FILE_INFO_STANDARD);
4211 else
4212 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004213 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004214 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 } else
4216 rc = -ENOMEM;
4217 }
4218 cifs_buf_release(pSMB);
4219 if (rc == -EAGAIN)
4220 goto QPathInfoRetry;
4221
4222 return rc;
4223}
4224
4225int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004226CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004227 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4228{
4229 struct smb_t2_qfi_req *pSMB = NULL;
4230 struct smb_t2_qfi_rsp *pSMBr = NULL;
4231 int rc = 0;
4232 int bytes_returned;
4233 __u16 params, byte_count;
4234
4235UnixQFileInfoRetry:
4236 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4237 (void **) &pSMBr);
4238 if (rc)
4239 return rc;
4240
4241 params = 2 /* level */ + 2 /* fid */;
4242 pSMB->t2.TotalDataCount = 0;
4243 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4244 /* BB find exact max data count below from sess structure BB */
4245 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4246 pSMB->t2.MaxSetupCount = 0;
4247 pSMB->t2.Reserved = 0;
4248 pSMB->t2.Flags = 0;
4249 pSMB->t2.Timeout = 0;
4250 pSMB->t2.Reserved2 = 0;
4251 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4252 Fid) - 4);
4253 pSMB->t2.DataCount = 0;
4254 pSMB->t2.DataOffset = 0;
4255 pSMB->t2.SetupCount = 1;
4256 pSMB->t2.Reserved3 = 0;
4257 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4258 byte_count = params + 1 /* pad */ ;
4259 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4260 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4261 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4262 pSMB->Pad = 0;
4263 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004264 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004265 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004266
4267 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4268 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4269 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004270 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004271 } else { /* decode response */
4272 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4273
Jeff Layton820a8032011-05-04 08:05:26 -04004274 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004275 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 -05004276 rc = -EIO; /* bad smb */
4277 } else {
4278 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4279 memcpy((char *) pFindData,
4280 (char *) &pSMBr->hdr.Protocol +
4281 data_offset,
4282 sizeof(FILE_UNIX_BASIC_INFO));
4283 }
4284 }
4285
4286 cifs_buf_release(pSMB);
4287 if (rc == -EAGAIN)
4288 goto UnixQFileInfoRetry;
4289
4290 return rc;
4291}
4292
4293int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004294CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004296 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004297 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
4299/* SMB_QUERY_FILE_UNIX_BASIC */
4300 TRANSACTION2_QPI_REQ *pSMB = NULL;
4301 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4302 int rc = 0;
4303 int bytes_returned = 0;
4304 int name_len;
4305 __u16 params, byte_count;
4306
Joe Perchesf96637b2013-05-04 22:12:25 -05004307 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308UnixQPathInfoRetry:
4309 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4310 (void **) &pSMBr);
4311 if (rc)
4312 return rc;
4313
4314 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4315 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004316 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4317 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 name_len++; /* trailing null */
4319 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004320 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 name_len = strnlen(searchName, PATH_MAX);
4322 name_len++; /* trailing null */
4323 strncpy(pSMB->FileName, searchName, name_len);
4324 }
4325
Steve French50c2f752007-07-13 00:33:32 +00004326 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 pSMB->TotalDataCount = 0;
4328 pSMB->MaxParameterCount = cpu_to_le16(2);
4329 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004330 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 pSMB->MaxSetupCount = 0;
4332 pSMB->Reserved = 0;
4333 pSMB->Flags = 0;
4334 pSMB->Timeout = 0;
4335 pSMB->Reserved2 = 0;
4336 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004337 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 pSMB->DataCount = 0;
4339 pSMB->DataOffset = 0;
4340 pSMB->SetupCount = 1;
4341 pSMB->Reserved3 = 0;
4342 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4343 byte_count = params + 1 /* pad */ ;
4344 pSMB->TotalParameterCount = cpu_to_le16(params);
4345 pSMB->ParameterCount = pSMB->TotalParameterCount;
4346 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4347 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004348 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 pSMB->ByteCount = cpu_to_le16(byte_count);
4350
4351 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4352 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4353 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004354 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 } else { /* decode response */
4356 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4357
Jeff Layton820a8032011-05-04 08:05:26 -04004358 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004359 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 -07004360 rc = -EIO; /* bad smb */
4361 } else {
4362 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4363 memcpy((char *) pFindData,
4364 (char *) &pSMBr->hdr.Protocol +
4365 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004366 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
4368 }
4369 cifs_buf_release(pSMB);
4370 if (rc == -EAGAIN)
4371 goto UnixQPathInfoRetry;
4372
4373 return rc;
4374}
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376/* xid, tcon, searchName and codepage are input parms, rest are returned */
4377int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004378CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004379 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004380 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004381 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382{
4383/* level 257 SMB_ */
4384 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4385 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004386 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 int rc = 0;
4388 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004389 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004391 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Joe Perchesf96637b2013-05-04 22:12:25 -05004393 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
4395findFirstRetry:
4396 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4397 (void **) &pSMBr);
4398 if (rc)
4399 return rc;
4400
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004401 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004402 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4405 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004406 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4407 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004408 /* We can not add the asterik earlier in case
4409 it got remapped to 0xF03A as if it were part of the
4410 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004412 if (msearch) {
4413 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4414 pSMB->FileName[name_len+1] = 0;
4415 pSMB->FileName[name_len+2] = '*';
4416 pSMB->FileName[name_len+3] = 0;
4417 name_len += 4; /* now the trailing null */
4418 /* null terminate just in case */
4419 pSMB->FileName[name_len] = 0;
4420 pSMB->FileName[name_len+1] = 0;
4421 name_len += 2;
4422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 } else { /* BB add check for overrun of SMB buf BB */
4424 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004426 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 free buffer exit; BB */
4428 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004429 if (msearch) {
4430 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4431 pSMB->FileName[name_len+1] = '*';
4432 pSMB->FileName[name_len+2] = 0;
4433 name_len += 3;
4434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
4436
4437 params = 12 + name_len /* includes null */ ;
4438 pSMB->TotalDataCount = 0; /* no EAs */
4439 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004440 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 pSMB->MaxSetupCount = 0;
4442 pSMB->Reserved = 0;
4443 pSMB->Flags = 0;
4444 pSMB->Timeout = 0;
4445 pSMB->Reserved2 = 0;
4446 byte_count = params + 1 /* pad */ ;
4447 pSMB->TotalParameterCount = cpu_to_le16(params);
4448 pSMB->ParameterCount = pSMB->TotalParameterCount;
4449 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004450 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4451 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 pSMB->DataCount = 0;
4453 pSMB->DataOffset = 0;
4454 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4455 pSMB->Reserved3 = 0;
4456 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4457 pSMB->SearchAttributes =
4458 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4459 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004460 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004461 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4463
4464 /* BB what should we set StorageType to? Does it matter? BB */
4465 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004466 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 pSMB->ByteCount = cpu_to_le16(byte_count);
4468
4469 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4470 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004471 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Steve French88274812006-03-09 22:21:45 +00004473 if (rc) {/* BB add logic to retry regular search if Unix search
4474 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004476 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004477
Steve French88274812006-03-09 22:21:45 +00004478 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 /* BB eventually could optimize out free and realloc of buf */
4481 /* for this case */
4482 if (rc == -EAGAIN)
4483 goto findFirstRetry;
4484 } else { /* decode response */
4485 /* BB remember to free buffer if error BB */
4486 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004487 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004488 unsigned int lnoff;
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004491 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 else
Steve French4b18f2a2008-04-29 00:06:05 +00004493 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
4495 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004496 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004497 psrch_inf->srch_entries_start =
4498 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4501 le16_to_cpu(pSMBr->t2.ParameterOffset));
4502
Steve French790fe572007-07-07 19:25:05 +00004503 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004504 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 else
Steve French4b18f2a2008-04-29 00:06:05 +00004506 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Steve French50c2f752007-07-13 00:33:32 +00004508 psrch_inf->entries_in_buffer =
4509 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004510 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004512 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004513 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004514 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004515 psrch_inf->last_entry = NULL;
4516 return rc;
4517 }
4518
Steve French0752f152008-10-07 20:03:33 +00004519 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004520 lnoff;
4521
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004522 if (pnetfid)
4523 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 } else {
4525 cifs_buf_release(pSMB);
4526 }
4527 }
4528
4529 return rc;
4530}
4531
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004532int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4533 __u16 searchHandle, __u16 search_flags,
4534 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4537 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004538 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 char *response_data;
4540 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004541 int bytes_returned;
4542 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 __u16 params, byte_count;
4544
Joe Perchesf96637b2013-05-04 22:12:25 -05004545 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Steve French4b18f2a2008-04-29 00:06:05 +00004547 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 return -ENOENT;
4549
4550 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4551 (void **) &pSMBr);
4552 if (rc)
4553 return rc;
4554
Steve French50c2f752007-07-13 00:33:32 +00004555 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 byte_count = 0;
4557 pSMB->TotalDataCount = 0; /* no EAs */
4558 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004559 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 pSMB->MaxSetupCount = 0;
4561 pSMB->Reserved = 0;
4562 pSMB->Flags = 0;
4563 pSMB->Timeout = 0;
4564 pSMB->Reserved2 = 0;
4565 pSMB->ParameterOffset = cpu_to_le16(
4566 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4567 pSMB->DataCount = 0;
4568 pSMB->DataOffset = 0;
4569 pSMB->SetupCount = 1;
4570 pSMB->Reserved3 = 0;
4571 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4572 pSMB->SearchHandle = searchHandle; /* always kept as le */
4573 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004574 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4576 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004577 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 name_len = psrch_inf->resume_name_len;
4580 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004581 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4583 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004584 /* 14 byte parm len above enough for 2 byte null terminator */
4585 pSMB->ResumeFileName[name_len] = 0;
4586 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 } else {
4588 rc = -EINVAL;
4589 goto FNext2_err_exit;
4590 }
4591 byte_count = params + 1 /* pad */ ;
4592 pSMB->TotalParameterCount = cpu_to_le16(params);
4593 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004594 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4598 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004599 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 if (rc) {
4601 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004602 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004603 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004604 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004606 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 } else { /* decode response */
4608 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004609
Steve French790fe572007-07-07 19:25:05 +00004610 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004611 unsigned int lnoff;
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 /* BB fixme add lock for file (srch_info) struct here */
4614 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004615 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 else
Steve French4b18f2a2008-04-29 00:06:05 +00004617 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 response_data = (char *) &pSMBr->hdr.Protocol +
4619 le16_to_cpu(pSMBr->t2.ParameterOffset);
4620 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4621 response_data = (char *)&pSMBr->hdr.Protocol +
4622 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004623 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004624 cifs_small_buf_release(
4625 psrch_inf->ntwrk_buf_start);
4626 else
4627 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 psrch_inf->srch_entries_start = response_data;
4629 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004630 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004631 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004632 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 else
Steve French4b18f2a2008-04-29 00:06:05 +00004634 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004635 psrch_inf->entries_in_buffer =
4636 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 psrch_inf->index_of_last_entry +=
4638 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004639 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004640 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004641 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004642 psrch_inf->last_entry = NULL;
4643 return rc;
4644 } else
4645 psrch_inf->last_entry =
4646 psrch_inf->srch_entries_start + lnoff;
4647
Joe Perchesf96637b2013-05-04 22:12:25 -05004648/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4649 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
4651 /* BB fixme add unlock here */
4652 }
4653
4654 }
4655
4656 /* BB On error, should we leave previous search buf (and count and
4657 last entry fields) intact or free the previous one? */
4658
4659 /* Note: On -EAGAIN error only caller can retry on handle based calls
4660 since file handle passed in no longer valid */
4661FNext2_err_exit:
4662 if (rc != 0)
4663 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return rc;
4665}
4666
4667int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004668CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004669 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
4671 int rc = 0;
4672 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Joe Perchesf96637b2013-05-04 22:12:25 -05004674 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4676
4677 /* no sense returning error if session restarted
4678 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004679 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return 0;
4681 if (rc)
4682 return rc;
4683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 pSMB->FileID = searchHandle;
4685 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004686 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004687 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004688 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004689 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004690
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004691 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693 /* Since session is dead, search handle closed on server already */
4694 if (rc == -EAGAIN)
4695 rc = 0;
4696
4697 return rc;
4698}
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004701CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004702 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004703 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
4705 int rc = 0;
4706 TRANSACTION2_QPI_REQ *pSMB = NULL;
4707 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4708 int name_len, bytes_returned;
4709 __u16 params, byte_count;
4710
Joe Perchesf96637b2013-05-04 22:12:25 -05004711 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004712 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004713 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715GetInodeNumberRetry:
4716 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004717 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 if (rc)
4719 return rc;
4720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4722 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004723 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004724 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004725 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 name_len++; /* trailing null */
4727 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004728 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004729 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004731 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
4733
4734 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4735 pSMB->TotalDataCount = 0;
4736 pSMB->MaxParameterCount = cpu_to_le16(2);
4737 /* BB find exact max data count below from sess structure BB */
4738 pSMB->MaxDataCount = cpu_to_le16(4000);
4739 pSMB->MaxSetupCount = 0;
4740 pSMB->Reserved = 0;
4741 pSMB->Flags = 0;
4742 pSMB->Timeout = 0;
4743 pSMB->Reserved2 = 0;
4744 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004745 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 pSMB->DataCount = 0;
4747 pSMB->DataOffset = 0;
4748 pSMB->SetupCount = 1;
4749 pSMB->Reserved3 = 0;
4750 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4751 byte_count = params + 1 /* pad */ ;
4752 pSMB->TotalParameterCount = cpu_to_le16(params);
4753 pSMB->ParameterCount = pSMB->TotalParameterCount;
4754 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4755 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004756 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 pSMB->ByteCount = cpu_to_le16(byte_count);
4758
4759 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4760 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4761 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004762 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 } else {
4764 /* decode response */
4765 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004767 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 /* If rc should we check for EOPNOSUPP and
4769 disable the srvino flag? or in caller? */
4770 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004771 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4773 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004774 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004776 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004777 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 rc = -EIO;
4779 goto GetInodeNumOut;
4780 }
4781 pfinfo = (struct file_internal_info *)
4782 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004783 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
4785 }
4786GetInodeNumOut:
4787 cifs_buf_release(pSMB);
4788 if (rc == -EAGAIN)
4789 goto GetInodeNumberRetry;
4790 return rc;
4791}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004794CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004795 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004796 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004797 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
4799/* TRANS2_GET_DFS_REFERRAL */
4800 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4801 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 int rc = 0;
4803 int bytes_returned;
4804 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004806 *num_of_nodes = 0;
4807 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Joe Perchesf96637b2013-05-04 22:12:25 -05004809 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 if (ses == NULL)
4811 return -ENODEV;
4812getDFSRetry:
4813 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4814 (void **) &pSMBr);
4815 if (rc)
4816 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004817
4818 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004819 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004820 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 pSMB->hdr.Tid = ses->ipc_tid;
4822 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004823 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004825 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 if (ses->capabilities & CAP_UNICODE) {
4829 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4830 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004831 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004832 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004833 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 name_len++; /* trailing null */
4835 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004836 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004837 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004839 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
4841
Dan Carpenter65c3b202015-04-30 17:30:24 +03004842 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004843 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004844
Steve French50c2f752007-07-13 00:33:32 +00004845 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 params = 2 /* level */ + name_len /*includes null */ ;
4848 pSMB->TotalDataCount = 0;
4849 pSMB->DataCount = 0;
4850 pSMB->DataOffset = 0;
4851 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004852 /* BB find exact max SMB PDU from sess structure BB */
4853 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 pSMB->MaxSetupCount = 0;
4855 pSMB->Reserved = 0;
4856 pSMB->Flags = 0;
4857 pSMB->Timeout = 0;
4858 pSMB->Reserved2 = 0;
4859 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004860 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 pSMB->SetupCount = 1;
4862 pSMB->Reserved3 = 0;
4863 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4864 byte_count = params + 3 /* pad */ ;
4865 pSMB->ParameterCount = cpu_to_le16(params);
4866 pSMB->TotalParameterCount = pSMB->ParameterCount;
4867 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004868 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 pSMB->ByteCount = cpu_to_le16(byte_count);
4870
4871 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4872 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4873 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004874 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004875 goto GetDFSRefExit;
4876 }
4877 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004879 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004880 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004881 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004882 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004884
Joe Perchesf96637b2013-05-04 22:12:25 -05004885 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4886 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004887
4888 /* parse returned result into more usable form */
Aurelien Aptel4ecce922017-02-13 16:03:47 +01004889 rc = parse_dfs_referrals(&pSMBr->dfs_data,
4890 le16_to_cpu(pSMBr->t2.DataCount),
4891 num_of_nodes, target_nodes, nls_codepage,
4892 remap, search_name,
Steve French284316d2017-03-02 15:42:48 -06004893 (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE) != 0);
Igor Mammedovfec45852008-05-16 13:06:30 +04004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004896 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 if (rc == -EAGAIN)
4899 goto getDFSRetry;
4900
4901 return rc;
4902}
4903
Steve French20962432005-09-21 22:05:57 -07004904/* Query File System Info such as free space to old servers such as Win 9x */
4905int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004906SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4907 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004908{
4909/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4910 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4911 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4912 FILE_SYSTEM_ALLOC_INFO *response_data;
4913 int rc = 0;
4914 int bytes_returned = 0;
4915 __u16 params, byte_count;
4916
Joe Perchesf96637b2013-05-04 22:12:25 -05004917 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004918oldQFSInfoRetry:
4919 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4920 (void **) &pSMBr);
4921 if (rc)
4922 return rc;
Steve French20962432005-09-21 22:05:57 -07004923
4924 params = 2; /* level */
4925 pSMB->TotalDataCount = 0;
4926 pSMB->MaxParameterCount = cpu_to_le16(2);
4927 pSMB->MaxDataCount = cpu_to_le16(1000);
4928 pSMB->MaxSetupCount = 0;
4929 pSMB->Reserved = 0;
4930 pSMB->Flags = 0;
4931 pSMB->Timeout = 0;
4932 pSMB->Reserved2 = 0;
4933 byte_count = params + 1 /* pad */ ;
4934 pSMB->TotalParameterCount = cpu_to_le16(params);
4935 pSMB->ParameterCount = pSMB->TotalParameterCount;
4936 pSMB->ParameterOffset = cpu_to_le16(offsetof(
4937 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
4938 pSMB->DataCount = 0;
4939 pSMB->DataOffset = 0;
4940 pSMB->SetupCount = 1;
4941 pSMB->Reserved3 = 0;
4942 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
4943 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004944 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07004945 pSMB->ByteCount = cpu_to_le16(byte_count);
4946
4947 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4948 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4949 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004950 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07004951 } else { /* decode response */
4952 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4953
Jeff Layton820a8032011-05-04 08:05:26 -04004954 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07004955 rc = -EIO; /* bad smb */
4956 else {
4957 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05004958 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04004959 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07004960
Steve French50c2f752007-07-13 00:33:32 +00004961 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07004962 (((char *) &pSMBr->hdr.Protocol) + data_offset);
4963 FSData->f_bsize =
4964 le16_to_cpu(response_data->BytesPerSector) *
4965 le32_to_cpu(response_data->
4966 SectorsPerAllocationUnit);
4967 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00004968 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07004969 FSData->f_bfree = FSData->f_bavail =
4970 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05004971 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
4972 (unsigned long long)FSData->f_blocks,
4973 (unsigned long long)FSData->f_bfree,
4974 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07004975 }
4976 }
4977 cifs_buf_release(pSMB);
4978
4979 if (rc == -EAGAIN)
4980 goto oldQFSInfoRetry;
4981
4982 return rc;
4983}
4984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004986CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4987 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988{
4989/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
4990 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4991 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4992 FILE_SYSTEM_INFO *response_data;
4993 int rc = 0;
4994 int bytes_returned = 0;
4995 __u16 params, byte_count;
4996
Joe Perchesf96637b2013-05-04 22:12:25 -05004997 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998QFSInfoRetry:
4999 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5000 (void **) &pSMBr);
5001 if (rc)
5002 return rc;
5003
5004 params = 2; /* level */
5005 pSMB->TotalDataCount = 0;
5006 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005007 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 pSMB->MaxSetupCount = 0;
5009 pSMB->Reserved = 0;
5010 pSMB->Flags = 0;
5011 pSMB->Timeout = 0;
5012 pSMB->Reserved2 = 0;
5013 byte_count = params + 1 /* pad */ ;
5014 pSMB->TotalParameterCount = cpu_to_le16(params);
5015 pSMB->ParameterCount = pSMB->TotalParameterCount;
5016 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005017 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 pSMB->DataCount = 0;
5019 pSMB->DataOffset = 0;
5020 pSMB->SetupCount = 1;
5021 pSMB->Reserved3 = 0;
5022 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5023 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005024 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 pSMB->ByteCount = cpu_to_le16(byte_count);
5026
5027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5029 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005030 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005032 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Jeff Layton820a8032011-05-04 08:05:26 -04005034 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 rc = -EIO; /* bad smb */
5036 else {
5037 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
5039 response_data =
5040 (FILE_SYSTEM_INFO
5041 *) (((char *) &pSMBr->hdr.Protocol) +
5042 data_offset);
5043 FSData->f_bsize =
5044 le32_to_cpu(response_data->BytesPerSector) *
5045 le32_to_cpu(response_data->
5046 SectorsPerAllocationUnit);
5047 FSData->f_blocks =
5048 le64_to_cpu(response_data->TotalAllocationUnits);
5049 FSData->f_bfree = FSData->f_bavail =
5050 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005051 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5052 (unsigned long long)FSData->f_blocks,
5053 (unsigned long long)FSData->f_bfree,
5054 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 }
5056 }
5057 cifs_buf_release(pSMB);
5058
5059 if (rc == -EAGAIN)
5060 goto QFSInfoRetry;
5061
5062 return rc;
5063}
5064
5065int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005066CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067{
5068/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5069 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5070 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5071 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5072 int rc = 0;
5073 int bytes_returned = 0;
5074 __u16 params, byte_count;
5075
Joe Perchesf96637b2013-05-04 22:12:25 -05005076 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077QFSAttributeRetry:
5078 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5079 (void **) &pSMBr);
5080 if (rc)
5081 return rc;
5082
5083 params = 2; /* level */
5084 pSMB->TotalDataCount = 0;
5085 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005086 /* BB find exact max SMB PDU from sess structure BB */
5087 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 pSMB->MaxSetupCount = 0;
5089 pSMB->Reserved = 0;
5090 pSMB->Flags = 0;
5091 pSMB->Timeout = 0;
5092 pSMB->Reserved2 = 0;
5093 byte_count = params + 1 /* pad */ ;
5094 pSMB->TotalParameterCount = cpu_to_le16(params);
5095 pSMB->ParameterCount = pSMB->TotalParameterCount;
5096 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005097 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 pSMB->DataCount = 0;
5099 pSMB->DataOffset = 0;
5100 pSMB->SetupCount = 1;
5101 pSMB->Reserved3 = 0;
5102 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5103 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005104 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 pSMB->ByteCount = cpu_to_le16(byte_count);
5106
5107 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5108 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5109 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005110 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 } else { /* decode response */
5112 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5113
Jeff Layton820a8032011-05-04 08:05:26 -04005114 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005115 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 rc = -EIO; /* bad smb */
5117 } else {
5118 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5119 response_data =
5120 (FILE_SYSTEM_ATTRIBUTE_INFO
5121 *) (((char *) &pSMBr->hdr.Protocol) +
5122 data_offset);
5123 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005124 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 }
5126 }
5127 cifs_buf_release(pSMB);
5128
5129 if (rc == -EAGAIN)
5130 goto QFSAttributeRetry;
5131
5132 return rc;
5133}
5134
5135int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005136CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137{
5138/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5139 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5140 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5141 FILE_SYSTEM_DEVICE_INFO *response_data;
5142 int rc = 0;
5143 int bytes_returned = 0;
5144 __u16 params, byte_count;
5145
Joe Perchesf96637b2013-05-04 22:12:25 -05005146 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147QFSDeviceRetry:
5148 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5149 (void **) &pSMBr);
5150 if (rc)
5151 return rc;
5152
5153 params = 2; /* level */
5154 pSMB->TotalDataCount = 0;
5155 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005156 /* BB find exact max SMB PDU from sess structure BB */
5157 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 pSMB->MaxSetupCount = 0;
5159 pSMB->Reserved = 0;
5160 pSMB->Flags = 0;
5161 pSMB->Timeout = 0;
5162 pSMB->Reserved2 = 0;
5163 byte_count = params + 1 /* pad */ ;
5164 pSMB->TotalParameterCount = cpu_to_le16(params);
5165 pSMB->ParameterCount = pSMB->TotalParameterCount;
5166 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005167 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
5169 pSMB->DataCount = 0;
5170 pSMB->DataOffset = 0;
5171 pSMB->SetupCount = 1;
5172 pSMB->Reserved3 = 0;
5173 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5174 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005175 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 pSMB->ByteCount = cpu_to_le16(byte_count);
5177
5178 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5179 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5180 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005181 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 } else { /* decode response */
5183 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5184
Jeff Layton820a8032011-05-04 08:05:26 -04005185 if (rc || get_bcc(&pSMBr->hdr) <
5186 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 rc = -EIO; /* bad smb */
5188 else {
5189 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5190 response_data =
Steve French737b7582005-04-28 22:41:06 -07005191 (FILE_SYSTEM_DEVICE_INFO *)
5192 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 data_offset);
5194 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005195 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 }
5197 }
5198 cifs_buf_release(pSMB);
5199
5200 if (rc == -EAGAIN)
5201 goto QFSDeviceRetry;
5202
5203 return rc;
5204}
5205
5206int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005207CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208{
5209/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5210 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5211 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5212 FILE_SYSTEM_UNIX_INFO *response_data;
5213 int rc = 0;
5214 int bytes_returned = 0;
5215 __u16 params, byte_count;
5216
Joe Perchesf96637b2013-05-04 22:12:25 -05005217 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005219 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5220 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 if (rc)
5222 return rc;
5223
5224 params = 2; /* level */
5225 pSMB->TotalDataCount = 0;
5226 pSMB->DataCount = 0;
5227 pSMB->DataOffset = 0;
5228 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005229 /* BB find exact max SMB PDU from sess structure BB */
5230 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 pSMB->MaxSetupCount = 0;
5232 pSMB->Reserved = 0;
5233 pSMB->Flags = 0;
5234 pSMB->Timeout = 0;
5235 pSMB->Reserved2 = 0;
5236 byte_count = params + 1 /* pad */ ;
5237 pSMB->ParameterCount = cpu_to_le16(params);
5238 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005239 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5240 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 pSMB->SetupCount = 1;
5242 pSMB->Reserved3 = 0;
5243 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5244 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005245 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 pSMB->ByteCount = cpu_to_le16(byte_count);
5247
5248 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5249 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5250 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005251 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 } else { /* decode response */
5253 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5254
Jeff Layton820a8032011-05-04 08:05:26 -04005255 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 rc = -EIO; /* bad smb */
5257 } else {
5258 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5259 response_data =
5260 (FILE_SYSTEM_UNIX_INFO
5261 *) (((char *) &pSMBr->hdr.Protocol) +
5262 data_offset);
5263 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005264 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 }
5266 }
5267 cifs_buf_release(pSMB);
5268
5269 if (rc == -EAGAIN)
5270 goto QFSUnixRetry;
5271
5272
5273 return rc;
5274}
5275
Jeremy Allisonac670552005-06-22 17:26:35 -07005276int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005277CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005278{
5279/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5280 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5281 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5282 int rc = 0;
5283 int bytes_returned = 0;
5284 __u16 params, param_offset, offset, byte_count;
5285
Joe Perchesf96637b2013-05-04 22:12:25 -05005286 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005287SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005288 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005289 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5290 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005291 if (rc)
5292 return rc;
5293
5294 params = 4; /* 2 bytes zero followed by info level. */
5295 pSMB->MaxSetupCount = 0;
5296 pSMB->Reserved = 0;
5297 pSMB->Flags = 0;
5298 pSMB->Timeout = 0;
5299 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005300 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5301 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005302 offset = param_offset + params;
5303
5304 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005305 /* BB find exact max SMB PDU from sess structure BB */
5306 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005307 pSMB->SetupCount = 1;
5308 pSMB->Reserved3 = 0;
5309 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5310 byte_count = 1 /* pad */ + params + 12;
5311
5312 pSMB->DataCount = cpu_to_le16(12);
5313 pSMB->ParameterCount = cpu_to_le16(params);
5314 pSMB->TotalDataCount = pSMB->DataCount;
5315 pSMB->TotalParameterCount = pSMB->ParameterCount;
5316 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5317 pSMB->DataOffset = cpu_to_le16(offset);
5318
5319 /* Params. */
5320 pSMB->FileNum = 0;
5321 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5322
5323 /* Data. */
5324 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5325 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5326 pSMB->ClientUnixCap = cpu_to_le64(cap);
5327
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005328 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005329 pSMB->ByteCount = cpu_to_le16(byte_count);
5330
5331 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5332 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5333 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005334 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005335 } else { /* decode response */
5336 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005337 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005338 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005339 }
5340 cifs_buf_release(pSMB);
5341
5342 if (rc == -EAGAIN)
5343 goto SETFSUnixRetry;
5344
5345 return rc;
5346}
5347
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
5350int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005351CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005352 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
5354/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5355 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5356 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5357 FILE_SYSTEM_POSIX_INFO *response_data;
5358 int rc = 0;
5359 int bytes_returned = 0;
5360 __u16 params, byte_count;
5361
Joe Perchesf96637b2013-05-04 22:12:25 -05005362 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363QFSPosixRetry:
5364 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5365 (void **) &pSMBr);
5366 if (rc)
5367 return rc;
5368
5369 params = 2; /* level */
5370 pSMB->TotalDataCount = 0;
5371 pSMB->DataCount = 0;
5372 pSMB->DataOffset = 0;
5373 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005374 /* BB find exact max SMB PDU from sess structure BB */
5375 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 pSMB->MaxSetupCount = 0;
5377 pSMB->Reserved = 0;
5378 pSMB->Flags = 0;
5379 pSMB->Timeout = 0;
5380 pSMB->Reserved2 = 0;
5381 byte_count = params + 1 /* pad */ ;
5382 pSMB->ParameterCount = cpu_to_le16(params);
5383 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005384 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5385 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 pSMB->SetupCount = 1;
5387 pSMB->Reserved3 = 0;
5388 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5389 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005390 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 pSMB->ByteCount = cpu_to_le16(byte_count);
5392
5393 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5394 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5395 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005396 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 } else { /* decode response */
5398 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5399
Jeff Layton820a8032011-05-04 08:05:26 -04005400 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 rc = -EIO; /* bad smb */
5402 } else {
5403 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5404 response_data =
5405 (FILE_SYSTEM_POSIX_INFO
5406 *) (((char *) &pSMBr->hdr.Protocol) +
5407 data_offset);
5408 FSData->f_bsize =
5409 le32_to_cpu(response_data->BlockSize);
5410 FSData->f_blocks =
5411 le64_to_cpu(response_data->TotalBlocks);
5412 FSData->f_bfree =
5413 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005414 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 FSData->f_bavail = FSData->f_bfree;
5416 } else {
5417 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005418 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 }
Steve French790fe572007-07-07 19:25:05 +00005420 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005422 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005423 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005425 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
5427 }
5428 cifs_buf_release(pSMB);
5429
5430 if (rc == -EAGAIN)
5431 goto QFSPosixRetry;
5432
5433 return rc;
5434}
5435
5436
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005437/*
5438 * We can not use write of zero bytes trick to set file size due to need for
5439 * large file support. Also note that this SetPathInfo is preferred to
5440 * SetFileInfo based method in next routine which is only needed to work around
5441 * a sharing violation bugin Samba which this routine can run into.
5442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005444CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005445 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5446 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447{
5448 struct smb_com_transaction2_spi_req *pSMB = NULL;
5449 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5450 struct file_end_of_file_info *parm_data;
5451 int name_len;
5452 int rc = 0;
5453 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005454 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 __u16 params, byte_count, data_count, param_offset, offset;
5457
Joe Perchesf96637b2013-05-04 22:12:25 -05005458 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459SetEOFRetry:
5460 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5461 (void **) &pSMBr);
5462 if (rc)
5463 return rc;
5464
5465 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5466 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005467 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5468 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 name_len++; /* trailing null */
5470 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005471 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005472 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005474 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
5476 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005477 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005479 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 pSMB->MaxSetupCount = 0;
5481 pSMB->Reserved = 0;
5482 pSMB->Flags = 0;
5483 pSMB->Timeout = 0;
5484 pSMB->Reserved2 = 0;
5485 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005486 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005488 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005489 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5490 pSMB->InformationLevel =
5491 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5492 else
5493 pSMB->InformationLevel =
5494 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5495 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5497 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005498 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 else
5500 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005501 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503
5504 parm_data =
5505 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5506 offset);
5507 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5508 pSMB->DataOffset = cpu_to_le16(offset);
5509 pSMB->SetupCount = 1;
5510 pSMB->Reserved3 = 0;
5511 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5512 byte_count = 3 /* pad */ + params + data_count;
5513 pSMB->DataCount = cpu_to_le16(data_count);
5514 pSMB->TotalDataCount = pSMB->DataCount;
5515 pSMB->ParameterCount = cpu_to_le16(params);
5516 pSMB->TotalParameterCount = pSMB->ParameterCount;
5517 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005518 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 parm_data->FileSize = cpu_to_le64(size);
5520 pSMB->ByteCount = cpu_to_le16(byte_count);
5521 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5522 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005523 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005524 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
5526 cifs_buf_release(pSMB);
5527
5528 if (rc == -EAGAIN)
5529 goto SetEOFRetry;
5530
5531 return rc;
5532}
5533
5534int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005535CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5536 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537{
5538 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 struct file_end_of_file_info *parm_data;
5540 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 __u16 params, param_offset, offset, byte_count, count;
5542
Joe Perchesf96637b2013-05-04 22:12:25 -05005543 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5544 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005545 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 if (rc)
5548 return rc;
5549
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005550 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5551 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005552
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 params = 6;
5554 pSMB->MaxSetupCount = 0;
5555 pSMB->Reserved = 0;
5556 pSMB->Flags = 0;
5557 pSMB->Timeout = 0;
5558 pSMB->Reserved2 = 0;
5559 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5560 offset = param_offset + params;
5561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 count = sizeof(struct file_end_of_file_info);
5563 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005564 /* BB find exact max SMB PDU from sess structure BB */
5565 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 pSMB->SetupCount = 1;
5567 pSMB->Reserved3 = 0;
5568 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5569 byte_count = 3 /* pad */ + params + count;
5570 pSMB->DataCount = cpu_to_le16(count);
5571 pSMB->ParameterCount = cpu_to_le16(params);
5572 pSMB->TotalDataCount = pSMB->DataCount;
5573 pSMB->TotalParameterCount = pSMB->ParameterCount;
5574 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5575 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005576 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5577 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 pSMB->DataOffset = cpu_to_le16(offset);
5579 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005580 pSMB->Fid = cfile->fid.netfid;
5581 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5583 pSMB->InformationLevel =
5584 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5585 else
5586 pSMB->InformationLevel =
5587 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005588 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5590 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005591 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 else
5593 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005594 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 }
5596 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005597 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005599 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005600 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005602 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5603 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 }
5605
Steve French50c2f752007-07-13 00:33:32 +00005606 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 since file handle passed in no longer valid */
5608
5609 return rc;
5610}
5611
Steve French50c2f752007-07-13 00:33:32 +00005612/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 an open handle, rather than by pathname - this is awkward due to
5614 potential access conflicts on the open, but it is unavoidable for these
5615 old servers since the only other choice is to go from 100 nanosecond DCE
5616 time and resort to the original setpathinfo level which takes the ancient
5617 DOS time format with 2 second granularity */
5618int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005619CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005620 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
5622 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 char *data_offset;
5624 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 __u16 params, param_offset, offset, byte_count, count;
5626
Joe Perchesf96637b2013-05-04 22:12:25 -05005627 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005628 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 if (rc)
5631 return rc;
5632
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005633 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5634 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 params = 6;
5637 pSMB->MaxSetupCount = 0;
5638 pSMB->Reserved = 0;
5639 pSMB->Flags = 0;
5640 pSMB->Timeout = 0;
5641 pSMB->Reserved2 = 0;
5642 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5643 offset = param_offset + params;
5644
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005645 data_offset = (char *)pSMB +
5646 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Steve French26f57362007-08-30 22:09:15 +00005648 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005650 /* BB find max SMB PDU from sess */
5651 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 pSMB->SetupCount = 1;
5653 pSMB->Reserved3 = 0;
5654 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5655 byte_count = 3 /* pad */ + params + count;
5656 pSMB->DataCount = cpu_to_le16(count);
5657 pSMB->ParameterCount = cpu_to_le16(params);
5658 pSMB->TotalDataCount = pSMB->DataCount;
5659 pSMB->TotalParameterCount = pSMB->ParameterCount;
5660 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5661 pSMB->DataOffset = cpu_to_le16(offset);
5662 pSMB->Fid = fid;
5663 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5664 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5665 else
5666 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5667 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005668 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005670 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005671 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005672 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005673 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005674 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5675 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Steve French50c2f752007-07-13 00:33:32 +00005677 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 since file handle passed in no longer valid */
5679
5680 return rc;
5681}
5682
Jeff Layton6d22f092008-09-23 11:48:35 -04005683int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005684CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005685 bool delete_file, __u16 fid, __u32 pid_of_opener)
5686{
5687 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5688 char *data_offset;
5689 int rc = 0;
5690 __u16 params, param_offset, offset, byte_count, count;
5691
Joe Perchesf96637b2013-05-04 22:12:25 -05005692 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005693 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5694
5695 if (rc)
5696 return rc;
5697
5698 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5699 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5700
5701 params = 6;
5702 pSMB->MaxSetupCount = 0;
5703 pSMB->Reserved = 0;
5704 pSMB->Flags = 0;
5705 pSMB->Timeout = 0;
5706 pSMB->Reserved2 = 0;
5707 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5708 offset = param_offset + params;
5709
5710 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5711
5712 count = 1;
5713 pSMB->MaxParameterCount = cpu_to_le16(2);
5714 /* BB find max SMB PDU from sess */
5715 pSMB->MaxDataCount = cpu_to_le16(1000);
5716 pSMB->SetupCount = 1;
5717 pSMB->Reserved3 = 0;
5718 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5719 byte_count = 3 /* pad */ + params + count;
5720 pSMB->DataCount = cpu_to_le16(count);
5721 pSMB->ParameterCount = cpu_to_le16(params);
5722 pSMB->TotalDataCount = pSMB->DataCount;
5723 pSMB->TotalParameterCount = pSMB->ParameterCount;
5724 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5725 pSMB->DataOffset = cpu_to_le16(offset);
5726 pSMB->Fid = fid;
5727 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5728 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005729 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005730 pSMB->ByteCount = cpu_to_le16(byte_count);
5731 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005732 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005733 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005734 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005735 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005736
5737 return rc;
5738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005741CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005742 const char *fileName, const FILE_BASIC_INFO *data,
5743 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744{
5745 TRANSACTION2_SPI_REQ *pSMB = NULL;
5746 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5747 int name_len;
5748 int rc = 0;
5749 int bytes_returned = 0;
5750 char *data_offset;
5751 __u16 params, param_offset, offset, byte_count, count;
5752
Joe Perchesf96637b2013-05-04 22:12:25 -05005753 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755SetTimesRetry:
5756 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5757 (void **) &pSMBr);
5758 if (rc)
5759 return rc;
5760
5761 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5762 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005763 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5764 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 name_len++; /* trailing null */
5766 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005767 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 name_len = strnlen(fileName, PATH_MAX);
5769 name_len++; /* trailing null */
5770 strncpy(pSMB->FileName, fileName, name_len);
5771 }
5772
5773 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005774 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005776 /* BB find max SMB PDU from sess structure BB */
5777 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 pSMB->MaxSetupCount = 0;
5779 pSMB->Reserved = 0;
5780 pSMB->Flags = 0;
5781 pSMB->Timeout = 0;
5782 pSMB->Reserved2 = 0;
5783 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005784 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 offset = param_offset + params;
5786 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5787 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5788 pSMB->DataOffset = cpu_to_le16(offset);
5789 pSMB->SetupCount = 1;
5790 pSMB->Reserved3 = 0;
5791 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5792 byte_count = 3 /* pad */ + params + count;
5793
5794 pSMB->DataCount = cpu_to_le16(count);
5795 pSMB->ParameterCount = cpu_to_le16(params);
5796 pSMB->TotalDataCount = pSMB->DataCount;
5797 pSMB->TotalParameterCount = pSMB->ParameterCount;
5798 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5799 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5800 else
5801 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5802 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005803 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005804 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 pSMB->ByteCount = cpu_to_le16(byte_count);
5806 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5807 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005808 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005809 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
5811 cifs_buf_release(pSMB);
5812
5813 if (rc == -EAGAIN)
5814 goto SetTimesRetry;
5815
5816 return rc;
5817}
5818
5819/* Can not be used to set time stamps yet (due to old DOS time format) */
5820/* Can be used to set attributes */
5821#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5822 handling it anyway and NT4 was what we thought it would be needed for
5823 Do not delete it until we prove whether needed for Win9x though */
5824int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005825CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 __u16 dos_attrs, const struct nls_table *nls_codepage)
5827{
5828 SETATTR_REQ *pSMB = NULL;
5829 SETATTR_RSP *pSMBr = NULL;
5830 int rc = 0;
5831 int bytes_returned;
5832 int name_len;
5833
Joe Perchesf96637b2013-05-04 22:12:25 -05005834 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
5836SetAttrLgcyRetry:
5837 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5838 (void **) &pSMBr);
5839 if (rc)
5840 return rc;
5841
5842 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5843 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005844 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5845 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 name_len++; /* trailing null */
5847 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005848 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 name_len = strnlen(fileName, PATH_MAX);
5850 name_len++; /* trailing null */
5851 strncpy(pSMB->fileName, fileName, name_len);
5852 }
5853 pSMB->attr = cpu_to_le16(dos_attrs);
5854 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005855 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5857 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5858 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005859 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005860 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
5862 cifs_buf_release(pSMB);
5863
5864 if (rc == -EAGAIN)
5865 goto SetAttrLgcyRetry;
5866
5867 return rc;
5868}
5869#endif /* temporarily unneeded SetAttr legacy function */
5870
Jeff Layton654cf142009-07-09 20:02:49 -04005871static void
5872cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5873 const struct cifs_unix_set_info_args *args)
5874{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005875 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005876 u64 mode = args->mode;
5877
Eric W. Biederman49418b22013-02-06 00:57:56 -08005878 if (uid_valid(args->uid))
5879 uid = from_kuid(&init_user_ns, args->uid);
5880 if (gid_valid(args->gid))
5881 gid = from_kgid(&init_user_ns, args->gid);
5882
Jeff Layton654cf142009-07-09 20:02:49 -04005883 /*
5884 * Samba server ignores set of file size to zero due to bugs in some
5885 * older clients, but we should be precise - we use SetFileSize to
5886 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005887 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005888 * zero instead of -1 here
5889 */
5890 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5891 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5892 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5893 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5894 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005895 data_offset->Uid = cpu_to_le64(uid);
5896 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005897 /* better to leave device as zero when it is */
5898 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5899 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5900 data_offset->Permissions = cpu_to_le64(mode);
5901
5902 if (S_ISREG(mode))
5903 data_offset->Type = cpu_to_le32(UNIX_FILE);
5904 else if (S_ISDIR(mode))
5905 data_offset->Type = cpu_to_le32(UNIX_DIR);
5906 else if (S_ISLNK(mode))
5907 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5908 else if (S_ISCHR(mode))
5909 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5910 else if (S_ISBLK(mode))
5911 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5912 else if (S_ISFIFO(mode))
5913 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5914 else if (S_ISSOCK(mode))
5915 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5916}
5917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005919CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005920 const struct cifs_unix_set_info_args *args,
5921 u16 fid, u32 pid_of_opener)
5922{
5923 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005924 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005925 int rc = 0;
5926 u16 params, param_offset, offset, byte_count, count;
5927
Joe Perchesf96637b2013-05-04 22:12:25 -05005928 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005929 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5930
5931 if (rc)
5932 return rc;
5933
5934 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5935 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5936
5937 params = 6;
5938 pSMB->MaxSetupCount = 0;
5939 pSMB->Reserved = 0;
5940 pSMB->Flags = 0;
5941 pSMB->Timeout = 0;
5942 pSMB->Reserved2 = 0;
5943 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5944 offset = param_offset + params;
5945
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005946 data_offset = (char *)pSMB +
5947 offsetof(struct smb_hdr, Protocol) + offset;
5948
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005949 count = sizeof(FILE_UNIX_BASIC_INFO);
5950
5951 pSMB->MaxParameterCount = cpu_to_le16(2);
5952 /* BB find max SMB PDU from sess */
5953 pSMB->MaxDataCount = cpu_to_le16(1000);
5954 pSMB->SetupCount = 1;
5955 pSMB->Reserved3 = 0;
5956 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5957 byte_count = 3 /* pad */ + params + count;
5958 pSMB->DataCount = cpu_to_le16(count);
5959 pSMB->ParameterCount = cpu_to_le16(params);
5960 pSMB->TotalDataCount = pSMB->DataCount;
5961 pSMB->TotalParameterCount = pSMB->ParameterCount;
5962 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5963 pSMB->DataOffset = cpu_to_le16(offset);
5964 pSMB->Fid = fid;
5965 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
5966 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005967 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005968 pSMB->ByteCount = cpu_to_le16(byte_count);
5969
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005970 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005971
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005972 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005973 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005974 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005975 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5976 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005977
5978 /* Note: On -EAGAIN error only caller can retry on handle based calls
5979 since file handle passed in no longer valid */
5980
5981 return rc;
5982}
5983
5984int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005985CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04005986 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04005987 const struct cifs_unix_set_info_args *args,
5988 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989{
5990 TRANSACTION2_SPI_REQ *pSMB = NULL;
5991 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5992 int name_len;
5993 int rc = 0;
5994 int bytes_returned = 0;
5995 FILE_UNIX_BASIC_INFO *data_offset;
5996 __u16 params, param_offset, offset, count, byte_count;
5997
Joe Perchesf96637b2013-05-04 22:12:25 -05005998 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999setPermsRetry:
6000 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6001 (void **) &pSMBr);
6002 if (rc)
6003 return rc;
6004
6005 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6006 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006007 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006008 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 name_len++; /* trailing null */
6010 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006011 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006012 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006014 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 }
6016
6017 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006018 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006020 /* BB find max SMB PDU from sess structure BB */
6021 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 pSMB->MaxSetupCount = 0;
6023 pSMB->Reserved = 0;
6024 pSMB->Flags = 0;
6025 pSMB->Timeout = 0;
6026 pSMB->Reserved2 = 0;
6027 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006028 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 offset = param_offset + params;
6030 data_offset =
6031 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6032 offset);
6033 memset(data_offset, 0, count);
6034 pSMB->DataOffset = cpu_to_le16(offset);
6035 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6036 pSMB->SetupCount = 1;
6037 pSMB->Reserved3 = 0;
6038 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6039 byte_count = 3 /* pad */ + params + count;
6040 pSMB->ParameterCount = cpu_to_le16(params);
6041 pSMB->DataCount = cpu_to_le16(count);
6042 pSMB->TotalParameterCount = pSMB->ParameterCount;
6043 pSMB->TotalDataCount = pSMB->DataCount;
6044 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6045 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006046 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006047
Jeff Layton654cf142009-07-09 20:02:49 -04006048 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
6050 pSMB->ByteCount = cpu_to_le16(byte_count);
6051 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6052 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006053 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006054 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
Steve French0d817bc2008-05-22 02:02:03 +00006056 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 if (rc == -EAGAIN)
6058 goto setPermsRetry;
6059 return rc;
6060}
6061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006063/*
6064 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6065 * function used by listxattr and getxattr type calls. When ea_name is set,
6066 * it looks for that attribute name and stuffs that value into the EAData
6067 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6068 * buffer. In both cases, the return value is either the length of the
6069 * resulting data or a negative error code. If EAData is a NULL pointer then
6070 * the data isn't copied to it, but the length is returned.
6071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006073CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006074 const unsigned char *searchName, const unsigned char *ea_name,
6075 char *EAData, size_t buf_size,
6076 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
6078 /* BB assumes one setup word */
6079 TRANSACTION2_QPI_REQ *pSMB = NULL;
6080 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6081 int rc = 0;
6082 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006083 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006084 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006085 struct fea *temp_fea;
6086 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006087 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006088 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006089 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
Joe Perchesf96637b2013-05-04 22:12:25 -05006091 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092QAllEAsRetry:
6093 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6094 (void **) &pSMBr);
6095 if (rc)
6096 return rc;
6097
6098 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006099 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006100 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6101 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006102 list_len++; /* trailing null */
6103 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006105 list_len = strnlen(searchName, PATH_MAX);
6106 list_len++; /* trailing null */
6107 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 }
6109
Jeff Layton6e462b92010-02-10 16:18:26 -05006110 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 pSMB->TotalDataCount = 0;
6112 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006113 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006114 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 pSMB->MaxSetupCount = 0;
6116 pSMB->Reserved = 0;
6117 pSMB->Flags = 0;
6118 pSMB->Timeout = 0;
6119 pSMB->Reserved2 = 0;
6120 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006121 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 pSMB->DataCount = 0;
6123 pSMB->DataOffset = 0;
6124 pSMB->SetupCount = 1;
6125 pSMB->Reserved3 = 0;
6126 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6127 byte_count = params + 1 /* pad */ ;
6128 pSMB->TotalParameterCount = cpu_to_le16(params);
6129 pSMB->ParameterCount = pSMB->TotalParameterCount;
6130 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6131 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006132 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 pSMB->ByteCount = cpu_to_le16(byte_count);
6134
6135 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6136 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6137 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006138 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006139 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006141
6142
6143 /* BB also check enough total bytes returned */
6144 /* BB we need to improve the validity checking
6145 of these trans2 responses */
6146
6147 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006148 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006149 rc = -EIO; /* bad smb */
6150 goto QAllEAsOut;
6151 }
6152
6153 /* check that length of list is not more than bcc */
6154 /* check that each entry does not go beyond length
6155 of list */
6156 /* check that each element of each entry does not
6157 go beyond end of list */
6158 /* validate_trans2_offsets() */
6159 /* BB check if start of smb + data_offset > &bcc+ bcc */
6160
6161 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6162 ea_response_data = (struct fealist *)
6163 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6164
Jeff Layton6e462b92010-02-10 16:18:26 -05006165 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006166 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006167 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006168 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006169 /* didn't find the named attribute */
6170 if (ea_name)
6171 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006172 goto QAllEAsOut;
6173 }
6174
Jeff Layton0cd126b2010-02-10 16:18:26 -05006175 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006176 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006177 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006178 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006179 rc = -EIO;
6180 goto QAllEAsOut;
6181 }
6182
Jeff Laytonf0d38682010-02-10 16:18:26 -05006183 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006184 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006185 temp_fea = ea_response_data->list;
6186 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006187 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006188 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006189 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006190
Jeff Layton6e462b92010-02-10 16:18:26 -05006191 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006192 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006193 /* make sure we can read name_len and value_len */
6194 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006195 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006196 rc = -EIO;
6197 goto QAllEAsOut;
6198 }
6199
6200 name_len = temp_fea->name_len;
6201 value_len = le16_to_cpu(temp_fea->value_len);
6202 list_len -= name_len + 1 + value_len;
6203 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006204 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006205 rc = -EIO;
6206 goto QAllEAsOut;
6207 }
6208
Jeff Layton31c05192010-02-10 16:18:26 -05006209 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006210 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006211 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006212 temp_ptr += name_len + 1;
6213 rc = value_len;
6214 if (buf_size == 0)
6215 goto QAllEAsOut;
6216 if ((size_t)value_len > buf_size) {
6217 rc = -ERANGE;
6218 goto QAllEAsOut;
6219 }
6220 memcpy(EAData, temp_ptr, value_len);
6221 goto QAllEAsOut;
6222 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006223 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006224 /* account for prefix user. and trailing null */
6225 rc += (5 + 1 + name_len);
6226 if (rc < (int) buf_size) {
6227 memcpy(EAData, "user.", 5);
6228 EAData += 5;
6229 memcpy(EAData, temp_ptr, name_len);
6230 EAData += name_len;
6231 /* null terminate name */
6232 *EAData = 0;
6233 ++EAData;
6234 } else if (buf_size == 0) {
6235 /* skip copy - calc size only */
6236 } else {
6237 /* stop before overrun buffer */
6238 rc = -ERANGE;
6239 break;
6240 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006241 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006242 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006243 temp_fea = (struct fea *)temp_ptr;
6244 }
6245
Jeff Layton31c05192010-02-10 16:18:26 -05006246 /* didn't find the named attribute */
6247 if (ea_name)
6248 rc = -ENODATA;
6249
Jeff Laytonf0d38682010-02-10 16:18:26 -05006250QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006251 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 if (rc == -EAGAIN)
6253 goto QAllEAsRetry;
6254
6255 return (ssize_t)rc;
6256}
6257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006259CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6260 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006261 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6262 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263{
6264 struct smb_com_transaction2_spi_req *pSMB = NULL;
6265 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6266 struct fealist *parm_data;
6267 int name_len;
6268 int rc = 0;
6269 int bytes_returned = 0;
6270 __u16 params, param_offset, byte_count, offset, count;
6271
Joe Perchesf96637b2013-05-04 22:12:25 -05006272 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273SetEARetry:
6274 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6275 (void **) &pSMBr);
6276 if (rc)
6277 return rc;
6278
6279 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6280 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006281 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6282 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 name_len++; /* trailing null */
6284 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006285 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 name_len = strnlen(fileName, PATH_MAX);
6287 name_len++; /* trailing null */
6288 strncpy(pSMB->FileName, fileName, name_len);
6289 }
6290
6291 params = 6 + name_len;
6292
6293 /* done calculating parms using name_len of file name,
6294 now use name_len to calculate length of ea name
6295 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006296 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 name_len = 0;
6298 else
Steve French50c2f752007-07-13 00:33:32 +00006299 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006301 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006303 /* BB find max SMB PDU from sess */
6304 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 pSMB->MaxSetupCount = 0;
6306 pSMB->Reserved = 0;
6307 pSMB->Flags = 0;
6308 pSMB->Timeout = 0;
6309 pSMB->Reserved2 = 0;
6310 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006311 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 offset = param_offset + params;
6313 pSMB->InformationLevel =
6314 cpu_to_le16(SMB_SET_FILE_EA);
6315
6316 parm_data =
6317 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6318 offset);
6319 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6320 pSMB->DataOffset = cpu_to_le16(offset);
6321 pSMB->SetupCount = 1;
6322 pSMB->Reserved3 = 0;
6323 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6324 byte_count = 3 /* pad */ + params + count;
6325 pSMB->DataCount = cpu_to_le16(count);
6326 parm_data->list_len = cpu_to_le32(count);
6327 parm_data->list[0].EA_flags = 0;
6328 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006329 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006331 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006332 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 parm_data->list[0].name[name_len] = 0;
6334 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6335 /* caller ensures that ea_value_len is less than 64K but
6336 we need to ensure that it fits within the smb */
6337
Steve French50c2f752007-07-13 00:33:32 +00006338 /*BB add length check to see if it would fit in
6339 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006340 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6341 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006342 memcpy(parm_data->list[0].name+name_len+1,
6343 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
6345 pSMB->TotalDataCount = pSMB->DataCount;
6346 pSMB->ParameterCount = cpu_to_le16(params);
6347 pSMB->TotalParameterCount = pSMB->ParameterCount;
6348 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006349 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 pSMB->ByteCount = cpu_to_le16(byte_count);
6351 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6352 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006353 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006354 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
6356 cifs_buf_release(pSMB);
6357
6358 if (rc == -EAGAIN)
6359 goto SetEARetry;
6360
6361 return rc;
6362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363#endif
Steve French0eff0e22011-02-24 05:39:23 +00006364
6365#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6366/*
6367 * Years ago the kernel added a "dnotify" function for Samba server,
6368 * to allow network clients (such as Windows) to display updated
6369 * lists of files in directory listings automatically when
6370 * files are added by one user when another user has the
6371 * same directory open on their desktop. The Linux cifs kernel
6372 * client hooked into the kernel side of this interface for
6373 * the same reason, but ironically when the VFS moved from
6374 * "dnotify" to "inotify" it became harder to plug in Linux
6375 * network file system clients (the most obvious use case
6376 * for notify interfaces is when multiple users can update
6377 * the contents of the same directory - exactly what network
6378 * file systems can do) although the server (Samba) could
6379 * still use it. For the short term we leave the worker
6380 * function ifdeffed out (below) until inotify is fixed
6381 * in the VFS to make it easier to plug in network file
6382 * system clients. If inotify turns out to be permanently
6383 * incompatible for network fs clients, we could instead simply
6384 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6385 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006386int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006387 const int notify_subdirs, const __u16 netfid,
6388 __u32 filter, struct file *pfile, int multishot,
6389 const struct nls_table *nls_codepage)
6390{
6391 int rc = 0;
6392 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6393 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6394 struct dir_notify_req *dnotify_req;
6395 int bytes_returned;
6396
Joe Perchesf96637b2013-05-04 22:12:25 -05006397 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006398 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6399 (void **) &pSMBr);
6400 if (rc)
6401 return rc;
6402
6403 pSMB->TotalParameterCount = 0 ;
6404 pSMB->TotalDataCount = 0;
6405 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006406 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006407 pSMB->MaxSetupCount = 4;
6408 pSMB->Reserved = 0;
6409 pSMB->ParameterOffset = 0;
6410 pSMB->DataCount = 0;
6411 pSMB->DataOffset = 0;
6412 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6413 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6414 pSMB->ParameterCount = pSMB->TotalParameterCount;
6415 if (notify_subdirs)
6416 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6417 pSMB->Reserved2 = 0;
6418 pSMB->CompletionFilter = cpu_to_le32(filter);
6419 pSMB->Fid = netfid; /* file handle always le */
6420 pSMB->ByteCount = 0;
6421
6422 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6423 (struct smb_hdr *)pSMBr, &bytes_returned,
6424 CIFS_ASYNC_OP);
6425 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006426 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006427 } else {
6428 /* Add file to outstanding requests */
6429 /* BB change to kmem cache alloc */
6430 dnotify_req = kmalloc(
6431 sizeof(struct dir_notify_req),
6432 GFP_KERNEL);
6433 if (dnotify_req) {
6434 dnotify_req->Pid = pSMB->hdr.Pid;
6435 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6436 dnotify_req->Mid = pSMB->hdr.Mid;
6437 dnotify_req->Tid = pSMB->hdr.Tid;
6438 dnotify_req->Uid = pSMB->hdr.Uid;
6439 dnotify_req->netfid = netfid;
6440 dnotify_req->pfile = pfile;
6441 dnotify_req->filter = filter;
6442 dnotify_req->multishot = multishot;
6443 spin_lock(&GlobalMid_Lock);
6444 list_add_tail(&dnotify_req->lhead,
6445 &GlobalDnotifyReqList);
6446 spin_unlock(&GlobalMid_Lock);
6447 } else
6448 rc = -ENOMEM;
6449 }
6450 cifs_buf_release(pSMB);
6451 return rc;
6452}
6453#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */