blob: 90b4f9f7de660a261b5f93322af59282294953fd [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 Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#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 */
Jeff Layton44772882010-10-15 15:34:03 -0400101 spin_lock(&cifs_file_list_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 }
Jeff Layton44772882010-10-15 15:34:03 -0400107 spin_unlock(&cifs_file_list_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400108 /*
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
110 * to this tcon.
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jeff Layton9162ab22009-09-03 12:07:17 -0400114/* reconnect the socket, tcon, and smb session if needed */
115static int
Steve French96daf2b2011-05-27 04:34:02 +0000116cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400117{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400118 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000119 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
122
123 /*
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
126 * calling routine
127 */
128 if (!tcon)
129 return 0;
130
131 ses = tcon->ses;
132 server = ses->server;
133
134 /*
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
137 */
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
143 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400144 return -ENODEV;
145 }
146 }
147
Jeff Layton9162ab22009-09-03 12:07:17 -0400148 /*
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
151 */
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400155
Steve Frenchfd88ce92011-04-12 01:01:14 +0000156 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400157 if (server->tcpStatus != CifsNeedReconnect)
158 break;
159
160 /*
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
163 * back on-line
164 */
Jeff Laytond4025392011-02-07 08:54:35 -0500165 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400167 return -EHOSTDOWN;
168 }
169 }
170
171 if (!ses->need_reconnect && !tcon->need_reconnect)
172 return 0;
173
174 nls_codepage = load_nls_default();
175
176 /*
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
179 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000180 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400183 rc = cifs_setup_session(0, ses, nls_codepage);
184
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000187 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400188 goto out;
189 }
190
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400191 cifs_mark_open_files_invalid(tcon);
Jeff Layton9162ab22009-09-03 12:07:17 -0400192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000193 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400195
196 if (rc)
197 goto out;
198
Jeff Layton9162ab22009-09-03 12:07:17 -0400199 atomic_inc(&tconInfoReconnectCount);
200
201 /* tell server Unix caps we support */
202 if (ses->capabilities & CAP_UNIX)
203 reset_cifs_unix_caps(0, tcon, NULL, NULL);
204
205 /*
206 * Removed call to reopen open files here. It is safer (and faster) to
207 * reopen files one at a time as needed in read and write.
208 *
209 * FIXME: what about file locks? don't we need to reclaim them ASAP?
210 */
211
212out:
213 /*
214 * Check if handle based operation so we know whether we can continue
215 * or not without returning to caller to reset file handle
216 */
217 switch (smb_command) {
218 case SMB_COM_READ_ANDX:
219 case SMB_COM_WRITE_ANDX:
220 case SMB_COM_CLOSE:
221 case SMB_COM_FIND_CLOSE2:
222 case SMB_COM_LOCKING_ANDX:
223 rc = -EAGAIN;
224 }
225
226 unload_nls(nls_codepage);
227 return rc;
228}
229
Steve Frenchad7a2922008-02-07 23:25:02 +0000230/* Allocate and return pointer to an SMB request buffer, and set basic
231 SMB information in the SMB header. If the return code is zero, this
232 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int
Steve French96daf2b2011-05-27 04:34:02 +0000234small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000235 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jeff Laytonf5695992010-09-29 15:27:08 -0400237 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jeff Layton9162ab22009-09-03 12:07:17 -0400239 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000240 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return rc;
242
243 *request_buf = cifs_small_buf_get();
244 if (*request_buf == NULL) {
245 /* BB should we add a retry in here if not a writepage? */
246 return -ENOMEM;
247 }
248
Steve French63135e02007-07-17 17:34:02 +0000249 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000250 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Steve French790fe572007-07-07 19:25:05 +0000252 if (tcon != NULL)
253 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700254
Jeff Laytonf5695992010-09-29 15:27:08 -0400255 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000256}
257
Steve French12b3b8f2006-02-09 21:12:47 +0000258int
Steve French50c2f752007-07-13 00:33:32 +0000259small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000260 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000261{
262 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000263 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000264
Steve French5815449d2006-02-14 01:36:20 +0000265 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000266 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000267 return rc;
268
Steve French04fdabe2006-02-10 05:52:50 +0000269 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400270 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000271 if (ses->capabilities & CAP_UNICODE)
272 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000273 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000274 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
275
276 /* uid, tid can stay at zero as set in header assemble */
277
Steve French50c2f752007-07-13 00:33:32 +0000278 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000279 this function is used after 1st of session setup requests */
280
281 return rc;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* If the return code is zero, this function must fill in request_buf pointer */
285static int
Steve French96daf2b2011-05-27 04:34:02 +0000286__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400287 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *request_buf = cifs_buf_get();
290 if (*request_buf == NULL) {
291 /* BB should we add a retry in here if not a writepage? */
292 return -ENOMEM;
293 }
294 /* Although the original thought was we needed the response buf for */
295 /* potential retries of smb operations it turns out we can determine */
296 /* from the mid flags when the request buffer can be resent without */
297 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000298 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000299 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000302 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Steve French790fe572007-07-07 19:25:05 +0000304 if (tcon != NULL)
305 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700306
Jeff Laytonf5695992010-09-29 15:27:08 -0400307 return 0;
308}
309
310/* If the return code is zero, this function must fill in request_buf pointer */
311static int
Steve French96daf2b2011-05-27 04:34:02 +0000312smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400313 void **request_buf, void **response_buf)
314{
315 int rc;
316
317 rc = cifs_reconnect_tcon(tcon, smb_command);
318 if (rc)
319 return rc;
320
321 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
322}
323
324static int
Steve French96daf2b2011-05-27 04:34:02 +0000325smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400326 void **request_buf, void **response_buf)
327{
328 if (tcon->ses->need_reconnect || tcon->need_reconnect)
329 return -EHOSTDOWN;
330
331 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Steve French50c2f752007-07-13 00:33:32 +0000334static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jeff Layton12df83c2011-01-20 13:36:51 -0500336 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Jeff Layton12df83c2011-01-20 13:36:51 -0500338 /* check for plausible wct */
339 if (pSMB->hdr.WordCount < 10)
340 goto vt2_err;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500343 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
344 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
345 goto vt2_err;
346
Jeff Layton12df83c2011-01-20 13:36:51 -0500347 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
348 if (total_size >= 512)
349 goto vt2_err;
350
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400351 /* check that bcc is at least as big as parms + data, and that it is
352 * less than negotiated smb buffer
353 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500354 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
355 if (total_size > get_bcc(&pSMB->hdr) ||
356 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
357 goto vt2_err;
358
359 return 0;
360vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000361 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Jeff Layton690c5222011-01-20 13:36:51 -0500365
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400366static int
Jeff Layton3f618222013-06-12 19:52:14 -0500367decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400368{
369 int rc = 0;
370 u16 count;
371 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500372 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400373
374 count = get_bcc(&pSMBr->hdr);
375 if (count < SMB1_CLIENT_GUID_SIZE)
376 return -EIO;
377
378 spin_lock(&cifs_tcp_ses_lock);
379 if (server->srv_count > 1) {
380 spin_unlock(&cifs_tcp_ses_lock);
381 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
382 cifs_dbg(FYI, "server UID changed\n");
383 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
384 }
385 } else {
386 spin_unlock(&cifs_tcp_ses_lock);
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
388 }
389
390 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500391 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400392 } else {
393 count -= SMB1_CLIENT_GUID_SIZE;
394 rc = decode_negTokenInit(
395 pSMBr->u.extended_response.SecurityBlob, count, server);
396 if (rc != 1)
397 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400398 }
399
400 return 0;
401}
402
Jeff Layton9ddec562013-05-26 07:00:58 -0400403int
Jeff Layton38d77c52013-05-26 07:01:00 -0400404cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400405{
Jeff Layton50285882013-06-27 12:45:00 -0400406 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
407 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400408 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
409
410 /*
411 * Is signing required by mnt options? If not then check
412 * global_secflags to see if it is there.
413 */
414 if (!mnt_sign_required)
415 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
416 CIFSSEC_MUST_SIGN);
417
418 /*
419 * If signing is required then it's automatically enabled too,
420 * otherwise, check to see if the secflags allow it.
421 */
422 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
423 (global_secflags & CIFSSEC_MAY_SIGN);
424
425 /* If server requires signing, does client allow it? */
426 if (srv_sign_required) {
427 if (!mnt_sign_enabled) {
428 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
429 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400430 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400431 server->sign = true;
432 }
433
434 /* If client requires signing, does server allow it? */
435 if (mnt_sign_required) {
436 if (!srv_sign_enabled) {
437 cifs_dbg(VFS, "Server does not support signing!");
438 return -ENOTSUPP;
439 }
440 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400441 }
442
443 return 0;
444}
445
Jeff Layton2190eca2013-05-26 07:00:57 -0400446#ifdef CONFIG_CIFS_WEAK_PW_HASH
447static int
Jeff Layton3f618222013-06-12 19:52:14 -0500448decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400449{
450 __s16 tmp;
451 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
452
453 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
454 return -EOPNOTSUPP;
455
Jeff Layton2190eca2013-05-26 07:00:57 -0400456 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
457 server->maxReq = min_t(unsigned int,
458 le16_to_cpu(rsp->MaxMpxCount),
459 cifs_max_pending);
460 set_credits(server, server->maxReq);
461 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Jeff Layton2190eca2013-05-26 07:00:57 -0400462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
465 server->max_rw = 0xFF00;
466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
467 } else {
468 server->max_rw = 0;/* do not need to use raw anyway */
469 server->capabilities = CAP_MPX_MODE;
470 }
471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
472 if (tmp == -1) {
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
478 * this requirement.
479 */
480 int val, seconds, remain, result;
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500677 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Steve French50c2f752007-07-13 00:33:32 +0000679 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500680 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (rc == -EAGAIN)
682 rc = 0;
683
684 return rc;
685}
686
Jeff Layton766fdbb2011-01-11 07:24:21 -0500687/*
688 * This is a no-op for now. We're not really interested in the reply, but
689 * rather in the fact that the server sent one and that server->lstrp
690 * gets updated.
691 *
692 * FIXME: maybe we should consider checking that the reply matches request?
693 */
694static void
695cifs_echo_callback(struct mid_q_entry *mid)
696{
697 struct TCP_Server_Info *server = mid->callback_data;
698
Christopher Oo5fb4e282015-06-25 16:10:48 -0700699 mutex_lock(&server->srv_mutex);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500700 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -0700701 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400702 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500703}
704
705int
706CIFSSMBEcho(struct TCP_Server_Info *server)
707{
708 ECHO_REQ *smb;
709 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400710 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700711 struct smb_rqst rqst = { .rq_iov = &iov,
712 .rq_nvec = 1 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500713
Joe Perchesf96637b2013-05-04 22:12:25 -0500714 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500715
716 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
717 if (rc)
718 return rc;
719
720 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000721 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500722 smb->hdr.WordCount = 1;
723 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400724 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500725 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000726 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400727 iov.iov_base = smb;
728 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500729
Jeff Laytonfec344e2012-09-18 16:20:35 -0700730 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400731 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500732 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500733 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500734
735 cifs_small_buf_release(smb);
736
737 return rc;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400741CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 LOGOFF_ANDX_REQ *pSMB;
744 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Joe Perchesf96637b2013-05-04 22:12:25 -0500746 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500747
748 /*
749 * BB: do we need to check validity of ses and server? They should
750 * always be valid since we have an active reference. If not, that
751 * should probably be a BUG()
752 */
753 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EIO;
755
Steve Frenchd7b619c2010-02-25 05:36:46 +0000756 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000757 if (ses->need_reconnect)
758 goto session_already_dead; /* no need to send SMBlogoff if uid
759 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
761 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000762 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return rc;
764 }
765
Pavel Shilovsky88257362012-05-23 14:01:59 +0400766 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700767
Jeff Layton38d77c52013-05-26 07:01:00 -0400768 if (ses->server->sign)
769 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 pSMB->hdr.Uid = ses->Suid;
772
773 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400774 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000775session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000776 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000779 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 error */
781 if (rc == -EAGAIN)
782 rc = 0;
783 return rc;
784}
785
786int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400787CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
788 const char *fileName, __u16 type,
789 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000790{
791 TRANSACTION2_SPI_REQ *pSMB = NULL;
792 TRANSACTION2_SPI_RSP *pSMBr = NULL;
793 struct unlink_psx_rq *pRqD;
794 int name_len;
795 int rc = 0;
796 int bytes_returned = 0;
797 __u16 params, param_offset, offset, byte_count;
798
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000800PsxDelete:
801 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
802 (void **) &pSMBr);
803 if (rc)
804 return rc;
805
806 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
807 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600808 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
809 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000810 name_len++; /* trailing null */
811 name_len *= 2;
812 } else { /* BB add path length overrun check */
813 name_len = strnlen(fileName, PATH_MAX);
814 name_len++; /* trailing null */
815 strncpy(pSMB->FileName, fileName, name_len);
816 }
817
818 params = 6 + name_len;
819 pSMB->MaxParameterCount = cpu_to_le16(2);
820 pSMB->MaxDataCount = 0; /* BB double check this with jra */
821 pSMB->MaxSetupCount = 0;
822 pSMB->Reserved = 0;
823 pSMB->Flags = 0;
824 pSMB->Timeout = 0;
825 pSMB->Reserved2 = 0;
826 param_offset = offsetof(struct smb_com_transaction2_spi_req,
827 InformationLevel) - 4;
828 offset = param_offset + params;
829
830 /* Setup pointer to Request Data (inode type) */
831 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
832 pRqD->type = cpu_to_le16(type);
833 pSMB->ParameterOffset = cpu_to_le16(param_offset);
834 pSMB->DataOffset = cpu_to_le16(offset);
835 pSMB->SetupCount = 1;
836 pSMB->Reserved3 = 0;
837 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
838 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
839
840 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
841 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
842 pSMB->ParameterCount = cpu_to_le16(params);
843 pSMB->TotalParameterCount = pSMB->ParameterCount;
844 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
845 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000846 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000847 pSMB->ByteCount = cpu_to_le16(byte_count);
848 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
849 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000850 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500851 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000852 cifs_buf_release(pSMB);
853
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400854 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000855
856 if (rc == -EAGAIN)
857 goto PsxDelete;
858
859 return rc;
860}
861
862int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700863CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
864 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 DELETE_FILE_REQ *pSMB = NULL;
867 DELETE_FILE_RSP *pSMBr = NULL;
868 int rc = 0;
869 int bytes_returned;
870 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500871 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873DelFileRetry:
874 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
875 (void **) &pSMBr);
876 if (rc)
877 return rc;
878
879 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700880 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
881 PATH_MAX, cifs_sb->local_nls,
882 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 name_len++; /* trailing null */
884 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700885 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700886 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700888 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 pSMB->SearchAttributes =
891 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
892 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000893 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 pSMB->ByteCount = cpu_to_le16(name_len + 1);
895 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
896 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400897 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000898 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500899 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 cifs_buf_release(pSMB);
902 if (rc == -EAGAIN)
903 goto DelFileRetry;
904
905 return rc;
906}
907
908int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400909CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
910 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 DELETE_DIRECTORY_REQ *pSMB = NULL;
913 DELETE_DIRECTORY_RSP *pSMBr = NULL;
914 int rc = 0;
915 int bytes_returned;
916 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500917 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Joe Perchesf96637b2013-05-04 22:12:25 -0500919 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920RmDirRetry:
921 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
922 (void **) &pSMBr);
923 if (rc)
924 return rc;
925
926 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400927 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
928 PATH_MAX, cifs_sb->local_nls,
929 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 name_len++; /* trailing null */
931 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700932 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400933 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400935 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000939 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 pSMB->ByteCount = cpu_to_le16(name_len + 1);
941 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
942 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400943 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000944 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500945 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 cifs_buf_release(pSMB);
948 if (rc == -EAGAIN)
949 goto RmDirRetry;
950 return rc;
951}
952
953int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300954CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
955 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 int rc = 0;
958 CREATE_DIRECTORY_REQ *pSMB = NULL;
959 CREATE_DIRECTORY_RSP *pSMBr = NULL;
960 int bytes_returned;
961 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500962 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Joe Perchesf96637b2013-05-04 22:12:25 -0500964 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965MkDirRetry:
966 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
967 (void **) &pSMBr);
968 if (rc)
969 return rc;
970
971 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600972 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300973 PATH_MAX, cifs_sb->local_nls,
974 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 name_len++; /* trailing null */
976 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700977 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 name_len = strnlen(name, PATH_MAX);
979 name_len++; /* trailing null */
980 strncpy(pSMB->DirName, name, name_len);
981 }
982
983 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000984 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 pSMB->ByteCount = cpu_to_le16(name_len + 1);
986 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
987 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400988 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000989 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500990 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 cifs_buf_release(pSMB);
993 if (rc == -EAGAIN)
994 goto MkDirRetry;
995 return rc;
996}
997
Steve French2dd29d32007-04-23 22:07:35 +0000998int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400999CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1000 __u32 posix_flags, __u64 mode, __u16 *netfid,
1001 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1002 const char *name, const struct nls_table *nls_codepage,
1003 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001004{
1005 TRANSACTION2_SPI_REQ *pSMB = NULL;
1006 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1007 int name_len;
1008 int rc = 0;
1009 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001010 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001011 OPEN_PSX_REQ *pdata;
1012 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001013
Joe Perchesf96637b2013-05-04 22:12:25 -05001014 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001015PsxCreat:
1016 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1017 (void **) &pSMBr);
1018 if (rc)
1019 return rc;
1020
1021 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1022 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001023 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1024 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001025 name_len++; /* trailing null */
1026 name_len *= 2;
1027 } else { /* BB improve the check for buffer overruns BB */
1028 name_len = strnlen(name, PATH_MAX);
1029 name_len++; /* trailing null */
1030 strncpy(pSMB->FileName, name, name_len);
1031 }
1032
1033 params = 6 + name_len;
1034 count = sizeof(OPEN_PSX_REQ);
1035 pSMB->MaxParameterCount = cpu_to_le16(2);
1036 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1037 pSMB->MaxSetupCount = 0;
1038 pSMB->Reserved = 0;
1039 pSMB->Flags = 0;
1040 pSMB->Timeout = 0;
1041 pSMB->Reserved2 = 0;
1042 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001043 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001044 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001045 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001046 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001047 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001048 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001049 pdata->OpenFlags = cpu_to_le32(*pOplock);
1050 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1051 pSMB->DataOffset = cpu_to_le16(offset);
1052 pSMB->SetupCount = 1;
1053 pSMB->Reserved3 = 0;
1054 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1055 byte_count = 3 /* pad */ + params + count;
1056
1057 pSMB->DataCount = cpu_to_le16(count);
1058 pSMB->ParameterCount = cpu_to_le16(params);
1059 pSMB->TotalDataCount = pSMB->DataCount;
1060 pSMB->TotalParameterCount = pSMB->ParameterCount;
1061 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1062 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001063 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001064 pSMB->ByteCount = cpu_to_le16(byte_count);
1065 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1066 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1067 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001068 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001069 goto psx_create_err;
1070 }
1071
Joe Perchesf96637b2013-05-04 22:12:25 -05001072 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001073 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1074
Jeff Layton820a8032011-05-04 08:05:26 -04001075 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001076 rc = -EIO; /* bad smb */
1077 goto psx_create_err;
1078 }
1079
1080 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001081 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001082 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001083
Steve French2dd29d32007-04-23 22:07:35 +00001084 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001085 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001086 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1087 /* Let caller know file was created so we can set the mode. */
1088 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001089 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001090 *pOplock |= CIFS_CREATE_ACTION;
1091 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001092 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1093 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001094 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001095 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001096 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001097 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001098 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001099 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001100 goto psx_create_err;
1101 }
Steve French50c2f752007-07-13 00:33:32 +00001102 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001103 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001104 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001105 }
Steve French2dd29d32007-04-23 22:07:35 +00001106
1107psx_create_err:
1108 cifs_buf_release(pSMB);
1109
Steve French65bc98b2009-07-10 15:27:25 +00001110 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001111 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001112 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001113 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001114
1115 if (rc == -EAGAIN)
1116 goto PsxCreat;
1117
Steve French50c2f752007-07-13 00:33:32 +00001118 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001119}
1120
Steve Frencha9d02ad2005-08-24 23:06:05 -07001121static __u16 convert_disposition(int disposition)
1122{
1123 __u16 ofun = 0;
1124
1125 switch (disposition) {
1126 case FILE_SUPERSEDE:
1127 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1128 break;
1129 case FILE_OPEN:
1130 ofun = SMBOPEN_OAPPEND;
1131 break;
1132 case FILE_CREATE:
1133 ofun = SMBOPEN_OCREATE;
1134 break;
1135 case FILE_OPEN_IF:
1136 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1137 break;
1138 case FILE_OVERWRITE:
1139 ofun = SMBOPEN_OTRUNC;
1140 break;
1141 case FILE_OVERWRITE_IF:
1142 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1143 break;
1144 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001145 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001146 ofun = SMBOPEN_OAPPEND; /* regular open */
1147 }
1148 return ofun;
1149}
1150
Jeff Layton35fc37d2008-05-14 10:22:03 -07001151static int
1152access_flags_to_smbopen_mode(const int access_flags)
1153{
1154 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1155
1156 if (masked_flags == GENERIC_READ)
1157 return SMBOPEN_READ;
1158 else if (masked_flags == GENERIC_WRITE)
1159 return SMBOPEN_WRITE;
1160
1161 /* just go for read/write */
1162 return SMBOPEN_READWRITE;
1163}
1164
Steve Frencha9d02ad2005-08-24 23:06:05 -07001165int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001166SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001167 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001168 const int access_flags, const int create_options, __u16 *netfid,
1169 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001170 const struct nls_table *nls_codepage, int remap)
1171{
1172 int rc = -EACCES;
1173 OPENX_REQ *pSMB = NULL;
1174 OPENX_RSP *pSMBr = NULL;
1175 int bytes_returned;
1176 int name_len;
1177 __u16 count;
1178
1179OldOpenRetry:
1180 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1181 (void **) &pSMBr);
1182 if (rc)
1183 return rc;
1184
1185 pSMB->AndXCommand = 0xFF; /* none */
1186
1187 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1188 count = 1; /* account for one byte pad to word boundary */
1189 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001190 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1191 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001192 name_len++; /* trailing null */
1193 name_len *= 2;
1194 } else { /* BB improve check for buffer overruns BB */
1195 count = 0; /* no pad */
1196 name_len = strnlen(fileName, PATH_MAX);
1197 name_len++; /* trailing null */
1198 strncpy(pSMB->fileName, fileName, name_len);
1199 }
1200 if (*pOplock & REQ_OPLOCK)
1201 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001202 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001203 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001204
Steve Frencha9d02ad2005-08-24 23:06:05 -07001205 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001206 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001207 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1208 /* set file as system file if special file such
1209 as fifo and server expecting SFU style and
1210 no Unix extensions */
1211
Steve French790fe572007-07-07 19:25:05 +00001212 if (create_options & CREATE_OPTION_SPECIAL)
1213 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001214 else /* BB FIXME BB */
1215 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001216
Jeff Layton67750fb2008-05-09 22:28:02 +00001217 if (create_options & CREATE_OPTION_READONLY)
1218 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001219
1220 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001221/* pSMB->CreateOptions = cpu_to_le32(create_options &
1222 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001223 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001224
1225 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001226 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001227 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001228 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001229
1230 pSMB->ByteCount = cpu_to_le16(count);
1231 /* long_op set to 1 to allow for oplock break timeouts */
1232 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001233 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001234 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001235 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001236 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001237 } else {
1238 /* BB verify if wct == 15 */
1239
Steve French582d21e2008-05-13 04:54:12 +00001240/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241
1242 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1243 /* Let caller know file was created so we can set the mode. */
1244 /* Do we care about the CreateAction in any other cases? */
1245 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001246/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001247 *pOplock |= CIFS_CREATE_ACTION; */
1248 /* BB FIXME END */
1249
Steve French790fe572007-07-07 19:25:05 +00001250 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001251 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1252 pfile_info->LastAccessTime = 0; /* BB fixme */
1253 pfile_info->LastWriteTime = 0; /* BB fixme */
1254 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001255 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001256 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001257 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001258 pfile_info->AllocationSize =
1259 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1260 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001261 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001262 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001263 }
1264 }
1265
1266 cifs_buf_release(pSMB);
1267 if (rc == -EAGAIN)
1268 goto OldOpenRetry;
1269 return rc;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001273CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1274 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001277 OPEN_REQ *req = NULL;
1278 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int bytes_returned;
1280 int name_len;
1281 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001282 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1283 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001284 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001285 const struct nls_table *nls = cifs_sb->local_nls;
1286 int create_options = oparms->create_options;
1287 int desired_access = oparms->desired_access;
1288 int disposition = oparms->disposition;
1289 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001292 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1293 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (rc)
1295 return rc;
1296
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001297 /* no commands go after this */
1298 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001300 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1301 /* account for one byte pad to word boundary */
1302 count = 1;
1303 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1304 path, PATH_MAX, nls, remap);
1305 /* trailing null */
1306 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001308 req->NameLength = cpu_to_le16(name_len);
1309 } else {
1310 /* BB improve check for buffer overruns BB */
1311 /* no pad */
1312 count = 0;
1313 name_len = strnlen(path, PATH_MAX);
1314 /* trailing null */
1315 name_len++;
1316 req->NameLength = cpu_to_le16(name_len);
1317 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001319
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001320 if (*oplock & REQ_OPLOCK)
1321 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1322 else if (*oplock & REQ_BATCHOPLOCK)
1323 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1324
1325 req->DesiredAccess = cpu_to_le32(desired_access);
1326 req->AllocationSize = 0;
1327
1328 /*
1329 * Set file as system file if special file such as fifo and server
1330 * expecting SFU style and no Unix extensions.
1331 */
1332 if (create_options & CREATE_OPTION_SPECIAL)
1333 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1334 else
1335 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1336
1337 /*
1338 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1339 * sensitive checks for other servers such as Samba.
1340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001342 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jeff Layton67750fb2008-05-09 22:28:02 +00001344 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001345 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001346
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001347 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1348 req->CreateDisposition = cpu_to_le32(disposition);
1349 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1350
Steve French09d1db52005-04-28 22:41:08 -07001351 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001352 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1353 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001356 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001358 req->ByteCount = cpu_to_le16(count);
1359 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1360 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001361 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001363 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001364 cifs_buf_release(req);
1365 if (rc == -EAGAIN)
1366 goto openRetry;
1367 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001369
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001370 /* 1 byte no need to le_to_cpu */
1371 *oplock = rsp->OplockLevel;
1372 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001373 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001374
1375 /* Let caller know file was created so we can set the mode. */
1376 /* Do we care about the CreateAction in any other cases? */
1377 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1378 *oplock |= CIFS_CREATE_ACTION;
1379
1380 if (buf) {
1381 /* copy from CreationTime to Attributes */
1382 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1383 /* the file_info buf is endian converted by caller */
1384 buf->AllocationSize = rsp->AllocationSize;
1385 buf->EndOfFile = rsp->EndOfFile;
1386 buf->NumberOfLinks = cpu_to_le32(1);
1387 buf->DeletePending = 0;
1388 }
1389
1390 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return rc;
1392}
1393
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001394/*
1395 * Discard any remaining data in the current SMB. To do this, we borrow the
1396 * current bigbuf.
1397 */
1398static int
1399cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1400{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001401 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001402 int remaining = rfclen + 4 - server->total_read;
1403 struct cifs_readdata *rdata = mid->callback_data;
1404
1405 while (remaining > 0) {
1406 int length;
1407
1408 length = cifs_read_from_socket(server, server->bigbuf,
1409 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001410 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001411 if (length < 0)
1412 return length;
1413 server->total_read += length;
1414 remaining -= length;
1415 }
1416
1417 dequeue_mid(mid, rdata->result);
1418 return 0;
1419}
1420
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001421int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001422cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1423{
1424 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001425 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001426 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001427 char *buf = server->smallbuf;
1428 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001429
Joe Perchesf96637b2013-05-04 22:12:25 -05001430 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1431 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001432
1433 /*
1434 * read the rest of READ_RSP header (sans Data array), or whatever we
1435 * can if there's not enough data. At this point, we've read down to
1436 * the Mid.
1437 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001438 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001439 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001440
Jeff Layton58195752012-09-19 06:22:34 -07001441 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1442 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001443
Jeff Layton58195752012-09-19 06:22:34 -07001444 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001445 if (length < 0)
1446 return length;
1447 server->total_read += length;
1448
1449 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001450 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001451 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001452 cifs_dbg(FYI, "%s: server returned error %d\n",
1453 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001454 return cifs_readv_discard(server, mid);
1455 }
1456
1457 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001458 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001459 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1460 __func__, server->total_read,
1461 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001462 rdata->result = -EIO;
1463 return cifs_readv_discard(server, mid);
1464 }
1465
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001466 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001467 if (data_offset < server->total_read) {
1468 /*
1469 * win2k8 sometimes sends an offset of 0 when the read
1470 * is beyond the EOF. Treat it as if the data starts just after
1471 * the header.
1472 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001473 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1474 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001475 data_offset = server->total_read;
1476 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1477 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001478 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1479 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001480 rdata->result = -EIO;
1481 return cifs_readv_discard(server, mid);
1482 }
1483
Joe Perchesf96637b2013-05-04 22:12:25 -05001484 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1485 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001486
1487 len = data_offset - server->total_read;
1488 if (len > 0) {
1489 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001490 rdata->iov.iov_base = buf + server->total_read;
1491 rdata->iov.iov_len = len;
1492 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001493 if (length < 0)
1494 return length;
1495 server->total_read += length;
1496 }
1497
1498 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001499 rdata->iov.iov_base = buf;
1500 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001501 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1502 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001503
1504 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001505 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001506 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001507 /* data_len is corrupt -- discard frame */
1508 rdata->result = -EIO;
1509 return cifs_readv_discard(server, mid);
1510 }
1511
Jeff Layton8321fec2012-09-19 06:22:32 -07001512 length = rdata->read_into_pages(server, rdata, data_len);
1513 if (length < 0)
1514 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001515
Jeff Layton8321fec2012-09-19 06:22:32 -07001516 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001517
Joe Perchesf96637b2013-05-04 22:12:25 -05001518 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1519 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001520
1521 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001522 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001523 return cifs_readv_discard(server, mid);
1524
1525 dequeue_mid(mid, false);
1526 return length;
1527}
1528
1529static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001530cifs_readv_callback(struct mid_q_entry *mid)
1531{
1532 struct cifs_readdata *rdata = mid->callback_data;
1533 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1534 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001535 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1536 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001537 .rq_pages = rdata->pages,
1538 .rq_npages = rdata->nr_pages,
1539 .rq_pagesz = rdata->pagesz,
1540 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001541
Joe Perchesf96637b2013-05-04 22:12:25 -05001542 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1543 __func__, mid->mid, mid->mid_state, rdata->result,
1544 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001546 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001547 case MID_RESPONSE_RECEIVED:
1548 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001549 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001550 int rc = 0;
1551
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001552 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001553 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001554 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001555 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1556 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001557 }
1558 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001559 task_io_account_read(rdata->got_bytes);
1560 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001561 break;
1562 case MID_REQUEST_SUBMITTED:
1563 case MID_RETRY_NEEDED:
1564 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001565 if (server->sign && rdata->got_bytes)
1566 /* reset bytes number since we can not check a sign */
1567 rdata->got_bytes = 0;
1568 /* FIXME: should this be counted toward the initiating task? */
1569 task_io_account_read(rdata->got_bytes);
1570 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001571 break;
1572 default:
1573 rdata->result = -EIO;
1574 }
1575
Jeff Laytonda472fc2012-03-23 14:40:53 -04001576 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001577 mutex_lock(&server->srv_mutex);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001578 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001579 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001580 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001581}
1582
1583/* cifs_async_readv - send an async write, and set up mid to handle result */
1584int
1585cifs_async_readv(struct cifs_readdata *rdata)
1586{
1587 int rc;
1588 READ_REQ *smb = NULL;
1589 int wct;
1590 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001591 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001592 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593
Joe Perchesf96637b2013-05-04 22:12:25 -05001594 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1595 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001596
1597 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1598 wct = 12;
1599 else {
1600 wct = 10; /* old style read */
1601 if ((rdata->offset >> 32) > 0) {
1602 /* can not handle this big offset for old */
1603 return -EIO;
1604 }
1605 }
1606
1607 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1608 if (rc)
1609 return rc;
1610
1611 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1612 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1613
1614 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001615 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001616 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1617 if (wct == 12)
1618 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1619 smb->Remaining = 0;
1620 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1621 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1622 if (wct == 12)
1623 smb->ByteCount = 0;
1624 else {
1625 /* old style read */
1626 struct smb_com_readx_req *smbr =
1627 (struct smb_com_readx_req *)smb;
1628 smbr->ByteCount = 0;
1629 }
1630
1631 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001632 rdata->iov.iov_base = smb;
1633 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001634
Jeff Layton6993f742012-05-16 07:13:17 -04001635 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001636 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1637 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001638
1639 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001640 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001641 else
1642 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001643
1644 cifs_small_buf_release(smb);
1645 return rc;
1646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001649CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1650 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 int rc = -EACCES;
1653 READ_REQ *pSMB = NULL;
1654 READ_RSP *pSMBr = NULL;
1655 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001656 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001657 int resp_buf_type = 0;
1658 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001659 __u32 pid = io_parms->pid;
1660 __u16 netfid = io_parms->netfid;
1661 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001662 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001663 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Joe Perchesf96637b2013-05-04 22:12:25 -05001665 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001666 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001667 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001668 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001669 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001670 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001671 /* can not handle this big offset for old */
1672 return -EIO;
1673 }
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001677 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (rc)
1679 return rc;
1680
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001681 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1682 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /* tcon and ses pointer are checked in smb_init */
1685 if (tcon->ses->server == NULL)
1686 return -ECONNABORTED;
1687
Steve Frenchec637e32005-12-12 20:53:18 -08001688 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001690 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001691 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001692 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 pSMB->Remaining = 0;
1695 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1696 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001697 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001698 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1699 else {
1700 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001701 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001702 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001703 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001704 }
Steve Frenchec637e32005-12-12 20:53:18 -08001705
1706 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001707 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001708 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001709 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001710 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001711 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001713 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 } else {
1715 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1716 data_length = data_length << 16;
1717 data_length += le16_to_cpu(pSMBr->DataLength);
1718 *nbytes = data_length;
1719
1720 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001721 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001723 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001724 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 rc = -EIO;
1726 *nbytes = 0;
1727 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001728 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001729 le16_to_cpu(pSMBr->DataOffset);
1730/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001731 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001732 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001733 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001734 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001735 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Steve French4b8f9302006-02-26 16:41:18 +00001739/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001740 if (*buf) {
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01001741 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001742 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001743 /* return buffer to caller to free */
1744 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001745 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001746 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001747 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001748 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001749 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001750
1751 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 since file handle passed in no longer valid */
1753 return rc;
1754}
1755
Steve Frenchec637e32005-12-12 20:53:18 -08001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001758CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001759 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001760 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int rc = -EACCES;
1763 WRITE_REQ *pSMB = NULL;
1764 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001765 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 __u32 bytes_sent;
1767 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001768 __u32 pid = io_parms->pid;
1769 __u16 netfid = io_parms->netfid;
1770 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001771 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001772 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Steve Frencha24e2d72010-04-03 17:20:21 +00001774 *nbytes = 0;
1775
Joe Perchesf96637b2013-05-04 22:12:25 -05001776 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001777 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001778 return -ECONNABORTED;
1779
Steve French790fe572007-07-07 19:25:05 +00001780 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001781 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001782 else {
Steve French1c955182005-08-30 20:58:07 -07001783 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001784 if ((offset >> 32) > 0) {
1785 /* can not handle big offset for old srv */
1786 return -EIO;
1787 }
1788 }
Steve French1c955182005-08-30 20:58:07 -07001789
1790 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 (void **) &pSMBr);
1792 if (rc)
1793 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001794
1795 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1796 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* tcon and ses pointer are checked in smb_init */
1799 if (tcon->ses->server == NULL)
1800 return -ECONNABORTED;
1801
1802 pSMB->AndXCommand = 0xFF; /* none */
1803 pSMB->Fid = netfid;
1804 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001805 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001806 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 pSMB->Reserved = 0xFFFFFFFF;
1809 pSMB->WriteMode = 0;
1810 pSMB->Remaining = 0;
1811
Steve French50c2f752007-07-13 00:33:32 +00001812 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 can send more if LARGE_WRITE_X capability returned by the server and if
1814 our buffer is big enough or if we convert to iovecs on socket writes
1815 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001816 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1818 } else {
1819 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1820 & ~0xFF;
1821 }
1822
1823 if (bytes_sent > count)
1824 bytes_sent = count;
1825 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001826 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001827 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001828 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001829 else if (ubuf) {
1830 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 cifs_buf_release(pSMB);
1832 return -EFAULT;
1833 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001834 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /* No buffer */
1836 cifs_buf_release(pSMB);
1837 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001838 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001839 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001840 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001841 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001842 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1845 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001846 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001847
Steve French790fe572007-07-07 19:25:05 +00001848 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001849 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001850 else { /* old style write has byte count 4 bytes earlier
1851 so 4 bytes pad */
1852 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001853 (struct smb_com_writex_req *)pSMB;
1854 pSMBW->ByteCount = cpu_to_le16(byte_count);
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1858 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001859 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001861 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 } else {
1863 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1864 *nbytes = (*nbytes) << 16;
1865 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301866
1867 /*
1868 * Mask off high 16 bits when bytes written as returned by the
1869 * server is greater than bytes requested by the client. Some
1870 * OS/2 servers are known to set incorrect CountHigh values.
1871 */
1872 if (*nbytes > count)
1873 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 cifs_buf_release(pSMB);
1877
Steve French50c2f752007-07-13 00:33:32 +00001878 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 since file handle passed in no longer valid */
1880
1881 return rc;
1882}
1883
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001884void
1885cifs_writedata_release(struct kref *refcount)
1886{
1887 struct cifs_writedata *wdata = container_of(refcount,
1888 struct cifs_writedata, refcount);
1889
1890 if (wdata->cfile)
1891 cifsFileInfo_put(wdata->cfile);
1892
1893 kfree(wdata);
1894}
1895
1896/*
1897 * Write failed with a retryable error. Resend the write request. It's also
1898 * possible that the page was redirtied so re-clean the page.
1899 */
1900static void
1901cifs_writev_requeue(struct cifs_writedata *wdata)
1902{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001903 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001904 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001905 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001906 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001907
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001908 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1909 i = 0;
1910 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001911 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001912 struct cifs_writedata *wdata2;
1913 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001914
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001915 wsize = server->ops->wp_retry_size(inode);
1916 if (wsize < rest_len) {
1917 nr_pages = wsize / PAGE_CACHE_SIZE;
1918 if (!nr_pages) {
1919 rc = -ENOTSUPP;
1920 break;
1921 }
1922 cur_len = nr_pages * PAGE_CACHE_SIZE;
1923 tailsz = PAGE_CACHE_SIZE;
1924 } else {
1925 nr_pages = DIV_ROUND_UP(rest_len, PAGE_CACHE_SIZE);
1926 cur_len = rest_len;
1927 tailsz = rest_len - (nr_pages - 1) * PAGE_CACHE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001928 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001929
1930 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1931 if (!wdata2) {
1932 rc = -ENOMEM;
1933 break;
1934 }
1935
1936 for (j = 0; j < nr_pages; j++) {
1937 wdata2->pages[j] = wdata->pages[i + j];
1938 lock_page(wdata2->pages[j]);
1939 clear_page_dirty_for_io(wdata2->pages[j]);
1940 }
1941
1942 wdata2->sync_mode = wdata->sync_mode;
1943 wdata2->nr_pages = nr_pages;
1944 wdata2->offset = page_offset(wdata2->pages[0]);
1945 wdata2->pagesz = PAGE_CACHE_SIZE;
1946 wdata2->tailsz = tailsz;
1947 wdata2->bytes = cur_len;
1948
1949 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1950 if (!wdata2->cfile) {
1951 cifs_dbg(VFS, "No writable handles for inode\n");
1952 rc = -EBADF;
1953 break;
1954 }
1955 wdata2->pid = wdata2->cfile->pid;
1956 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1957
1958 for (j = 0; j < nr_pages; j++) {
1959 unlock_page(wdata2->pages[j]);
1960 if (rc != 0 && rc != -EAGAIN) {
1961 SetPageError(wdata2->pages[j]);
1962 end_page_writeback(wdata2->pages[j]);
1963 page_cache_release(wdata2->pages[j]);
1964 }
1965 }
1966
1967 if (rc) {
1968 kref_put(&wdata2->refcount, cifs_writedata_release);
1969 if (rc == -EAGAIN)
1970 continue;
1971 break;
1972 }
1973
1974 rest_len -= cur_len;
1975 i += nr_pages;
1976 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001977
1978 mapping_set_error(inode->i_mapping, rc);
1979 kref_put(&wdata->refcount, cifs_writedata_release);
1980}
1981
Jeff Laytonc2e87642012-03-23 14:40:55 -04001982void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001983cifs_writev_complete(struct work_struct *work)
1984{
1985 struct cifs_writedata *wdata = container_of(work,
1986 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00001987 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001988 int i = 0;
1989
1990 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001991 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001992 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001993 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001994 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1995 wdata->bytes);
1996 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1997 return cifs_writev_requeue(wdata);
1998
1999 for (i = 0; i < wdata->nr_pages; i++) {
2000 struct page *page = wdata->pages[i];
2001 if (wdata->result == -EAGAIN)
2002 __set_page_dirty_nobuffers(page);
2003 else if (wdata->result < 0)
2004 SetPageError(page);
2005 end_page_writeback(page);
2006 page_cache_release(page);
2007 }
2008 if (wdata->result != -EAGAIN)
2009 mapping_set_error(inode->i_mapping, wdata->result);
2010 kref_put(&wdata->refcount, cifs_writedata_release);
2011}
2012
2013struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002014cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002015{
2016 struct cifs_writedata *wdata;
2017
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002018 /* writedata + number of page pointers */
2019 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002020 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002021 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002022 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002023 INIT_LIST_HEAD(&wdata->list);
2024 init_completion(&wdata->done);
2025 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002026 }
2027 return wdata;
2028}
2029
2030/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002031 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002032 * workqueue completion task.
2033 */
2034static void
2035cifs_writev_callback(struct mid_q_entry *mid)
2036{
2037 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002038 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002039 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040 unsigned int written;
2041 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2042
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002043 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002044 case MID_RESPONSE_RECEIVED:
2045 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2046 if (wdata->result != 0)
2047 break;
2048
2049 written = le16_to_cpu(smb->CountHigh);
2050 written <<= 16;
2051 written += le16_to_cpu(smb->Count);
2052 /*
2053 * Mask off high 16 bits when bytes written as returned
2054 * by the server is greater than bytes requested by the
2055 * client. OS/2 servers are known to set incorrect
2056 * CountHigh values.
2057 */
2058 if (written > wdata->bytes)
2059 written &= 0xFFFF;
2060
2061 if (written < wdata->bytes)
2062 wdata->result = -ENOSPC;
2063 else
2064 wdata->bytes = written;
2065 break;
2066 case MID_REQUEST_SUBMITTED:
2067 case MID_RETRY_NEEDED:
2068 wdata->result = -EAGAIN;
2069 break;
2070 default:
2071 wdata->result = -EIO;
2072 break;
2073 }
2074
Jeff Laytonda472fc2012-03-23 14:40:53 -04002075 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002076 mutex_lock(&server->srv_mutex);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002077 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002078 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002079 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002080}
2081
2082/* cifs_async_writev - send an async write, and set up mid to handle result */
2083int
Steve French4a5c80d2014-02-07 20:45:12 -06002084cifs_async_writev(struct cifs_writedata *wdata,
2085 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002086{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002087 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002088 WRITE_REQ *smb = NULL;
2089 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002090 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002091 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002092 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002093
2094 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2095 wct = 14;
2096 } else {
2097 wct = 12;
2098 if (wdata->offset >> 32 > 0) {
2099 /* can not handle big offset for old srv */
2100 return -EIO;
2101 }
2102 }
2103
2104 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2105 if (rc)
2106 goto async_writev_out;
2107
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002108 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2109 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002110
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002111 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002112 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002113 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2114 if (wct == 14)
2115 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2116 smb->Reserved = 0xFFFFFFFF;
2117 smb->WriteMode = 0;
2118 smb->Remaining = 0;
2119
2120 smb->DataOffset =
2121 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2122
2123 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002124 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2125 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002126
Jeff Laytoneddb0792012-09-18 16:20:35 -07002127 rqst.rq_iov = &iov;
2128 rqst.rq_nvec = 1;
2129 rqst.rq_pages = wdata->pages;
2130 rqst.rq_npages = wdata->nr_pages;
2131 rqst.rq_pagesz = wdata->pagesz;
2132 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002133
Joe Perchesf96637b2013-05-04 22:12:25 -05002134 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2135 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002136
2137 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2138 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2139
2140 if (wct == 14) {
2141 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2142 put_bcc(wdata->bytes + 1, &smb->hdr);
2143 } else {
2144 /* wct == 12 */
2145 struct smb_com_writex_req *smbw =
2146 (struct smb_com_writex_req *)smb;
2147 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2148 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002149 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002150 }
2151
2152 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002153 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2154 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002155
2156 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002157 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002158 else
Steve French4a5c80d2014-02-07 20:45:12 -06002159 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002160
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002161async_writev_out:
2162 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002163 return rc;
2164}
2165
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002166int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002167CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002168 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 int rc = -EACCES;
2171 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002172 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002173 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002174 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002175 __u32 pid = io_parms->pid;
2176 __u16 netfid = io_parms->netfid;
2177 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002178 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002179 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002181 *nbytes = 0;
2182
Joe Perchesf96637b2013-05-04 22:12:25 -05002183 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002184
Steve French4c3130e2008-12-09 00:28:16 +00002185 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002186 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002187 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002188 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002189 if ((offset >> 32) > 0) {
2190 /* can not handle big offset for old srv */
2191 return -EIO;
2192 }
2193 }
Steve French8cc64c62005-10-03 13:49:43 -07002194 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (rc)
2196 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002197
2198 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2199 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /* tcon and ses pointer are checked in smb_init */
2202 if (tcon->ses->server == NULL)
2203 return -ECONNABORTED;
2204
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002205 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 pSMB->Fid = netfid;
2207 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002208 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002209 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 pSMB->Reserved = 0xFFFFFFFF;
2211 pSMB->WriteMode = 0;
2212 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002215 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Steve French3e844692005-10-03 13:37:24 -07002217 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2218 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002219 /* header + 1 byte pad */
2220 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002221 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002222 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002223 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002224 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002225 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002226 pSMB->ByteCount = cpu_to_le16(count + 1);
2227 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002228 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002229 (struct smb_com_writex_req *)pSMB;
2230 pSMBW->ByteCount = cpu_to_le16(count + 5);
2231 }
Steve French3e844692005-10-03 13:37:24 -07002232 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002233 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002234 iov[0].iov_len = smb_hdr_len + 4;
2235 else /* wct == 12 pad bigger by four bytes */
2236 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002237
Steve French3e844692005-10-03 13:37:24 -07002238
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002239 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002240 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002242 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002243 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002244 /* presumably this can not happen, but best to be safe */
2245 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002246 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002247 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002248 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2249 *nbytes = (*nbytes) << 16;
2250 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302251
2252 /*
2253 * Mask off high 16 bits when bytes written as returned by the
2254 * server is greater than bytes requested by the client. OS/2
2255 * servers are known to set incorrect CountHigh values.
2256 */
2257 if (*nbytes > count)
2258 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Steve French4b8f9302006-02-26 16:41:18 +00002261/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002262 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Steve French50c2f752007-07-13 00:33:32 +00002264 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 since file handle passed in no longer valid */
2266
2267 return rc;
2268}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002269
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002270int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2271 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002272 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2273{
2274 int rc = 0;
2275 LOCK_REQ *pSMB = NULL;
2276 struct kvec iov[2];
2277 int resp_buf_type;
2278 __u16 count;
2279
Joe Perchesf96637b2013-05-04 22:12:25 -05002280 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2281 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002282
2283 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2284 if (rc)
2285 return rc;
2286
2287 pSMB->Timeout = 0;
2288 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2289 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2290 pSMB->LockType = lock_type;
2291 pSMB->AndXCommand = 0xFF; /* none */
2292 pSMB->Fid = netfid; /* netfid stays le */
2293
2294 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2295 inc_rfc1001_len(pSMB, count);
2296 pSMB->ByteCount = cpu_to_le16(count);
2297
2298 iov[0].iov_base = (char *)pSMB;
2299 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2300 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2301 iov[1].iov_base = (char *)buf;
2302 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2303
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002304 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002305 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2306 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002307 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002308
2309 return rc;
2310}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002313CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002314 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002316 const __u32 numLock, const __u8 lockType,
2317 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 int rc = 0;
2320 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002321/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002323 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 __u16 count;
2325
Joe Perchesf96637b2013-05-04 22:12:25 -05002326 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2327 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002328 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (rc)
2331 return rc;
2332
Steve French790fe572007-07-07 19:25:05 +00002333 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002334 /* no response expected */
2335 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002337 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002338 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2340 } else {
2341 pSMB->Timeout = 0;
2342 }
2343
2344 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2345 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2346 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002347 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pSMB->AndXCommand = 0xFF; /* none */
2349 pSMB->Fid = smb_file_id; /* netfid stays le */
2350
Steve French790fe572007-07-07 19:25:05 +00002351 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002352 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* BB where to store pid high? */
2354 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2355 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2356 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2357 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2358 count = sizeof(LOCKING_ANDX_RANGE);
2359 } else {
2360 /* oplock break */
2361 count = 0;
2362 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002363 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 pSMB->ByteCount = cpu_to_le16(count);
2365
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002366 if (waitFlag) {
2367 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002368 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002369 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002370 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002371 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002372 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002373 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002374 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002375 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002376 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Steve French50c2f752007-07-13 00:33:32 +00002378 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 since file handle passed in no longer valid */
2380 return rc;
2381}
2382
2383int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002384CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002385 const __u16 smb_file_id, const __u32 netpid,
2386 const loff_t start_offset, const __u64 len,
2387 struct file_lock *pLockData, const __u16 lock_type,
2388 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002389{
2390 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2391 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002392 struct cifs_posix_lock *parm_data;
2393 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002394 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002395 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002396 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002397 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002398 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002399
Joe Perchesf96637b2013-05-04 22:12:25 -05002400 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002401
Steve French08547b02006-02-28 22:39:25 +00002402 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2403
2404 if (rc)
2405 return rc;
2406
2407 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2408
Steve French50c2f752007-07-13 00:33:32 +00002409 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002410 pSMB->MaxSetupCount = 0;
2411 pSMB->Reserved = 0;
2412 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002413 pSMB->Reserved2 = 0;
2414 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2415 offset = param_offset + params;
2416
Steve French08547b02006-02-28 22:39:25 +00002417 count = sizeof(struct cifs_posix_lock);
2418 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002419 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002420 pSMB->SetupCount = 1;
2421 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002422 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002423 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2424 else
2425 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2426 byte_count = 3 /* pad */ + params + count;
2427 pSMB->DataCount = cpu_to_le16(count);
2428 pSMB->ParameterCount = cpu_to_le16(params);
2429 pSMB->TotalDataCount = pSMB->DataCount;
2430 pSMB->TotalParameterCount = pSMB->ParameterCount;
2431 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002432 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002433 (((char *) &pSMB->hdr.Protocol) + offset);
2434
2435 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002436 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002437 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002438 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002439 pSMB->Timeout = cpu_to_le32(-1);
2440 } else
2441 pSMB->Timeout = 0;
2442
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002443 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002444 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002445 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002446
2447 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002448 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002449 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2450 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002451 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002452 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002453 if (waitFlag) {
2454 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2455 (struct smb_hdr *) pSMBr, &bytes_returned);
2456 } else {
Steve French133672e2007-11-13 22:41:37 +00002457 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002458 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002459 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2460 &resp_buf_type, timeout);
2461 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2462 not try to free it twice below on exit */
2463 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002464 }
2465
Steve French08547b02006-02-28 22:39:25 +00002466 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002467 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002468 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002469 /* lock structure can be returned on get */
2470 __u16 data_offset;
2471 __u16 data_count;
2472 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002473
Jeff Layton820a8032011-05-04 08:05:26 -04002474 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002475 rc = -EIO; /* bad smb */
2476 goto plk_err_exit;
2477 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002478 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2479 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002480 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002481 rc = -EIO;
2482 goto plk_err_exit;
2483 }
2484 parm_data = (struct cifs_posix_lock *)
2485 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002486 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002487 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002488 else {
2489 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002490 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002491 pLockData->fl_type = F_RDLCK;
2492 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002493 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002494 pLockData->fl_type = F_WRLCK;
2495
Steve French5443d132011-03-13 05:08:25 +00002496 pLockData->fl_start = le64_to_cpu(parm_data->start);
2497 pLockData->fl_end = pLockData->fl_start +
2498 le64_to_cpu(parm_data->length) - 1;
2499 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002500 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002501 }
Steve French50c2f752007-07-13 00:33:32 +00002502
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002503plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002504 if (pSMB)
2505 cifs_small_buf_release(pSMB);
2506
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002507 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French133672e2007-11-13 22:41:37 +00002508
Steve French08547b02006-02-28 22:39:25 +00002509 /* Note: On -EAGAIN error only caller can retry on handle based calls
2510 since file handle passed in no longer valid */
2511
2512 return rc;
2513}
2514
2515
2516int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002517CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 int rc = 0;
2520 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002521 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523/* do not retry on dead session on close */
2524 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002525 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return 0;
2527 if (rc)
2528 return rc;
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002531 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002533 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002534 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002536 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002538 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540 }
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002543 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 rc = 0;
2545
2546 return rc;
2547}
2548
2549int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002550CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002551{
2552 int rc = 0;
2553 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002554 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002555
2556 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2557 if (rc)
2558 return rc;
2559
2560 pSMB->FileID = (__u16) smb_file_id;
2561 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002562 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002563 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002564 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002565 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002566
2567 return rc;
2568}
2569
2570int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002571CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002572 const char *from_name, const char *to_name,
2573 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
2575 int rc = 0;
2576 RENAME_REQ *pSMB = NULL;
2577 RENAME_RSP *pSMBr = NULL;
2578 int bytes_returned;
2579 int name_len, name_len2;
2580 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002581 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Joe Perchesf96637b2013-05-04 22:12:25 -05002583 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584renameRetry:
2585 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2586 (void **) &pSMBr);
2587 if (rc)
2588 return rc;
2589
2590 pSMB->BufferFormat = 0x04;
2591 pSMB->SearchAttributes =
2592 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2593 ATTR_DIRECTORY);
2594
2595 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002596 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2597 from_name, PATH_MAX,
2598 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 name_len++; /* trailing null */
2600 name_len *= 2;
2601 pSMB->OldFileName[name_len] = 0x04; /* pad */
2602 /* protocol requires ASCII signature byte on Unicode string */
2603 pSMB->OldFileName[name_len + 1] = 0x00;
2604 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002605 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002606 to_name, PATH_MAX, cifs_sb->local_nls,
2607 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2609 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002610 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002611 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002613 strncpy(pSMB->OldFileName, from_name, name_len);
2614 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 name_len2++; /* trailing null */
2616 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002617 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 name_len2++; /* trailing null */
2619 name_len2++; /* signature byte */
2620 }
2621
2622 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002623 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 pSMB->ByteCount = cpu_to_le16(count);
2625
2626 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2627 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002628 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002629 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002630 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 cifs_buf_release(pSMB);
2633
2634 if (rc == -EAGAIN)
2635 goto renameRetry;
2636
2637 return rc;
2638}
2639
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002640int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002641 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002642 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
2644 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2645 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002646 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 char *data_offset;
2648 char dummy_string[30];
2649 int rc = 0;
2650 int bytes_returned = 0;
2651 int len_of_str;
2652 __u16 params, param_offset, offset, count, byte_count;
2653
Joe Perchesf96637b2013-05-04 22:12:25 -05002654 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2656 (void **) &pSMBr);
2657 if (rc)
2658 return rc;
2659
2660 params = 6;
2661 pSMB->MaxSetupCount = 0;
2662 pSMB->Reserved = 0;
2663 pSMB->Flags = 0;
2664 pSMB->Timeout = 0;
2665 pSMB->Reserved2 = 0;
2666 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2667 offset = param_offset + params;
2668
2669 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2670 rename_info = (struct set_file_rename *) data_offset;
2671 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002672 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 pSMB->SetupCount = 1;
2674 pSMB->Reserved3 = 0;
2675 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2676 byte_count = 3 /* pad */ + params;
2677 pSMB->ParameterCount = cpu_to_le16(params);
2678 pSMB->TotalParameterCount = pSMB->ParameterCount;
2679 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2680 pSMB->DataOffset = cpu_to_le16(offset);
2681 /* construct random name ".cifs_tmp<inodenum><mid>" */
2682 rename_info->overwrite = cpu_to_le32(1);
2683 rename_info->root_fid = 0;
2684 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002685 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002686 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002687 len_of_str =
2688 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002689 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002691 len_of_str =
2692 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002693 target_name, PATH_MAX, nls_codepage,
2694 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002697 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 byte_count += count;
2699 pSMB->DataCount = cpu_to_le16(count);
2700 pSMB->TotalDataCount = pSMB->DataCount;
2701 pSMB->Fid = netfid;
2702 pSMB->InformationLevel =
2703 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2704 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002705 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 pSMB->ByteCount = cpu_to_le16(byte_count);
2707 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002708 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002709 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002710 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002711 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2712 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 cifs_buf_release(pSMB);
2715
2716 /* Note: On -EAGAIN error only caller can retry on handle based calls
2717 since file handle passed in no longer valid */
2718
2719 return rc;
2720}
2721
2722int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002723CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2724 const char *fromName, const __u16 target_tid, const char *toName,
2725 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 int rc = 0;
2728 COPY_REQ *pSMB = NULL;
2729 COPY_RSP *pSMBr = NULL;
2730 int bytes_returned;
2731 int name_len, name_len2;
2732 __u16 count;
2733
Joe Perchesf96637b2013-05-04 22:12:25 -05002734 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735copyRetry:
2736 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2737 (void **) &pSMBr);
2738 if (rc)
2739 return rc;
2740
2741 pSMB->BufferFormat = 0x04;
2742 pSMB->Tid2 = target_tid;
2743
2744 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2745
2746 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002747 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2748 fromName, PATH_MAX, nls_codepage,
2749 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 name_len++; /* trailing null */
2751 name_len *= 2;
2752 pSMB->OldFileName[name_len] = 0x04; /* pad */
2753 /* protocol requires ASCII signature byte on Unicode string */
2754 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002755 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002756 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2757 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2759 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002760 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 name_len = strnlen(fromName, PATH_MAX);
2762 name_len++; /* trailing null */
2763 strncpy(pSMB->OldFileName, fromName, name_len);
2764 name_len2 = strnlen(toName, PATH_MAX);
2765 name_len2++; /* trailing null */
2766 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2767 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2768 name_len2++; /* trailing null */
2769 name_len2++; /* signature byte */
2770 }
2771
2772 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002773 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 pSMB->ByteCount = cpu_to_le16(count);
2775
2776 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2777 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2778 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002779 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2780 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Steve French0d817bc2008-05-22 02:02:03 +00002782 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 if (rc == -EAGAIN)
2785 goto copyRetry;
2786
2787 return rc;
2788}
2789
2790int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002791CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002793 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 TRANSACTION2_SPI_REQ *pSMB = NULL;
2796 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2797 char *data_offset;
2798 int name_len;
2799 int name_len_target;
2800 int rc = 0;
2801 int bytes_returned = 0;
2802 __u16 params, param_offset, offset, byte_count;
2803
Joe Perchesf96637b2013-05-04 22:12:25 -05002804 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805createSymLinkRetry:
2806 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2807 (void **) &pSMBr);
2808 if (rc)
2809 return rc;
2810
2811 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2812 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002813 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2814 /* find define for this maxpathcomponent */
2815 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 name_len++; /* trailing null */
2817 name_len *= 2;
2818
Steve French50c2f752007-07-13 00:33:32 +00002819 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 name_len = strnlen(fromName, PATH_MAX);
2821 name_len++; /* trailing null */
2822 strncpy(pSMB->FileName, fromName, name_len);
2823 }
2824 params = 6 + name_len;
2825 pSMB->MaxSetupCount = 0;
2826 pSMB->Reserved = 0;
2827 pSMB->Flags = 0;
2828 pSMB->Timeout = 0;
2829 pSMB->Reserved2 = 0;
2830 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002831 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 offset = param_offset + params;
2833
2834 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2835 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2836 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002837 cifsConvertToUTF16((__le16 *) data_offset, toName,
2838 /* find define for this maxpathcomponent */
2839 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 name_len_target++; /* trailing null */
2841 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002842 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 name_len_target = strnlen(toName, PATH_MAX);
2844 name_len_target++; /* trailing null */
2845 strncpy(data_offset, toName, name_len_target);
2846 }
2847
2848 pSMB->MaxParameterCount = cpu_to_le16(2);
2849 /* BB find exact max on data count below from sess */
2850 pSMB->MaxDataCount = cpu_to_le16(1000);
2851 pSMB->SetupCount = 1;
2852 pSMB->Reserved3 = 0;
2853 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2854 byte_count = 3 /* pad */ + params + name_len_target;
2855 pSMB->DataCount = cpu_to_le16(name_len_target);
2856 pSMB->ParameterCount = cpu_to_le16(params);
2857 pSMB->TotalDataCount = pSMB->DataCount;
2858 pSMB->TotalParameterCount = pSMB->ParameterCount;
2859 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2860 pSMB->DataOffset = cpu_to_le16(offset);
2861 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2862 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002863 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 pSMB->ByteCount = cpu_to_le16(byte_count);
2865 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2866 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002867 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002868 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002869 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2870 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Steve French0d817bc2008-05-22 02:02:03 +00002872 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 if (rc == -EAGAIN)
2875 goto createSymLinkRetry;
2876
2877 return rc;
2878}
2879
2880int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002881CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002883 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 TRANSACTION2_SPI_REQ *pSMB = NULL;
2886 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2887 char *data_offset;
2888 int name_len;
2889 int name_len_target;
2890 int rc = 0;
2891 int bytes_returned = 0;
2892 __u16 params, param_offset, offset, byte_count;
2893
Joe Perchesf96637b2013-05-04 22:12:25 -05002894 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895createHardLinkRetry:
2896 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2897 (void **) &pSMBr);
2898 if (rc)
2899 return rc;
2900
2901 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002902 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2903 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 name_len++; /* trailing null */
2905 name_len *= 2;
2906
Steve French50c2f752007-07-13 00:33:32 +00002907 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 name_len = strnlen(toName, PATH_MAX);
2909 name_len++; /* trailing null */
2910 strncpy(pSMB->FileName, toName, name_len);
2911 }
2912 params = 6 + name_len;
2913 pSMB->MaxSetupCount = 0;
2914 pSMB->Reserved = 0;
2915 pSMB->Flags = 0;
2916 pSMB->Timeout = 0;
2917 pSMB->Reserved2 = 0;
2918 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002919 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 offset = param_offset + params;
2921
2922 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2923 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2924 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002925 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2926 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 name_len_target++; /* trailing null */
2928 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002929 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 name_len_target = strnlen(fromName, PATH_MAX);
2931 name_len_target++; /* trailing null */
2932 strncpy(data_offset, fromName, name_len_target);
2933 }
2934
2935 pSMB->MaxParameterCount = cpu_to_le16(2);
2936 /* BB find exact max on data count below from sess*/
2937 pSMB->MaxDataCount = cpu_to_le16(1000);
2938 pSMB->SetupCount = 1;
2939 pSMB->Reserved3 = 0;
2940 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2941 byte_count = 3 /* pad */ + params + name_len_target;
2942 pSMB->ParameterCount = cpu_to_le16(params);
2943 pSMB->TotalParameterCount = pSMB->ParameterCount;
2944 pSMB->DataCount = cpu_to_le16(name_len_target);
2945 pSMB->TotalDataCount = pSMB->DataCount;
2946 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2947 pSMB->DataOffset = cpu_to_le16(offset);
2948 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2949 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002950 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 pSMB->ByteCount = cpu_to_le16(byte_count);
2952 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2953 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002954 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002955 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002956 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2957 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 cifs_buf_release(pSMB);
2960 if (rc == -EAGAIN)
2961 goto createHardLinkRetry;
2962
2963 return rc;
2964}
2965
2966int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002967CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002968 const char *from_name, const char *to_name,
2969 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 int rc = 0;
2972 NT_RENAME_REQ *pSMB = NULL;
2973 RENAME_RSP *pSMBr = NULL;
2974 int bytes_returned;
2975 int name_len, name_len2;
2976 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002977 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Joe Perchesf96637b2013-05-04 22:12:25 -05002979 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980winCreateHardLinkRetry:
2981
2982 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2983 (void **) &pSMBr);
2984 if (rc)
2985 return rc;
2986
2987 pSMB->SearchAttributes =
2988 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2989 ATTR_DIRECTORY);
2990 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2991 pSMB->ClusterCount = 0;
2992
2993 pSMB->BufferFormat = 0x04;
2994
2995 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2996 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002997 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2998 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 name_len++; /* trailing null */
3000 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003001
3002 /* protocol specifies ASCII buffer format (0x04) for unicode */
3003 pSMB->OldFileName[name_len] = 0x04;
3004 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003006 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003007 to_name, PATH_MAX, cifs_sb->local_nls,
3008 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3010 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003011 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003012 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003014 strncpy(pSMB->OldFileName, from_name, name_len);
3015 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 name_len2++; /* trailing null */
3017 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003018 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 name_len2++; /* trailing null */
3020 name_len2++; /* signature byte */
3021 }
3022
3023 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003024 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 pSMB->ByteCount = cpu_to_le16(count);
3026
3027 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3028 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003029 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003030 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003031 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 cifs_buf_release(pSMB);
3034 if (rc == -EAGAIN)
3035 goto winCreateHardLinkRetry;
3036
3037 return rc;
3038}
3039
3040int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003041CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003042 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003043 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045/* SMB_QUERY_FILE_UNIX_LINK */
3046 TRANSACTION2_QPI_REQ *pSMB = NULL;
3047 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3048 int rc = 0;
3049 int bytes_returned;
3050 int name_len;
3051 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003052 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Joe Perchesf96637b2013-05-04 22:12:25 -05003054 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056querySymLinkRetry:
3057 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3058 (void **) &pSMBr);
3059 if (rc)
3060 return rc;
3061
3062 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3063 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003064 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3065 searchName, PATH_MAX, nls_codepage,
3066 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 name_len++; /* trailing null */
3068 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003069 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 name_len = strnlen(searchName, PATH_MAX);
3071 name_len++; /* trailing null */
3072 strncpy(pSMB->FileName, searchName, name_len);
3073 }
3074
3075 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3076 pSMB->TotalDataCount = 0;
3077 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003078 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 pSMB->MaxSetupCount = 0;
3080 pSMB->Reserved = 0;
3081 pSMB->Flags = 0;
3082 pSMB->Timeout = 0;
3083 pSMB->Reserved2 = 0;
3084 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003085 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 pSMB->DataCount = 0;
3087 pSMB->DataOffset = 0;
3088 pSMB->SetupCount = 1;
3089 pSMB->Reserved3 = 0;
3090 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3091 byte_count = params + 1 /* pad */ ;
3092 pSMB->TotalParameterCount = cpu_to_le16(params);
3093 pSMB->ParameterCount = pSMB->TotalParameterCount;
3094 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3095 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003096 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 pSMB->ByteCount = cpu_to_le16(byte_count);
3098
3099 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3100 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3101 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003102 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 } else {
3104 /* decode response */
3105
3106 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003108 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003109 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003111 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003112 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Jeff Layton460b9692009-04-30 07:17:56 -04003114 data_start = ((char *) &pSMBr->hdr.Protocol) +
3115 le16_to_cpu(pSMBr->t2.DataOffset);
3116
Steve French0e0d2cf2009-05-01 05:27:32 +00003117 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3118 is_unicode = true;
3119 else
3120 is_unicode = false;
3121
Steve French737b7582005-04-28 22:41:06 -07003122 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003123 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3124 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003125 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003126 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 }
3129 cifs_buf_release(pSMB);
3130 if (rc == -EAGAIN)
3131 goto querySymLinkRetry;
3132 return rc;
3133}
3134
Steve Frenchc52a9552011-02-24 06:16:22 +00003135/*
3136 * Recent Windows versions now create symlinks more frequently
3137 * and they use the "reparse point" mechanism below. We can of course
3138 * do symlinks nicely to Samba and other servers which support the
3139 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3140 * "MF" symlinks optionally, but for recent Windows we really need to
3141 * reenable the code below and fix the cifs_symlink callers to handle this.
3142 * In the interim this code has been moved to its own config option so
3143 * it is not compiled in by default until callers fixed up and more tested.
3144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003146CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3147 __u16 fid, char **symlinkinfo,
3148 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 int rc = 0;
3151 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003152 struct smb_com_transaction_ioctl_req *pSMB;
3153 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003154 bool is_unicode;
3155 unsigned int sub_len;
3156 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003157 struct reparse_symlink_data *reparse_buf;
3158 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003159 __u32 data_offset, data_count;
3160 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003162 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3164 (void **) &pSMBr);
3165 if (rc)
3166 return rc;
3167
3168 pSMB->TotalParameterCount = 0 ;
3169 pSMB->TotalDataCount = 0;
3170 pSMB->MaxParameterCount = cpu_to_le32(2);
3171 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003172 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 pSMB->MaxSetupCount = 4;
3174 pSMB->Reserved = 0;
3175 pSMB->ParameterOffset = 0;
3176 pSMB->DataCount = 0;
3177 pSMB->DataOffset = 0;
3178 pSMB->SetupCount = 4;
3179 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3180 pSMB->ParameterCount = pSMB->TotalParameterCount;
3181 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3182 pSMB->IsFsctl = 1; /* FSCTL */
3183 pSMB->IsRootFlag = 0;
3184 pSMB->Fid = fid; /* file handle always le */
3185 pSMB->ByteCount = 0;
3186
3187 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3188 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3189 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003190 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003191 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 }
Steve French989c7e52009-05-02 05:32:20 +00003193
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003194 data_offset = le32_to_cpu(pSMBr->DataOffset);
3195 data_count = le32_to_cpu(pSMBr->DataCount);
3196 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3197 /* BB also check enough total bytes returned */
3198 rc = -EIO; /* bad smb */
3199 goto qreparse_out;
3200 }
3201 if (!data_count || (data_count > 2048)) {
3202 rc = -EIO;
3203 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3204 goto qreparse_out;
3205 }
3206 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003207 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003208 ((char *)&pSMBr->hdr.Protocol + data_offset);
3209 if ((char *)reparse_buf >= end_of_smb) {
3210 rc = -EIO;
3211 goto qreparse_out;
3212 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003213 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3214 cifs_dbg(FYI, "NFS style reparse tag\n");
3215 posix_buf = (struct reparse_posix_data *)reparse_buf;
3216
3217 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3218 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3219 le64_to_cpu(posix_buf->InodeType));
3220 rc = -EOPNOTSUPP;
3221 goto qreparse_out;
3222 }
3223 is_unicode = true;
3224 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3225 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3226 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3227 rc = -EIO;
3228 goto qreparse_out;
3229 }
3230 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3231 sub_len, is_unicode, nls_codepage);
3232 goto qreparse_out;
3233 } else if (reparse_buf->ReparseTag !=
3234 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3235 rc = -EOPNOTSUPP;
3236 goto qreparse_out;
3237 }
3238
3239 /* Reparse tag is NTFS symlink */
3240 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3241 reparse_buf->PathBuffer;
3242 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3243 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003244 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3245 rc = -EIO;
3246 goto qreparse_out;
3247 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003248 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3249 is_unicode = true;
3250 else
3251 is_unicode = false;
3252
3253 /* BB FIXME investigate remapping reserved chars here */
3254 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3255 nls_codepage);
3256 if (!*symlinkinfo)
3257 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003259 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003261 /*
3262 * Note: On -EAGAIN error only caller can retry on handle based calls
3263 * since file handle passed in no longer valid.
3264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return rc;
3266}
3267
Steve Frenchc7f508a2013-10-14 15:27:32 -05003268int
3269CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3270 __u16 fid)
3271{
3272 int rc = 0;
3273 int bytes_returned;
3274 struct smb_com_transaction_compr_ioctl_req *pSMB;
3275 struct smb_com_transaction_ioctl_rsp *pSMBr;
3276
3277 cifs_dbg(FYI, "Set compression for %u\n", fid);
3278 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3279 (void **) &pSMBr);
3280 if (rc)
3281 return rc;
3282
3283 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3284
3285 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003286 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003287 pSMB->MaxParameterCount = 0;
3288 pSMB->MaxDataCount = 0;
3289 pSMB->MaxSetupCount = 4;
3290 pSMB->Reserved = 0;
3291 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003292 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003293 pSMB->DataOffset =
3294 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3295 compression_state) - 4); /* 84 */
3296 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003297 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003298 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003299 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003300 pSMB->IsFsctl = 1; /* FSCTL */
3301 pSMB->IsRootFlag = 0;
3302 pSMB->Fid = fid; /* file handle always le */
3303 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003304 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003305 inc_rfc1001_len(pSMB, 5);
3306
3307 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3308 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3309 if (rc)
3310 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3311
3312 cifs_buf_release(pSMB);
3313
3314 /*
3315 * Note: On -EAGAIN error only caller can retry on handle based calls
3316 * since file handle passed in no longer valid.
3317 */
3318 return rc;
3319}
3320
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322#ifdef CONFIG_CIFS_POSIX
3323
3324/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003325static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3326 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
3328 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003329 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3330 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3331 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003332/*
3333 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3334 ace->e_perm, ace->e_tag, ace->e_id);
3335*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 return;
3338}
3339
3340/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003341static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3342 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 int size = 0;
3345 int i;
3346 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003347 struct cifs_posix_ace *pACE;
3348 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3349 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
3351 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3352 return -EOPNOTSUPP;
3353
Steve French790fe572007-07-07 19:25:05 +00003354 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 count = le16_to_cpu(cifs_acl->access_entry_count);
3356 pACE = &cifs_acl->ace_array[0];
3357 size = sizeof(struct cifs_posix_acl);
3358 size += sizeof(struct cifs_posix_ace) * count;
3359 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003360 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003361 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3362 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return -EINVAL;
3364 }
Steve French790fe572007-07-07 19:25:05 +00003365 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 count = le16_to_cpu(cifs_acl->access_entry_count);
3367 size = sizeof(struct cifs_posix_acl);
3368 size += sizeof(struct cifs_posix_ace) * count;
3369/* skip past access ACEs to get to default ACEs */
3370 pACE = &cifs_acl->ace_array[count];
3371 count = le16_to_cpu(cifs_acl->default_entry_count);
3372 size += sizeof(struct cifs_posix_ace) * count;
3373 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003374 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return -EINVAL;
3376 } else {
3377 /* illegal type */
3378 return -EINVAL;
3379 }
3380
3381 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003382 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003383 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003384 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return -ERANGE;
3386 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003387 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003388 for (i = 0; i < count ; i++) {
3389 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3390 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 }
3392 }
3393 return size;
3394}
3395
Steve French50c2f752007-07-13 00:33:32 +00003396static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3397 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 __u16 rc = 0; /* 0 = ACL converted ok */
3400
Steve Frenchff7feac2005-11-15 16:45:16 -08003401 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3402 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003404 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 /* Probably no need to le convert -1 on any arch but can not hurt */
3406 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003407 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003408 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003409/*
3410 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3411 ace->e_perm, ace->e_tag, ace->e_id);
3412*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return rc;
3414}
3415
3416/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003417static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3418 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
3420 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003421 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3422 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 int count;
3424 int i;
3425
Steve French790fe572007-07-07 19:25:05 +00003426 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return 0;
3428
3429 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003430 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3431 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003432 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003433 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3434 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return 0;
3436 }
3437 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003438 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003439 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003440 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003441 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003442 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003443 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003444 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003445 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 return 0;
3447 }
Steve French50c2f752007-07-13 00:33:32 +00003448 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3450 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003451 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 /* ACE not converted */
3453 break;
3454 }
3455 }
Steve French790fe572007-07-07 19:25:05 +00003456 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3458 rc += sizeof(struct cifs_posix_acl);
3459 /* BB add check to make sure ACL does not overflow SMB */
3460 }
3461 return rc;
3462}
3463
3464int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003465CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003466 const unsigned char *searchName,
3467 char *acl_inf, const int buflen, const int acl_type,
3468 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
3470/* SMB_QUERY_POSIX_ACL */
3471 TRANSACTION2_QPI_REQ *pSMB = NULL;
3472 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3473 int rc = 0;
3474 int bytes_returned;
3475 int name_len;
3476 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003477
Joe Perchesf96637b2013-05-04 22:12:25 -05003478 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480queryAclRetry:
3481 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3482 (void **) &pSMBr);
3483 if (rc)
3484 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3487 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003488 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3489 searchName, PATH_MAX, nls_codepage,
3490 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 name_len++; /* trailing null */
3492 name_len *= 2;
3493 pSMB->FileName[name_len] = 0;
3494 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003495 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 name_len = strnlen(searchName, PATH_MAX);
3497 name_len++; /* trailing null */
3498 strncpy(pSMB->FileName, searchName, name_len);
3499 }
3500
3501 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3502 pSMB->TotalDataCount = 0;
3503 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003504 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 pSMB->MaxDataCount = cpu_to_le16(4000);
3506 pSMB->MaxSetupCount = 0;
3507 pSMB->Reserved = 0;
3508 pSMB->Flags = 0;
3509 pSMB->Timeout = 0;
3510 pSMB->Reserved2 = 0;
3511 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003512 offsetof(struct smb_com_transaction2_qpi_req,
3513 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 pSMB->DataCount = 0;
3515 pSMB->DataOffset = 0;
3516 pSMB->SetupCount = 1;
3517 pSMB->Reserved3 = 0;
3518 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3519 byte_count = params + 1 /* pad */ ;
3520 pSMB->TotalParameterCount = cpu_to_le16(params);
3521 pSMB->ParameterCount = pSMB->TotalParameterCount;
3522 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3523 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003524 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 pSMB->ByteCount = cpu_to_le16(byte_count);
3526
3527 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3528 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003529 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003531 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 } else {
3533 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003537 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 rc = -EIO; /* bad smb */
3539 else {
3540 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3541 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3542 rc = cifs_copy_posix_acl(acl_inf,
3543 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003544 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
3546 }
3547 cifs_buf_release(pSMB);
3548 if (rc == -EAGAIN)
3549 goto queryAclRetry;
3550 return rc;
3551}
3552
3553int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003554CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003555 const unsigned char *fileName,
3556 const char *local_acl, const int buflen,
3557 const int acl_type,
3558 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 struct smb_com_transaction2_spi_req *pSMB = NULL;
3561 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3562 char *parm_data;
3563 int name_len;
3564 int rc = 0;
3565 int bytes_returned = 0;
3566 __u16 params, byte_count, data_count, param_offset, offset;
3567
Joe Perchesf96637b2013-05-04 22:12:25 -05003568 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569setAclRetry:
3570 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003571 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 if (rc)
3573 return rc;
3574 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3575 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003576 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3577 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 name_len++; /* trailing null */
3579 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003580 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 name_len = strnlen(fileName, PATH_MAX);
3582 name_len++; /* trailing null */
3583 strncpy(pSMB->FileName, fileName, name_len);
3584 }
3585 params = 6 + name_len;
3586 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003587 /* BB find max SMB size from sess */
3588 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 pSMB->MaxSetupCount = 0;
3590 pSMB->Reserved = 0;
3591 pSMB->Flags = 0;
3592 pSMB->Timeout = 0;
3593 pSMB->Reserved2 = 0;
3594 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003595 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 offset = param_offset + params;
3597 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3598 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3599
3600 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003601 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Steve French790fe572007-07-07 19:25:05 +00003603 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 rc = -EOPNOTSUPP;
3605 goto setACLerrorExit;
3606 }
3607 pSMB->DataOffset = cpu_to_le16(offset);
3608 pSMB->SetupCount = 1;
3609 pSMB->Reserved3 = 0;
3610 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3611 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3612 byte_count = 3 /* pad */ + params + data_count;
3613 pSMB->DataCount = cpu_to_le16(data_count);
3614 pSMB->TotalDataCount = pSMB->DataCount;
3615 pSMB->ParameterCount = cpu_to_le16(params);
3616 pSMB->TotalParameterCount = pSMB->ParameterCount;
3617 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003618 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 pSMB->ByteCount = cpu_to_le16(byte_count);
3620 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003621 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003622 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003623 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
3625setACLerrorExit:
3626 cifs_buf_release(pSMB);
3627 if (rc == -EAGAIN)
3628 goto setAclRetry;
3629 return rc;
3630}
3631
Steve Frenchf654bac2005-04-28 22:41:04 -07003632/* BB fix tabs in this function FIXME BB */
3633int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003634CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003635 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003636{
Steve French50c2f752007-07-13 00:33:32 +00003637 int rc = 0;
3638 struct smb_t2_qfi_req *pSMB = NULL;
3639 struct smb_t2_qfi_rsp *pSMBr = NULL;
3640 int bytes_returned;
3641 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003642
Joe Perchesf96637b2013-05-04 22:12:25 -05003643 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003644 if (tcon == NULL)
3645 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003646
3647GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003648 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3649 (void **) &pSMBr);
3650 if (rc)
3651 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003652
Steve Frenchad7a2922008-02-07 23:25:02 +00003653 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003654 pSMB->t2.TotalDataCount = 0;
3655 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3656 /* BB find exact max data count below from sess structure BB */
3657 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3658 pSMB->t2.MaxSetupCount = 0;
3659 pSMB->t2.Reserved = 0;
3660 pSMB->t2.Flags = 0;
3661 pSMB->t2.Timeout = 0;
3662 pSMB->t2.Reserved2 = 0;
3663 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3664 Fid) - 4);
3665 pSMB->t2.DataCount = 0;
3666 pSMB->t2.DataOffset = 0;
3667 pSMB->t2.SetupCount = 1;
3668 pSMB->t2.Reserved3 = 0;
3669 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3670 byte_count = params + 1 /* pad */ ;
3671 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3672 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3673 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3674 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003675 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003676 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003677 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003678
Steve French790fe572007-07-07 19:25:05 +00003679 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3680 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3681 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003682 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003683 } else {
3684 /* decode response */
3685 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003686 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003687 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003688 /* If rc should we check for EOPNOSUPP and
3689 disable the srvino flag? or in caller? */
3690 rc = -EIO; /* bad smb */
3691 else {
3692 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3693 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3694 struct file_chattr_info *pfinfo;
3695 /* BB Do we need a cast or hash here ? */
3696 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003697 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003698 rc = -EIO;
3699 goto GetExtAttrOut;
3700 }
3701 pfinfo = (struct file_chattr_info *)
3702 (data_offset + (char *) &pSMBr->hdr.Protocol);
3703 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003704 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003705 }
3706 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003707GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003708 cifs_buf_release(pSMB);
3709 if (rc == -EAGAIN)
3710 goto GetExtAttrRetry;
3711 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003712}
3713
Steve Frenchf654bac2005-04-28 22:41:04 -07003714#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Jeff Layton79df1ba2010-12-06 12:52:08 -05003716#ifdef CONFIG_CIFS_ACL
3717/*
3718 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3719 * all NT TRANSACTS that we init here have total parm and data under about 400
3720 * bytes (to fit in small cifs buffer size), which is the case so far, it
3721 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3722 * returned setup area) and MaxParameterCount (returned parms size) must be set
3723 * by caller
3724 */
3725static int
3726smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003727 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003728 void **ret_buf)
3729{
3730 int rc;
3731 __u32 temp_offset;
3732 struct smb_com_ntransact_req *pSMB;
3733
3734 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3735 (void **)&pSMB);
3736 if (rc)
3737 return rc;
3738 *ret_buf = (void *)pSMB;
3739 pSMB->Reserved = 0;
3740 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3741 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003742 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003743 pSMB->ParameterCount = pSMB->TotalParameterCount;
3744 pSMB->DataCount = pSMB->TotalDataCount;
3745 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3746 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3747 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3748 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3749 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3750 pSMB->SubCommand = cpu_to_le16(sub_command);
3751 return 0;
3752}
3753
3754static int
3755validate_ntransact(char *buf, char **ppparm, char **ppdata,
3756 __u32 *pparmlen, __u32 *pdatalen)
3757{
3758 char *end_of_smb;
3759 __u32 data_count, data_offset, parm_count, parm_offset;
3760 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003761 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003762
3763 *pdatalen = 0;
3764 *pparmlen = 0;
3765
3766 if (buf == NULL)
3767 return -EINVAL;
3768
3769 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3770
Jeff Layton820a8032011-05-04 08:05:26 -04003771 bcc = get_bcc(&pSMBr->hdr);
3772 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003773 (char *)&pSMBr->ByteCount;
3774
3775 data_offset = le32_to_cpu(pSMBr->DataOffset);
3776 data_count = le32_to_cpu(pSMBr->DataCount);
3777 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3778 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3779
3780 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3781 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3782
3783 /* should we also check that parm and data areas do not overlap? */
3784 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003785 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003786 return -EINVAL;
3787 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003788 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003789 return -EINVAL;
3790 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003791 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003792 return -EINVAL;
3793 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003794 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3795 *ppdata, data_count, (data_count + *ppdata),
3796 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003797 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003798 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003799 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003800 return -EINVAL;
3801 }
3802 *pdatalen = data_count;
3803 *pparmlen = parm_count;
3804 return 0;
3805}
3806
Steve French0a4b92c2006-01-12 15:44:21 -08003807/* Get Security Descriptor (by handle) from remote server for a file or dir */
3808int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003809CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003810 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003811{
3812 int rc = 0;
3813 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003814 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003815 struct kvec iov[1];
3816
Joe Perchesf96637b2013-05-04 22:12:25 -05003817 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003818
Steve French630f3f0c2007-10-25 21:17:17 +00003819 *pbuflen = 0;
3820 *acl_inf = NULL;
3821
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003822 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003823 8 /* parm len */, tcon, (void **) &pSMB);
3824 if (rc)
3825 return rc;
3826
3827 pSMB->MaxParameterCount = cpu_to_le32(4);
3828 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3829 pSMB->MaxSetupCount = 0;
3830 pSMB->Fid = fid; /* file handle always le */
3831 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3832 CIFS_ACL_DACL);
3833 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003834 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003835 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003836 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003837
Steve Frencha761ac52007-10-18 21:45:27 +00003838 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003839 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003840 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003841 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003842 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003843 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003844 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003845 __u32 parm_len;
3846 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003847 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003848 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003849
3850/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003851 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003852 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003853 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003854 goto qsec_out;
3855 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3856
Joe Perchesf96637b2013-05-04 22:12:25 -05003857 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3858 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003859
3860 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3861 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003862 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003863 goto qsec_out;
3864 }
3865
3866/* BB check that data area is minimum length and as big as acl_len */
3867
Steve Frenchaf6f4612007-10-16 18:40:37 +00003868 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003869 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003870 cifs_dbg(VFS, "acl length %d does not match %d\n",
3871 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003872 if (*pbuflen > acl_len)
3873 *pbuflen = acl_len;
3874 }
Steve French0a4b92c2006-01-12 15:44:21 -08003875
Steve French630f3f0c2007-10-25 21:17:17 +00003876 /* check if buffer is big enough for the acl
3877 header followed by the smallest SID */
3878 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3879 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003880 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003881 rc = -EINVAL;
3882 *pbuflen = 0;
3883 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003884 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003885 if (*acl_inf == NULL) {
3886 *pbuflen = 0;
3887 rc = -ENOMEM;
3888 }
Steve French630f3f0c2007-10-25 21:17:17 +00003889 }
Steve French0a4b92c2006-01-12 15:44:21 -08003890 }
3891qsec_out:
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01003892 free_rsp_buf(buf_type, iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003893/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003894 return rc;
3895}
Steve French97837582007-12-31 07:47:21 +00003896
3897int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003898CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003899 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003900{
3901 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3902 int rc = 0;
3903 int bytes_returned = 0;
3904 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003905 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003906
3907setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003908 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003909 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003910 return rc;
Steve French97837582007-12-31 07:47:21 +00003911
3912 pSMB->MaxSetupCount = 0;
3913 pSMB->Reserved = 0;
3914
3915 param_count = 8;
3916 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3917 data_count = acllen;
3918 data_offset = param_offset + param_count;
3919 byte_count = 3 /* pad */ + param_count;
3920
3921 pSMB->DataCount = cpu_to_le32(data_count);
3922 pSMB->TotalDataCount = pSMB->DataCount;
3923 pSMB->MaxParameterCount = cpu_to_le32(4);
3924 pSMB->MaxDataCount = cpu_to_le32(16384);
3925 pSMB->ParameterCount = cpu_to_le32(param_count);
3926 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3927 pSMB->TotalParameterCount = pSMB->ParameterCount;
3928 pSMB->DataOffset = cpu_to_le32(data_offset);
3929 pSMB->SetupCount = 0;
3930 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3931 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3932
3933 pSMB->Fid = fid; /* file handle always le */
3934 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003935 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003936
3937 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003938 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3939 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003940 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003941 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003942 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003943
3944 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3945 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3946
Joe Perchesf96637b2013-05-04 22:12:25 -05003947 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3948 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003949 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003950 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003951 cifs_buf_release(pSMB);
3952
3953 if (rc == -EAGAIN)
3954 goto setCifsAclRetry;
3955
3956 return (rc);
3957}
3958
Jeff Layton79df1ba2010-12-06 12:52:08 -05003959#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003960
Steve French6b8edfe2005-08-23 20:26:03 -07003961/* Legacy Query Path Information call for lookup to old servers such
3962 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003963int
3964SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3965 const char *search_name, FILE_ALL_INFO *data,
3966 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003967{
Steve Frenchad7a2922008-02-07 23:25:02 +00003968 QUERY_INFORMATION_REQ *pSMB;
3969 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003970 int rc = 0;
3971 int bytes_returned;
3972 int name_len;
3973
Joe Perchesf96637b2013-05-04 22:12:25 -05003974 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003975QInfRetry:
3976 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003977 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003978 if (rc)
3979 return rc;
3980
3981 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3982 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003983 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003984 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003985 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003986 name_len++; /* trailing null */
3987 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003988 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003989 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003990 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003991 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003992 }
3993 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003994 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003995 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003996 pSMB->ByteCount = cpu_to_le16(name_len);
3997
3998 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003999 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004000 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004001 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004002 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004003 struct timespec ts;
4004 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004005
4006 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004007 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004008 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004009 ts.tv_nsec = 0;
4010 ts.tv_sec = time;
4011 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004012 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4013 data->LastWriteTime = data->ChangeTime;
4014 data->LastAccessTime = 0;
4015 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004016 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004017 data->EndOfFile = data->AllocationSize;
4018 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004019 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004020 } else
4021 rc = -EIO; /* bad buffer passed in */
4022
4023 cifs_buf_release(pSMB);
4024
4025 if (rc == -EAGAIN)
4026 goto QInfRetry;
4027
4028 return rc;
4029}
4030
Jeff Laytonbcd53572010-02-12 07:44:16 -05004031int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004032CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004033 u16 netfid, FILE_ALL_INFO *pFindData)
4034{
4035 struct smb_t2_qfi_req *pSMB = NULL;
4036 struct smb_t2_qfi_rsp *pSMBr = NULL;
4037 int rc = 0;
4038 int bytes_returned;
4039 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004040
Jeff Laytonbcd53572010-02-12 07:44:16 -05004041QFileInfoRetry:
4042 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4043 (void **) &pSMBr);
4044 if (rc)
4045 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004046
Jeff Laytonbcd53572010-02-12 07:44:16 -05004047 params = 2 /* level */ + 2 /* fid */;
4048 pSMB->t2.TotalDataCount = 0;
4049 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4050 /* BB find exact max data count below from sess structure BB */
4051 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4052 pSMB->t2.MaxSetupCount = 0;
4053 pSMB->t2.Reserved = 0;
4054 pSMB->t2.Flags = 0;
4055 pSMB->t2.Timeout = 0;
4056 pSMB->t2.Reserved2 = 0;
4057 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4058 Fid) - 4);
4059 pSMB->t2.DataCount = 0;
4060 pSMB->t2.DataOffset = 0;
4061 pSMB->t2.SetupCount = 1;
4062 pSMB->t2.Reserved3 = 0;
4063 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4064 byte_count = params + 1 /* pad */ ;
4065 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4066 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4067 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4068 pSMB->Pad = 0;
4069 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004070 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004071 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004072
4073 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4074 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4075 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004076 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004077 } else { /* decode response */
4078 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4079
4080 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4081 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004082 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004083 rc = -EIO; /* bad smb */
4084 else if (pFindData) {
4085 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4086 memcpy((char *) pFindData,
4087 (char *) &pSMBr->hdr.Protocol +
4088 data_offset, sizeof(FILE_ALL_INFO));
4089 } else
4090 rc = -ENOMEM;
4091 }
4092 cifs_buf_release(pSMB);
4093 if (rc == -EAGAIN)
4094 goto QFileInfoRetry;
4095
4096 return rc;
4097}
Steve French6b8edfe2005-08-23 20:26:03 -07004098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004100CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004101 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004102 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004103 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004105 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 TRANSACTION2_QPI_REQ *pSMB = NULL;
4107 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4108 int rc = 0;
4109 int bytes_returned;
4110 int name_len;
4111 __u16 params, byte_count;
4112
Joe Perchesf96637b2013-05-04 22:12:25 -05004113 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114QPathInfoRetry:
4115 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4116 (void **) &pSMBr);
4117 if (rc)
4118 return rc;
4119
4120 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4121 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004122 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004123 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 name_len++; /* trailing null */
4125 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004126 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004127 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004129 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
4131
Steve French50c2f752007-07-13 00:33:32 +00004132 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 pSMB->TotalDataCount = 0;
4134 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004135 /* BB find exact max SMB PDU from sess structure BB */
4136 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 pSMB->MaxSetupCount = 0;
4138 pSMB->Reserved = 0;
4139 pSMB->Flags = 0;
4140 pSMB->Timeout = 0;
4141 pSMB->Reserved2 = 0;
4142 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004143 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 pSMB->DataCount = 0;
4145 pSMB->DataOffset = 0;
4146 pSMB->SetupCount = 1;
4147 pSMB->Reserved3 = 0;
4148 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4149 byte_count = params + 1 /* pad */ ;
4150 pSMB->TotalParameterCount = cpu_to_le16(params);
4151 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004152 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004153 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4154 else
4155 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004157 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 pSMB->ByteCount = cpu_to_le16(byte_count);
4159
4160 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4161 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4162 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004163 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 } else { /* decode response */
4165 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4166
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004167 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4168 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004169 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004171 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004172 rc = -EIO; /* 24 or 26 expected but we do not read
4173 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004174 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004175 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004177
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004178 /*
4179 * On legacy responses we do not read the last field,
4180 * EAsize, fortunately since it varies by subdialect and
4181 * also note it differs on Set vs Get, ie two bytes or 4
4182 * bytes depending but we don't care here.
4183 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004184 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004185 size = sizeof(FILE_INFO_STANDARD);
4186 else
4187 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004188 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004189 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 } else
4191 rc = -ENOMEM;
4192 }
4193 cifs_buf_release(pSMB);
4194 if (rc == -EAGAIN)
4195 goto QPathInfoRetry;
4196
4197 return rc;
4198}
4199
4200int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004201CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004202 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4203{
4204 struct smb_t2_qfi_req *pSMB = NULL;
4205 struct smb_t2_qfi_rsp *pSMBr = NULL;
4206 int rc = 0;
4207 int bytes_returned;
4208 __u16 params, byte_count;
4209
4210UnixQFileInfoRetry:
4211 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4212 (void **) &pSMBr);
4213 if (rc)
4214 return rc;
4215
4216 params = 2 /* level */ + 2 /* fid */;
4217 pSMB->t2.TotalDataCount = 0;
4218 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4219 /* BB find exact max data count below from sess structure BB */
4220 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4221 pSMB->t2.MaxSetupCount = 0;
4222 pSMB->t2.Reserved = 0;
4223 pSMB->t2.Flags = 0;
4224 pSMB->t2.Timeout = 0;
4225 pSMB->t2.Reserved2 = 0;
4226 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4227 Fid) - 4);
4228 pSMB->t2.DataCount = 0;
4229 pSMB->t2.DataOffset = 0;
4230 pSMB->t2.SetupCount = 1;
4231 pSMB->t2.Reserved3 = 0;
4232 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4233 byte_count = params + 1 /* pad */ ;
4234 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4235 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4236 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4237 pSMB->Pad = 0;
4238 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004239 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004240 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004241
4242 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4243 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4244 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004245 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004246 } else { /* decode response */
4247 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4248
Jeff Layton820a8032011-05-04 08:05:26 -04004249 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004250 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 -05004251 rc = -EIO; /* bad smb */
4252 } else {
4253 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4254 memcpy((char *) pFindData,
4255 (char *) &pSMBr->hdr.Protocol +
4256 data_offset,
4257 sizeof(FILE_UNIX_BASIC_INFO));
4258 }
4259 }
4260
4261 cifs_buf_release(pSMB);
4262 if (rc == -EAGAIN)
4263 goto UnixQFileInfoRetry;
4264
4265 return rc;
4266}
4267
4268int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004269CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004271 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004272 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273{
4274/* SMB_QUERY_FILE_UNIX_BASIC */
4275 TRANSACTION2_QPI_REQ *pSMB = NULL;
4276 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4277 int rc = 0;
4278 int bytes_returned = 0;
4279 int name_len;
4280 __u16 params, byte_count;
4281
Joe Perchesf96637b2013-05-04 22:12:25 -05004282 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283UnixQPathInfoRetry:
4284 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4285 (void **) &pSMBr);
4286 if (rc)
4287 return rc;
4288
4289 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4290 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004291 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4292 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 name_len++; /* trailing null */
4294 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004295 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 name_len = strnlen(searchName, PATH_MAX);
4297 name_len++; /* trailing null */
4298 strncpy(pSMB->FileName, searchName, name_len);
4299 }
4300
Steve French50c2f752007-07-13 00:33:32 +00004301 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 pSMB->TotalDataCount = 0;
4303 pSMB->MaxParameterCount = cpu_to_le16(2);
4304 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004305 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 pSMB->MaxSetupCount = 0;
4307 pSMB->Reserved = 0;
4308 pSMB->Flags = 0;
4309 pSMB->Timeout = 0;
4310 pSMB->Reserved2 = 0;
4311 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004312 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 pSMB->DataCount = 0;
4314 pSMB->DataOffset = 0;
4315 pSMB->SetupCount = 1;
4316 pSMB->Reserved3 = 0;
4317 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4318 byte_count = params + 1 /* pad */ ;
4319 pSMB->TotalParameterCount = cpu_to_le16(params);
4320 pSMB->ParameterCount = pSMB->TotalParameterCount;
4321 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4322 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004323 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 pSMB->ByteCount = cpu_to_le16(byte_count);
4325
4326 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4327 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4328 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004329 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 } else { /* decode response */
4331 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4332
Jeff Layton820a8032011-05-04 08:05:26 -04004333 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004334 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 -07004335 rc = -EIO; /* bad smb */
4336 } else {
4337 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4338 memcpy((char *) pFindData,
4339 (char *) &pSMBr->hdr.Protocol +
4340 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004341 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 }
4343 }
4344 cifs_buf_release(pSMB);
4345 if (rc == -EAGAIN)
4346 goto UnixQPathInfoRetry;
4347
4348 return rc;
4349}
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351/* xid, tcon, searchName and codepage are input parms, rest are returned */
4352int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004353CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004354 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004355 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004356 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
4358/* level 257 SMB_ */
4359 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4360 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004361 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 int rc = 0;
4363 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004364 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004366 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Joe Perchesf96637b2013-05-04 22:12:25 -05004368 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
4370findFirstRetry:
4371 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4372 (void **) &pSMBr);
4373 if (rc)
4374 return rc;
4375
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004376 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004377 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4380 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004381 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4382 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004383 /* We can not add the asterik earlier in case
4384 it got remapped to 0xF03A as if it were part of the
4385 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004387 if (msearch) {
4388 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4389 pSMB->FileName[name_len+1] = 0;
4390 pSMB->FileName[name_len+2] = '*';
4391 pSMB->FileName[name_len+3] = 0;
4392 name_len += 4; /* now the trailing null */
4393 /* null terminate just in case */
4394 pSMB->FileName[name_len] = 0;
4395 pSMB->FileName[name_len+1] = 0;
4396 name_len += 2;
4397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 } else { /* BB add check for overrun of SMB buf BB */
4399 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004401 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 free buffer exit; BB */
4403 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004404 if (msearch) {
4405 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4406 pSMB->FileName[name_len+1] = '*';
4407 pSMB->FileName[name_len+2] = 0;
4408 name_len += 3;
4409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
4411
4412 params = 12 + name_len /* includes null */ ;
4413 pSMB->TotalDataCount = 0; /* no EAs */
4414 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004415 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 pSMB->MaxSetupCount = 0;
4417 pSMB->Reserved = 0;
4418 pSMB->Flags = 0;
4419 pSMB->Timeout = 0;
4420 pSMB->Reserved2 = 0;
4421 byte_count = params + 1 /* pad */ ;
4422 pSMB->TotalParameterCount = cpu_to_le16(params);
4423 pSMB->ParameterCount = pSMB->TotalParameterCount;
4424 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004425 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4426 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 pSMB->DataCount = 0;
4428 pSMB->DataOffset = 0;
4429 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4430 pSMB->Reserved3 = 0;
4431 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4432 pSMB->SearchAttributes =
4433 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4434 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004435 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004436 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4438
4439 /* BB what should we set StorageType to? Does it matter? BB */
4440 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004441 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 pSMB->ByteCount = cpu_to_le16(byte_count);
4443
4444 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4445 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004446 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Steve French88274812006-03-09 22:21:45 +00004448 if (rc) {/* BB add logic to retry regular search if Unix search
4449 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004451 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004452
Steve French88274812006-03-09 22:21:45 +00004453 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
4455 /* BB eventually could optimize out free and realloc of buf */
4456 /* for this case */
4457 if (rc == -EAGAIN)
4458 goto findFirstRetry;
4459 } else { /* decode response */
4460 /* BB remember to free buffer if error BB */
4461 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004462 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004463 unsigned int lnoff;
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004466 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 else
Steve French4b18f2a2008-04-29 00:06:05 +00004468 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004471 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004472 psrch_inf->srch_entries_start =
4473 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4476 le16_to_cpu(pSMBr->t2.ParameterOffset));
4477
Steve French790fe572007-07-07 19:25:05 +00004478 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004479 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 else
Steve French4b18f2a2008-04-29 00:06:05 +00004481 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Steve French50c2f752007-07-13 00:33:32 +00004483 psrch_inf->entries_in_buffer =
4484 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004485 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004487 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004488 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004489 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004490 psrch_inf->last_entry = NULL;
4491 return rc;
4492 }
4493
Steve French0752f152008-10-07 20:03:33 +00004494 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004495 lnoff;
4496
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004497 if (pnetfid)
4498 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 } else {
4500 cifs_buf_release(pSMB);
4501 }
4502 }
4503
4504 return rc;
4505}
4506
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004507int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4508 __u16 searchHandle, __u16 search_flags,
4509 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4512 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004513 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 char *response_data;
4515 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004516 int bytes_returned;
4517 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 __u16 params, byte_count;
4519
Joe Perchesf96637b2013-05-04 22:12:25 -05004520 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Steve French4b18f2a2008-04-29 00:06:05 +00004522 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return -ENOENT;
4524
4525 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4526 (void **) &pSMBr);
4527 if (rc)
4528 return rc;
4529
Steve French50c2f752007-07-13 00:33:32 +00004530 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 byte_count = 0;
4532 pSMB->TotalDataCount = 0; /* no EAs */
4533 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004534 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 pSMB->MaxSetupCount = 0;
4536 pSMB->Reserved = 0;
4537 pSMB->Flags = 0;
4538 pSMB->Timeout = 0;
4539 pSMB->Reserved2 = 0;
4540 pSMB->ParameterOffset = cpu_to_le16(
4541 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4542 pSMB->DataCount = 0;
4543 pSMB->DataOffset = 0;
4544 pSMB->SetupCount = 1;
4545 pSMB->Reserved3 = 0;
4546 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4547 pSMB->SearchHandle = searchHandle; /* always kept as le */
4548 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004549 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4551 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004552 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
4554 name_len = psrch_inf->resume_name_len;
4555 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004556 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4558 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004559 /* 14 byte parm len above enough for 2 byte null terminator */
4560 pSMB->ResumeFileName[name_len] = 0;
4561 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 } else {
4563 rc = -EINVAL;
4564 goto FNext2_err_exit;
4565 }
4566 byte_count = params + 1 /* pad */ ;
4567 pSMB->TotalParameterCount = cpu_to_le16(params);
4568 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004569 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4573 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004574 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 if (rc) {
4576 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004577 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004578 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004579 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004581 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 } else { /* decode response */
4583 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004584
Steve French790fe572007-07-07 19:25:05 +00004585 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004586 unsigned int lnoff;
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 /* BB fixme add lock for file (srch_info) struct here */
4589 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004590 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 else
Steve French4b18f2a2008-04-29 00:06:05 +00004592 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 response_data = (char *) &pSMBr->hdr.Protocol +
4594 le16_to_cpu(pSMBr->t2.ParameterOffset);
4595 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4596 response_data = (char *)&pSMBr->hdr.Protocol +
4597 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004598 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004599 cifs_small_buf_release(
4600 psrch_inf->ntwrk_buf_start);
4601 else
4602 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 psrch_inf->srch_entries_start = response_data;
4604 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004605 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004606 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004607 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 else
Steve French4b18f2a2008-04-29 00:06:05 +00004609 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004610 psrch_inf->entries_in_buffer =
4611 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 psrch_inf->index_of_last_entry +=
4613 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004614 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004615 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004616 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004617 psrch_inf->last_entry = NULL;
4618 return rc;
4619 } else
4620 psrch_inf->last_entry =
4621 psrch_inf->srch_entries_start + lnoff;
4622
Joe Perchesf96637b2013-05-04 22:12:25 -05004623/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4624 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 /* BB fixme add unlock here */
4627 }
4628
4629 }
4630
4631 /* BB On error, should we leave previous search buf (and count and
4632 last entry fields) intact or free the previous one? */
4633
4634 /* Note: On -EAGAIN error only caller can retry on handle based calls
4635 since file handle passed in no longer valid */
4636FNext2_err_exit:
4637 if (rc != 0)
4638 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 return rc;
4640}
4641
4642int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004643CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004644 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
4646 int rc = 0;
4647 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Joe Perchesf96637b2013-05-04 22:12:25 -05004649 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4651
4652 /* no sense returning error if session restarted
4653 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004654 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return 0;
4656 if (rc)
4657 return rc;
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 pSMB->FileID = searchHandle;
4660 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004661 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004662 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004663 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004664
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004665 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 /* Since session is dead, search handle closed on server already */
4668 if (rc == -EAGAIN)
4669 rc = 0;
4670
4671 return rc;
4672}
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004675CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004676 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004677 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678{
4679 int rc = 0;
4680 TRANSACTION2_QPI_REQ *pSMB = NULL;
4681 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4682 int name_len, bytes_returned;
4683 __u16 params, byte_count;
4684
Joe Perchesf96637b2013-05-04 22:12:25 -05004685 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004686 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004687 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
4689GetInodeNumberRetry:
4690 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004691 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (rc)
4693 return rc;
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4696 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004697 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004698 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004699 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 name_len++; /* trailing null */
4701 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004702 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004703 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004705 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
4708 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4709 pSMB->TotalDataCount = 0;
4710 pSMB->MaxParameterCount = cpu_to_le16(2);
4711 /* BB find exact max data count below from sess structure BB */
4712 pSMB->MaxDataCount = cpu_to_le16(4000);
4713 pSMB->MaxSetupCount = 0;
4714 pSMB->Reserved = 0;
4715 pSMB->Flags = 0;
4716 pSMB->Timeout = 0;
4717 pSMB->Reserved2 = 0;
4718 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004719 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 pSMB->DataCount = 0;
4721 pSMB->DataOffset = 0;
4722 pSMB->SetupCount = 1;
4723 pSMB->Reserved3 = 0;
4724 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4725 byte_count = params + 1 /* pad */ ;
4726 pSMB->TotalParameterCount = cpu_to_le16(params);
4727 pSMB->ParameterCount = pSMB->TotalParameterCount;
4728 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4729 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004730 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 pSMB->ByteCount = cpu_to_le16(byte_count);
4732
4733 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4734 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4735 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004736 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 } else {
4738 /* decode response */
4739 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004741 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 /* If rc should we check for EOPNOSUPP and
4743 disable the srvino flag? or in caller? */
4744 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004745 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4747 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004748 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004750 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004751 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 rc = -EIO;
4753 goto GetInodeNumOut;
4754 }
4755 pfinfo = (struct file_internal_info *)
4756 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004757 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
4759 }
4760GetInodeNumOut:
4761 cifs_buf_release(pSMB);
4762 if (rc == -EAGAIN)
4763 goto GetInodeNumberRetry;
4764 return rc;
4765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Igor Mammedovfec45852008-05-16 13:06:30 +04004767/* parses DFS refferal V3 structure
4768 * caller is responsible for freeing target_nodes
4769 * returns:
4770 * on success - 0
4771 * on failure - errno
4772 */
4773static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004774parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004775 unsigned int *num_of_nodes,
4776 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004777 const struct nls_table *nls_codepage, int remap,
4778 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004779{
4780 int i, rc = 0;
4781 char *data_end;
4782 bool is_unicode;
4783 struct dfs_referral_level_3 *ref;
4784
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004785 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4786 is_unicode = true;
4787 else
4788 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004789 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4790
4791 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004792 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4793 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004794 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004795 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004796 }
4797
4798 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004799 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004800 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4801 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004802 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004803 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004804 }
4805
4806 /* get the upper boundary of the resp buffer */
4807 data_end = (char *)(&(pSMBr->PathConsumed)) +
4808 le16_to_cpu(pSMBr->t2.DataCount);
4809
Joe Perchesf96637b2013-05-04 22:12:25 -05004810 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4811 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004812
Joe Perchesf96637b2013-05-04 22:12:25 -05004813 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4814 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004815 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004816 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004817 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004818 }
4819
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004820 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004821 for (i = 0; i < *num_of_nodes; i++) {
4822 char *temp;
4823 int max_len;
4824 struct dfs_info3_param *node = (*target_nodes)+i;
4825
Steve French0e0d2cf2009-05-01 05:27:32 +00004826 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004827 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004828 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4829 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004830 if (tmp == NULL) {
4831 rc = -ENOMEM;
4832 goto parse_DFS_referrals_exit;
4833 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004834 cifsConvertToUTF16((__le16 *) tmp, searchName,
4835 PATH_MAX, nls_codepage, remap);
4836 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004837 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004838 nls_codepage);
4839 kfree(tmp);
4840 } else
4841 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4842
Igor Mammedovfec45852008-05-16 13:06:30 +04004843 node->server_type = le16_to_cpu(ref->ServerType);
4844 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4845
4846 /* copy DfsPath */
4847 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4848 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004849 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4850 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004851 if (!node->path_name) {
4852 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004853 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004854 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004855
4856 /* copy link target UNC */
4857 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4858 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004859 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4860 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004861 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004862 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004863 goto parse_DFS_referrals_exit;
4864 }
4865
4866 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004867 }
4868
Steve Frencha1fe78f2008-05-16 18:48:38 +00004869parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004870 if (rc) {
4871 free_dfs_info_array(*target_nodes, *num_of_nodes);
4872 *target_nodes = NULL;
4873 *num_of_nodes = 0;
4874 }
4875 return rc;
4876}
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004879CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004880 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004881 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004882 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
4884/* TRANS2_GET_DFS_REFERRAL */
4885 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4886 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 int rc = 0;
4888 int bytes_returned;
4889 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004891 *num_of_nodes = 0;
4892 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Joe Perchesf96637b2013-05-04 22:12:25 -05004894 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (ses == NULL)
4896 return -ENODEV;
4897getDFSRetry:
4898 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4899 (void **) &pSMBr);
4900 if (rc)
4901 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004902
4903 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004904 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004905 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 pSMB->hdr.Tid = ses->ipc_tid;
4907 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004908 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004910 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
4913 if (ses->capabilities & CAP_UNICODE) {
4914 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4915 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004916 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004917 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004918 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 name_len++; /* trailing null */
4920 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004921 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004922 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004924 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 }
4926
Dan Carpenter65c3b202015-04-30 17:30:24 +03004927 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004928 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004929
Steve French50c2f752007-07-13 00:33:32 +00004930 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 params = 2 /* level */ + name_len /*includes null */ ;
4933 pSMB->TotalDataCount = 0;
4934 pSMB->DataCount = 0;
4935 pSMB->DataOffset = 0;
4936 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004937 /* BB find exact max SMB PDU from sess structure BB */
4938 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 pSMB->MaxSetupCount = 0;
4940 pSMB->Reserved = 0;
4941 pSMB->Flags = 0;
4942 pSMB->Timeout = 0;
4943 pSMB->Reserved2 = 0;
4944 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004945 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 pSMB->SetupCount = 1;
4947 pSMB->Reserved3 = 0;
4948 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4949 byte_count = params + 3 /* pad */ ;
4950 pSMB->ParameterCount = cpu_to_le16(params);
4951 pSMB->TotalParameterCount = pSMB->ParameterCount;
4952 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004953 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 pSMB->ByteCount = cpu_to_le16(byte_count);
4955
4956 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4957 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4958 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004959 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004960 goto GetDFSRefExit;
4961 }
4962 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004964 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004965 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004966 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004967 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004969
Joe Perchesf96637b2013-05-04 22:12:25 -05004970 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4971 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004972
4973 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004974 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004975 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004976 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004979 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 if (rc == -EAGAIN)
4982 goto getDFSRetry;
4983
4984 return rc;
4985}
4986
Steve French20962432005-09-21 22:05:57 -07004987/* Query File System Info such as free space to old servers such as Win 9x */
4988int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004989SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4990 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004991{
4992/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4993 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4994 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4995 FILE_SYSTEM_ALLOC_INFO *response_data;
4996 int rc = 0;
4997 int bytes_returned = 0;
4998 __u16 params, byte_count;
4999
Joe Perchesf96637b2013-05-04 22:12:25 -05005000 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005001oldQFSInfoRetry:
5002 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5003 (void **) &pSMBr);
5004 if (rc)
5005 return rc;
Steve French20962432005-09-21 22:05:57 -07005006
5007 params = 2; /* level */
5008 pSMB->TotalDataCount = 0;
5009 pSMB->MaxParameterCount = cpu_to_le16(2);
5010 pSMB->MaxDataCount = cpu_to_le16(1000);
5011 pSMB->MaxSetupCount = 0;
5012 pSMB->Reserved = 0;
5013 pSMB->Flags = 0;
5014 pSMB->Timeout = 0;
5015 pSMB->Reserved2 = 0;
5016 byte_count = params + 1 /* pad */ ;
5017 pSMB->TotalParameterCount = cpu_to_le16(params);
5018 pSMB->ParameterCount = pSMB->TotalParameterCount;
5019 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5020 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5021 pSMB->DataCount = 0;
5022 pSMB->DataOffset = 0;
5023 pSMB->SetupCount = 1;
5024 pSMB->Reserved3 = 0;
5025 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5026 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005027 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005028 pSMB->ByteCount = cpu_to_le16(byte_count);
5029
5030 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5031 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5032 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005033 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005034 } else { /* decode response */
5035 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5036
Jeff Layton820a8032011-05-04 08:05:26 -04005037 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005038 rc = -EIO; /* bad smb */
5039 else {
5040 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005041 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005042 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005043
Steve French50c2f752007-07-13 00:33:32 +00005044 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005045 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5046 FSData->f_bsize =
5047 le16_to_cpu(response_data->BytesPerSector) *
5048 le32_to_cpu(response_data->
5049 SectorsPerAllocationUnit);
5050 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005051 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005052 FSData->f_bfree = FSData->f_bavail =
5053 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005054 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5055 (unsigned long long)FSData->f_blocks,
5056 (unsigned long long)FSData->f_bfree,
5057 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005058 }
5059 }
5060 cifs_buf_release(pSMB);
5061
5062 if (rc == -EAGAIN)
5063 goto oldQFSInfoRetry;
5064
5065 return rc;
5066}
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005069CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5070 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071{
5072/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5073 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5074 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5075 FILE_SYSTEM_INFO *response_data;
5076 int rc = 0;
5077 int bytes_returned = 0;
5078 __u16 params, byte_count;
5079
Joe Perchesf96637b2013-05-04 22:12:25 -05005080 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081QFSInfoRetry:
5082 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5083 (void **) &pSMBr);
5084 if (rc)
5085 return rc;
5086
5087 params = 2; /* level */
5088 pSMB->TotalDataCount = 0;
5089 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005090 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 pSMB->MaxSetupCount = 0;
5092 pSMB->Reserved = 0;
5093 pSMB->Flags = 0;
5094 pSMB->Timeout = 0;
5095 pSMB->Reserved2 = 0;
5096 byte_count = params + 1 /* pad */ ;
5097 pSMB->TotalParameterCount = cpu_to_le16(params);
5098 pSMB->ParameterCount = pSMB->TotalParameterCount;
5099 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005100 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 pSMB->DataCount = 0;
5102 pSMB->DataOffset = 0;
5103 pSMB->SetupCount = 1;
5104 pSMB->Reserved3 = 0;
5105 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5106 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005107 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 pSMB->ByteCount = cpu_to_le16(byte_count);
5109
5110 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5111 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5112 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005113 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005115 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
Jeff Layton820a8032011-05-04 08:05:26 -04005117 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 rc = -EIO; /* bad smb */
5119 else {
5120 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 response_data =
5123 (FILE_SYSTEM_INFO
5124 *) (((char *) &pSMBr->hdr.Protocol) +
5125 data_offset);
5126 FSData->f_bsize =
5127 le32_to_cpu(response_data->BytesPerSector) *
5128 le32_to_cpu(response_data->
5129 SectorsPerAllocationUnit);
5130 FSData->f_blocks =
5131 le64_to_cpu(response_data->TotalAllocationUnits);
5132 FSData->f_bfree = FSData->f_bavail =
5133 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005134 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5135 (unsigned long long)FSData->f_blocks,
5136 (unsigned long long)FSData->f_bfree,
5137 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 }
5139 }
5140 cifs_buf_release(pSMB);
5141
5142 if (rc == -EAGAIN)
5143 goto QFSInfoRetry;
5144
5145 return rc;
5146}
5147
5148int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005149CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150{
5151/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5152 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5153 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5154 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5155 int rc = 0;
5156 int bytes_returned = 0;
5157 __u16 params, byte_count;
5158
Joe Perchesf96637b2013-05-04 22:12:25 -05005159 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160QFSAttributeRetry:
5161 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5162 (void **) &pSMBr);
5163 if (rc)
5164 return rc;
5165
5166 params = 2; /* level */
5167 pSMB->TotalDataCount = 0;
5168 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005169 /* BB find exact max SMB PDU from sess structure BB */
5170 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 pSMB->MaxSetupCount = 0;
5172 pSMB->Reserved = 0;
5173 pSMB->Flags = 0;
5174 pSMB->Timeout = 0;
5175 pSMB->Reserved2 = 0;
5176 byte_count = params + 1 /* pad */ ;
5177 pSMB->TotalParameterCount = cpu_to_le16(params);
5178 pSMB->ParameterCount = pSMB->TotalParameterCount;
5179 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005180 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 pSMB->DataCount = 0;
5182 pSMB->DataOffset = 0;
5183 pSMB->SetupCount = 1;
5184 pSMB->Reserved3 = 0;
5185 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5186 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005187 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 pSMB->ByteCount = cpu_to_le16(byte_count);
5189
5190 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5191 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5192 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005193 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 } else { /* decode response */
5195 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5196
Jeff Layton820a8032011-05-04 08:05:26 -04005197 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005198 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 rc = -EIO; /* bad smb */
5200 } else {
5201 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5202 response_data =
5203 (FILE_SYSTEM_ATTRIBUTE_INFO
5204 *) (((char *) &pSMBr->hdr.Protocol) +
5205 data_offset);
5206 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005207 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 }
5209 }
5210 cifs_buf_release(pSMB);
5211
5212 if (rc == -EAGAIN)
5213 goto QFSAttributeRetry;
5214
5215 return rc;
5216}
5217
5218int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005219CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
5221/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5222 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5223 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5224 FILE_SYSTEM_DEVICE_INFO *response_data;
5225 int rc = 0;
5226 int bytes_returned = 0;
5227 __u16 params, byte_count;
5228
Joe Perchesf96637b2013-05-04 22:12:25 -05005229 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230QFSDeviceRetry:
5231 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5232 (void **) &pSMBr);
5233 if (rc)
5234 return rc;
5235
5236 params = 2; /* level */
5237 pSMB->TotalDataCount = 0;
5238 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005239 /* BB find exact max SMB PDU from sess structure BB */
5240 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 pSMB->MaxSetupCount = 0;
5242 pSMB->Reserved = 0;
5243 pSMB->Flags = 0;
5244 pSMB->Timeout = 0;
5245 pSMB->Reserved2 = 0;
5246 byte_count = params + 1 /* pad */ ;
5247 pSMB->TotalParameterCount = cpu_to_le16(params);
5248 pSMB->ParameterCount = pSMB->TotalParameterCount;
5249 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005250 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 pSMB->DataCount = 0;
5253 pSMB->DataOffset = 0;
5254 pSMB->SetupCount = 1;
5255 pSMB->Reserved3 = 0;
5256 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5257 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005258 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 pSMB->ByteCount = cpu_to_le16(byte_count);
5260
5261 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5262 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5263 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005264 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 } else { /* decode response */
5266 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5267
Jeff Layton820a8032011-05-04 08:05:26 -04005268 if (rc || get_bcc(&pSMBr->hdr) <
5269 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 rc = -EIO; /* bad smb */
5271 else {
5272 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5273 response_data =
Steve French737b7582005-04-28 22:41:06 -07005274 (FILE_SYSTEM_DEVICE_INFO *)
5275 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 data_offset);
5277 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005278 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 }
5280 }
5281 cifs_buf_release(pSMB);
5282
5283 if (rc == -EAGAIN)
5284 goto QFSDeviceRetry;
5285
5286 return rc;
5287}
5288
5289int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005290CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291{
5292/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5293 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5294 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5295 FILE_SYSTEM_UNIX_INFO *response_data;
5296 int rc = 0;
5297 int bytes_returned = 0;
5298 __u16 params, byte_count;
5299
Joe Perchesf96637b2013-05-04 22:12:25 -05005300 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005302 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5303 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (rc)
5305 return rc;
5306
5307 params = 2; /* level */
5308 pSMB->TotalDataCount = 0;
5309 pSMB->DataCount = 0;
5310 pSMB->DataOffset = 0;
5311 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005312 /* BB find exact max SMB PDU from sess structure BB */
5313 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 pSMB->MaxSetupCount = 0;
5315 pSMB->Reserved = 0;
5316 pSMB->Flags = 0;
5317 pSMB->Timeout = 0;
5318 pSMB->Reserved2 = 0;
5319 byte_count = params + 1 /* pad */ ;
5320 pSMB->ParameterCount = cpu_to_le16(params);
5321 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005322 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5323 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 pSMB->SetupCount = 1;
5325 pSMB->Reserved3 = 0;
5326 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5327 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005328 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 pSMB->ByteCount = cpu_to_le16(byte_count);
5330
5331 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5332 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5333 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005334 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 } else { /* decode response */
5336 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5337
Jeff Layton820a8032011-05-04 08:05:26 -04005338 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 rc = -EIO; /* bad smb */
5340 } else {
5341 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5342 response_data =
5343 (FILE_SYSTEM_UNIX_INFO
5344 *) (((char *) &pSMBr->hdr.Protocol) +
5345 data_offset);
5346 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005347 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 }
5349 }
5350 cifs_buf_release(pSMB);
5351
5352 if (rc == -EAGAIN)
5353 goto QFSUnixRetry;
5354
5355
5356 return rc;
5357}
5358
Jeremy Allisonac670552005-06-22 17:26:35 -07005359int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005360CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005361{
5362/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5363 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5364 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5365 int rc = 0;
5366 int bytes_returned = 0;
5367 __u16 params, param_offset, offset, byte_count;
5368
Joe Perchesf96637b2013-05-04 22:12:25 -05005369 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005370SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005371 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005372 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5373 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005374 if (rc)
5375 return rc;
5376
5377 params = 4; /* 2 bytes zero followed by info level. */
5378 pSMB->MaxSetupCount = 0;
5379 pSMB->Reserved = 0;
5380 pSMB->Flags = 0;
5381 pSMB->Timeout = 0;
5382 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005383 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5384 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005385 offset = param_offset + params;
5386
5387 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005388 /* BB find exact max SMB PDU from sess structure BB */
5389 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005390 pSMB->SetupCount = 1;
5391 pSMB->Reserved3 = 0;
5392 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5393 byte_count = 1 /* pad */ + params + 12;
5394
5395 pSMB->DataCount = cpu_to_le16(12);
5396 pSMB->ParameterCount = cpu_to_le16(params);
5397 pSMB->TotalDataCount = pSMB->DataCount;
5398 pSMB->TotalParameterCount = pSMB->ParameterCount;
5399 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5400 pSMB->DataOffset = cpu_to_le16(offset);
5401
5402 /* Params. */
5403 pSMB->FileNum = 0;
5404 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5405
5406 /* Data. */
5407 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5408 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5409 pSMB->ClientUnixCap = cpu_to_le64(cap);
5410
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005411 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005412 pSMB->ByteCount = cpu_to_le16(byte_count);
5413
5414 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5415 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5416 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005417 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005418 } else { /* decode response */
5419 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005420 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005421 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005422 }
5423 cifs_buf_release(pSMB);
5424
5425 if (rc == -EAGAIN)
5426 goto SETFSUnixRetry;
5427
5428 return rc;
5429}
5430
5431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005434CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005435 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
5437/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5438 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5439 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5440 FILE_SYSTEM_POSIX_INFO *response_data;
5441 int rc = 0;
5442 int bytes_returned = 0;
5443 __u16 params, byte_count;
5444
Joe Perchesf96637b2013-05-04 22:12:25 -05005445 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446QFSPosixRetry:
5447 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5448 (void **) &pSMBr);
5449 if (rc)
5450 return rc;
5451
5452 params = 2; /* level */
5453 pSMB->TotalDataCount = 0;
5454 pSMB->DataCount = 0;
5455 pSMB->DataOffset = 0;
5456 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005457 /* BB find exact max SMB PDU from sess structure BB */
5458 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 pSMB->MaxSetupCount = 0;
5460 pSMB->Reserved = 0;
5461 pSMB->Flags = 0;
5462 pSMB->Timeout = 0;
5463 pSMB->Reserved2 = 0;
5464 byte_count = params + 1 /* pad */ ;
5465 pSMB->ParameterCount = cpu_to_le16(params);
5466 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005467 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5468 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 pSMB->SetupCount = 1;
5470 pSMB->Reserved3 = 0;
5471 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5472 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005473 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 pSMB->ByteCount = cpu_to_le16(byte_count);
5475
5476 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5477 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5478 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005479 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 } else { /* decode response */
5481 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5482
Jeff Layton820a8032011-05-04 08:05:26 -04005483 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 rc = -EIO; /* bad smb */
5485 } else {
5486 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5487 response_data =
5488 (FILE_SYSTEM_POSIX_INFO
5489 *) (((char *) &pSMBr->hdr.Protocol) +
5490 data_offset);
5491 FSData->f_bsize =
5492 le32_to_cpu(response_data->BlockSize);
5493 FSData->f_blocks =
5494 le64_to_cpu(response_data->TotalBlocks);
5495 FSData->f_bfree =
5496 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005497 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 FSData->f_bavail = FSData->f_bfree;
5499 } else {
5500 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005501 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
Steve French790fe572007-07-07 19:25:05 +00005503 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005505 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005506 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005508 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 }
5510 }
5511 cifs_buf_release(pSMB);
5512
5513 if (rc == -EAGAIN)
5514 goto QFSPosixRetry;
5515
5516 return rc;
5517}
5518
5519
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005520/*
5521 * We can not use write of zero bytes trick to set file size due to need for
5522 * large file support. Also note that this SetPathInfo is preferred to
5523 * SetFileInfo based method in next routine which is only needed to work around
5524 * a sharing violation bugin Samba which this routine can run into.
5525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005527CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005528 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5529 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
5531 struct smb_com_transaction2_spi_req *pSMB = NULL;
5532 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5533 struct file_end_of_file_info *parm_data;
5534 int name_len;
5535 int rc = 0;
5536 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005537 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 __u16 params, byte_count, data_count, param_offset, offset;
5540
Joe Perchesf96637b2013-05-04 22:12:25 -05005541 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542SetEOFRetry:
5543 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5544 (void **) &pSMBr);
5545 if (rc)
5546 return rc;
5547
5548 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5549 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005550 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5551 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 name_len++; /* trailing null */
5553 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005554 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005555 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005557 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 }
5559 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005560 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005562 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 pSMB->MaxSetupCount = 0;
5564 pSMB->Reserved = 0;
5565 pSMB->Flags = 0;
5566 pSMB->Timeout = 0;
5567 pSMB->Reserved2 = 0;
5568 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005569 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005571 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005572 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5573 pSMB->InformationLevel =
5574 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5575 else
5576 pSMB->InformationLevel =
5577 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5578 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5580 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005581 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 else
5583 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005584 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 }
5586
5587 parm_data =
5588 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5589 offset);
5590 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5591 pSMB->DataOffset = cpu_to_le16(offset);
5592 pSMB->SetupCount = 1;
5593 pSMB->Reserved3 = 0;
5594 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5595 byte_count = 3 /* pad */ + params + data_count;
5596 pSMB->DataCount = cpu_to_le16(data_count);
5597 pSMB->TotalDataCount = pSMB->DataCount;
5598 pSMB->ParameterCount = cpu_to_le16(params);
5599 pSMB->TotalParameterCount = pSMB->ParameterCount;
5600 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005601 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 parm_data->FileSize = cpu_to_le64(size);
5603 pSMB->ByteCount = cpu_to_le16(byte_count);
5604 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5605 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005606 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005607 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 cifs_buf_release(pSMB);
5610
5611 if (rc == -EAGAIN)
5612 goto SetEOFRetry;
5613
5614 return rc;
5615}
5616
5617int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005618CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5619 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620{
5621 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 struct file_end_of_file_info *parm_data;
5623 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 __u16 params, param_offset, offset, byte_count, count;
5625
Joe Perchesf96637b2013-05-04 22:12:25 -05005626 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5627 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005628 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 if (rc)
5631 return rc;
5632
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005633 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5634 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 params = 6;
5637 pSMB->MaxSetupCount = 0;
5638 pSMB->Reserved = 0;
5639 pSMB->Flags = 0;
5640 pSMB->Timeout = 0;
5641 pSMB->Reserved2 = 0;
5642 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5643 offset = param_offset + params;
5644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 count = sizeof(struct file_end_of_file_info);
5646 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005647 /* BB find exact max SMB PDU from sess structure BB */
5648 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 pSMB->SetupCount = 1;
5650 pSMB->Reserved3 = 0;
5651 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5652 byte_count = 3 /* pad */ + params + count;
5653 pSMB->DataCount = cpu_to_le16(count);
5654 pSMB->ParameterCount = cpu_to_le16(params);
5655 pSMB->TotalDataCount = pSMB->DataCount;
5656 pSMB->TotalParameterCount = pSMB->ParameterCount;
5657 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5658 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005659 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5660 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 pSMB->DataOffset = cpu_to_le16(offset);
5662 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005663 pSMB->Fid = cfile->fid.netfid;
5664 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5666 pSMB->InformationLevel =
5667 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5668 else
5669 pSMB->InformationLevel =
5670 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005671 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5673 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005674 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 else
5676 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005677 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 }
5679 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005680 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005682 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005684 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5685 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 }
5687
Steve French50c2f752007-07-13 00:33:32 +00005688 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 since file handle passed in no longer valid */
5690
5691 return rc;
5692}
5693
Steve French50c2f752007-07-13 00:33:32 +00005694/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 an open handle, rather than by pathname - this is awkward due to
5696 potential access conflicts on the open, but it is unavoidable for these
5697 old servers since the only other choice is to go from 100 nanosecond DCE
5698 time and resort to the original setpathinfo level which takes the ancient
5699 DOS time format with 2 second granularity */
5700int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005701CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005702 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
5704 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 char *data_offset;
5706 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 __u16 params, param_offset, offset, byte_count, count;
5708
Joe Perchesf96637b2013-05-04 22:12:25 -05005709 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005710 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 if (rc)
5713 return rc;
5714
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005715 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5716 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 params = 6;
5719 pSMB->MaxSetupCount = 0;
5720 pSMB->Reserved = 0;
5721 pSMB->Flags = 0;
5722 pSMB->Timeout = 0;
5723 pSMB->Reserved2 = 0;
5724 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5725 offset = param_offset + params;
5726
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005727 data_offset = (char *)pSMB +
5728 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Steve French26f57362007-08-30 22:09:15 +00005730 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005732 /* BB find max SMB PDU from sess */
5733 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 pSMB->SetupCount = 1;
5735 pSMB->Reserved3 = 0;
5736 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5737 byte_count = 3 /* pad */ + params + count;
5738 pSMB->DataCount = cpu_to_le16(count);
5739 pSMB->ParameterCount = cpu_to_le16(params);
5740 pSMB->TotalDataCount = pSMB->DataCount;
5741 pSMB->TotalParameterCount = pSMB->ParameterCount;
5742 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5743 pSMB->DataOffset = cpu_to_le16(offset);
5744 pSMB->Fid = fid;
5745 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5746 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5747 else
5748 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5749 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005750 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005752 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005753 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005754 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005755 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5756 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Steve French50c2f752007-07-13 00:33:32 +00005758 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 since file handle passed in no longer valid */
5760
5761 return rc;
5762}
5763
Jeff Layton6d22f092008-09-23 11:48:35 -04005764int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005765CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005766 bool delete_file, __u16 fid, __u32 pid_of_opener)
5767{
5768 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5769 char *data_offset;
5770 int rc = 0;
5771 __u16 params, param_offset, offset, byte_count, count;
5772
Joe Perchesf96637b2013-05-04 22:12:25 -05005773 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005774 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5775
5776 if (rc)
5777 return rc;
5778
5779 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5780 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5781
5782 params = 6;
5783 pSMB->MaxSetupCount = 0;
5784 pSMB->Reserved = 0;
5785 pSMB->Flags = 0;
5786 pSMB->Timeout = 0;
5787 pSMB->Reserved2 = 0;
5788 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5789 offset = param_offset + params;
5790
5791 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5792
5793 count = 1;
5794 pSMB->MaxParameterCount = cpu_to_le16(2);
5795 /* BB find max SMB PDU from sess */
5796 pSMB->MaxDataCount = cpu_to_le16(1000);
5797 pSMB->SetupCount = 1;
5798 pSMB->Reserved3 = 0;
5799 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5800 byte_count = 3 /* pad */ + params + count;
5801 pSMB->DataCount = cpu_to_le16(count);
5802 pSMB->ParameterCount = cpu_to_le16(params);
5803 pSMB->TotalDataCount = pSMB->DataCount;
5804 pSMB->TotalParameterCount = pSMB->ParameterCount;
5805 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5806 pSMB->DataOffset = cpu_to_le16(offset);
5807 pSMB->Fid = fid;
5808 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5809 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005810 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005811 pSMB->ByteCount = cpu_to_le16(byte_count);
5812 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005813 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005814 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005815 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005816
5817 return rc;
5818}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005821CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005822 const char *fileName, const FILE_BASIC_INFO *data,
5823 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
5825 TRANSACTION2_SPI_REQ *pSMB = NULL;
5826 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5827 int name_len;
5828 int rc = 0;
5829 int bytes_returned = 0;
5830 char *data_offset;
5831 __u16 params, param_offset, offset, byte_count, count;
5832
Joe Perchesf96637b2013-05-04 22:12:25 -05005833 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
5835SetTimesRetry:
5836 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5837 (void **) &pSMBr);
5838 if (rc)
5839 return rc;
5840
5841 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5842 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005843 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5844 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 name_len++; /* trailing null */
5846 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005847 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 name_len = strnlen(fileName, PATH_MAX);
5849 name_len++; /* trailing null */
5850 strncpy(pSMB->FileName, fileName, name_len);
5851 }
5852
5853 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005854 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005856 /* BB find max SMB PDU from sess structure BB */
5857 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 pSMB->MaxSetupCount = 0;
5859 pSMB->Reserved = 0;
5860 pSMB->Flags = 0;
5861 pSMB->Timeout = 0;
5862 pSMB->Reserved2 = 0;
5863 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005864 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 offset = param_offset + params;
5866 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5867 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5868 pSMB->DataOffset = cpu_to_le16(offset);
5869 pSMB->SetupCount = 1;
5870 pSMB->Reserved3 = 0;
5871 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5872 byte_count = 3 /* pad */ + params + count;
5873
5874 pSMB->DataCount = cpu_to_le16(count);
5875 pSMB->ParameterCount = cpu_to_le16(params);
5876 pSMB->TotalDataCount = pSMB->DataCount;
5877 pSMB->TotalParameterCount = pSMB->ParameterCount;
5878 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5879 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5880 else
5881 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5882 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005883 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005884 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 pSMB->ByteCount = cpu_to_le16(byte_count);
5886 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5887 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005888 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005889 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
5891 cifs_buf_release(pSMB);
5892
5893 if (rc == -EAGAIN)
5894 goto SetTimesRetry;
5895
5896 return rc;
5897}
5898
5899/* Can not be used to set time stamps yet (due to old DOS time format) */
5900/* Can be used to set attributes */
5901#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5902 handling it anyway and NT4 was what we thought it would be needed for
5903 Do not delete it until we prove whether needed for Win9x though */
5904int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005905CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 __u16 dos_attrs, const struct nls_table *nls_codepage)
5907{
5908 SETATTR_REQ *pSMB = NULL;
5909 SETATTR_RSP *pSMBr = NULL;
5910 int rc = 0;
5911 int bytes_returned;
5912 int name_len;
5913
Joe Perchesf96637b2013-05-04 22:12:25 -05005914 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
5916SetAttrLgcyRetry:
5917 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5918 (void **) &pSMBr);
5919 if (rc)
5920 return rc;
5921
5922 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5923 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005924 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5925 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 name_len++; /* trailing null */
5927 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005928 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 name_len = strnlen(fileName, PATH_MAX);
5930 name_len++; /* trailing null */
5931 strncpy(pSMB->fileName, fileName, name_len);
5932 }
5933 pSMB->attr = cpu_to_le16(dos_attrs);
5934 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005935 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5937 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5938 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005939 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005940 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 cifs_buf_release(pSMB);
5943
5944 if (rc == -EAGAIN)
5945 goto SetAttrLgcyRetry;
5946
5947 return rc;
5948}
5949#endif /* temporarily unneeded SetAttr legacy function */
5950
Jeff Layton654cf142009-07-09 20:02:49 -04005951static void
5952cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5953 const struct cifs_unix_set_info_args *args)
5954{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005955 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005956 u64 mode = args->mode;
5957
Eric W. Biederman49418b22013-02-06 00:57:56 -08005958 if (uid_valid(args->uid))
5959 uid = from_kuid(&init_user_ns, args->uid);
5960 if (gid_valid(args->gid))
5961 gid = from_kgid(&init_user_ns, args->gid);
5962
Jeff Layton654cf142009-07-09 20:02:49 -04005963 /*
5964 * Samba server ignores set of file size to zero due to bugs in some
5965 * older clients, but we should be precise - we use SetFileSize to
5966 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005967 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005968 * zero instead of -1 here
5969 */
5970 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5971 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5972 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5973 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5974 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005975 data_offset->Uid = cpu_to_le64(uid);
5976 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005977 /* better to leave device as zero when it is */
5978 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5979 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5980 data_offset->Permissions = cpu_to_le64(mode);
5981
5982 if (S_ISREG(mode))
5983 data_offset->Type = cpu_to_le32(UNIX_FILE);
5984 else if (S_ISDIR(mode))
5985 data_offset->Type = cpu_to_le32(UNIX_DIR);
5986 else if (S_ISLNK(mode))
5987 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5988 else if (S_ISCHR(mode))
5989 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5990 else if (S_ISBLK(mode))
5991 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5992 else if (S_ISFIFO(mode))
5993 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5994 else if (S_ISSOCK(mode))
5995 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5996}
5997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005999CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006000 const struct cifs_unix_set_info_args *args,
6001 u16 fid, u32 pid_of_opener)
6002{
6003 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006004 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006005 int rc = 0;
6006 u16 params, param_offset, offset, byte_count, count;
6007
Joe Perchesf96637b2013-05-04 22:12:25 -05006008 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006009 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6010
6011 if (rc)
6012 return rc;
6013
6014 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6015 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6016
6017 params = 6;
6018 pSMB->MaxSetupCount = 0;
6019 pSMB->Reserved = 0;
6020 pSMB->Flags = 0;
6021 pSMB->Timeout = 0;
6022 pSMB->Reserved2 = 0;
6023 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6024 offset = param_offset + params;
6025
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006026 data_offset = (char *)pSMB +
6027 offsetof(struct smb_hdr, Protocol) + offset;
6028
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006029 count = sizeof(FILE_UNIX_BASIC_INFO);
6030
6031 pSMB->MaxParameterCount = cpu_to_le16(2);
6032 /* BB find max SMB PDU from sess */
6033 pSMB->MaxDataCount = cpu_to_le16(1000);
6034 pSMB->SetupCount = 1;
6035 pSMB->Reserved3 = 0;
6036 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6037 byte_count = 3 /* pad */ + params + count;
6038 pSMB->DataCount = cpu_to_le16(count);
6039 pSMB->ParameterCount = cpu_to_le16(params);
6040 pSMB->TotalDataCount = pSMB->DataCount;
6041 pSMB->TotalParameterCount = pSMB->ParameterCount;
6042 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6043 pSMB->DataOffset = cpu_to_le16(offset);
6044 pSMB->Fid = fid;
6045 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6046 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006047 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006048 pSMB->ByteCount = cpu_to_le16(byte_count);
6049
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006050 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006051
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006052 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006053 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006054 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6055 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006056
6057 /* Note: On -EAGAIN error only caller can retry on handle based calls
6058 since file handle passed in no longer valid */
6059
6060 return rc;
6061}
6062
6063int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006064CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006065 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006066 const struct cifs_unix_set_info_args *args,
6067 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068{
6069 TRANSACTION2_SPI_REQ *pSMB = NULL;
6070 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6071 int name_len;
6072 int rc = 0;
6073 int bytes_returned = 0;
6074 FILE_UNIX_BASIC_INFO *data_offset;
6075 __u16 params, param_offset, offset, count, byte_count;
6076
Joe Perchesf96637b2013-05-04 22:12:25 -05006077 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078setPermsRetry:
6079 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6080 (void **) &pSMBr);
6081 if (rc)
6082 return rc;
6083
6084 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6085 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006086 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006087 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 name_len++; /* trailing null */
6089 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006090 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006091 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006093 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 }
6095
6096 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006097 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006099 /* BB find max SMB PDU from sess structure BB */
6100 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 pSMB->MaxSetupCount = 0;
6102 pSMB->Reserved = 0;
6103 pSMB->Flags = 0;
6104 pSMB->Timeout = 0;
6105 pSMB->Reserved2 = 0;
6106 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006107 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 offset = param_offset + params;
6109 data_offset =
6110 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6111 offset);
6112 memset(data_offset, 0, count);
6113 pSMB->DataOffset = cpu_to_le16(offset);
6114 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6115 pSMB->SetupCount = 1;
6116 pSMB->Reserved3 = 0;
6117 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6118 byte_count = 3 /* pad */ + params + count;
6119 pSMB->ParameterCount = cpu_to_le16(params);
6120 pSMB->DataCount = cpu_to_le16(count);
6121 pSMB->TotalParameterCount = pSMB->ParameterCount;
6122 pSMB->TotalDataCount = pSMB->DataCount;
6123 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6124 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006125 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006126
Jeff Layton654cf142009-07-09 20:02:49 -04006127 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
6129 pSMB->ByteCount = cpu_to_le16(byte_count);
6130 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6131 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006132 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006133 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Steve French0d817bc2008-05-22 02:02:03 +00006135 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 if (rc == -EAGAIN)
6137 goto setPermsRetry;
6138 return rc;
6139}
6140
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006142/*
6143 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6144 * function used by listxattr and getxattr type calls. When ea_name is set,
6145 * it looks for that attribute name and stuffs that value into the EAData
6146 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6147 * buffer. In both cases, the return value is either the length of the
6148 * resulting data or a negative error code. If EAData is a NULL pointer then
6149 * the data isn't copied to it, but the length is returned.
6150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006152CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006153 const unsigned char *searchName, const unsigned char *ea_name,
6154 char *EAData, size_t buf_size,
6155 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156{
6157 /* BB assumes one setup word */
6158 TRANSACTION2_QPI_REQ *pSMB = NULL;
6159 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6160 int rc = 0;
6161 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006162 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006163 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006164 struct fea *temp_fea;
6165 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006166 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006167 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006168 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
Joe Perchesf96637b2013-05-04 22:12:25 -05006170 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171QAllEAsRetry:
6172 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6173 (void **) &pSMBr);
6174 if (rc)
6175 return rc;
6176
6177 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006178 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006179 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6180 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006181 list_len++; /* trailing null */
6182 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006184 list_len = strnlen(searchName, PATH_MAX);
6185 list_len++; /* trailing null */
6186 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 }
6188
Jeff Layton6e462b92010-02-10 16:18:26 -05006189 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 pSMB->TotalDataCount = 0;
6191 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006192 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006193 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 pSMB->MaxSetupCount = 0;
6195 pSMB->Reserved = 0;
6196 pSMB->Flags = 0;
6197 pSMB->Timeout = 0;
6198 pSMB->Reserved2 = 0;
6199 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006200 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 pSMB->DataCount = 0;
6202 pSMB->DataOffset = 0;
6203 pSMB->SetupCount = 1;
6204 pSMB->Reserved3 = 0;
6205 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6206 byte_count = params + 1 /* pad */ ;
6207 pSMB->TotalParameterCount = cpu_to_le16(params);
6208 pSMB->ParameterCount = pSMB->TotalParameterCount;
6209 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6210 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006211 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 pSMB->ByteCount = cpu_to_le16(byte_count);
6213
6214 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6215 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6216 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006217 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006218 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006220
6221
6222 /* BB also check enough total bytes returned */
6223 /* BB we need to improve the validity checking
6224 of these trans2 responses */
6225
6226 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006227 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006228 rc = -EIO; /* bad smb */
6229 goto QAllEAsOut;
6230 }
6231
6232 /* check that length of list is not more than bcc */
6233 /* check that each entry does not go beyond length
6234 of list */
6235 /* check that each element of each entry does not
6236 go beyond end of list */
6237 /* validate_trans2_offsets() */
6238 /* BB check if start of smb + data_offset > &bcc+ bcc */
6239
6240 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6241 ea_response_data = (struct fealist *)
6242 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6243
Jeff Layton6e462b92010-02-10 16:18:26 -05006244 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006245 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006246 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006247 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006248 /* didn't find the named attribute */
6249 if (ea_name)
6250 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006251 goto QAllEAsOut;
6252 }
6253
Jeff Layton0cd126b2010-02-10 16:18:26 -05006254 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006255 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006256 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006257 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006258 rc = -EIO;
6259 goto QAllEAsOut;
6260 }
6261
Jeff Laytonf0d38682010-02-10 16:18:26 -05006262 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006263 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006264 temp_fea = ea_response_data->list;
6265 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006266 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006267 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006268 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006269
Jeff Layton6e462b92010-02-10 16:18:26 -05006270 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006271 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006272 /* make sure we can read name_len and value_len */
6273 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006274 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006275 rc = -EIO;
6276 goto QAllEAsOut;
6277 }
6278
6279 name_len = temp_fea->name_len;
6280 value_len = le16_to_cpu(temp_fea->value_len);
6281 list_len -= name_len + 1 + value_len;
6282 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006283 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006284 rc = -EIO;
6285 goto QAllEAsOut;
6286 }
6287
Jeff Layton31c05192010-02-10 16:18:26 -05006288 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006289 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006290 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006291 temp_ptr += name_len + 1;
6292 rc = value_len;
6293 if (buf_size == 0)
6294 goto QAllEAsOut;
6295 if ((size_t)value_len > buf_size) {
6296 rc = -ERANGE;
6297 goto QAllEAsOut;
6298 }
6299 memcpy(EAData, temp_ptr, value_len);
6300 goto QAllEAsOut;
6301 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006302 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006303 /* account for prefix user. and trailing null */
6304 rc += (5 + 1 + name_len);
6305 if (rc < (int) buf_size) {
6306 memcpy(EAData, "user.", 5);
6307 EAData += 5;
6308 memcpy(EAData, temp_ptr, name_len);
6309 EAData += name_len;
6310 /* null terminate name */
6311 *EAData = 0;
6312 ++EAData;
6313 } else if (buf_size == 0) {
6314 /* skip copy - calc size only */
6315 } else {
6316 /* stop before overrun buffer */
6317 rc = -ERANGE;
6318 break;
6319 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006320 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006321 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006322 temp_fea = (struct fea *)temp_ptr;
6323 }
6324
Jeff Layton31c05192010-02-10 16:18:26 -05006325 /* didn't find the named attribute */
6326 if (ea_name)
6327 rc = -ENODATA;
6328
Jeff Laytonf0d38682010-02-10 16:18:26 -05006329QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006330 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 if (rc == -EAGAIN)
6332 goto QAllEAsRetry;
6333
6334 return (ssize_t)rc;
6335}
6336
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006338CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6339 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006340 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6341 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
6343 struct smb_com_transaction2_spi_req *pSMB = NULL;
6344 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6345 struct fealist *parm_data;
6346 int name_len;
6347 int rc = 0;
6348 int bytes_returned = 0;
6349 __u16 params, param_offset, byte_count, offset, count;
6350
Joe Perchesf96637b2013-05-04 22:12:25 -05006351 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352SetEARetry:
6353 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6354 (void **) &pSMBr);
6355 if (rc)
6356 return rc;
6357
6358 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6359 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006360 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6361 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 name_len++; /* trailing null */
6363 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006364 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 name_len = strnlen(fileName, PATH_MAX);
6366 name_len++; /* trailing null */
6367 strncpy(pSMB->FileName, fileName, name_len);
6368 }
6369
6370 params = 6 + name_len;
6371
6372 /* done calculating parms using name_len of file name,
6373 now use name_len to calculate length of ea name
6374 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006375 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 name_len = 0;
6377 else
Steve French50c2f752007-07-13 00:33:32 +00006378 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006380 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006382 /* BB find max SMB PDU from sess */
6383 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 pSMB->MaxSetupCount = 0;
6385 pSMB->Reserved = 0;
6386 pSMB->Flags = 0;
6387 pSMB->Timeout = 0;
6388 pSMB->Reserved2 = 0;
6389 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006390 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 offset = param_offset + params;
6392 pSMB->InformationLevel =
6393 cpu_to_le16(SMB_SET_FILE_EA);
6394
6395 parm_data =
6396 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6397 offset);
6398 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6399 pSMB->DataOffset = cpu_to_le16(offset);
6400 pSMB->SetupCount = 1;
6401 pSMB->Reserved3 = 0;
6402 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6403 byte_count = 3 /* pad */ + params + count;
6404 pSMB->DataCount = cpu_to_le16(count);
6405 parm_data->list_len = cpu_to_le32(count);
6406 parm_data->list[0].EA_flags = 0;
6407 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006408 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006410 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006411 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 parm_data->list[0].name[name_len] = 0;
6413 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6414 /* caller ensures that ea_value_len is less than 64K but
6415 we need to ensure that it fits within the smb */
6416
Steve French50c2f752007-07-13 00:33:32 +00006417 /*BB add length check to see if it would fit in
6418 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006419 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6420 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006421 memcpy(parm_data->list[0].name+name_len+1,
6422 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
6424 pSMB->TotalDataCount = pSMB->DataCount;
6425 pSMB->ParameterCount = cpu_to_le16(params);
6426 pSMB->TotalParameterCount = pSMB->ParameterCount;
6427 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006428 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 pSMB->ByteCount = cpu_to_le16(byte_count);
6430 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6431 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006432 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006433 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
6435 cifs_buf_release(pSMB);
6436
6437 if (rc == -EAGAIN)
6438 goto SetEARetry;
6439
6440 return rc;
6441}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442#endif
Steve French0eff0e22011-02-24 05:39:23 +00006443
6444#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6445/*
6446 * Years ago the kernel added a "dnotify" function for Samba server,
6447 * to allow network clients (such as Windows) to display updated
6448 * lists of files in directory listings automatically when
6449 * files are added by one user when another user has the
6450 * same directory open on their desktop. The Linux cifs kernel
6451 * client hooked into the kernel side of this interface for
6452 * the same reason, but ironically when the VFS moved from
6453 * "dnotify" to "inotify" it became harder to plug in Linux
6454 * network file system clients (the most obvious use case
6455 * for notify interfaces is when multiple users can update
6456 * the contents of the same directory - exactly what network
6457 * file systems can do) although the server (Samba) could
6458 * still use it. For the short term we leave the worker
6459 * function ifdeffed out (below) until inotify is fixed
6460 * in the VFS to make it easier to plug in network file
6461 * system clients. If inotify turns out to be permanently
6462 * incompatible for network fs clients, we could instead simply
6463 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6464 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006465int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006466 const int notify_subdirs, const __u16 netfid,
6467 __u32 filter, struct file *pfile, int multishot,
6468 const struct nls_table *nls_codepage)
6469{
6470 int rc = 0;
6471 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6472 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6473 struct dir_notify_req *dnotify_req;
6474 int bytes_returned;
6475
Joe Perchesf96637b2013-05-04 22:12:25 -05006476 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006477 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6478 (void **) &pSMBr);
6479 if (rc)
6480 return rc;
6481
6482 pSMB->TotalParameterCount = 0 ;
6483 pSMB->TotalDataCount = 0;
6484 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006485 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006486 pSMB->MaxSetupCount = 4;
6487 pSMB->Reserved = 0;
6488 pSMB->ParameterOffset = 0;
6489 pSMB->DataCount = 0;
6490 pSMB->DataOffset = 0;
6491 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6492 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6493 pSMB->ParameterCount = pSMB->TotalParameterCount;
6494 if (notify_subdirs)
6495 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6496 pSMB->Reserved2 = 0;
6497 pSMB->CompletionFilter = cpu_to_le32(filter);
6498 pSMB->Fid = netfid; /* file handle always le */
6499 pSMB->ByteCount = 0;
6500
6501 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6502 (struct smb_hdr *)pSMBr, &bytes_returned,
6503 CIFS_ASYNC_OP);
6504 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006505 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006506 } else {
6507 /* Add file to outstanding requests */
6508 /* BB change to kmem cache alloc */
6509 dnotify_req = kmalloc(
6510 sizeof(struct dir_notify_req),
6511 GFP_KERNEL);
6512 if (dnotify_req) {
6513 dnotify_req->Pid = pSMB->hdr.Pid;
6514 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6515 dnotify_req->Mid = pSMB->hdr.Mid;
6516 dnotify_req->Tid = pSMB->hdr.Tid;
6517 dnotify_req->Uid = pSMB->hdr.Uid;
6518 dnotify_req->netfid = netfid;
6519 dnotify_req->pfile = pfile;
6520 dnotify_req->filter = filter;
6521 dnotify_req->multishot = multishot;
6522 spin_lock(&GlobalMid_Lock);
6523 list_add_tail(&dnotify_req->lhead,
6524 &GlobalDnotifyReqList);
6525 spin_unlock(&GlobalMid_Lock);
6526 } else
6527 rc = -ENOMEM;
6528 }
6529 cifs_buf_release(pSMB);
6530 return rc;
6531}
6532#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */