blob: f5099fb8a22f14e4c1ca2b18ff135d6b29de8e01 [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
1403cifs_discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001404{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001405 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 Shilovskyc42a6ab2016-11-17 16:20:23 -08001429 length = cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001430 dequeue_mid(mid, rdata->result);
1431 return length;
1432}
1433
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001434int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001435cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1436{
1437 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001438 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001439 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001440 char *buf = server->smallbuf;
1441 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001442
Joe Perchesf96637b2013-05-04 22:12:25 -05001443 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1444 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001445
1446 /*
1447 * read the rest of READ_RSP header (sans Data array), or whatever we
1448 * can if there's not enough data. At this point, we've read down to
1449 * the Mid.
1450 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001451 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001452 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001453
Al Viroa6137302016-01-09 19:37:16 -05001454 length = cifs_read_from_socket(server,
1455 buf + HEADER_SIZE(server) - 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001456 if (length < 0)
1457 return length;
1458 server->total_read += length;
1459
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001460 if (server->ops->is_status_pending &&
1461 server->ops->is_status_pending(buf, server, 0)) {
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001462 cifs_discard_remaining_data(server);
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001463 return -1;
1464 }
1465
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001466 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001467 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001468 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001469 cifs_dbg(FYI, "%s: server returned error %d\n",
1470 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001471 return cifs_readv_discard(server, mid);
1472 }
1473
1474 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001475 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001476 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1477 __func__, server->total_read,
1478 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001479 rdata->result = -EIO;
1480 return cifs_readv_discard(server, mid);
1481 }
1482
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001483 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001484 if (data_offset < server->total_read) {
1485 /*
1486 * win2k8 sometimes sends an offset of 0 when the read
1487 * is beyond the EOF. Treat it as if the data starts just after
1488 * the header.
1489 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001490 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1491 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001492 data_offset = server->total_read;
1493 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1494 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001495 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1496 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001497 rdata->result = -EIO;
1498 return cifs_readv_discard(server, mid);
1499 }
1500
Joe Perchesf96637b2013-05-04 22:12:25 -05001501 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1502 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001503
1504 len = data_offset - server->total_read;
1505 if (len > 0) {
1506 /* read any junk before data into the rest of smallbuf */
Al Viroa6137302016-01-09 19:37:16 -05001507 length = cifs_read_from_socket(server,
1508 buf + server->total_read, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001509 if (length < 0)
1510 return length;
1511 server->total_read += length;
1512 }
1513
1514 /* set up first iov for signature check */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001515 rdata->iov[0].iov_base = buf;
1516 rdata->iov[0].iov_len = 4;
1517 rdata->iov[1].iov_base = buf + 4;
1518 rdata->iov[1].iov_len = server->total_read - 4;
1519 cifs_dbg(FYI, "0: iov_base=%p iov_len=%u\n",
1520 rdata->iov[0].iov_base, server->total_read);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001521
1522 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001523 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001524 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001525 /* data_len is corrupt -- discard frame */
1526 rdata->result = -EIO;
1527 return cifs_readv_discard(server, mid);
1528 }
1529
Jeff Layton8321fec2012-09-19 06:22:32 -07001530 length = rdata->read_into_pages(server, rdata, data_len);
1531 if (length < 0)
1532 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001533
Jeff Layton8321fec2012-09-19 06:22:32 -07001534 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535
Joe Perchesf96637b2013-05-04 22:12:25 -05001536 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1537 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001538
1539 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001540 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001541 return cifs_readv_discard(server, mid);
1542
1543 dequeue_mid(mid, false);
1544 return length;
1545}
1546
1547static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001548cifs_readv_callback(struct mid_q_entry *mid)
1549{
1550 struct cifs_readdata *rdata = mid->callback_data;
1551 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1552 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001553 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1554 .rq_nvec = 2,
Jeff Layton8321fec2012-09-19 06:22:32 -07001555 .rq_pages = rdata->pages,
1556 .rq_npages = rdata->nr_pages,
1557 .rq_pagesz = rdata->pagesz,
1558 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001559
Joe Perchesf96637b2013-05-04 22:12:25 -05001560 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1561 __func__, mid->mid, mid->mid_state, rdata->result,
1562 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001563
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001564 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001565 case MID_RESPONSE_RECEIVED:
1566 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001567 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001568 int rc = 0;
1569
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001570 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001571 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001572 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001573 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1574 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001575 }
1576 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001577 task_io_account_read(rdata->got_bytes);
1578 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001579 break;
1580 case MID_REQUEST_SUBMITTED:
1581 case MID_RETRY_NEEDED:
1582 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001583 if (server->sign && rdata->got_bytes)
1584 /* reset bytes number since we can not check a sign */
1585 rdata->got_bytes = 0;
1586 /* FIXME: should this be counted toward the initiating task? */
1587 task_io_account_read(rdata->got_bytes);
1588 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001589 break;
1590 default:
1591 rdata->result = -EIO;
1592 }
1593
Jeff Laytonda472fc2012-03-23 14:40:53 -04001594 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001595 mutex_lock(&server->srv_mutex);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001596 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001597 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001598 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001599}
1600
1601/* cifs_async_readv - send an async write, and set up mid to handle result */
1602int
1603cifs_async_readv(struct cifs_readdata *rdata)
1604{
1605 int rc;
1606 READ_REQ *smb = NULL;
1607 int wct;
1608 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001609 struct smb_rqst rqst = { .rq_iov = rdata->iov,
1610 .rq_nvec = 2 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001611
Joe Perchesf96637b2013-05-04 22:12:25 -05001612 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1613 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001614
1615 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1616 wct = 12;
1617 else {
1618 wct = 10; /* old style read */
1619 if ((rdata->offset >> 32) > 0) {
1620 /* can not handle this big offset for old */
1621 return -EIO;
1622 }
1623 }
1624
1625 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1626 if (rc)
1627 return rc;
1628
1629 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1630 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1631
1632 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001633 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001634 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1635 if (wct == 12)
1636 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1637 smb->Remaining = 0;
1638 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1639 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1640 if (wct == 12)
1641 smb->ByteCount = 0;
1642 else {
1643 /* old style read */
1644 struct smb_com_readx_req *smbr =
1645 (struct smb_com_readx_req *)smb;
1646 smbr->ByteCount = 0;
1647 }
1648
1649 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08001650 rdata->iov[0].iov_base = smb;
1651 rdata->iov[0].iov_len = 4;
1652 rdata->iov[1].iov_base = (char *)smb + 4;
1653 rdata->iov[1].iov_len = get_rfc1002_length(smb);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001654
Jeff Layton6993f742012-05-16 07:13:17 -04001655 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001656 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08001657 cifs_readv_callback, NULL, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001658
1659 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001660 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001661 else
1662 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001663
1664 cifs_small_buf_release(smb);
1665 return rc;
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001669CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1670 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 int rc = -EACCES;
1673 READ_REQ *pSMB = NULL;
1674 READ_RSP *pSMBr = NULL;
1675 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001676 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001677 int resp_buf_type = 0;
1678 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001679 struct kvec rsp_iov;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001680 __u32 pid = io_parms->pid;
1681 __u16 netfid = io_parms->netfid;
1682 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001683 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001684 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Joe Perchesf96637b2013-05-04 22:12:25 -05001686 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001687 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001688 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001689 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001690 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001691 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001692 /* can not handle this big offset for old */
1693 return -EIO;
1694 }
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001698 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (rc)
1700 return rc;
1701
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001702 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1703 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /* tcon and ses pointer are checked in smb_init */
1706 if (tcon->ses->server == NULL)
1707 return -ECONNABORTED;
1708
Steve Frenchec637e32005-12-12 20:53:18 -08001709 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001711 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001712 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001713 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 pSMB->Remaining = 0;
1716 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1717 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001718 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001719 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1720 else {
1721 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001722 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001723 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001724 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001725 }
Steve Frenchec637e32005-12-12 20:53:18 -08001726
1727 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001728 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001729 rc = SendReceive2(xid, tcon->ses, iov, 1, &resp_buf_type,
1730 CIFS_LOG_ERROR, &rsp_iov);
1731 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001732 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001733 pSMBr = (READ_RSP *)rsp_iov.iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001735 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 } else {
1737 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1738 data_length = data_length << 16;
1739 data_length += le16_to_cpu(pSMBr->DataLength);
1740 *nbytes = data_length;
1741
1742 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001743 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001745 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001746 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 rc = -EIO;
1748 *nbytes = 0;
1749 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001750 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001751 le16_to_cpu(pSMBr->DataOffset);
1752/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001753 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001754 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001755 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001756 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001757 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Steve French790fe572007-07-07 19:25:05 +00001761 if (*buf) {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001762 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French790fe572007-07-07 19:25:05 +00001763 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001764 /* return buffer to caller to free */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07001765 *buf = rsp_iov.iov_base;
Steve French790fe572007-07-07 19:25:05 +00001766 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001767 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001768 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001769 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001770 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001771
1772 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 since file handle passed in no longer valid */
1774 return rc;
1775}
1776
Steve Frenchec637e32005-12-12 20:53:18 -08001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001779CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Al Virodbbab322016-09-05 17:53:43 -04001780 unsigned int *nbytes, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 int rc = -EACCES;
1783 WRITE_REQ *pSMB = NULL;
1784 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001785 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 __u32 bytes_sent;
1787 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001788 __u32 pid = io_parms->pid;
1789 __u16 netfid = io_parms->netfid;
1790 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001791 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001792 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Steve Frencha24e2d72010-04-03 17:20:21 +00001794 *nbytes = 0;
1795
Joe Perchesf96637b2013-05-04 22:12:25 -05001796 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001797 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001798 return -ECONNABORTED;
1799
Steve French790fe572007-07-07 19:25:05 +00001800 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001801 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001802 else {
Steve French1c955182005-08-30 20:58:07 -07001803 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001804 if ((offset >> 32) > 0) {
1805 /* can not handle big offset for old srv */
1806 return -EIO;
1807 }
1808 }
Steve French1c955182005-08-30 20:58:07 -07001809
1810 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 (void **) &pSMBr);
1812 if (rc)
1813 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001814
1815 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1816 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /* tcon and ses pointer are checked in smb_init */
1819 if (tcon->ses->server == NULL)
1820 return -ECONNABORTED;
1821
1822 pSMB->AndXCommand = 0xFF; /* none */
1823 pSMB->Fid = netfid;
1824 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001825 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001826 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 pSMB->Reserved = 0xFFFFFFFF;
1829 pSMB->WriteMode = 0;
1830 pSMB->Remaining = 0;
1831
Steve French50c2f752007-07-13 00:33:32 +00001832 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 can send more if LARGE_WRITE_X capability returned by the server and if
1834 our buffer is big enough or if we convert to iovecs on socket writes
1835 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001836 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1838 } else {
1839 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1840 & ~0xFF;
1841 }
1842
1843 if (bytes_sent > count)
1844 bytes_sent = count;
1845 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001846 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001847 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001848 memcpy(pSMB->Data, buf, bytes_sent);
Al Virodbbab322016-09-05 17:53:43 -04001849 else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* No buffer */
1851 cifs_buf_release(pSMB);
1852 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001853 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001854 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001855 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001856 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001857 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1860 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001861 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001862
Steve French790fe572007-07-07 19:25:05 +00001863 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001864 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001865 else { /* old style write has byte count 4 bytes earlier
1866 so 4 bytes pad */
1867 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001868 (struct smb_com_writex_req *)pSMB;
1869 pSMBW->ByteCount = cpu_to_le16(byte_count);
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Al Virodbbab322016-09-05 17:53:43 -04001873 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001874 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001876 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 } else {
1878 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1879 *nbytes = (*nbytes) << 16;
1880 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301881
1882 /*
1883 * Mask off high 16 bits when bytes written as returned by the
1884 * server is greater than bytes requested by the client. Some
1885 * OS/2 servers are known to set incorrect CountHigh values.
1886 */
1887 if (*nbytes > count)
1888 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
1891 cifs_buf_release(pSMB);
1892
Steve French50c2f752007-07-13 00:33:32 +00001893 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 since file handle passed in no longer valid */
1895
1896 return rc;
1897}
1898
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001899void
1900cifs_writedata_release(struct kref *refcount)
1901{
1902 struct cifs_writedata *wdata = container_of(refcount,
1903 struct cifs_writedata, refcount);
1904
1905 if (wdata->cfile)
1906 cifsFileInfo_put(wdata->cfile);
1907
1908 kfree(wdata);
1909}
1910
1911/*
1912 * Write failed with a retryable error. Resend the write request. It's also
1913 * possible that the page was redirtied so re-clean the page.
1914 */
1915static void
1916cifs_writev_requeue(struct cifs_writedata *wdata)
1917{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001918 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001919 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001920 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001921 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001922
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001923 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1924 i = 0;
1925 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001926 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001927 struct cifs_writedata *wdata2;
1928 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001929
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001930 wsize = server->ops->wp_retry_size(inode);
1931 if (wsize < rest_len) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001932 nr_pages = wsize / PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001933 if (!nr_pages) {
1934 rc = -ENOTSUPP;
1935 break;
1936 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001937 cur_len = nr_pages * PAGE_SIZE;
1938 tailsz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001939 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001940 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001941 cur_len = rest_len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001942 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001943 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001944
1945 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1946 if (!wdata2) {
1947 rc = -ENOMEM;
1948 break;
1949 }
1950
1951 for (j = 0; j < nr_pages; j++) {
1952 wdata2->pages[j] = wdata->pages[i + j];
1953 lock_page(wdata2->pages[j]);
1954 clear_page_dirty_for_io(wdata2->pages[j]);
1955 }
1956
1957 wdata2->sync_mode = wdata->sync_mode;
1958 wdata2->nr_pages = nr_pages;
1959 wdata2->offset = page_offset(wdata2->pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001960 wdata2->pagesz = PAGE_SIZE;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001961 wdata2->tailsz = tailsz;
1962 wdata2->bytes = cur_len;
1963
1964 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1965 if (!wdata2->cfile) {
1966 cifs_dbg(VFS, "No writable handles for inode\n");
1967 rc = -EBADF;
1968 break;
1969 }
1970 wdata2->pid = wdata2->cfile->pid;
1971 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1972
1973 for (j = 0; j < nr_pages; j++) {
1974 unlock_page(wdata2->pages[j]);
1975 if (rc != 0 && rc != -EAGAIN) {
1976 SetPageError(wdata2->pages[j]);
1977 end_page_writeback(wdata2->pages[j]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001978 put_page(wdata2->pages[j]);
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001979 }
1980 }
1981
1982 if (rc) {
1983 kref_put(&wdata2->refcount, cifs_writedata_release);
1984 if (rc == -EAGAIN)
1985 continue;
1986 break;
1987 }
1988
1989 rest_len -= cur_len;
1990 i += nr_pages;
1991 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001992
1993 mapping_set_error(inode->i_mapping, rc);
1994 kref_put(&wdata->refcount, cifs_writedata_release);
1995}
1996
Jeff Laytonc2e87642012-03-23 14:40:55 -04001997void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001998cifs_writev_complete(struct work_struct *work)
1999{
2000 struct cifs_writedata *wdata = container_of(work,
2001 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002002 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002003 int i = 0;
2004
2005 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002006 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002007 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002008 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002009 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2010 wdata->bytes);
2011 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2012 return cifs_writev_requeue(wdata);
2013
2014 for (i = 0; i < wdata->nr_pages; i++) {
2015 struct page *page = wdata->pages[i];
2016 if (wdata->result == -EAGAIN)
2017 __set_page_dirty_nobuffers(page);
2018 else if (wdata->result < 0)
2019 SetPageError(page);
2020 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002021 put_page(page);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002022 }
2023 if (wdata->result != -EAGAIN)
2024 mapping_set_error(inode->i_mapping, wdata->result);
2025 kref_put(&wdata->refcount, cifs_writedata_release);
2026}
2027
2028struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002029cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002030{
2031 struct cifs_writedata *wdata;
2032
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002033 /* writedata + number of page pointers */
2034 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002035 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002036 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002037 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002038 INIT_LIST_HEAD(&wdata->list);
2039 init_completion(&wdata->done);
2040 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002041 }
2042 return wdata;
2043}
2044
2045/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002046 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002047 * workqueue completion task.
2048 */
2049static void
2050cifs_writev_callback(struct mid_q_entry *mid)
2051{
2052 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002053 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002054 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002055 unsigned int written;
2056 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2057
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002058 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002059 case MID_RESPONSE_RECEIVED:
2060 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2061 if (wdata->result != 0)
2062 break;
2063
2064 written = le16_to_cpu(smb->CountHigh);
2065 written <<= 16;
2066 written += le16_to_cpu(smb->Count);
2067 /*
2068 * Mask off high 16 bits when bytes written as returned
2069 * by the server is greater than bytes requested by the
2070 * client. OS/2 servers are known to set incorrect
2071 * CountHigh values.
2072 */
2073 if (written > wdata->bytes)
2074 written &= 0xFFFF;
2075
2076 if (written < wdata->bytes)
2077 wdata->result = -ENOSPC;
2078 else
2079 wdata->bytes = written;
2080 break;
2081 case MID_REQUEST_SUBMITTED:
2082 case MID_RETRY_NEEDED:
2083 wdata->result = -EAGAIN;
2084 break;
2085 default:
2086 wdata->result = -EIO;
2087 break;
2088 }
2089
Jeff Laytonda472fc2012-03-23 14:40:53 -04002090 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002091 mutex_lock(&server->srv_mutex);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002092 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002093 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002094 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002095}
2096
2097/* cifs_async_writev - send an async write, and set up mid to handle result */
2098int
Steve French4a5c80d2014-02-07 20:45:12 -06002099cifs_async_writev(struct cifs_writedata *wdata,
2100 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002101{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002102 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002103 WRITE_REQ *smb = NULL;
2104 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002105 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002106 struct kvec iov[2];
Jeff Laytonfec344e2012-09-18 16:20:35 -07002107 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002108
2109 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2110 wct = 14;
2111 } else {
2112 wct = 12;
2113 if (wdata->offset >> 32 > 0) {
2114 /* can not handle big offset for old srv */
2115 return -EIO;
2116 }
2117 }
2118
2119 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2120 if (rc)
2121 goto async_writev_out;
2122
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002123 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2124 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002125
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002126 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002127 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002128 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2129 if (wct == 14)
2130 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2131 smb->Reserved = 0xFFFFFFFF;
2132 smb->WriteMode = 0;
2133 smb->Remaining = 0;
2134
2135 smb->DataOffset =
2136 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2137
2138 /* 4 for RFC1001 length + 1 for BCC */
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002139 iov[0].iov_len = 4;
2140 iov[0].iov_base = smb;
2141 iov[1].iov_len = get_rfc1002_length(smb) + 1;
2142 iov[1].iov_base = (char *)smb + 4;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002143
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002144 rqst.rq_iov = iov;
2145 rqst.rq_nvec = 2;
Jeff Laytoneddb0792012-09-18 16:20:35 -07002146 rqst.rq_pages = wdata->pages;
2147 rqst.rq_npages = wdata->nr_pages;
2148 rqst.rq_pagesz = wdata->pagesz;
2149 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002150
Joe Perchesf96637b2013-05-04 22:12:25 -05002151 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2152 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002153
2154 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2155 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2156
2157 if (wct == 14) {
2158 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2159 put_bcc(wdata->bytes + 1, &smb->hdr);
2160 } else {
2161 /* wct == 12 */
2162 struct smb_com_writex_req *smbw =
2163 (struct smb_com_writex_req *)smb;
2164 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2165 put_bcc(wdata->bytes + 5, &smbw->hdr);
Pavel Shilovsky738f9de2016-11-23 15:14:57 -08002166 iov[1].iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002167 }
2168
2169 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002170 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
Pavel Shilovsky9b7c18a2016-11-16 14:06:17 -08002171 cifs_writev_callback, NULL, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002172
2173 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002174 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002175 else
Steve French4a5c80d2014-02-07 20:45:12 -06002176 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002177
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002178async_writev_out:
2179 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002180 return rc;
2181}
2182
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002183int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002184CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002185 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 int rc = -EACCES;
2188 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002189 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002190 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002191 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002192 __u32 pid = io_parms->pid;
2193 __u16 netfid = io_parms->netfid;
2194 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002195 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002196 unsigned int count = io_parms->length;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002197 struct kvec rsp_iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002199 *nbytes = 0;
2200
Joe Perchesf96637b2013-05-04 22:12:25 -05002201 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002202
Steve French4c3130e2008-12-09 00:28:16 +00002203 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002204 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002205 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002206 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002207 if ((offset >> 32) > 0) {
2208 /* can not handle big offset for old srv */
2209 return -EIO;
2210 }
2211 }
Steve French8cc64c62005-10-03 13:49:43 -07002212 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (rc)
2214 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002215
2216 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2217 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /* tcon and ses pointer are checked in smb_init */
2220 if (tcon->ses->server == NULL)
2221 return -ECONNABORTED;
2222
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002223 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 pSMB->Fid = netfid;
2225 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002226 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002227 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 pSMB->Reserved = 0xFFFFFFFF;
2229 pSMB->WriteMode = 0;
2230 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002233 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Steve French3e844692005-10-03 13:37:24 -07002235 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2236 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002237 /* header + 1 byte pad */
2238 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002239 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002240 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002241 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002242 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002243 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002244 pSMB->ByteCount = cpu_to_le16(count + 1);
2245 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002246 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002247 (struct smb_com_writex_req *)pSMB;
2248 pSMBW->ByteCount = cpu_to_le16(count + 5);
2249 }
Steve French3e844692005-10-03 13:37:24 -07002250 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002251 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002252 iov[0].iov_len = smb_hdr_len + 4;
2253 else /* wct == 12 pad bigger by four bytes */
2254 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002255
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002256 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0,
2257 &rsp_iov);
2258 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002259 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002261 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002262 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002263 /* presumably this can not happen, but best to be safe */
2264 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002265 } else {
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002266 WRITE_RSP *pSMBr = (WRITE_RSP *)rsp_iov.iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002267 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2268 *nbytes = (*nbytes) << 16;
2269 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302270
2271 /*
2272 * Mask off high 16 bits when bytes written as returned by the
2273 * server is greater than bytes requested by the client. OS/2
2274 * servers are known to set incorrect CountHigh values.
2275 */
2276 if (*nbytes > count)
2277 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002280 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Steve French50c2f752007-07-13 00:33:32 +00002282 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 since file handle passed in no longer valid */
2284
2285 return rc;
2286}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002287
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002288int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2289 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002290 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2291{
2292 int rc = 0;
2293 LOCK_REQ *pSMB = NULL;
2294 struct kvec iov[2];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002295 struct kvec rsp_iov;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002296 int resp_buf_type;
2297 __u16 count;
2298
Joe Perchesf96637b2013-05-04 22:12:25 -05002299 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2300 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002301
2302 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2303 if (rc)
2304 return rc;
2305
2306 pSMB->Timeout = 0;
2307 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2308 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2309 pSMB->LockType = lock_type;
2310 pSMB->AndXCommand = 0xFF; /* none */
2311 pSMB->Fid = netfid; /* netfid stays le */
2312
2313 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2314 inc_rfc1001_len(pSMB, count);
2315 pSMB->ByteCount = cpu_to_le16(count);
2316
2317 iov[0].iov_base = (char *)pSMB;
2318 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2319 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2320 iov[1].iov_base = (char *)buf;
2321 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2322
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002323 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002324 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP,
2325 &rsp_iov);
2326 cifs_small_buf_release(pSMB);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002327 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002328 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002329
2330 return rc;
2331}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002334CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002335 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002337 const __u32 numLock, const __u8 lockType,
2338 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 int rc = 0;
2341 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002342/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002344 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 __u16 count;
2346
Joe Perchesf96637b2013-05-04 22:12:25 -05002347 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2348 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002349 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (rc)
2352 return rc;
2353
Steve French790fe572007-07-07 19:25:05 +00002354 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002355 /* no response expected */
2356 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002358 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002359 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2361 } else {
2362 pSMB->Timeout = 0;
2363 }
2364
2365 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2366 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2367 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002368 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 pSMB->AndXCommand = 0xFF; /* none */
2370 pSMB->Fid = smb_file_id; /* netfid stays le */
2371
Steve French790fe572007-07-07 19:25:05 +00002372 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002373 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /* BB where to store pid high? */
2375 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2376 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2377 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2378 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2379 count = sizeof(LOCKING_ANDX_RANGE);
2380 } else {
2381 /* oplock break */
2382 count = 0;
2383 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002384 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 pSMB->ByteCount = cpu_to_le16(count);
2386
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002387 if (waitFlag)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002388 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002389 (struct smb_hdr *) pSMB, &bytes_returned);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002390 else
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002391 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002392 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002393 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002394 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002395 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Steve French50c2f752007-07-13 00:33:32 +00002397 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 since file handle passed in no longer valid */
2399 return rc;
2400}
2401
2402int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002403CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002404 const __u16 smb_file_id, const __u32 netpid,
2405 const loff_t start_offset, const __u64 len,
2406 struct file_lock *pLockData, const __u16 lock_type,
2407 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002408{
2409 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2410 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002411 struct cifs_posix_lock *parm_data;
2412 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002413 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002414 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002415 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002416 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002417 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002418 struct kvec rsp_iov;
Steve French08547b02006-02-28 22:39:25 +00002419
Joe Perchesf96637b2013-05-04 22:12:25 -05002420 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002421
Steve French08547b02006-02-28 22:39:25 +00002422 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2423
2424 if (rc)
2425 return rc;
2426
2427 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2428
Steve French50c2f752007-07-13 00:33:32 +00002429 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002430 pSMB->MaxSetupCount = 0;
2431 pSMB->Reserved = 0;
2432 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002433 pSMB->Reserved2 = 0;
2434 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2435 offset = param_offset + params;
2436
Steve French08547b02006-02-28 22:39:25 +00002437 count = sizeof(struct cifs_posix_lock);
2438 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002439 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002440 pSMB->SetupCount = 1;
2441 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002442 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002443 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2444 else
2445 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2446 byte_count = 3 /* pad */ + params + count;
2447 pSMB->DataCount = cpu_to_le16(count);
2448 pSMB->ParameterCount = cpu_to_le16(params);
2449 pSMB->TotalDataCount = pSMB->DataCount;
2450 pSMB->TotalParameterCount = pSMB->ParameterCount;
2451 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002452 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002453 (((char *) &pSMB->hdr.Protocol) + offset);
2454
2455 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002456 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002457 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002458 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002459 pSMB->Timeout = cpu_to_le32(-1);
2460 } else
2461 pSMB->Timeout = 0;
2462
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002463 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002464 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002465 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002466
2467 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002468 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002469 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2470 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002471 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002472 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002473 if (waitFlag) {
2474 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2475 (struct smb_hdr *) pSMBr, &bytes_returned);
2476 } else {
Steve French133672e2007-11-13 22:41:37 +00002477 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002478 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002479 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002480 &resp_buf_type, timeout, &rsp_iov);
2481 pSMBr = (struct smb_com_transaction2_sfi_rsp *)rsp_iov.iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002482 }
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002483 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002484
Steve French08547b02006-02-28 22:39:25 +00002485 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002486 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002487 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002488 /* lock structure can be returned on get */
2489 __u16 data_offset;
2490 __u16 data_count;
2491 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002492
Jeff Layton820a8032011-05-04 08:05:26 -04002493 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002494 rc = -EIO; /* bad smb */
2495 goto plk_err_exit;
2496 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002497 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2498 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002499 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002500 rc = -EIO;
2501 goto plk_err_exit;
2502 }
2503 parm_data = (struct cifs_posix_lock *)
2504 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002505 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002506 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002507 else {
2508 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002509 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002510 pLockData->fl_type = F_RDLCK;
2511 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002512 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002513 pLockData->fl_type = F_WRLCK;
2514
Steve French5443d132011-03-13 05:08:25 +00002515 pLockData->fl_start = le64_to_cpu(parm_data->start);
2516 pLockData->fl_end = pLockData->fl_start +
2517 le64_to_cpu(parm_data->length) - 1;
2518 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002519 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002520 }
Steve French50c2f752007-07-13 00:33:32 +00002521
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002522plk_err_exit:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002523 free_rsp_buf(resp_buf_type, rsp_iov.iov_base);
Steve French133672e2007-11-13 22:41:37 +00002524
Steve French08547b02006-02-28 22:39:25 +00002525 /* Note: On -EAGAIN error only caller can retry on handle based calls
2526 since file handle passed in no longer valid */
2527
2528 return rc;
2529}
2530
2531
2532int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002533CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 int rc = 0;
2536 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002537 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539/* do not retry on dead session on close */
2540 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002541 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return 0;
2543 if (rc)
2544 return rc;
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002547 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002549 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002550 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002551 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002553 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002555 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557 }
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002560 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 rc = 0;
2562
2563 return rc;
2564}
2565
2566int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002567CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002568{
2569 int rc = 0;
2570 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002571 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002572
2573 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2574 if (rc)
2575 return rc;
2576
2577 pSMB->FileID = (__u16) smb_file_id;
2578 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002579 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07002580 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002581 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002582 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002583 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002584
2585 return rc;
2586}
2587
2588int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002589CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002590 const char *from_name, const char *to_name,
2591 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 int rc = 0;
2594 RENAME_REQ *pSMB = NULL;
2595 RENAME_RSP *pSMBr = NULL;
2596 int bytes_returned;
2597 int name_len, name_len2;
2598 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002599 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Joe Perchesf96637b2013-05-04 22:12:25 -05002601 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602renameRetry:
2603 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2604 (void **) &pSMBr);
2605 if (rc)
2606 return rc;
2607
2608 pSMB->BufferFormat = 0x04;
2609 pSMB->SearchAttributes =
2610 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2611 ATTR_DIRECTORY);
2612
2613 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002614 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2615 from_name, PATH_MAX,
2616 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 name_len++; /* trailing null */
2618 name_len *= 2;
2619 pSMB->OldFileName[name_len] = 0x04; /* pad */
2620 /* protocol requires ASCII signature byte on Unicode string */
2621 pSMB->OldFileName[name_len + 1] = 0x00;
2622 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002623 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002624 to_name, PATH_MAX, cifs_sb->local_nls,
2625 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2627 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002628 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002629 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002631 strncpy(pSMB->OldFileName, from_name, name_len);
2632 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 name_len2++; /* trailing null */
2634 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002635 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 name_len2++; /* trailing null */
2637 name_len2++; /* signature byte */
2638 }
2639
2640 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002641 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 pSMB->ByteCount = cpu_to_le16(count);
2643
2644 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2645 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002646 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002647 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002648 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 cifs_buf_release(pSMB);
2651
2652 if (rc == -EAGAIN)
2653 goto renameRetry;
2654
2655 return rc;
2656}
2657
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002658int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002659 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002660 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2663 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002664 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 char *data_offset;
2666 char dummy_string[30];
2667 int rc = 0;
2668 int bytes_returned = 0;
2669 int len_of_str;
2670 __u16 params, param_offset, offset, count, byte_count;
2671
Joe Perchesf96637b2013-05-04 22:12:25 -05002672 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2674 (void **) &pSMBr);
2675 if (rc)
2676 return rc;
2677
2678 params = 6;
2679 pSMB->MaxSetupCount = 0;
2680 pSMB->Reserved = 0;
2681 pSMB->Flags = 0;
2682 pSMB->Timeout = 0;
2683 pSMB->Reserved2 = 0;
2684 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2685 offset = param_offset + params;
2686
2687 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2688 rename_info = (struct set_file_rename *) data_offset;
2689 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002690 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 pSMB->SetupCount = 1;
2692 pSMB->Reserved3 = 0;
2693 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2694 byte_count = 3 /* pad */ + params;
2695 pSMB->ParameterCount = cpu_to_le16(params);
2696 pSMB->TotalParameterCount = pSMB->ParameterCount;
2697 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2698 pSMB->DataOffset = cpu_to_le16(offset);
2699 /* construct random name ".cifs_tmp<inodenum><mid>" */
2700 rename_info->overwrite = cpu_to_le32(1);
2701 rename_info->root_fid = 0;
2702 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002703 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002704 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002705 len_of_str =
2706 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002707 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002709 len_of_str =
2710 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002711 target_name, PATH_MAX, nls_codepage,
2712 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
2714 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002715 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 byte_count += count;
2717 pSMB->DataCount = cpu_to_le16(count);
2718 pSMB->TotalDataCount = pSMB->DataCount;
2719 pSMB->Fid = netfid;
2720 pSMB->InformationLevel =
2721 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2722 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002723 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 pSMB->ByteCount = cpu_to_le16(byte_count);
2725 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002726 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002727 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002728 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002729 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2730 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 cifs_buf_release(pSMB);
2733
2734 /* Note: On -EAGAIN error only caller can retry on handle based calls
2735 since file handle passed in no longer valid */
2736
2737 return rc;
2738}
2739
2740int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002741CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2742 const char *fromName, const __u16 target_tid, const char *toName,
2743 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 int rc = 0;
2746 COPY_REQ *pSMB = NULL;
2747 COPY_RSP *pSMBr = NULL;
2748 int bytes_returned;
2749 int name_len, name_len2;
2750 __u16 count;
2751
Joe Perchesf96637b2013-05-04 22:12:25 -05002752 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753copyRetry:
2754 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2755 (void **) &pSMBr);
2756 if (rc)
2757 return rc;
2758
2759 pSMB->BufferFormat = 0x04;
2760 pSMB->Tid2 = target_tid;
2761
2762 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2763
2764 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002765 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2766 fromName, PATH_MAX, nls_codepage,
2767 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 name_len++; /* trailing null */
2769 name_len *= 2;
2770 pSMB->OldFileName[name_len] = 0x04; /* pad */
2771 /* protocol requires ASCII signature byte on Unicode string */
2772 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002773 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002774 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2775 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2777 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002778 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 name_len = strnlen(fromName, PATH_MAX);
2780 name_len++; /* trailing null */
2781 strncpy(pSMB->OldFileName, fromName, name_len);
2782 name_len2 = strnlen(toName, PATH_MAX);
2783 name_len2++; /* trailing null */
2784 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2785 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2786 name_len2++; /* trailing null */
2787 name_len2++; /* signature byte */
2788 }
2789
2790 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002791 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 pSMB->ByteCount = cpu_to_le16(count);
2793
2794 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2795 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2796 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002797 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2798 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Steve French0d817bc2008-05-22 02:02:03 +00002800 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 if (rc == -EAGAIN)
2803 goto copyRetry;
2804
2805 return rc;
2806}
2807
2808int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002809CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002811 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 TRANSACTION2_SPI_REQ *pSMB = NULL;
2814 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2815 char *data_offset;
2816 int name_len;
2817 int name_len_target;
2818 int rc = 0;
2819 int bytes_returned = 0;
2820 __u16 params, param_offset, offset, byte_count;
2821
Joe Perchesf96637b2013-05-04 22:12:25 -05002822 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823createSymLinkRetry:
2824 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2825 (void **) &pSMBr);
2826 if (rc)
2827 return rc;
2828
2829 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2830 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002831 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2832 /* find define for this maxpathcomponent */
2833 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 name_len++; /* trailing null */
2835 name_len *= 2;
2836
Steve French50c2f752007-07-13 00:33:32 +00002837 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 name_len = strnlen(fromName, PATH_MAX);
2839 name_len++; /* trailing null */
2840 strncpy(pSMB->FileName, fromName, name_len);
2841 }
2842 params = 6 + name_len;
2843 pSMB->MaxSetupCount = 0;
2844 pSMB->Reserved = 0;
2845 pSMB->Flags = 0;
2846 pSMB->Timeout = 0;
2847 pSMB->Reserved2 = 0;
2848 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002849 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 offset = param_offset + params;
2851
2852 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2853 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2854 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002855 cifsConvertToUTF16((__le16 *) data_offset, toName,
2856 /* find define for this maxpathcomponent */
2857 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 name_len_target++; /* trailing null */
2859 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002860 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 name_len_target = strnlen(toName, PATH_MAX);
2862 name_len_target++; /* trailing null */
2863 strncpy(data_offset, toName, name_len_target);
2864 }
2865
2866 pSMB->MaxParameterCount = cpu_to_le16(2);
2867 /* BB find exact max on data count below from sess */
2868 pSMB->MaxDataCount = cpu_to_le16(1000);
2869 pSMB->SetupCount = 1;
2870 pSMB->Reserved3 = 0;
2871 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2872 byte_count = 3 /* pad */ + params + name_len_target;
2873 pSMB->DataCount = cpu_to_le16(name_len_target);
2874 pSMB->ParameterCount = cpu_to_le16(params);
2875 pSMB->TotalDataCount = pSMB->DataCount;
2876 pSMB->TotalParameterCount = pSMB->ParameterCount;
2877 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2878 pSMB->DataOffset = cpu_to_le16(offset);
2879 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2880 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002881 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 pSMB->ByteCount = cpu_to_le16(byte_count);
2883 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2884 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002885 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002886 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002887 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2888 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Steve French0d817bc2008-05-22 02:02:03 +00002890 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 if (rc == -EAGAIN)
2893 goto createSymLinkRetry;
2894
2895 return rc;
2896}
2897
2898int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002899CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002901 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 TRANSACTION2_SPI_REQ *pSMB = NULL;
2904 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2905 char *data_offset;
2906 int name_len;
2907 int name_len_target;
2908 int rc = 0;
2909 int bytes_returned = 0;
2910 __u16 params, param_offset, offset, byte_count;
2911
Joe Perchesf96637b2013-05-04 22:12:25 -05002912 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913createHardLinkRetry:
2914 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2915 (void **) &pSMBr);
2916 if (rc)
2917 return rc;
2918
2919 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002920 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2921 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 name_len++; /* trailing null */
2923 name_len *= 2;
2924
Steve French50c2f752007-07-13 00:33:32 +00002925 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 name_len = strnlen(toName, PATH_MAX);
2927 name_len++; /* trailing null */
2928 strncpy(pSMB->FileName, toName, name_len);
2929 }
2930 params = 6 + name_len;
2931 pSMB->MaxSetupCount = 0;
2932 pSMB->Reserved = 0;
2933 pSMB->Flags = 0;
2934 pSMB->Timeout = 0;
2935 pSMB->Reserved2 = 0;
2936 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002937 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 offset = param_offset + params;
2939
2940 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2941 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2942 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002943 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2944 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 name_len_target++; /* trailing null */
2946 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002947 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 name_len_target = strnlen(fromName, PATH_MAX);
2949 name_len_target++; /* trailing null */
2950 strncpy(data_offset, fromName, name_len_target);
2951 }
2952
2953 pSMB->MaxParameterCount = cpu_to_le16(2);
2954 /* BB find exact max on data count below from sess*/
2955 pSMB->MaxDataCount = cpu_to_le16(1000);
2956 pSMB->SetupCount = 1;
2957 pSMB->Reserved3 = 0;
2958 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2959 byte_count = 3 /* pad */ + params + name_len_target;
2960 pSMB->ParameterCount = cpu_to_le16(params);
2961 pSMB->TotalParameterCount = pSMB->ParameterCount;
2962 pSMB->DataCount = cpu_to_le16(name_len_target);
2963 pSMB->TotalDataCount = pSMB->DataCount;
2964 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2965 pSMB->DataOffset = cpu_to_le16(offset);
2966 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2967 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002968 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 pSMB->ByteCount = cpu_to_le16(byte_count);
2970 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2971 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002972 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002973 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002974 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2975 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 cifs_buf_release(pSMB);
2978 if (rc == -EAGAIN)
2979 goto createHardLinkRetry;
2980
2981 return rc;
2982}
2983
2984int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002985CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002986 const char *from_name, const char *to_name,
2987 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 int rc = 0;
2990 NT_RENAME_REQ *pSMB = NULL;
2991 RENAME_RSP *pSMBr = NULL;
2992 int bytes_returned;
2993 int name_len, name_len2;
2994 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002995 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Joe Perchesf96637b2013-05-04 22:12:25 -05002997 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998winCreateHardLinkRetry:
2999
3000 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
3001 (void **) &pSMBr);
3002 if (rc)
3003 return rc;
3004
3005 pSMB->SearchAttributes =
3006 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3007 ATTR_DIRECTORY);
3008 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3009 pSMB->ClusterCount = 0;
3010
3011 pSMB->BufferFormat = 0x04;
3012
3013 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3014 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003015 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3016 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 name_len++; /* trailing null */
3018 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003019
3020 /* protocol specifies ASCII buffer format (0x04) for unicode */
3021 pSMB->OldFileName[name_len] = 0x04;
3022 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003024 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003025 to_name, PATH_MAX, cifs_sb->local_nls,
3026 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3028 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003029 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003030 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003032 strncpy(pSMB->OldFileName, from_name, name_len);
3033 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 name_len2++; /* trailing null */
3035 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003036 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 name_len2++; /* trailing null */
3038 name_len2++; /* signature byte */
3039 }
3040
3041 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003042 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 pSMB->ByteCount = cpu_to_le16(count);
3044
3045 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3046 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003047 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003048 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003049 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003050
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 cifs_buf_release(pSMB);
3052 if (rc == -EAGAIN)
3053 goto winCreateHardLinkRetry;
3054
3055 return rc;
3056}
3057
3058int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003059CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003060 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003061 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063/* SMB_QUERY_FILE_UNIX_LINK */
3064 TRANSACTION2_QPI_REQ *pSMB = NULL;
3065 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3066 int rc = 0;
3067 int bytes_returned;
3068 int name_len;
3069 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003070 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Joe Perchesf96637b2013-05-04 22:12:25 -05003072 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074querySymLinkRetry:
3075 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3076 (void **) &pSMBr);
3077 if (rc)
3078 return rc;
3079
3080 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3081 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003082 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3083 searchName, PATH_MAX, nls_codepage,
3084 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 name_len++; /* trailing null */
3086 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003087 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 name_len = strnlen(searchName, PATH_MAX);
3089 name_len++; /* trailing null */
3090 strncpy(pSMB->FileName, searchName, name_len);
3091 }
3092
3093 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3094 pSMB->TotalDataCount = 0;
3095 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003096 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 pSMB->MaxSetupCount = 0;
3098 pSMB->Reserved = 0;
3099 pSMB->Flags = 0;
3100 pSMB->Timeout = 0;
3101 pSMB->Reserved2 = 0;
3102 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003103 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 pSMB->DataCount = 0;
3105 pSMB->DataOffset = 0;
3106 pSMB->SetupCount = 1;
3107 pSMB->Reserved3 = 0;
3108 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3109 byte_count = params + 1 /* pad */ ;
3110 pSMB->TotalParameterCount = cpu_to_le16(params);
3111 pSMB->ParameterCount = pSMB->TotalParameterCount;
3112 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3113 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003114 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 pSMB->ByteCount = cpu_to_le16(byte_count);
3116
3117 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3118 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3119 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003120 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 } else {
3122 /* decode response */
3123
3124 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003126 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003127 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003129 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003130 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Jeff Layton460b9692009-04-30 07:17:56 -04003132 data_start = ((char *) &pSMBr->hdr.Protocol) +
3133 le16_to_cpu(pSMBr->t2.DataOffset);
3134
Steve French0e0d2cf2009-05-01 05:27:32 +00003135 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3136 is_unicode = true;
3137 else
3138 is_unicode = false;
3139
Steve French737b7582005-04-28 22:41:06 -07003140 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003141 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3142 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003143 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003144 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146 }
3147 cifs_buf_release(pSMB);
3148 if (rc == -EAGAIN)
3149 goto querySymLinkRetry;
3150 return rc;
3151}
3152
Steve Frenchc52a95542011-02-24 06:16:22 +00003153/*
3154 * Recent Windows versions now create symlinks more frequently
3155 * and they use the "reparse point" mechanism below. We can of course
3156 * do symlinks nicely to Samba and other servers which support the
3157 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3158 * "MF" symlinks optionally, but for recent Windows we really need to
3159 * reenable the code below and fix the cifs_symlink callers to handle this.
3160 * In the interim this code has been moved to its own config option so
3161 * it is not compiled in by default until callers fixed up and more tested.
3162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003164CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3165 __u16 fid, char **symlinkinfo,
3166 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
3168 int rc = 0;
3169 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003170 struct smb_com_transaction_ioctl_req *pSMB;
3171 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003172 bool is_unicode;
3173 unsigned int sub_len;
3174 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003175 struct reparse_symlink_data *reparse_buf;
3176 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003177 __u32 data_offset, data_count;
3178 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003180 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3182 (void **) &pSMBr);
3183 if (rc)
3184 return rc;
3185
3186 pSMB->TotalParameterCount = 0 ;
3187 pSMB->TotalDataCount = 0;
3188 pSMB->MaxParameterCount = cpu_to_le32(2);
3189 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003190 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 pSMB->MaxSetupCount = 4;
3192 pSMB->Reserved = 0;
3193 pSMB->ParameterOffset = 0;
3194 pSMB->DataCount = 0;
3195 pSMB->DataOffset = 0;
3196 pSMB->SetupCount = 4;
3197 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3198 pSMB->ParameterCount = pSMB->TotalParameterCount;
3199 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3200 pSMB->IsFsctl = 1; /* FSCTL */
3201 pSMB->IsRootFlag = 0;
3202 pSMB->Fid = fid; /* file handle always le */
3203 pSMB->ByteCount = 0;
3204
3205 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3206 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3207 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003208 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003209 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Steve French989c7e52009-05-02 05:32:20 +00003211
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003212 data_offset = le32_to_cpu(pSMBr->DataOffset);
3213 data_count = le32_to_cpu(pSMBr->DataCount);
3214 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3215 /* BB also check enough total bytes returned */
3216 rc = -EIO; /* bad smb */
3217 goto qreparse_out;
3218 }
3219 if (!data_count || (data_count > 2048)) {
3220 rc = -EIO;
3221 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3222 goto qreparse_out;
3223 }
3224 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003225 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003226 ((char *)&pSMBr->hdr.Protocol + data_offset);
3227 if ((char *)reparse_buf >= end_of_smb) {
3228 rc = -EIO;
3229 goto qreparse_out;
3230 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003231 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3232 cifs_dbg(FYI, "NFS style reparse tag\n");
3233 posix_buf = (struct reparse_posix_data *)reparse_buf;
3234
3235 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3236 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3237 le64_to_cpu(posix_buf->InodeType));
3238 rc = -EOPNOTSUPP;
3239 goto qreparse_out;
3240 }
3241 is_unicode = true;
3242 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3243 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3244 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3245 rc = -EIO;
3246 goto qreparse_out;
3247 }
3248 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3249 sub_len, is_unicode, nls_codepage);
3250 goto qreparse_out;
3251 } else if (reparse_buf->ReparseTag !=
3252 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3253 rc = -EOPNOTSUPP;
3254 goto qreparse_out;
3255 }
3256
3257 /* Reparse tag is NTFS symlink */
3258 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3259 reparse_buf->PathBuffer;
3260 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3261 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003262 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3263 rc = -EIO;
3264 goto qreparse_out;
3265 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003266 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3267 is_unicode = true;
3268 else
3269 is_unicode = false;
3270
3271 /* BB FIXME investigate remapping reserved chars here */
3272 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3273 nls_codepage);
3274 if (!*symlinkinfo)
3275 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003277 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003279 /*
3280 * Note: On -EAGAIN error only caller can retry on handle based calls
3281 * since file handle passed in no longer valid.
3282 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return rc;
3284}
3285
Steve Frenchc7f508a2013-10-14 15:27:32 -05003286int
3287CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3288 __u16 fid)
3289{
3290 int rc = 0;
3291 int bytes_returned;
3292 struct smb_com_transaction_compr_ioctl_req *pSMB;
3293 struct smb_com_transaction_ioctl_rsp *pSMBr;
3294
3295 cifs_dbg(FYI, "Set compression for %u\n", fid);
3296 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3297 (void **) &pSMBr);
3298 if (rc)
3299 return rc;
3300
3301 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3302
3303 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003304 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003305 pSMB->MaxParameterCount = 0;
3306 pSMB->MaxDataCount = 0;
3307 pSMB->MaxSetupCount = 4;
3308 pSMB->Reserved = 0;
3309 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003310 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003311 pSMB->DataOffset =
3312 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3313 compression_state) - 4); /* 84 */
3314 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003315 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003316 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003317 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003318 pSMB->IsFsctl = 1; /* FSCTL */
3319 pSMB->IsRootFlag = 0;
3320 pSMB->Fid = fid; /* file handle always le */
3321 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003322 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003323 inc_rfc1001_len(pSMB, 5);
3324
3325 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3326 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3327 if (rc)
3328 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3329
3330 cifs_buf_release(pSMB);
3331
3332 /*
3333 * Note: On -EAGAIN error only caller can retry on handle based calls
3334 * since file handle passed in no longer valid.
3335 */
3336 return rc;
3337}
3338
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340#ifdef CONFIG_CIFS_POSIX
3341
3342/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003343static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
Steve French50c2f752007-07-13 00:33:32 +00003344 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
3346 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003347 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3348 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3349 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003350/*
3351 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3352 ace->e_perm, ace->e_tag, ace->e_id);
3353*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 return;
3356}
3357
3358/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003359static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3360 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
3362 int size = 0;
3363 int i;
3364 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003365 struct cifs_posix_ace *pACE;
3366 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003367 struct posix_acl_xattr_header *local_acl = (void *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3370 return -EOPNOTSUPP;
3371
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003372 if (acl_type == ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 count = le16_to_cpu(cifs_acl->access_entry_count);
3374 pACE = &cifs_acl->ace_array[0];
3375 size = sizeof(struct cifs_posix_acl);
3376 size += sizeof(struct cifs_posix_ace) * count;
3377 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003378 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003379 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3380 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return -EINVAL;
3382 }
Andreas Gruenbacher45987e02016-04-14 00:30:14 +02003383 } else if (acl_type == ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 count = le16_to_cpu(cifs_acl->access_entry_count);
3385 size = sizeof(struct cifs_posix_acl);
3386 size += sizeof(struct cifs_posix_ace) * count;
3387/* skip past access ACEs to get to default ACEs */
3388 pACE = &cifs_acl->ace_array[count];
3389 count = le16_to_cpu(cifs_acl->default_entry_count);
3390 size += sizeof(struct cifs_posix_ace) * count;
3391 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003392 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return -EINVAL;
3394 } else {
3395 /* illegal type */
3396 return -EINVAL;
3397 }
3398
3399 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003400 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003401 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003402 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return -ERANGE;
3404 } else /* buffer big enough */ {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003405 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
3406
Steve Frenchff7feac2005-11-15 16:45:16 -08003407 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003408 for (i = 0; i < count ; i++) {
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003409 cifs_convert_ace(&ace[i], pACE);
Steve French50c2f752007-07-13 00:33:32 +00003410 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 }
3412 }
3413 return size;
3414}
3415
Steve French50c2f752007-07-13 00:33:32 +00003416static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003417 const struct posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
3419 __u16 rc = 0; /* 0 = ACL converted ok */
3420
Steve Frenchff7feac2005-11-15 16:45:16 -08003421 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3422 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003424 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /* Probably no need to le convert -1 on any arch but can not hurt */
3426 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003427 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003428 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003429/*
3430 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3431 ace->e_perm, ace->e_tag, ace->e_id);
3432*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return rc;
3434}
3435
3436/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003437static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3438 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
3440 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003441 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
Andreas Gruenbacher2211d5b2016-09-27 13:03:22 +02003442 struct posix_acl_xattr_header *local_acl = (void *)pACL;
Eryu Guanae9ebe72016-10-24 20:46:40 +08003443 struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 int count;
3445 int i;
3446
Steve French790fe572007-07-07 19:25:05 +00003447 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 return 0;
3449
3450 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003451 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3452 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003453 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003454 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3455 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return 0;
3457 }
3458 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003459 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003460 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003461 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003462 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003463 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003464 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003465 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003466 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 return 0;
3468 }
Steve French50c2f752007-07-13 00:33:32 +00003469 for (i = 0; i < count; i++) {
Eryu Guanae9ebe72016-10-24 20:46:40 +08003470 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
Steve French790fe572007-07-07 19:25:05 +00003471 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 /* ACE not converted */
3473 break;
3474 }
3475 }
Steve French790fe572007-07-07 19:25:05 +00003476 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3478 rc += sizeof(struct cifs_posix_acl);
3479 /* BB add check to make sure ACL does not overflow SMB */
3480 }
3481 return rc;
3482}
3483
3484int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003485CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003486 const unsigned char *searchName,
3487 char *acl_inf, const int buflen, const int acl_type,
3488 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490/* SMB_QUERY_POSIX_ACL */
3491 TRANSACTION2_QPI_REQ *pSMB = NULL;
3492 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3493 int rc = 0;
3494 int bytes_returned;
3495 int name_len;
3496 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003497
Joe Perchesf96637b2013-05-04 22:12:25 -05003498 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500queryAclRetry:
3501 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3502 (void **) &pSMBr);
3503 if (rc)
3504 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3507 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003508 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3509 searchName, PATH_MAX, nls_codepage,
3510 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 name_len++; /* trailing null */
3512 name_len *= 2;
3513 pSMB->FileName[name_len] = 0;
3514 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003515 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 name_len = strnlen(searchName, PATH_MAX);
3517 name_len++; /* trailing null */
3518 strncpy(pSMB->FileName, searchName, name_len);
3519 }
3520
3521 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3522 pSMB->TotalDataCount = 0;
3523 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003524 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 pSMB->MaxDataCount = cpu_to_le16(4000);
3526 pSMB->MaxSetupCount = 0;
3527 pSMB->Reserved = 0;
3528 pSMB->Flags = 0;
3529 pSMB->Timeout = 0;
3530 pSMB->Reserved2 = 0;
3531 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003532 offsetof(struct smb_com_transaction2_qpi_req,
3533 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 pSMB->DataCount = 0;
3535 pSMB->DataOffset = 0;
3536 pSMB->SetupCount = 1;
3537 pSMB->Reserved3 = 0;
3538 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3539 byte_count = params + 1 /* pad */ ;
3540 pSMB->TotalParameterCount = cpu_to_le16(params);
3541 pSMB->ParameterCount = pSMB->TotalParameterCount;
3542 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3543 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003544 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 pSMB->ByteCount = cpu_to_le16(byte_count);
3546
3547 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3548 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003549 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003551 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 } else {
3553 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003557 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 rc = -EIO; /* bad smb */
3559 else {
3560 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3561 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3562 rc = cifs_copy_posix_acl(acl_inf,
3563 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003564 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
3566 }
3567 cifs_buf_release(pSMB);
3568 if (rc == -EAGAIN)
3569 goto queryAclRetry;
3570 return rc;
3571}
3572
3573int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003574CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003575 const unsigned char *fileName,
3576 const char *local_acl, const int buflen,
3577 const int acl_type,
3578 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
3580 struct smb_com_transaction2_spi_req *pSMB = NULL;
3581 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3582 char *parm_data;
3583 int name_len;
3584 int rc = 0;
3585 int bytes_returned = 0;
3586 __u16 params, byte_count, data_count, param_offset, offset;
3587
Joe Perchesf96637b2013-05-04 22:12:25 -05003588 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589setAclRetry:
3590 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003591 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (rc)
3593 return rc;
3594 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3595 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003596 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3597 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 name_len++; /* trailing null */
3599 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003600 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 name_len = strnlen(fileName, PATH_MAX);
3602 name_len++; /* trailing null */
3603 strncpy(pSMB->FileName, fileName, name_len);
3604 }
3605 params = 6 + name_len;
3606 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003607 /* BB find max SMB size from sess */
3608 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 pSMB->MaxSetupCount = 0;
3610 pSMB->Reserved = 0;
3611 pSMB->Flags = 0;
3612 pSMB->Timeout = 0;
3613 pSMB->Reserved2 = 0;
3614 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003615 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 offset = param_offset + params;
3617 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3618 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3619
3620 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003621 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Steve French790fe572007-07-07 19:25:05 +00003623 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 rc = -EOPNOTSUPP;
3625 goto setACLerrorExit;
3626 }
3627 pSMB->DataOffset = cpu_to_le16(offset);
3628 pSMB->SetupCount = 1;
3629 pSMB->Reserved3 = 0;
3630 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3631 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3632 byte_count = 3 /* pad */ + params + data_count;
3633 pSMB->DataCount = cpu_to_le16(data_count);
3634 pSMB->TotalDataCount = pSMB->DataCount;
3635 pSMB->ParameterCount = cpu_to_le16(params);
3636 pSMB->TotalParameterCount = pSMB->ParameterCount;
3637 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003638 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 pSMB->ByteCount = cpu_to_le16(byte_count);
3640 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003641 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003642 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003643 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645setACLerrorExit:
3646 cifs_buf_release(pSMB);
3647 if (rc == -EAGAIN)
3648 goto setAclRetry;
3649 return rc;
3650}
3651
Steve Frenchf654bac2005-04-28 22:41:04 -07003652/* BB fix tabs in this function FIXME BB */
3653int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003654CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003655 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003656{
Steve French50c2f752007-07-13 00:33:32 +00003657 int rc = 0;
3658 struct smb_t2_qfi_req *pSMB = NULL;
3659 struct smb_t2_qfi_rsp *pSMBr = NULL;
3660 int bytes_returned;
3661 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003662
Joe Perchesf96637b2013-05-04 22:12:25 -05003663 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003664 if (tcon == NULL)
3665 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003666
3667GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003668 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3669 (void **) &pSMBr);
3670 if (rc)
3671 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003672
Steve Frenchad7a2922008-02-07 23:25:02 +00003673 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003674 pSMB->t2.TotalDataCount = 0;
3675 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3676 /* BB find exact max data count below from sess structure BB */
3677 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3678 pSMB->t2.MaxSetupCount = 0;
3679 pSMB->t2.Reserved = 0;
3680 pSMB->t2.Flags = 0;
3681 pSMB->t2.Timeout = 0;
3682 pSMB->t2.Reserved2 = 0;
3683 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3684 Fid) - 4);
3685 pSMB->t2.DataCount = 0;
3686 pSMB->t2.DataOffset = 0;
3687 pSMB->t2.SetupCount = 1;
3688 pSMB->t2.Reserved3 = 0;
3689 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3690 byte_count = params + 1 /* pad */ ;
3691 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3692 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3693 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3694 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003695 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003696 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003697 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003698
Steve French790fe572007-07-07 19:25:05 +00003699 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3700 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3701 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003702 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003703 } else {
3704 /* decode response */
3705 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003706 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003707 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003708 /* If rc should we check for EOPNOSUPP and
3709 disable the srvino flag? or in caller? */
3710 rc = -EIO; /* bad smb */
3711 else {
3712 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3713 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3714 struct file_chattr_info *pfinfo;
3715 /* BB Do we need a cast or hash here ? */
3716 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003717 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003718 rc = -EIO;
3719 goto GetExtAttrOut;
3720 }
3721 pfinfo = (struct file_chattr_info *)
3722 (data_offset + (char *) &pSMBr->hdr.Protocol);
3723 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003724 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003725 }
3726 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003727GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003728 cifs_buf_release(pSMB);
3729 if (rc == -EAGAIN)
3730 goto GetExtAttrRetry;
3731 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003732}
3733
Steve Frenchf654bac2005-04-28 22:41:04 -07003734#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Jeff Layton79df1ba2010-12-06 12:52:08 -05003736#ifdef CONFIG_CIFS_ACL
3737/*
3738 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3739 * all NT TRANSACTS that we init here have total parm and data under about 400
3740 * bytes (to fit in small cifs buffer size), which is the case so far, it
3741 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3742 * returned setup area) and MaxParameterCount (returned parms size) must be set
3743 * by caller
3744 */
3745static int
3746smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003747 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003748 void **ret_buf)
3749{
3750 int rc;
3751 __u32 temp_offset;
3752 struct smb_com_ntransact_req *pSMB;
3753
3754 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3755 (void **)&pSMB);
3756 if (rc)
3757 return rc;
3758 *ret_buf = (void *)pSMB;
3759 pSMB->Reserved = 0;
3760 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3761 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003762 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003763 pSMB->ParameterCount = pSMB->TotalParameterCount;
3764 pSMB->DataCount = pSMB->TotalDataCount;
3765 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3766 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3767 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3768 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3769 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3770 pSMB->SubCommand = cpu_to_le16(sub_command);
3771 return 0;
3772}
3773
3774static int
3775validate_ntransact(char *buf, char **ppparm, char **ppdata,
3776 __u32 *pparmlen, __u32 *pdatalen)
3777{
3778 char *end_of_smb;
3779 __u32 data_count, data_offset, parm_count, parm_offset;
3780 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003781 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003782
3783 *pdatalen = 0;
3784 *pparmlen = 0;
3785
3786 if (buf == NULL)
3787 return -EINVAL;
3788
3789 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3790
Jeff Layton820a8032011-05-04 08:05:26 -04003791 bcc = get_bcc(&pSMBr->hdr);
3792 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003793 (char *)&pSMBr->ByteCount;
3794
3795 data_offset = le32_to_cpu(pSMBr->DataOffset);
3796 data_count = le32_to_cpu(pSMBr->DataCount);
3797 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3798 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3799
3800 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3801 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3802
3803 /* should we also check that parm and data areas do not overlap? */
3804 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003805 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003806 return -EINVAL;
3807 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003808 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003809 return -EINVAL;
3810 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003811 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003812 return -EINVAL;
3813 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003814 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3815 *ppdata, data_count, (data_count + *ppdata),
3816 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003817 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003818 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003819 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003820 return -EINVAL;
3821 }
3822 *pdatalen = data_count;
3823 *pparmlen = parm_count;
3824 return 0;
3825}
3826
Steve French0a4b92c2006-01-12 15:44:21 -08003827/* Get Security Descriptor (by handle) from remote server for a file or dir */
3828int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003829CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003830 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003831{
3832 int rc = 0;
3833 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003834 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003835 struct kvec iov[1];
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003836 struct kvec rsp_iov;
Steve French0a4b92c2006-01-12 15:44:21 -08003837
Joe Perchesf96637b2013-05-04 22:12:25 -05003838 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003839
Steve French630f3f0c2007-10-25 21:17:17 +00003840 *pbuflen = 0;
3841 *acl_inf = NULL;
3842
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003843 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003844 8 /* parm len */, tcon, (void **) &pSMB);
3845 if (rc)
3846 return rc;
3847
3848 pSMB->MaxParameterCount = cpu_to_le32(4);
3849 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3850 pSMB->MaxSetupCount = 0;
3851 pSMB->Fid = fid; /* file handle always le */
3852 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3853 CIFS_ACL_DACL);
3854 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003855 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003856 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003857 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003858
Steve Frencha761ac52007-10-18 21:45:27 +00003859 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003860 0, &rsp_iov);
3861 cifs_small_buf_release(pSMB);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003862 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003863 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003864 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003865 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003866 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003867 __u32 parm_len;
3868 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003869 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003870 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003871
3872/* validate_nttransact */
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003873 rc = validate_ntransact(rsp_iov.iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003874 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003875 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003876 goto qsec_out;
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003877 pSMBr = (struct smb_com_ntransact_rsp *)rsp_iov.iov_base;
Steve French0a4b92c2006-01-12 15:44:21 -08003878
Joe Perchesf96637b2013-05-04 22:12:25 -05003879 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3880 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003881
3882 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3883 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003884 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003885 goto qsec_out;
3886 }
3887
3888/* BB check that data area is minimum length and as big as acl_len */
3889
Steve Frenchaf6f4612007-10-16 18:40:37 +00003890 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003891 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003892 cifs_dbg(VFS, "acl length %d does not match %d\n",
3893 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003894 if (*pbuflen > acl_len)
3895 *pbuflen = acl_len;
3896 }
Steve French0a4b92c2006-01-12 15:44:21 -08003897
Steve French630f3f0c2007-10-25 21:17:17 +00003898 /* check if buffer is big enough for the acl
3899 header followed by the smallest SID */
3900 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3901 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003902 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003903 rc = -EINVAL;
3904 *pbuflen = 0;
3905 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003906 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003907 if (*acl_inf == NULL) {
3908 *pbuflen = 0;
3909 rc = -ENOMEM;
3910 }
Steve French630f3f0c2007-10-25 21:17:17 +00003911 }
Steve French0a4b92c2006-01-12 15:44:21 -08003912 }
3913qsec_out:
Pavel Shilovskyda502f72016-10-25 11:38:47 -07003914 free_rsp_buf(buf_type, rsp_iov.iov_base);
Steve French0a4b92c2006-01-12 15:44:21 -08003915 return rc;
3916}
Steve French97837582007-12-31 07:47:21 +00003917
3918int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003919CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003920 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003921{
3922 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3923 int rc = 0;
3924 int bytes_returned = 0;
3925 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003926 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003927
3928setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003929 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003930 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003931 return rc;
Steve French97837582007-12-31 07:47:21 +00003932
3933 pSMB->MaxSetupCount = 0;
3934 pSMB->Reserved = 0;
3935
3936 param_count = 8;
3937 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3938 data_count = acllen;
3939 data_offset = param_offset + param_count;
3940 byte_count = 3 /* pad */ + param_count;
3941
3942 pSMB->DataCount = cpu_to_le32(data_count);
3943 pSMB->TotalDataCount = pSMB->DataCount;
3944 pSMB->MaxParameterCount = cpu_to_le32(4);
3945 pSMB->MaxDataCount = cpu_to_le32(16384);
3946 pSMB->ParameterCount = cpu_to_le32(param_count);
3947 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3948 pSMB->TotalParameterCount = pSMB->ParameterCount;
3949 pSMB->DataOffset = cpu_to_le32(data_offset);
3950 pSMB->SetupCount = 0;
3951 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3952 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3953
3954 pSMB->Fid = fid; /* file handle always le */
3955 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003956 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003957
3958 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003959 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3960 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003961 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003962 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003963 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003964
3965 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3966 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3967
Joe Perchesf96637b2013-05-04 22:12:25 -05003968 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3969 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003970 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003971 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003972 cifs_buf_release(pSMB);
3973
3974 if (rc == -EAGAIN)
3975 goto setCifsAclRetry;
3976
3977 return (rc);
3978}
3979
Jeff Layton79df1ba2010-12-06 12:52:08 -05003980#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003981
Steve French6b8edfe2005-08-23 20:26:03 -07003982/* Legacy Query Path Information call for lookup to old servers such
3983 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003984int
3985SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3986 const char *search_name, FILE_ALL_INFO *data,
3987 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003988{
Steve Frenchad7a2922008-02-07 23:25:02 +00003989 QUERY_INFORMATION_REQ *pSMB;
3990 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003991 int rc = 0;
3992 int bytes_returned;
3993 int name_len;
3994
Joe Perchesf96637b2013-05-04 22:12:25 -05003995 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003996QInfRetry:
3997 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003998 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003999 if (rc)
4000 return rc;
4001
4002 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4003 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004004 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004005 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004006 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004007 name_len++; /* trailing null */
4008 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004009 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004010 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004011 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004012 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004013 }
4014 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004015 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004016 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004017 pSMB->ByteCount = cpu_to_le16(name_len);
4018
4019 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004020 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004021 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004022 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004023 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004024 struct timespec ts;
4025 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004026
4027 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004028 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004029 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004030 ts.tv_nsec = 0;
4031 ts.tv_sec = time;
4032 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004033 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4034 data->LastWriteTime = data->ChangeTime;
4035 data->LastAccessTime = 0;
4036 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004037 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004038 data->EndOfFile = data->AllocationSize;
4039 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004040 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004041 } else
4042 rc = -EIO; /* bad buffer passed in */
4043
4044 cifs_buf_release(pSMB);
4045
4046 if (rc == -EAGAIN)
4047 goto QInfRetry;
4048
4049 return rc;
4050}
4051
Jeff Laytonbcd53572010-02-12 07:44:16 -05004052int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004053CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004054 u16 netfid, FILE_ALL_INFO *pFindData)
4055{
4056 struct smb_t2_qfi_req *pSMB = NULL;
4057 struct smb_t2_qfi_rsp *pSMBr = NULL;
4058 int rc = 0;
4059 int bytes_returned;
4060 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004061
Jeff Laytonbcd53572010-02-12 07:44:16 -05004062QFileInfoRetry:
4063 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4064 (void **) &pSMBr);
4065 if (rc)
4066 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004067
Jeff Laytonbcd53572010-02-12 07:44:16 -05004068 params = 2 /* level */ + 2 /* fid */;
4069 pSMB->t2.TotalDataCount = 0;
4070 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4071 /* BB find exact max data count below from sess structure BB */
4072 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4073 pSMB->t2.MaxSetupCount = 0;
4074 pSMB->t2.Reserved = 0;
4075 pSMB->t2.Flags = 0;
4076 pSMB->t2.Timeout = 0;
4077 pSMB->t2.Reserved2 = 0;
4078 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4079 Fid) - 4);
4080 pSMB->t2.DataCount = 0;
4081 pSMB->t2.DataOffset = 0;
4082 pSMB->t2.SetupCount = 1;
4083 pSMB->t2.Reserved3 = 0;
4084 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4085 byte_count = params + 1 /* pad */ ;
4086 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4087 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4088 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4089 pSMB->Pad = 0;
4090 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004091 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004092 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004093
4094 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4095 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4096 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004097 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004098 } else { /* decode response */
4099 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4100
4101 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4102 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004103 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004104 rc = -EIO; /* bad smb */
4105 else if (pFindData) {
4106 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4107 memcpy((char *) pFindData,
4108 (char *) &pSMBr->hdr.Protocol +
4109 data_offset, sizeof(FILE_ALL_INFO));
4110 } else
4111 rc = -ENOMEM;
4112 }
4113 cifs_buf_release(pSMB);
4114 if (rc == -EAGAIN)
4115 goto QFileInfoRetry;
4116
4117 return rc;
4118}
Steve French6b8edfe2005-08-23 20:26:03 -07004119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004121CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004122 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004123 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004124 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004126 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 TRANSACTION2_QPI_REQ *pSMB = NULL;
4128 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4129 int rc = 0;
4130 int bytes_returned;
4131 int name_len;
4132 __u16 params, byte_count;
4133
Joe Perchesf96637b2013-05-04 22:12:25 -05004134 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135QPathInfoRetry:
4136 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4137 (void **) &pSMBr);
4138 if (rc)
4139 return rc;
4140
4141 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4142 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004143 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004144 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 name_len++; /* trailing null */
4146 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004147 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004148 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004150 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 }
4152
Steve French50c2f752007-07-13 00:33:32 +00004153 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 pSMB->TotalDataCount = 0;
4155 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004156 /* BB find exact max SMB PDU from sess structure BB */
4157 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 pSMB->MaxSetupCount = 0;
4159 pSMB->Reserved = 0;
4160 pSMB->Flags = 0;
4161 pSMB->Timeout = 0;
4162 pSMB->Reserved2 = 0;
4163 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004164 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 pSMB->DataCount = 0;
4166 pSMB->DataOffset = 0;
4167 pSMB->SetupCount = 1;
4168 pSMB->Reserved3 = 0;
4169 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4170 byte_count = params + 1 /* pad */ ;
4171 pSMB->TotalParameterCount = cpu_to_le16(params);
4172 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004173 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004174 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4175 else
4176 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004178 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 pSMB->ByteCount = cpu_to_le16(byte_count);
4180
4181 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4182 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4183 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004184 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 } else { /* decode response */
4186 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4187
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004188 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4189 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004190 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004192 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004193 rc = -EIO; /* 24 or 26 expected but we do not read
4194 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004195 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004196 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004198
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004199 /*
4200 * On legacy responses we do not read the last field,
4201 * EAsize, fortunately since it varies by subdialect and
4202 * also note it differs on Set vs Get, ie two bytes or 4
4203 * bytes depending but we don't care here.
4204 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004205 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004206 size = sizeof(FILE_INFO_STANDARD);
4207 else
4208 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004209 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004210 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 } else
4212 rc = -ENOMEM;
4213 }
4214 cifs_buf_release(pSMB);
4215 if (rc == -EAGAIN)
4216 goto QPathInfoRetry;
4217
4218 return rc;
4219}
4220
4221int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004222CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004223 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4224{
4225 struct smb_t2_qfi_req *pSMB = NULL;
4226 struct smb_t2_qfi_rsp *pSMBr = NULL;
4227 int rc = 0;
4228 int bytes_returned;
4229 __u16 params, byte_count;
4230
4231UnixQFileInfoRetry:
4232 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4233 (void **) &pSMBr);
4234 if (rc)
4235 return rc;
4236
4237 params = 2 /* level */ + 2 /* fid */;
4238 pSMB->t2.TotalDataCount = 0;
4239 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4240 /* BB find exact max data count below from sess structure BB */
4241 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4242 pSMB->t2.MaxSetupCount = 0;
4243 pSMB->t2.Reserved = 0;
4244 pSMB->t2.Flags = 0;
4245 pSMB->t2.Timeout = 0;
4246 pSMB->t2.Reserved2 = 0;
4247 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4248 Fid) - 4);
4249 pSMB->t2.DataCount = 0;
4250 pSMB->t2.DataOffset = 0;
4251 pSMB->t2.SetupCount = 1;
4252 pSMB->t2.Reserved3 = 0;
4253 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4254 byte_count = params + 1 /* pad */ ;
4255 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4256 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4257 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4258 pSMB->Pad = 0;
4259 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004260 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004261 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004262
4263 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4264 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4265 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004266 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004267 } else { /* decode response */
4268 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4269
Jeff Layton820a8032011-05-04 08:05:26 -04004270 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004271 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 -05004272 rc = -EIO; /* bad smb */
4273 } else {
4274 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4275 memcpy((char *) pFindData,
4276 (char *) &pSMBr->hdr.Protocol +
4277 data_offset,
4278 sizeof(FILE_UNIX_BASIC_INFO));
4279 }
4280 }
4281
4282 cifs_buf_release(pSMB);
4283 if (rc == -EAGAIN)
4284 goto UnixQFileInfoRetry;
4285
4286 return rc;
4287}
4288
4289int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004290CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004292 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004293 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
4295/* SMB_QUERY_FILE_UNIX_BASIC */
4296 TRANSACTION2_QPI_REQ *pSMB = NULL;
4297 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4298 int rc = 0;
4299 int bytes_returned = 0;
4300 int name_len;
4301 __u16 params, byte_count;
4302
Joe Perchesf96637b2013-05-04 22:12:25 -05004303 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304UnixQPathInfoRetry:
4305 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4306 (void **) &pSMBr);
4307 if (rc)
4308 return rc;
4309
4310 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4311 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004312 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4313 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 name_len++; /* trailing null */
4315 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004316 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 name_len = strnlen(searchName, PATH_MAX);
4318 name_len++; /* trailing null */
4319 strncpy(pSMB->FileName, searchName, name_len);
4320 }
4321
Steve French50c2f752007-07-13 00:33:32 +00004322 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 pSMB->TotalDataCount = 0;
4324 pSMB->MaxParameterCount = cpu_to_le16(2);
4325 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004326 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 pSMB->MaxSetupCount = 0;
4328 pSMB->Reserved = 0;
4329 pSMB->Flags = 0;
4330 pSMB->Timeout = 0;
4331 pSMB->Reserved2 = 0;
4332 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004333 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 pSMB->DataCount = 0;
4335 pSMB->DataOffset = 0;
4336 pSMB->SetupCount = 1;
4337 pSMB->Reserved3 = 0;
4338 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4339 byte_count = params + 1 /* pad */ ;
4340 pSMB->TotalParameterCount = cpu_to_le16(params);
4341 pSMB->ParameterCount = pSMB->TotalParameterCount;
4342 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4343 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004344 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 pSMB->ByteCount = cpu_to_le16(byte_count);
4346
4347 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4348 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4349 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004350 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 } else { /* decode response */
4352 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4353
Jeff Layton820a8032011-05-04 08:05:26 -04004354 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004355 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 -07004356 rc = -EIO; /* bad smb */
4357 } else {
4358 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4359 memcpy((char *) pFindData,
4360 (char *) &pSMBr->hdr.Protocol +
4361 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004362 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 }
4364 }
4365 cifs_buf_release(pSMB);
4366 if (rc == -EAGAIN)
4367 goto UnixQPathInfoRetry;
4368
4369 return rc;
4370}
4371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372/* xid, tcon, searchName and codepage are input parms, rest are returned */
4373int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004374CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004375 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004376 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004377 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379/* level 257 SMB_ */
4380 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4381 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004382 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 int rc = 0;
4384 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004385 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004387 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Joe Perchesf96637b2013-05-04 22:12:25 -05004389 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391findFirstRetry:
4392 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4393 (void **) &pSMBr);
4394 if (rc)
4395 return rc;
4396
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004397 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004398 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4401 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004402 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4403 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004404 /* We can not add the asterik earlier in case
4405 it got remapped to 0xF03A as if it were part of the
4406 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004408 if (msearch) {
4409 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4410 pSMB->FileName[name_len+1] = 0;
4411 pSMB->FileName[name_len+2] = '*';
4412 pSMB->FileName[name_len+3] = 0;
4413 name_len += 4; /* now the trailing null */
4414 /* null terminate just in case */
4415 pSMB->FileName[name_len] = 0;
4416 pSMB->FileName[name_len+1] = 0;
4417 name_len += 2;
4418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 } else { /* BB add check for overrun of SMB buf BB */
4420 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004422 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 free buffer exit; BB */
4424 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004425 if (msearch) {
4426 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4427 pSMB->FileName[name_len+1] = '*';
4428 pSMB->FileName[name_len+2] = 0;
4429 name_len += 3;
4430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
4432
4433 params = 12 + name_len /* includes null */ ;
4434 pSMB->TotalDataCount = 0; /* no EAs */
4435 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004436 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 pSMB->MaxSetupCount = 0;
4438 pSMB->Reserved = 0;
4439 pSMB->Flags = 0;
4440 pSMB->Timeout = 0;
4441 pSMB->Reserved2 = 0;
4442 byte_count = params + 1 /* pad */ ;
4443 pSMB->TotalParameterCount = cpu_to_le16(params);
4444 pSMB->ParameterCount = pSMB->TotalParameterCount;
4445 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004446 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4447 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 pSMB->DataCount = 0;
4449 pSMB->DataOffset = 0;
4450 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4451 pSMB->Reserved3 = 0;
4452 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4453 pSMB->SearchAttributes =
4454 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4455 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004456 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004457 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4459
4460 /* BB what should we set StorageType to? Does it matter? BB */
4461 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004462 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 pSMB->ByteCount = cpu_to_le16(byte_count);
4464
4465 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4466 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004467 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Steve French88274812006-03-09 22:21:45 +00004469 if (rc) {/* BB add logic to retry regular search if Unix search
4470 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004472 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004473
Steve French88274812006-03-09 22:21:45 +00004474 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 /* BB eventually could optimize out free and realloc of buf */
4477 /* for this case */
4478 if (rc == -EAGAIN)
4479 goto findFirstRetry;
4480 } else { /* decode response */
4481 /* BB remember to free buffer if error BB */
4482 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004483 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004484 unsigned int lnoff;
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004487 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 else
Steve French4b18f2a2008-04-29 00:06:05 +00004489 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
4491 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004492 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004493 psrch_inf->srch_entries_start =
4494 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4497 le16_to_cpu(pSMBr->t2.ParameterOffset));
4498
Steve French790fe572007-07-07 19:25:05 +00004499 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004500 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 else
Steve French4b18f2a2008-04-29 00:06:05 +00004502 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Steve French50c2f752007-07-13 00:33:32 +00004504 psrch_inf->entries_in_buffer =
4505 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004506 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004508 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004509 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004510 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004511 psrch_inf->last_entry = NULL;
4512 return rc;
4513 }
4514
Steve French0752f152008-10-07 20:03:33 +00004515 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004516 lnoff;
4517
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004518 if (pnetfid)
4519 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 } else {
4521 cifs_buf_release(pSMB);
4522 }
4523 }
4524
4525 return rc;
4526}
4527
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004528int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4529 __u16 searchHandle, __u16 search_flags,
4530 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
4532 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4533 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004534 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 char *response_data;
4536 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004537 int bytes_returned;
4538 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 __u16 params, byte_count;
4540
Joe Perchesf96637b2013-05-04 22:12:25 -05004541 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Steve French4b18f2a2008-04-29 00:06:05 +00004543 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 return -ENOENT;
4545
4546 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4547 (void **) &pSMBr);
4548 if (rc)
4549 return rc;
4550
Steve French50c2f752007-07-13 00:33:32 +00004551 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 byte_count = 0;
4553 pSMB->TotalDataCount = 0; /* no EAs */
4554 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004555 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 pSMB->MaxSetupCount = 0;
4557 pSMB->Reserved = 0;
4558 pSMB->Flags = 0;
4559 pSMB->Timeout = 0;
4560 pSMB->Reserved2 = 0;
4561 pSMB->ParameterOffset = cpu_to_le16(
4562 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4563 pSMB->DataCount = 0;
4564 pSMB->DataOffset = 0;
4565 pSMB->SetupCount = 1;
4566 pSMB->Reserved3 = 0;
4567 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4568 pSMB->SearchHandle = searchHandle; /* always kept as le */
4569 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004570 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4572 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004573 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
4575 name_len = psrch_inf->resume_name_len;
4576 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004577 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4579 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004580 /* 14 byte parm len above enough for 2 byte null terminator */
4581 pSMB->ResumeFileName[name_len] = 0;
4582 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 } else {
4584 rc = -EINVAL;
4585 goto FNext2_err_exit;
4586 }
4587 byte_count = params + 1 /* pad */ ;
4588 pSMB->TotalParameterCount = cpu_to_le16(params);
4589 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004590 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4594 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004595 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 if (rc) {
4597 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004598 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004599 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004600 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004602 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 } else { /* decode response */
4604 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004605
Steve French790fe572007-07-07 19:25:05 +00004606 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004607 unsigned int lnoff;
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /* BB fixme add lock for file (srch_info) struct here */
4610 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004611 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 else
Steve French4b18f2a2008-04-29 00:06:05 +00004613 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 response_data = (char *) &pSMBr->hdr.Protocol +
4615 le16_to_cpu(pSMBr->t2.ParameterOffset);
4616 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4617 response_data = (char *)&pSMBr->hdr.Protocol +
4618 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004619 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004620 cifs_small_buf_release(
4621 psrch_inf->ntwrk_buf_start);
4622 else
4623 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 psrch_inf->srch_entries_start = response_data;
4625 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004626 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004627 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004628 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 else
Steve French4b18f2a2008-04-29 00:06:05 +00004630 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004631 psrch_inf->entries_in_buffer =
4632 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 psrch_inf->index_of_last_entry +=
4634 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004635 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004636 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004637 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004638 psrch_inf->last_entry = NULL;
4639 return rc;
4640 } else
4641 psrch_inf->last_entry =
4642 psrch_inf->srch_entries_start + lnoff;
4643
Joe Perchesf96637b2013-05-04 22:12:25 -05004644/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4645 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647 /* BB fixme add unlock here */
4648 }
4649
4650 }
4651
4652 /* BB On error, should we leave previous search buf (and count and
4653 last entry fields) intact or free the previous one? */
4654
4655 /* Note: On -EAGAIN error only caller can retry on handle based calls
4656 since file handle passed in no longer valid */
4657FNext2_err_exit:
4658 if (rc != 0)
4659 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 return rc;
4661}
4662
4663int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004664CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004665 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
4667 int rc = 0;
4668 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Joe Perchesf96637b2013-05-04 22:12:25 -05004670 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4672
4673 /* no sense returning error if session restarted
4674 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004675 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 return 0;
4677 if (rc)
4678 return rc;
4679
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 pSMB->FileID = searchHandle;
4681 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004682 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07004683 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00004684 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004685 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004686
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004687 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
4689 /* Since session is dead, search handle closed on server already */
4690 if (rc == -EAGAIN)
4691 rc = 0;
4692
4693 return rc;
4694}
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004697CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004698 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004699 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
4701 int rc = 0;
4702 TRANSACTION2_QPI_REQ *pSMB = NULL;
4703 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4704 int name_len, bytes_returned;
4705 __u16 params, byte_count;
4706
Joe Perchesf96637b2013-05-04 22:12:25 -05004707 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004708 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004709 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711GetInodeNumberRetry:
4712 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004713 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (rc)
4715 return rc;
4716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4718 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004719 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004720 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004721 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 name_len++; /* trailing null */
4723 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004724 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004725 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004727 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
4729
4730 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4731 pSMB->TotalDataCount = 0;
4732 pSMB->MaxParameterCount = cpu_to_le16(2);
4733 /* BB find exact max data count below from sess structure BB */
4734 pSMB->MaxDataCount = cpu_to_le16(4000);
4735 pSMB->MaxSetupCount = 0;
4736 pSMB->Reserved = 0;
4737 pSMB->Flags = 0;
4738 pSMB->Timeout = 0;
4739 pSMB->Reserved2 = 0;
4740 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004741 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 pSMB->DataCount = 0;
4743 pSMB->DataOffset = 0;
4744 pSMB->SetupCount = 1;
4745 pSMB->Reserved3 = 0;
4746 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4747 byte_count = params + 1 /* pad */ ;
4748 pSMB->TotalParameterCount = cpu_to_le16(params);
4749 pSMB->ParameterCount = pSMB->TotalParameterCount;
4750 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4751 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004752 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 pSMB->ByteCount = cpu_to_le16(byte_count);
4754
4755 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4756 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4757 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004758 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 } else {
4760 /* decode response */
4761 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004763 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* If rc should we check for EOPNOSUPP and
4765 disable the srvino flag? or in caller? */
4766 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004767 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4769 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004770 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004772 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004773 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 rc = -EIO;
4775 goto GetInodeNumOut;
4776 }
4777 pfinfo = (struct file_internal_info *)
4778 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004779 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 }
4781 }
4782GetInodeNumOut:
4783 cifs_buf_release(pSMB);
4784 if (rc == -EAGAIN)
4785 goto GetInodeNumberRetry;
4786 return rc;
4787}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Igor Mammedovfec45852008-05-16 13:06:30 +04004789/* parses DFS refferal V3 structure
4790 * caller is responsible for freeing target_nodes
4791 * returns:
4792 * on success - 0
4793 * on failure - errno
4794 */
4795static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004796parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004797 unsigned int *num_of_nodes,
4798 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004799 const struct nls_table *nls_codepage, int remap,
4800 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004801{
4802 int i, rc = 0;
4803 char *data_end;
4804 bool is_unicode;
4805 struct dfs_referral_level_3 *ref;
4806
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004807 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4808 is_unicode = true;
4809 else
4810 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004811 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4812
4813 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004814 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4815 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004816 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004817 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004818 }
4819
4820 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004821 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004822 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4823 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004824 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004825 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004826 }
4827
4828 /* get the upper boundary of the resp buffer */
4829 data_end = (char *)(&(pSMBr->PathConsumed)) +
4830 le16_to_cpu(pSMBr->t2.DataCount);
4831
Joe Perchesf96637b2013-05-04 22:12:25 -05004832 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4833 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004834
Joe Perchesf96637b2013-05-04 22:12:25 -05004835 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4836 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004837 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004838 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004839 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004840 }
4841
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08004842 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004843 for (i = 0; i < *num_of_nodes; i++) {
4844 char *temp;
4845 int max_len;
4846 struct dfs_info3_param *node = (*target_nodes)+i;
4847
Steve French0e0d2cf2009-05-01 05:27:32 +00004848 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004849 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004850 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4851 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004852 if (tmp == NULL) {
4853 rc = -ENOMEM;
4854 goto parse_DFS_referrals_exit;
4855 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004856 cifsConvertToUTF16((__le16 *) tmp, searchName,
4857 PATH_MAX, nls_codepage, remap);
4858 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004859 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004860 nls_codepage);
4861 kfree(tmp);
4862 } else
4863 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4864
Igor Mammedovfec45852008-05-16 13:06:30 +04004865 node->server_type = le16_to_cpu(ref->ServerType);
4866 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4867
4868 /* copy DfsPath */
4869 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4870 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004871 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4872 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004873 if (!node->path_name) {
4874 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004875 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004876 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004877
4878 /* copy link target UNC */
4879 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4880 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004881 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4882 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004883 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004884 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004885 goto parse_DFS_referrals_exit;
4886 }
4887
4888 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004889 }
4890
Steve Frencha1fe78f2008-05-16 18:48:38 +00004891parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004892 if (rc) {
4893 free_dfs_info_array(*target_nodes, *num_of_nodes);
4894 *target_nodes = NULL;
4895 *num_of_nodes = 0;
4896 }
4897 return rc;
4898}
4899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004901CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004902 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004903 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004904 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
4906/* TRANS2_GET_DFS_REFERRAL */
4907 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4908 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 int rc = 0;
4910 int bytes_returned;
4911 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004913 *num_of_nodes = 0;
4914 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Joe Perchesf96637b2013-05-04 22:12:25 -05004916 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 if (ses == NULL)
4918 return -ENODEV;
4919getDFSRetry:
4920 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4921 (void **) &pSMBr);
4922 if (rc)
4923 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004924
4925 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004926 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004927 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 pSMB->hdr.Tid = ses->ipc_tid;
4929 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004930 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004932 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
4935 if (ses->capabilities & CAP_UNICODE) {
4936 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4937 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004938 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004939 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004940 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 name_len++; /* trailing null */
4942 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004943 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004944 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004946 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
4948
Dan Carpenter65c3b202015-04-30 17:30:24 +03004949 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004950 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004951
Steve French50c2f752007-07-13 00:33:32 +00004952 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 params = 2 /* level */ + name_len /*includes null */ ;
4955 pSMB->TotalDataCount = 0;
4956 pSMB->DataCount = 0;
4957 pSMB->DataOffset = 0;
4958 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004959 /* BB find exact max SMB PDU from sess structure BB */
4960 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 pSMB->MaxSetupCount = 0;
4962 pSMB->Reserved = 0;
4963 pSMB->Flags = 0;
4964 pSMB->Timeout = 0;
4965 pSMB->Reserved2 = 0;
4966 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004967 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 pSMB->SetupCount = 1;
4969 pSMB->Reserved3 = 0;
4970 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4971 byte_count = params + 3 /* pad */ ;
4972 pSMB->ParameterCount = cpu_to_le16(params);
4973 pSMB->TotalParameterCount = pSMB->ParameterCount;
4974 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004975 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 pSMB->ByteCount = cpu_to_le16(byte_count);
4977
4978 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4979 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4980 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004981 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004982 goto GetDFSRefExit;
4983 }
4984 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004986 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004987 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004988 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004989 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004991
Joe Perchesf96637b2013-05-04 22:12:25 -05004992 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4993 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004994
4995 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004996 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004997 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004998 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00005001 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
5003 if (rc == -EAGAIN)
5004 goto getDFSRetry;
5005
5006 return rc;
5007}
5008
Steve French20962432005-09-21 22:05:57 -07005009/* Query File System Info such as free space to old servers such as Win 9x */
5010int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005011SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5012 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005013{
5014/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5015 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5016 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5017 FILE_SYSTEM_ALLOC_INFO *response_data;
5018 int rc = 0;
5019 int bytes_returned = 0;
5020 __u16 params, byte_count;
5021
Joe Perchesf96637b2013-05-04 22:12:25 -05005022 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005023oldQFSInfoRetry:
5024 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5025 (void **) &pSMBr);
5026 if (rc)
5027 return rc;
Steve French20962432005-09-21 22:05:57 -07005028
5029 params = 2; /* level */
5030 pSMB->TotalDataCount = 0;
5031 pSMB->MaxParameterCount = cpu_to_le16(2);
5032 pSMB->MaxDataCount = cpu_to_le16(1000);
5033 pSMB->MaxSetupCount = 0;
5034 pSMB->Reserved = 0;
5035 pSMB->Flags = 0;
5036 pSMB->Timeout = 0;
5037 pSMB->Reserved2 = 0;
5038 byte_count = params + 1 /* pad */ ;
5039 pSMB->TotalParameterCount = cpu_to_le16(params);
5040 pSMB->ParameterCount = pSMB->TotalParameterCount;
5041 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5042 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5043 pSMB->DataCount = 0;
5044 pSMB->DataOffset = 0;
5045 pSMB->SetupCount = 1;
5046 pSMB->Reserved3 = 0;
5047 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5048 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005049 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005050 pSMB->ByteCount = cpu_to_le16(byte_count);
5051
5052 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5053 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5054 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005055 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005056 } else { /* decode response */
5057 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5058
Jeff Layton820a8032011-05-04 08:05:26 -04005059 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005060 rc = -EIO; /* bad smb */
5061 else {
5062 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005063 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005064 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005065
Steve French50c2f752007-07-13 00:33:32 +00005066 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005067 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5068 FSData->f_bsize =
5069 le16_to_cpu(response_data->BytesPerSector) *
5070 le32_to_cpu(response_data->
5071 SectorsPerAllocationUnit);
5072 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005073 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005074 FSData->f_bfree = FSData->f_bavail =
5075 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005076 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5077 (unsigned long long)FSData->f_blocks,
5078 (unsigned long long)FSData->f_bfree,
5079 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005080 }
5081 }
5082 cifs_buf_release(pSMB);
5083
5084 if (rc == -EAGAIN)
5085 goto oldQFSInfoRetry;
5086
5087 return rc;
5088}
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005091CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5092 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
5094/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5095 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5096 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5097 FILE_SYSTEM_INFO *response_data;
5098 int rc = 0;
5099 int bytes_returned = 0;
5100 __u16 params, byte_count;
5101
Joe Perchesf96637b2013-05-04 22:12:25 -05005102 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103QFSInfoRetry:
5104 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5105 (void **) &pSMBr);
5106 if (rc)
5107 return rc;
5108
5109 params = 2; /* level */
5110 pSMB->TotalDataCount = 0;
5111 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005112 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 pSMB->MaxSetupCount = 0;
5114 pSMB->Reserved = 0;
5115 pSMB->Flags = 0;
5116 pSMB->Timeout = 0;
5117 pSMB->Reserved2 = 0;
5118 byte_count = params + 1 /* pad */ ;
5119 pSMB->TotalParameterCount = cpu_to_le16(params);
5120 pSMB->ParameterCount = pSMB->TotalParameterCount;
5121 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005122 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 pSMB->DataCount = 0;
5124 pSMB->DataOffset = 0;
5125 pSMB->SetupCount = 1;
5126 pSMB->Reserved3 = 0;
5127 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5128 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005129 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 pSMB->ByteCount = cpu_to_le16(byte_count);
5131
5132 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5133 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5134 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005135 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005137 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Jeff Layton820a8032011-05-04 08:05:26 -04005139 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 rc = -EIO; /* bad smb */
5141 else {
5142 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 response_data =
5145 (FILE_SYSTEM_INFO
5146 *) (((char *) &pSMBr->hdr.Protocol) +
5147 data_offset);
5148 FSData->f_bsize =
5149 le32_to_cpu(response_data->BytesPerSector) *
5150 le32_to_cpu(response_data->
5151 SectorsPerAllocationUnit);
5152 FSData->f_blocks =
5153 le64_to_cpu(response_data->TotalAllocationUnits);
5154 FSData->f_bfree = FSData->f_bavail =
5155 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005156 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5157 (unsigned long long)FSData->f_blocks,
5158 (unsigned long long)FSData->f_bfree,
5159 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161 }
5162 cifs_buf_release(pSMB);
5163
5164 if (rc == -EAGAIN)
5165 goto QFSInfoRetry;
5166
5167 return rc;
5168}
5169
5170int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005171CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172{
5173/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5174 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5175 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5176 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5177 int rc = 0;
5178 int bytes_returned = 0;
5179 __u16 params, byte_count;
5180
Joe Perchesf96637b2013-05-04 22:12:25 -05005181 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182QFSAttributeRetry:
5183 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5184 (void **) &pSMBr);
5185 if (rc)
5186 return rc;
5187
5188 params = 2; /* level */
5189 pSMB->TotalDataCount = 0;
5190 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005191 /* BB find exact max SMB PDU from sess structure BB */
5192 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 pSMB->MaxSetupCount = 0;
5194 pSMB->Reserved = 0;
5195 pSMB->Flags = 0;
5196 pSMB->Timeout = 0;
5197 pSMB->Reserved2 = 0;
5198 byte_count = params + 1 /* pad */ ;
5199 pSMB->TotalParameterCount = cpu_to_le16(params);
5200 pSMB->ParameterCount = pSMB->TotalParameterCount;
5201 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005202 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 pSMB->DataCount = 0;
5204 pSMB->DataOffset = 0;
5205 pSMB->SetupCount = 1;
5206 pSMB->Reserved3 = 0;
5207 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5208 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005209 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 pSMB->ByteCount = cpu_to_le16(byte_count);
5211
5212 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5213 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5214 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005215 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 } else { /* decode response */
5217 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5218
Jeff Layton820a8032011-05-04 08:05:26 -04005219 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005220 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 rc = -EIO; /* bad smb */
5222 } else {
5223 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5224 response_data =
5225 (FILE_SYSTEM_ATTRIBUTE_INFO
5226 *) (((char *) &pSMBr->hdr.Protocol) +
5227 data_offset);
5228 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005229 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
5231 }
5232 cifs_buf_release(pSMB);
5233
5234 if (rc == -EAGAIN)
5235 goto QFSAttributeRetry;
5236
5237 return rc;
5238}
5239
5240int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005241CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
5243/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5244 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5245 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5246 FILE_SYSTEM_DEVICE_INFO *response_data;
5247 int rc = 0;
5248 int bytes_returned = 0;
5249 __u16 params, byte_count;
5250
Joe Perchesf96637b2013-05-04 22:12:25 -05005251 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252QFSDeviceRetry:
5253 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5254 (void **) &pSMBr);
5255 if (rc)
5256 return rc;
5257
5258 params = 2; /* level */
5259 pSMB->TotalDataCount = 0;
5260 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005261 /* BB find exact max SMB PDU from sess structure BB */
5262 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 pSMB->MaxSetupCount = 0;
5264 pSMB->Reserved = 0;
5265 pSMB->Flags = 0;
5266 pSMB->Timeout = 0;
5267 pSMB->Reserved2 = 0;
5268 byte_count = params + 1 /* pad */ ;
5269 pSMB->TotalParameterCount = cpu_to_le16(params);
5270 pSMB->ParameterCount = pSMB->TotalParameterCount;
5271 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005272 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
5274 pSMB->DataCount = 0;
5275 pSMB->DataOffset = 0;
5276 pSMB->SetupCount = 1;
5277 pSMB->Reserved3 = 0;
5278 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5279 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005280 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 pSMB->ByteCount = cpu_to_le16(byte_count);
5282
5283 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5284 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5285 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005286 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 } else { /* decode response */
5288 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5289
Jeff Layton820a8032011-05-04 08:05:26 -04005290 if (rc || get_bcc(&pSMBr->hdr) <
5291 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 rc = -EIO; /* bad smb */
5293 else {
5294 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5295 response_data =
Steve French737b7582005-04-28 22:41:06 -07005296 (FILE_SYSTEM_DEVICE_INFO *)
5297 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 data_offset);
5299 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005300 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 }
5302 }
5303 cifs_buf_release(pSMB);
5304
5305 if (rc == -EAGAIN)
5306 goto QFSDeviceRetry;
5307
5308 return rc;
5309}
5310
5311int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005312CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313{
5314/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5315 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5316 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5317 FILE_SYSTEM_UNIX_INFO *response_data;
5318 int rc = 0;
5319 int bytes_returned = 0;
5320 __u16 params, byte_count;
5321
Joe Perchesf96637b2013-05-04 22:12:25 -05005322 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005324 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5325 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 if (rc)
5327 return rc;
5328
5329 params = 2; /* level */
5330 pSMB->TotalDataCount = 0;
5331 pSMB->DataCount = 0;
5332 pSMB->DataOffset = 0;
5333 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005334 /* BB find exact max SMB PDU from sess structure BB */
5335 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 pSMB->MaxSetupCount = 0;
5337 pSMB->Reserved = 0;
5338 pSMB->Flags = 0;
5339 pSMB->Timeout = 0;
5340 pSMB->Reserved2 = 0;
5341 byte_count = params + 1 /* pad */ ;
5342 pSMB->ParameterCount = cpu_to_le16(params);
5343 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005344 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5345 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 pSMB->SetupCount = 1;
5347 pSMB->Reserved3 = 0;
5348 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5349 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005350 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 pSMB->ByteCount = cpu_to_le16(byte_count);
5352
5353 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5354 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5355 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005356 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 } else { /* decode response */
5358 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5359
Jeff Layton820a8032011-05-04 08:05:26 -04005360 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 rc = -EIO; /* bad smb */
5362 } else {
5363 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5364 response_data =
5365 (FILE_SYSTEM_UNIX_INFO
5366 *) (((char *) &pSMBr->hdr.Protocol) +
5367 data_offset);
5368 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005369 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371 }
5372 cifs_buf_release(pSMB);
5373
5374 if (rc == -EAGAIN)
5375 goto QFSUnixRetry;
5376
5377
5378 return rc;
5379}
5380
Jeremy Allisonac670552005-06-22 17:26:35 -07005381int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005382CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005383{
5384/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5385 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5386 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5387 int rc = 0;
5388 int bytes_returned = 0;
5389 __u16 params, param_offset, offset, byte_count;
5390
Joe Perchesf96637b2013-05-04 22:12:25 -05005391 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005392SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005393 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005394 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5395 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005396 if (rc)
5397 return rc;
5398
5399 params = 4; /* 2 bytes zero followed by info level. */
5400 pSMB->MaxSetupCount = 0;
5401 pSMB->Reserved = 0;
5402 pSMB->Flags = 0;
5403 pSMB->Timeout = 0;
5404 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005405 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5406 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005407 offset = param_offset + params;
5408
5409 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005410 /* BB find exact max SMB PDU from sess structure BB */
5411 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005412 pSMB->SetupCount = 1;
5413 pSMB->Reserved3 = 0;
5414 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5415 byte_count = 1 /* pad */ + params + 12;
5416
5417 pSMB->DataCount = cpu_to_le16(12);
5418 pSMB->ParameterCount = cpu_to_le16(params);
5419 pSMB->TotalDataCount = pSMB->DataCount;
5420 pSMB->TotalParameterCount = pSMB->ParameterCount;
5421 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5422 pSMB->DataOffset = cpu_to_le16(offset);
5423
5424 /* Params. */
5425 pSMB->FileNum = 0;
5426 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5427
5428 /* Data. */
5429 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5430 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5431 pSMB->ClientUnixCap = cpu_to_le64(cap);
5432
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005433 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005434 pSMB->ByteCount = cpu_to_le16(byte_count);
5435
5436 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5437 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5438 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005439 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005440 } else { /* decode response */
5441 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005442 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005443 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005444 }
5445 cifs_buf_release(pSMB);
5446
5447 if (rc == -EAGAIN)
5448 goto SETFSUnixRetry;
5449
5450 return rc;
5451}
5452
5453
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
5455int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005456CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005457 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458{
5459/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5460 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5461 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5462 FILE_SYSTEM_POSIX_INFO *response_data;
5463 int rc = 0;
5464 int bytes_returned = 0;
5465 __u16 params, byte_count;
5466
Joe Perchesf96637b2013-05-04 22:12:25 -05005467 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468QFSPosixRetry:
5469 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5470 (void **) &pSMBr);
5471 if (rc)
5472 return rc;
5473
5474 params = 2; /* level */
5475 pSMB->TotalDataCount = 0;
5476 pSMB->DataCount = 0;
5477 pSMB->DataOffset = 0;
5478 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005479 /* BB find exact max SMB PDU from sess structure BB */
5480 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 pSMB->MaxSetupCount = 0;
5482 pSMB->Reserved = 0;
5483 pSMB->Flags = 0;
5484 pSMB->Timeout = 0;
5485 pSMB->Reserved2 = 0;
5486 byte_count = params + 1 /* pad */ ;
5487 pSMB->ParameterCount = cpu_to_le16(params);
5488 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005489 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5490 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 pSMB->SetupCount = 1;
5492 pSMB->Reserved3 = 0;
5493 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5494 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005495 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 pSMB->ByteCount = cpu_to_le16(byte_count);
5497
5498 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5499 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5500 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005501 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 } else { /* decode response */
5503 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5504
Jeff Layton820a8032011-05-04 08:05:26 -04005505 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 rc = -EIO; /* bad smb */
5507 } else {
5508 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5509 response_data =
5510 (FILE_SYSTEM_POSIX_INFO
5511 *) (((char *) &pSMBr->hdr.Protocol) +
5512 data_offset);
5513 FSData->f_bsize =
5514 le32_to_cpu(response_data->BlockSize);
5515 FSData->f_blocks =
5516 le64_to_cpu(response_data->TotalBlocks);
5517 FSData->f_bfree =
5518 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005519 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 FSData->f_bavail = FSData->f_bfree;
5521 } else {
5522 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005523 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 }
Steve French790fe572007-07-07 19:25:05 +00005525 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005527 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005528 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005530 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 }
5532 }
5533 cifs_buf_release(pSMB);
5534
5535 if (rc == -EAGAIN)
5536 goto QFSPosixRetry;
5537
5538 return rc;
5539}
5540
5541
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005542/*
5543 * We can not use write of zero bytes trick to set file size due to need for
5544 * large file support. Also note that this SetPathInfo is preferred to
5545 * SetFileInfo based method in next routine which is only needed to work around
5546 * a sharing violation bugin Samba which this routine can run into.
5547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005549CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005550 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5551 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
5553 struct smb_com_transaction2_spi_req *pSMB = NULL;
5554 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5555 struct file_end_of_file_info *parm_data;
5556 int name_len;
5557 int rc = 0;
5558 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005559 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 __u16 params, byte_count, data_count, param_offset, offset;
5562
Joe Perchesf96637b2013-05-04 22:12:25 -05005563 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564SetEOFRetry:
5565 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5566 (void **) &pSMBr);
5567 if (rc)
5568 return rc;
5569
5570 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5571 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005572 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5573 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 name_len++; /* trailing null */
5575 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005576 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005577 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005579 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 }
5581 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005582 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005584 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 pSMB->MaxSetupCount = 0;
5586 pSMB->Reserved = 0;
5587 pSMB->Flags = 0;
5588 pSMB->Timeout = 0;
5589 pSMB->Reserved2 = 0;
5590 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005591 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005593 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005594 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5595 pSMB->InformationLevel =
5596 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5597 else
5598 pSMB->InformationLevel =
5599 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5600 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5602 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005603 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 else
5605 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005606 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
5608
5609 parm_data =
5610 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5611 offset);
5612 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5613 pSMB->DataOffset = cpu_to_le16(offset);
5614 pSMB->SetupCount = 1;
5615 pSMB->Reserved3 = 0;
5616 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5617 byte_count = 3 /* pad */ + params + data_count;
5618 pSMB->DataCount = cpu_to_le16(data_count);
5619 pSMB->TotalDataCount = pSMB->DataCount;
5620 pSMB->ParameterCount = cpu_to_le16(params);
5621 pSMB->TotalParameterCount = pSMB->ParameterCount;
5622 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005623 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 parm_data->FileSize = cpu_to_le64(size);
5625 pSMB->ByteCount = cpu_to_le16(byte_count);
5626 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5627 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005628 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005629 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
5631 cifs_buf_release(pSMB);
5632
5633 if (rc == -EAGAIN)
5634 goto SetEOFRetry;
5635
5636 return rc;
5637}
5638
5639int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005640CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5641 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642{
5643 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 struct file_end_of_file_info *parm_data;
5645 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 __u16 params, param_offset, offset, byte_count, count;
5647
Joe Perchesf96637b2013-05-04 22:12:25 -05005648 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5649 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005650 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 if (rc)
5653 return rc;
5654
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005655 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5656 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005657
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 params = 6;
5659 pSMB->MaxSetupCount = 0;
5660 pSMB->Reserved = 0;
5661 pSMB->Flags = 0;
5662 pSMB->Timeout = 0;
5663 pSMB->Reserved2 = 0;
5664 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5665 offset = param_offset + params;
5666
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 count = sizeof(struct file_end_of_file_info);
5668 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005669 /* BB find exact max SMB PDU from sess structure BB */
5670 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 pSMB->SetupCount = 1;
5672 pSMB->Reserved3 = 0;
5673 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5674 byte_count = 3 /* pad */ + params + count;
5675 pSMB->DataCount = cpu_to_le16(count);
5676 pSMB->ParameterCount = cpu_to_le16(params);
5677 pSMB->TotalDataCount = pSMB->DataCount;
5678 pSMB->TotalParameterCount = pSMB->ParameterCount;
5679 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5680 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005681 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5682 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 pSMB->DataOffset = cpu_to_le16(offset);
5684 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005685 pSMB->Fid = cfile->fid.netfid;
5686 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5688 pSMB->InformationLevel =
5689 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5690 else
5691 pSMB->InformationLevel =
5692 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005693 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5695 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005696 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 else
5698 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005699 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 }
5701 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005702 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005704 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005705 cifs_small_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005707 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5708 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 }
5710
Steve French50c2f752007-07-13 00:33:32 +00005711 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 since file handle passed in no longer valid */
5713
5714 return rc;
5715}
5716
Steve French50c2f752007-07-13 00:33:32 +00005717/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 an open handle, rather than by pathname - this is awkward due to
5719 potential access conflicts on the open, but it is unavoidable for these
5720 old servers since the only other choice is to go from 100 nanosecond DCE
5721 time and resort to the original setpathinfo level which takes the ancient
5722 DOS time format with 2 second granularity */
5723int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005724CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005725 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
5727 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 char *data_offset;
5729 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 __u16 params, param_offset, offset, byte_count, count;
5731
Joe Perchesf96637b2013-05-04 22:12:25 -05005732 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005733 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 if (rc)
5736 return rc;
5737
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005738 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5739 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005740
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 params = 6;
5742 pSMB->MaxSetupCount = 0;
5743 pSMB->Reserved = 0;
5744 pSMB->Flags = 0;
5745 pSMB->Timeout = 0;
5746 pSMB->Reserved2 = 0;
5747 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5748 offset = param_offset + params;
5749
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005750 data_offset = (char *)pSMB +
5751 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Steve French26f57362007-08-30 22:09:15 +00005753 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005755 /* BB find max SMB PDU from sess */
5756 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 pSMB->SetupCount = 1;
5758 pSMB->Reserved3 = 0;
5759 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5760 byte_count = 3 /* pad */ + params + count;
5761 pSMB->DataCount = cpu_to_le16(count);
5762 pSMB->ParameterCount = cpu_to_le16(params);
5763 pSMB->TotalDataCount = pSMB->DataCount;
5764 pSMB->TotalParameterCount = pSMB->ParameterCount;
5765 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5766 pSMB->DataOffset = cpu_to_le16(offset);
5767 pSMB->Fid = fid;
5768 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5769 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5770 else
5771 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5772 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005773 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005775 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005776 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005777 cifs_small_buf_release(pSMB);
Steve Frenchad7a2922008-02-07 23:25:02 +00005778 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005779 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5780 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
Steve French50c2f752007-07-13 00:33:32 +00005782 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 since file handle passed in no longer valid */
5784
5785 return rc;
5786}
5787
Jeff Layton6d22f092008-09-23 11:48:35 -04005788int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005789CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005790 bool delete_file, __u16 fid, __u32 pid_of_opener)
5791{
5792 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5793 char *data_offset;
5794 int rc = 0;
5795 __u16 params, param_offset, offset, byte_count, count;
5796
Joe Perchesf96637b2013-05-04 22:12:25 -05005797 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005798 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5799
5800 if (rc)
5801 return rc;
5802
5803 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5804 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5805
5806 params = 6;
5807 pSMB->MaxSetupCount = 0;
5808 pSMB->Reserved = 0;
5809 pSMB->Flags = 0;
5810 pSMB->Timeout = 0;
5811 pSMB->Reserved2 = 0;
5812 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5813 offset = param_offset + params;
5814
5815 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5816
5817 count = 1;
5818 pSMB->MaxParameterCount = cpu_to_le16(2);
5819 /* BB find max SMB PDU from sess */
5820 pSMB->MaxDataCount = cpu_to_le16(1000);
5821 pSMB->SetupCount = 1;
5822 pSMB->Reserved3 = 0;
5823 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5824 byte_count = 3 /* pad */ + params + count;
5825 pSMB->DataCount = cpu_to_le16(count);
5826 pSMB->ParameterCount = cpu_to_le16(params);
5827 pSMB->TotalDataCount = pSMB->DataCount;
5828 pSMB->TotalParameterCount = pSMB->ParameterCount;
5829 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5830 pSMB->DataOffset = cpu_to_le16(offset);
5831 pSMB->Fid = fid;
5832 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5833 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005834 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005835 pSMB->ByteCount = cpu_to_le16(byte_count);
5836 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005837 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07005838 cifs_small_buf_release(pSMB);
Jeff Layton6d22f092008-09-23 11:48:35 -04005839 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005840 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005841
5842 return rc;
5843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005846CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005847 const char *fileName, const FILE_BASIC_INFO *data,
5848 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
5850 TRANSACTION2_SPI_REQ *pSMB = NULL;
5851 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5852 int name_len;
5853 int rc = 0;
5854 int bytes_returned = 0;
5855 char *data_offset;
5856 __u16 params, param_offset, offset, byte_count, count;
5857
Joe Perchesf96637b2013-05-04 22:12:25 -05005858 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860SetTimesRetry:
5861 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5862 (void **) &pSMBr);
5863 if (rc)
5864 return rc;
5865
5866 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5867 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005868 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5869 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 name_len++; /* trailing null */
5871 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005872 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 name_len = strnlen(fileName, PATH_MAX);
5874 name_len++; /* trailing null */
5875 strncpy(pSMB->FileName, fileName, name_len);
5876 }
5877
5878 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005879 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005881 /* BB find max SMB PDU from sess structure BB */
5882 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 pSMB->MaxSetupCount = 0;
5884 pSMB->Reserved = 0;
5885 pSMB->Flags = 0;
5886 pSMB->Timeout = 0;
5887 pSMB->Reserved2 = 0;
5888 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005889 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 offset = param_offset + params;
5891 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5892 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5893 pSMB->DataOffset = cpu_to_le16(offset);
5894 pSMB->SetupCount = 1;
5895 pSMB->Reserved3 = 0;
5896 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5897 byte_count = 3 /* pad */ + params + count;
5898
5899 pSMB->DataCount = cpu_to_le16(count);
5900 pSMB->ParameterCount = cpu_to_le16(params);
5901 pSMB->TotalDataCount = pSMB->DataCount;
5902 pSMB->TotalParameterCount = pSMB->ParameterCount;
5903 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5904 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5905 else
5906 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5907 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005908 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005909 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 pSMB->ByteCount = cpu_to_le16(byte_count);
5911 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5912 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005913 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005914 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
5916 cifs_buf_release(pSMB);
5917
5918 if (rc == -EAGAIN)
5919 goto SetTimesRetry;
5920
5921 return rc;
5922}
5923
5924/* Can not be used to set time stamps yet (due to old DOS time format) */
5925/* Can be used to set attributes */
5926#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5927 handling it anyway and NT4 was what we thought it would be needed for
5928 Do not delete it until we prove whether needed for Win9x though */
5929int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005930CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 __u16 dos_attrs, const struct nls_table *nls_codepage)
5932{
5933 SETATTR_REQ *pSMB = NULL;
5934 SETATTR_RSP *pSMBr = NULL;
5935 int rc = 0;
5936 int bytes_returned;
5937 int name_len;
5938
Joe Perchesf96637b2013-05-04 22:12:25 -05005939 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941SetAttrLgcyRetry:
5942 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5943 (void **) &pSMBr);
5944 if (rc)
5945 return rc;
5946
5947 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5948 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005949 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5950 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 name_len++; /* trailing null */
5952 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005953 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 name_len = strnlen(fileName, PATH_MAX);
5955 name_len++; /* trailing null */
5956 strncpy(pSMB->fileName, fileName, name_len);
5957 }
5958 pSMB->attr = cpu_to_le16(dos_attrs);
5959 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005960 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5962 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5963 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005964 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005965 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 cifs_buf_release(pSMB);
5968
5969 if (rc == -EAGAIN)
5970 goto SetAttrLgcyRetry;
5971
5972 return rc;
5973}
5974#endif /* temporarily unneeded SetAttr legacy function */
5975
Jeff Layton654cf142009-07-09 20:02:49 -04005976static void
5977cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5978 const struct cifs_unix_set_info_args *args)
5979{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005980 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005981 u64 mode = args->mode;
5982
Eric W. Biederman49418b22013-02-06 00:57:56 -08005983 if (uid_valid(args->uid))
5984 uid = from_kuid(&init_user_ns, args->uid);
5985 if (gid_valid(args->gid))
5986 gid = from_kgid(&init_user_ns, args->gid);
5987
Jeff Layton654cf142009-07-09 20:02:49 -04005988 /*
5989 * Samba server ignores set of file size to zero due to bugs in some
5990 * older clients, but we should be precise - we use SetFileSize to
5991 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005992 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005993 * zero instead of -1 here
5994 */
5995 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5996 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5997 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5998 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5999 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08006000 data_offset->Uid = cpu_to_le64(uid);
6001 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04006002 /* better to leave device as zero when it is */
6003 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
6004 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
6005 data_offset->Permissions = cpu_to_le64(mode);
6006
6007 if (S_ISREG(mode))
6008 data_offset->Type = cpu_to_le32(UNIX_FILE);
6009 else if (S_ISDIR(mode))
6010 data_offset->Type = cpu_to_le32(UNIX_DIR);
6011 else if (S_ISLNK(mode))
6012 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6013 else if (S_ISCHR(mode))
6014 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6015 else if (S_ISBLK(mode))
6016 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6017 else if (S_ISFIFO(mode))
6018 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6019 else if (S_ISSOCK(mode))
6020 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6021}
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006024CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006025 const struct cifs_unix_set_info_args *args,
6026 u16 fid, u32 pid_of_opener)
6027{
6028 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006029 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006030 int rc = 0;
6031 u16 params, param_offset, offset, byte_count, count;
6032
Joe Perchesf96637b2013-05-04 22:12:25 -05006033 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006034 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6035
6036 if (rc)
6037 return rc;
6038
6039 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6040 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6041
6042 params = 6;
6043 pSMB->MaxSetupCount = 0;
6044 pSMB->Reserved = 0;
6045 pSMB->Flags = 0;
6046 pSMB->Timeout = 0;
6047 pSMB->Reserved2 = 0;
6048 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6049 offset = param_offset + params;
6050
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006051 data_offset = (char *)pSMB +
6052 offsetof(struct smb_hdr, Protocol) + offset;
6053
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006054 count = sizeof(FILE_UNIX_BASIC_INFO);
6055
6056 pSMB->MaxParameterCount = cpu_to_le16(2);
6057 /* BB find max SMB PDU from sess */
6058 pSMB->MaxDataCount = cpu_to_le16(1000);
6059 pSMB->SetupCount = 1;
6060 pSMB->Reserved3 = 0;
6061 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6062 byte_count = 3 /* pad */ + params + count;
6063 pSMB->DataCount = cpu_to_le16(count);
6064 pSMB->ParameterCount = cpu_to_le16(params);
6065 pSMB->TotalDataCount = pSMB->DataCount;
6066 pSMB->TotalParameterCount = pSMB->ParameterCount;
6067 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6068 pSMB->DataOffset = cpu_to_le16(offset);
6069 pSMB->Fid = fid;
6070 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6071 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006072 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006073 pSMB->ByteCount = cpu_to_le16(byte_count);
6074
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006075 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006076
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006077 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovskyda502f72016-10-25 11:38:47 -07006078 cifs_small_buf_release(pSMB);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006079 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006080 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6081 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006082
6083 /* Note: On -EAGAIN error only caller can retry on handle based calls
6084 since file handle passed in no longer valid */
6085
6086 return rc;
6087}
6088
6089int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006090CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006091 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006092 const struct cifs_unix_set_info_args *args,
6093 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094{
6095 TRANSACTION2_SPI_REQ *pSMB = NULL;
6096 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6097 int name_len;
6098 int rc = 0;
6099 int bytes_returned = 0;
6100 FILE_UNIX_BASIC_INFO *data_offset;
6101 __u16 params, param_offset, offset, count, byte_count;
6102
Joe Perchesf96637b2013-05-04 22:12:25 -05006103 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104setPermsRetry:
6105 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6106 (void **) &pSMBr);
6107 if (rc)
6108 return rc;
6109
6110 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6111 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006112 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006113 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 name_len++; /* trailing null */
6115 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006116 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006117 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006119 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 }
6121
6122 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006123 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006125 /* BB find max SMB PDU from sess structure BB */
6126 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 pSMB->MaxSetupCount = 0;
6128 pSMB->Reserved = 0;
6129 pSMB->Flags = 0;
6130 pSMB->Timeout = 0;
6131 pSMB->Reserved2 = 0;
6132 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006133 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 offset = param_offset + params;
6135 data_offset =
6136 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6137 offset);
6138 memset(data_offset, 0, count);
6139 pSMB->DataOffset = cpu_to_le16(offset);
6140 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6141 pSMB->SetupCount = 1;
6142 pSMB->Reserved3 = 0;
6143 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6144 byte_count = 3 /* pad */ + params + count;
6145 pSMB->ParameterCount = cpu_to_le16(params);
6146 pSMB->DataCount = cpu_to_le16(count);
6147 pSMB->TotalParameterCount = pSMB->ParameterCount;
6148 pSMB->TotalDataCount = pSMB->DataCount;
6149 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6150 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006151 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006152
Jeff Layton654cf142009-07-09 20:02:49 -04006153 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
6155 pSMB->ByteCount = cpu_to_le16(byte_count);
6156 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6157 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006158 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006159 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
Steve French0d817bc2008-05-22 02:02:03 +00006161 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 if (rc == -EAGAIN)
6163 goto setPermsRetry;
6164 return rc;
6165}
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006168/*
6169 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6170 * function used by listxattr and getxattr type calls. When ea_name is set,
6171 * it looks for that attribute name and stuffs that value into the EAData
6172 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6173 * buffer. In both cases, the return value is either the length of the
6174 * resulting data or a negative error code. If EAData is a NULL pointer then
6175 * the data isn't copied to it, but the length is returned.
6176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006178CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006179 const unsigned char *searchName, const unsigned char *ea_name,
6180 char *EAData, size_t buf_size,
6181 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182{
6183 /* BB assumes one setup word */
6184 TRANSACTION2_QPI_REQ *pSMB = NULL;
6185 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6186 int rc = 0;
6187 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006188 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006189 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006190 struct fea *temp_fea;
6191 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006192 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006193 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006194 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
Joe Perchesf96637b2013-05-04 22:12:25 -05006196 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197QAllEAsRetry:
6198 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6199 (void **) &pSMBr);
6200 if (rc)
6201 return rc;
6202
6203 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006204 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006205 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6206 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006207 list_len++; /* trailing null */
6208 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006210 list_len = strnlen(searchName, PATH_MAX);
6211 list_len++; /* trailing null */
6212 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 }
6214
Jeff Layton6e462b92010-02-10 16:18:26 -05006215 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 pSMB->TotalDataCount = 0;
6217 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006218 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006219 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 pSMB->MaxSetupCount = 0;
6221 pSMB->Reserved = 0;
6222 pSMB->Flags = 0;
6223 pSMB->Timeout = 0;
6224 pSMB->Reserved2 = 0;
6225 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006226 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 pSMB->DataCount = 0;
6228 pSMB->DataOffset = 0;
6229 pSMB->SetupCount = 1;
6230 pSMB->Reserved3 = 0;
6231 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6232 byte_count = params + 1 /* pad */ ;
6233 pSMB->TotalParameterCount = cpu_to_le16(params);
6234 pSMB->ParameterCount = pSMB->TotalParameterCount;
6235 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6236 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006237 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 pSMB->ByteCount = cpu_to_le16(byte_count);
6239
6240 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6241 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6242 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006243 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006244 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006246
6247
6248 /* BB also check enough total bytes returned */
6249 /* BB we need to improve the validity checking
6250 of these trans2 responses */
6251
6252 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006253 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006254 rc = -EIO; /* bad smb */
6255 goto QAllEAsOut;
6256 }
6257
6258 /* check that length of list is not more than bcc */
6259 /* check that each entry does not go beyond length
6260 of list */
6261 /* check that each element of each entry does not
6262 go beyond end of list */
6263 /* validate_trans2_offsets() */
6264 /* BB check if start of smb + data_offset > &bcc+ bcc */
6265
6266 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6267 ea_response_data = (struct fealist *)
6268 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6269
Jeff Layton6e462b92010-02-10 16:18:26 -05006270 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006271 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006272 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006273 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006274 /* didn't find the named attribute */
6275 if (ea_name)
6276 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006277 goto QAllEAsOut;
6278 }
6279
Jeff Layton0cd126b2010-02-10 16:18:26 -05006280 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006281 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006282 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006283 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006284 rc = -EIO;
6285 goto QAllEAsOut;
6286 }
6287
Jeff Laytonf0d38682010-02-10 16:18:26 -05006288 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006289 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006290 temp_fea = ea_response_data->list;
6291 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006292 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006293 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006294 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006295
Jeff Layton6e462b92010-02-10 16:18:26 -05006296 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006297 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006298 /* make sure we can read name_len and value_len */
6299 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006300 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006301 rc = -EIO;
6302 goto QAllEAsOut;
6303 }
6304
6305 name_len = temp_fea->name_len;
6306 value_len = le16_to_cpu(temp_fea->value_len);
6307 list_len -= name_len + 1 + value_len;
6308 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006309 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006310 rc = -EIO;
6311 goto QAllEAsOut;
6312 }
6313
Jeff Layton31c05192010-02-10 16:18:26 -05006314 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006315 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006316 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006317 temp_ptr += name_len + 1;
6318 rc = value_len;
6319 if (buf_size == 0)
6320 goto QAllEAsOut;
6321 if ((size_t)value_len > buf_size) {
6322 rc = -ERANGE;
6323 goto QAllEAsOut;
6324 }
6325 memcpy(EAData, temp_ptr, value_len);
6326 goto QAllEAsOut;
6327 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006328 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006329 /* account for prefix user. and trailing null */
6330 rc += (5 + 1 + name_len);
6331 if (rc < (int) buf_size) {
6332 memcpy(EAData, "user.", 5);
6333 EAData += 5;
6334 memcpy(EAData, temp_ptr, name_len);
6335 EAData += name_len;
6336 /* null terminate name */
6337 *EAData = 0;
6338 ++EAData;
6339 } else if (buf_size == 0) {
6340 /* skip copy - calc size only */
6341 } else {
6342 /* stop before overrun buffer */
6343 rc = -ERANGE;
6344 break;
6345 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006346 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006347 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006348 temp_fea = (struct fea *)temp_ptr;
6349 }
6350
Jeff Layton31c05192010-02-10 16:18:26 -05006351 /* didn't find the named attribute */
6352 if (ea_name)
6353 rc = -ENODATA;
6354
Jeff Laytonf0d38682010-02-10 16:18:26 -05006355QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006356 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 if (rc == -EAGAIN)
6358 goto QAllEAsRetry;
6359
6360 return (ssize_t)rc;
6361}
6362
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006364CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6365 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006366 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6367 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368{
6369 struct smb_com_transaction2_spi_req *pSMB = NULL;
6370 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6371 struct fealist *parm_data;
6372 int name_len;
6373 int rc = 0;
6374 int bytes_returned = 0;
6375 __u16 params, param_offset, byte_count, offset, count;
6376
Joe Perchesf96637b2013-05-04 22:12:25 -05006377 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378SetEARetry:
6379 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6380 (void **) &pSMBr);
6381 if (rc)
6382 return rc;
6383
6384 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6385 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006386 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6387 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 name_len++; /* trailing null */
6389 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006390 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 name_len = strnlen(fileName, PATH_MAX);
6392 name_len++; /* trailing null */
6393 strncpy(pSMB->FileName, fileName, name_len);
6394 }
6395
6396 params = 6 + name_len;
6397
6398 /* done calculating parms using name_len of file name,
6399 now use name_len to calculate length of ea name
6400 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006401 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 name_len = 0;
6403 else
Steve French50c2f752007-07-13 00:33:32 +00006404 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Steve Frenchdae5dbd2007-12-30 23:49:57 +00006406 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006408 /* BB find max SMB PDU from sess */
6409 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 pSMB->MaxSetupCount = 0;
6411 pSMB->Reserved = 0;
6412 pSMB->Flags = 0;
6413 pSMB->Timeout = 0;
6414 pSMB->Reserved2 = 0;
6415 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006416 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 offset = param_offset + params;
6418 pSMB->InformationLevel =
6419 cpu_to_le16(SMB_SET_FILE_EA);
6420
6421 parm_data =
6422 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6423 offset);
6424 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6425 pSMB->DataOffset = cpu_to_le16(offset);
6426 pSMB->SetupCount = 1;
6427 pSMB->Reserved3 = 0;
6428 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6429 byte_count = 3 /* pad */ + params + count;
6430 pSMB->DataCount = cpu_to_le16(count);
6431 parm_data->list_len = cpu_to_le32(count);
6432 parm_data->list[0].EA_flags = 0;
6433 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006434 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006436 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006437 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 parm_data->list[0].name[name_len] = 0;
6439 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6440 /* caller ensures that ea_value_len is less than 64K but
6441 we need to ensure that it fits within the smb */
6442
Steve French50c2f752007-07-13 00:33:32 +00006443 /*BB add length check to see if it would fit in
6444 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006445 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6446 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006447 memcpy(parm_data->list[0].name+name_len+1,
6448 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
6450 pSMB->TotalDataCount = pSMB->DataCount;
6451 pSMB->ParameterCount = cpu_to_le16(params);
6452 pSMB->TotalParameterCount = pSMB->ParameterCount;
6453 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006454 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 pSMB->ByteCount = cpu_to_le16(byte_count);
6456 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6457 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006458 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006459 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
6461 cifs_buf_release(pSMB);
6462
6463 if (rc == -EAGAIN)
6464 goto SetEARetry;
6465
6466 return rc;
6467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468#endif
Steve French0eff0e22011-02-24 05:39:23 +00006469
6470#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6471/*
6472 * Years ago the kernel added a "dnotify" function for Samba server,
6473 * to allow network clients (such as Windows) to display updated
6474 * lists of files in directory listings automatically when
6475 * files are added by one user when another user has the
6476 * same directory open on their desktop. The Linux cifs kernel
6477 * client hooked into the kernel side of this interface for
6478 * the same reason, but ironically when the VFS moved from
6479 * "dnotify" to "inotify" it became harder to plug in Linux
6480 * network file system clients (the most obvious use case
6481 * for notify interfaces is when multiple users can update
6482 * the contents of the same directory - exactly what network
6483 * file systems can do) although the server (Samba) could
6484 * still use it. For the short term we leave the worker
6485 * function ifdeffed out (below) until inotify is fixed
6486 * in the VFS to make it easier to plug in network file
6487 * system clients. If inotify turns out to be permanently
6488 * incompatible for network fs clients, we could instead simply
6489 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6490 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006491int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006492 const int notify_subdirs, const __u16 netfid,
6493 __u32 filter, struct file *pfile, int multishot,
6494 const struct nls_table *nls_codepage)
6495{
6496 int rc = 0;
6497 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6498 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6499 struct dir_notify_req *dnotify_req;
6500 int bytes_returned;
6501
Joe Perchesf96637b2013-05-04 22:12:25 -05006502 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006503 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6504 (void **) &pSMBr);
6505 if (rc)
6506 return rc;
6507
6508 pSMB->TotalParameterCount = 0 ;
6509 pSMB->TotalDataCount = 0;
6510 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006511 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006512 pSMB->MaxSetupCount = 4;
6513 pSMB->Reserved = 0;
6514 pSMB->ParameterOffset = 0;
6515 pSMB->DataCount = 0;
6516 pSMB->DataOffset = 0;
6517 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6518 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6519 pSMB->ParameterCount = pSMB->TotalParameterCount;
6520 if (notify_subdirs)
6521 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6522 pSMB->Reserved2 = 0;
6523 pSMB->CompletionFilter = cpu_to_le32(filter);
6524 pSMB->Fid = netfid; /* file handle always le */
6525 pSMB->ByteCount = 0;
6526
6527 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6528 (struct smb_hdr *)pSMBr, &bytes_returned,
6529 CIFS_ASYNC_OP);
6530 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006531 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006532 } else {
6533 /* Add file to outstanding requests */
6534 /* BB change to kmem cache alloc */
6535 dnotify_req = kmalloc(
6536 sizeof(struct dir_notify_req),
6537 GFP_KERNEL);
6538 if (dnotify_req) {
6539 dnotify_req->Pid = pSMB->hdr.Pid;
6540 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6541 dnotify_req->Mid = pSMB->hdr.Mid;
6542 dnotify_req->Tid = pSMB->hdr.Tid;
6543 dnotify_req->Uid = pSMB->hdr.Uid;
6544 dnotify_req->netfid = netfid;
6545 dnotify_req->pfile = pfile;
6546 dnotify_req->filter = filter;
6547 dnotify_req->multishot = multishot;
6548 spin_lock(&GlobalMid_Lock);
6549 list_add_tail(&dnotify_req->lhead,
6550 &GlobalDnotifyReqList);
6551 spin_unlock(&GlobalMid_Lock);
6552 } else
6553 rc = -ENOMEM;
6554 }
6555 cifs_buf_release(pSMB);
6556 return rc;
6557}
6558#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */