blob: fa13d5e79f643afb4fcfdb92333a68786e5dff32 [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);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400628 } else if ((pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
Steve French07cc6cf2011-05-27 04:12:29 +0000629 server->capabilities & CAP_EXTENDED_SECURITY) &&
Jeff Laytone598d1d82013-05-26 07:00:59 -0400630 (pSMBr->EncryptionKeyLength == 0)) {
631 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500632 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400633 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000634 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400635 } else {
636 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000637 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400638 }
Steve French254e55e2006-06-04 05:53:15 +0000639
640signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400641 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400642 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000643neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700644 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000645
Joe Perchesf96637b2013-05-04 22:12:25 -0500646 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return rc;
648}
649
650int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400651CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Joe Perchesf96637b2013-05-04 22:12:25 -0500656 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500657
658 /* BB: do we need to check this? These should never be NULL. */
659 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
660 return -EIO;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500663 * No need to return error on this operation if tid invalidated and
664 * closed on server already e.g. due to tcp session crashing. Also,
665 * the tcon is no longer on the list, so no need to take lock before
666 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Steve French268875b2009-06-25 00:29:21 +0000668 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Steve French50c2f752007-07-13 00:33:32 +0000671 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700672 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500673 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return rc;
Steve French133672e2007-11-13 22:41:37 +0000675
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400676 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500678 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Steve French50c2f752007-07-13 00:33:32 +0000680 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500681 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (rc == -EAGAIN)
683 rc = 0;
684
685 return rc;
686}
687
Jeff Layton766fdbb2011-01-11 07:24:21 -0500688/*
689 * This is a no-op for now. We're not really interested in the reply, but
690 * rather in the fact that the server sent one and that server->lstrp
691 * gets updated.
692 *
693 * FIXME: maybe we should consider checking that the reply matches request?
694 */
695static void
696cifs_echo_callback(struct mid_q_entry *mid)
697{
698 struct TCP_Server_Info *server = mid->callback_data;
699
700 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400701 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500702}
703
704int
705CIFSSMBEcho(struct TCP_Server_Info *server)
706{
707 ECHO_REQ *smb;
708 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400709 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700710 struct smb_rqst rqst = { .rq_iov = &iov,
711 .rq_nvec = 1 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500712
Joe Perchesf96637b2013-05-04 22:12:25 -0500713 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500714
715 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
716 if (rc)
717 return rc;
718
719 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000720 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500721 smb->hdr.WordCount = 1;
722 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400723 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500724 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000725 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400726 iov.iov_base = smb;
727 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500728
Jeff Laytonfec344e2012-09-18 16:20:35 -0700729 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400730 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500731 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500732 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500733
734 cifs_small_buf_release(smb);
735
736 return rc;
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400740CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 LOGOFF_ANDX_REQ *pSMB;
743 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Joe Perchesf96637b2013-05-04 22:12:25 -0500745 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500746
747 /*
748 * BB: do we need to check validity of ses and server? They should
749 * always be valid since we have an active reference. If not, that
750 * should probably be a BUG()
751 */
752 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -EIO;
754
Steve Frenchd7b619c2010-02-25 05:36:46 +0000755 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000756 if (ses->need_reconnect)
757 goto session_already_dead; /* no need to send SMBlogoff if uid
758 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
760 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000761 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return rc;
763 }
764
Pavel Shilovsky88257362012-05-23 14:01:59 +0400765 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700766
Jeff Layton38d77c52013-05-26 07:01:00 -0400767 if (ses->server->sign)
768 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 pSMB->hdr.Uid = ses->Suid;
771
772 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400773 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000774session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000775 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000778 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 error */
780 if (rc == -EAGAIN)
781 rc = 0;
782 return rc;
783}
784
785int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400786CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
787 const char *fileName, __u16 type,
788 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000789{
790 TRANSACTION2_SPI_REQ *pSMB = NULL;
791 TRANSACTION2_SPI_RSP *pSMBr = NULL;
792 struct unlink_psx_rq *pRqD;
793 int name_len;
794 int rc = 0;
795 int bytes_returned = 0;
796 __u16 params, param_offset, offset, byte_count;
797
Joe Perchesf96637b2013-05-04 22:12:25 -0500798 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000799PsxDelete:
800 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
801 (void **) &pSMBr);
802 if (rc)
803 return rc;
804
805 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
806 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600807 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
808 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000809 name_len++; /* trailing null */
810 name_len *= 2;
811 } else { /* BB add path length overrun check */
812 name_len = strnlen(fileName, PATH_MAX);
813 name_len++; /* trailing null */
814 strncpy(pSMB->FileName, fileName, name_len);
815 }
816
817 params = 6 + name_len;
818 pSMB->MaxParameterCount = cpu_to_le16(2);
819 pSMB->MaxDataCount = 0; /* BB double check this with jra */
820 pSMB->MaxSetupCount = 0;
821 pSMB->Reserved = 0;
822 pSMB->Flags = 0;
823 pSMB->Timeout = 0;
824 pSMB->Reserved2 = 0;
825 param_offset = offsetof(struct smb_com_transaction2_spi_req,
826 InformationLevel) - 4;
827 offset = param_offset + params;
828
829 /* Setup pointer to Request Data (inode type) */
830 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
831 pRqD->type = cpu_to_le16(type);
832 pSMB->ParameterOffset = cpu_to_le16(param_offset);
833 pSMB->DataOffset = cpu_to_le16(offset);
834 pSMB->SetupCount = 1;
835 pSMB->Reserved3 = 0;
836 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
837 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
838
839 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
840 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
841 pSMB->ParameterCount = cpu_to_le16(params);
842 pSMB->TotalParameterCount = pSMB->ParameterCount;
843 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
844 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000845 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000846 pSMB->ByteCount = cpu_to_le16(byte_count);
847 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
848 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000849 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500850 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000851 cifs_buf_release(pSMB);
852
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400853 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000854
855 if (rc == -EAGAIN)
856 goto PsxDelete;
857
858 return rc;
859}
860
861int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700862CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
863 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 DELETE_FILE_REQ *pSMB = NULL;
866 DELETE_FILE_RSP *pSMBr = NULL;
867 int rc = 0;
868 int bytes_returned;
869 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500870 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872DelFileRetry:
873 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
874 (void **) &pSMBr);
875 if (rc)
876 return rc;
877
878 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700879 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
880 PATH_MAX, cifs_sb->local_nls,
881 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 name_len++; /* trailing null */
883 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700884 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700885 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700887 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 pSMB->SearchAttributes =
890 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
891 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000892 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 pSMB->ByteCount = cpu_to_le16(name_len + 1);
894 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
895 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400896 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000897 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500898 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 cifs_buf_release(pSMB);
901 if (rc == -EAGAIN)
902 goto DelFileRetry;
903
904 return rc;
905}
906
907int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400908CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
909 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 DELETE_DIRECTORY_REQ *pSMB = NULL;
912 DELETE_DIRECTORY_RSP *pSMBr = NULL;
913 int rc = 0;
914 int bytes_returned;
915 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500916 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Joe Perchesf96637b2013-05-04 22:12:25 -0500918 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919RmDirRetry:
920 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
921 (void **) &pSMBr);
922 if (rc)
923 return rc;
924
925 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400926 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
927 PATH_MAX, cifs_sb->local_nls,
928 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 name_len++; /* trailing null */
930 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700931 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400932 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400934 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000938 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 pSMB->ByteCount = cpu_to_le16(name_len + 1);
940 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
941 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400942 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000943 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500944 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 cifs_buf_release(pSMB);
947 if (rc == -EAGAIN)
948 goto RmDirRetry;
949 return rc;
950}
951
952int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300953CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
954 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 int rc = 0;
957 CREATE_DIRECTORY_REQ *pSMB = NULL;
958 CREATE_DIRECTORY_RSP *pSMBr = NULL;
959 int bytes_returned;
960 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500961 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Joe Perchesf96637b2013-05-04 22:12:25 -0500963 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964MkDirRetry:
965 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
966 (void **) &pSMBr);
967 if (rc)
968 return rc;
969
970 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600971 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300972 PATH_MAX, cifs_sb->local_nls,
973 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 name_len++; /* trailing null */
975 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700976 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 name_len = strnlen(name, PATH_MAX);
978 name_len++; /* trailing null */
979 strncpy(pSMB->DirName, name, name_len);
980 }
981
982 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000983 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 pSMB->ByteCount = cpu_to_le16(name_len + 1);
985 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
986 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400987 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000988 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500989 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 cifs_buf_release(pSMB);
992 if (rc == -EAGAIN)
993 goto MkDirRetry;
994 return rc;
995}
996
Steve French2dd29d32007-04-23 22:07:35 +0000997int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400998CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
999 __u32 posix_flags, __u64 mode, __u16 *netfid,
1000 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1001 const char *name, const struct nls_table *nls_codepage,
1002 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001003{
1004 TRANSACTION2_SPI_REQ *pSMB = NULL;
1005 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1006 int name_len;
1007 int rc = 0;
1008 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001009 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001010 OPEN_PSX_REQ *pdata;
1011 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001012
Joe Perchesf96637b2013-05-04 22:12:25 -05001013 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001014PsxCreat:
1015 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1016 (void **) &pSMBr);
1017 if (rc)
1018 return rc;
1019
1020 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1021 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001022 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1023 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001024 name_len++; /* trailing null */
1025 name_len *= 2;
1026 } else { /* BB improve the check for buffer overruns BB */
1027 name_len = strnlen(name, PATH_MAX);
1028 name_len++; /* trailing null */
1029 strncpy(pSMB->FileName, name, name_len);
1030 }
1031
1032 params = 6 + name_len;
1033 count = sizeof(OPEN_PSX_REQ);
1034 pSMB->MaxParameterCount = cpu_to_le16(2);
1035 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1036 pSMB->MaxSetupCount = 0;
1037 pSMB->Reserved = 0;
1038 pSMB->Flags = 0;
1039 pSMB->Timeout = 0;
1040 pSMB->Reserved2 = 0;
1041 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001042 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001043 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001044 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001045 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001046 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001047 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001048 pdata->OpenFlags = cpu_to_le32(*pOplock);
1049 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1050 pSMB->DataOffset = cpu_to_le16(offset);
1051 pSMB->SetupCount = 1;
1052 pSMB->Reserved3 = 0;
1053 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1054 byte_count = 3 /* pad */ + params + count;
1055
1056 pSMB->DataCount = cpu_to_le16(count);
1057 pSMB->ParameterCount = cpu_to_le16(params);
1058 pSMB->TotalDataCount = pSMB->DataCount;
1059 pSMB->TotalParameterCount = pSMB->ParameterCount;
1060 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1061 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001062 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001063 pSMB->ByteCount = cpu_to_le16(byte_count);
1064 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1065 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1066 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001067 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001068 goto psx_create_err;
1069 }
1070
Joe Perchesf96637b2013-05-04 22:12:25 -05001071 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001072 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1073
Jeff Layton820a8032011-05-04 08:05:26 -04001074 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001075 rc = -EIO; /* bad smb */
1076 goto psx_create_err;
1077 }
1078
1079 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001080 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001081 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001082
Steve French2dd29d32007-04-23 22:07:35 +00001083 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001084 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001085 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1086 /* Let caller know file was created so we can set the mode. */
1087 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001088 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001089 *pOplock |= CIFS_CREATE_ACTION;
1090 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001091 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1092 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001093 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001094 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001095 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001096 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001097 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001098 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001099 goto psx_create_err;
1100 }
Steve French50c2f752007-07-13 00:33:32 +00001101 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001102 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001103 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001104 }
Steve French2dd29d32007-04-23 22:07:35 +00001105
1106psx_create_err:
1107 cifs_buf_release(pSMB);
1108
Steve French65bc98b2009-07-10 15:27:25 +00001109 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001110 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001111 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001112 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001113
1114 if (rc == -EAGAIN)
1115 goto PsxCreat;
1116
Steve French50c2f752007-07-13 00:33:32 +00001117 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001118}
1119
Steve Frencha9d02ad2005-08-24 23:06:05 -07001120static __u16 convert_disposition(int disposition)
1121{
1122 __u16 ofun = 0;
1123
1124 switch (disposition) {
1125 case FILE_SUPERSEDE:
1126 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1127 break;
1128 case FILE_OPEN:
1129 ofun = SMBOPEN_OAPPEND;
1130 break;
1131 case FILE_CREATE:
1132 ofun = SMBOPEN_OCREATE;
1133 break;
1134 case FILE_OPEN_IF:
1135 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1136 break;
1137 case FILE_OVERWRITE:
1138 ofun = SMBOPEN_OTRUNC;
1139 break;
1140 case FILE_OVERWRITE_IF:
1141 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1142 break;
1143 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001144 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001145 ofun = SMBOPEN_OAPPEND; /* regular open */
1146 }
1147 return ofun;
1148}
1149
Jeff Layton35fc37d2008-05-14 10:22:03 -07001150static int
1151access_flags_to_smbopen_mode(const int access_flags)
1152{
1153 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1154
1155 if (masked_flags == GENERIC_READ)
1156 return SMBOPEN_READ;
1157 else if (masked_flags == GENERIC_WRITE)
1158 return SMBOPEN_WRITE;
1159
1160 /* just go for read/write */
1161 return SMBOPEN_READWRITE;
1162}
1163
Steve Frencha9d02ad2005-08-24 23:06:05 -07001164int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001165SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001166 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001167 const int access_flags, const int create_options, __u16 *netfid,
1168 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001169 const struct nls_table *nls_codepage, int remap)
1170{
1171 int rc = -EACCES;
1172 OPENX_REQ *pSMB = NULL;
1173 OPENX_RSP *pSMBr = NULL;
1174 int bytes_returned;
1175 int name_len;
1176 __u16 count;
1177
1178OldOpenRetry:
1179 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1180 (void **) &pSMBr);
1181 if (rc)
1182 return rc;
1183
1184 pSMB->AndXCommand = 0xFF; /* none */
1185
1186 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1187 count = 1; /* account for one byte pad to word boundary */
1188 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001189 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1190 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001191 name_len++; /* trailing null */
1192 name_len *= 2;
1193 } else { /* BB improve check for buffer overruns BB */
1194 count = 0; /* no pad */
1195 name_len = strnlen(fileName, PATH_MAX);
1196 name_len++; /* trailing null */
1197 strncpy(pSMB->fileName, fileName, name_len);
1198 }
1199 if (*pOplock & REQ_OPLOCK)
1200 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001201 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001202 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001203
Steve Frencha9d02ad2005-08-24 23:06:05 -07001204 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001205 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001206 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1207 /* set file as system file if special file such
1208 as fifo and server expecting SFU style and
1209 no Unix extensions */
1210
Steve French790fe572007-07-07 19:25:05 +00001211 if (create_options & CREATE_OPTION_SPECIAL)
1212 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001213 else /* BB FIXME BB */
1214 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001215
Jeff Layton67750fb2008-05-09 22:28:02 +00001216 if (create_options & CREATE_OPTION_READONLY)
1217 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001218
1219 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001220/* pSMB->CreateOptions = cpu_to_le32(create_options &
1221 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001222 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001223
1224 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001225 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001226 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001227 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001228
1229 pSMB->ByteCount = cpu_to_le16(count);
1230 /* long_op set to 1 to allow for oplock break timeouts */
1231 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001232 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001233 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001234 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001235 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001236 } else {
1237 /* BB verify if wct == 15 */
1238
Steve French582d21e2008-05-13 04:54:12 +00001239/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001240
1241 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1242 /* Let caller know file was created so we can set the mode. */
1243 /* Do we care about the CreateAction in any other cases? */
1244 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001245/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001246 *pOplock |= CIFS_CREATE_ACTION; */
1247 /* BB FIXME END */
1248
Steve French790fe572007-07-07 19:25:05 +00001249 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001250 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1251 pfile_info->LastAccessTime = 0; /* BB fixme */
1252 pfile_info->LastWriteTime = 0; /* BB fixme */
1253 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001254 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001255 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001256 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001257 pfile_info->AllocationSize =
1258 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1259 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001260 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001261 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001262 }
1263 }
1264
1265 cifs_buf_release(pSMB);
1266 if (rc == -EAGAIN)
1267 goto OldOpenRetry;
1268 return rc;
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001272CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1273 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001276 OPEN_REQ *req = NULL;
1277 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int bytes_returned;
1279 int name_len;
1280 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001281 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1282 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001283 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001284 const struct nls_table *nls = cifs_sb->local_nls;
1285 int create_options = oparms->create_options;
1286 int desired_access = oparms->desired_access;
1287 int disposition = oparms->disposition;
1288 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001291 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1292 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (rc)
1294 return rc;
1295
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001296 /* no commands go after this */
1297 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001299 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1300 /* account for one byte pad to word boundary */
1301 count = 1;
1302 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1303 path, PATH_MAX, nls, remap);
1304 /* trailing null */
1305 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001307 req->NameLength = cpu_to_le16(name_len);
1308 } else {
1309 /* BB improve check for buffer overruns BB */
1310 /* no pad */
1311 count = 0;
1312 name_len = strnlen(path, PATH_MAX);
1313 /* trailing null */
1314 name_len++;
1315 req->NameLength = cpu_to_le16(name_len);
1316 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001318
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001319 if (*oplock & REQ_OPLOCK)
1320 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1321 else if (*oplock & REQ_BATCHOPLOCK)
1322 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1323
1324 req->DesiredAccess = cpu_to_le32(desired_access);
1325 req->AllocationSize = 0;
1326
1327 /*
1328 * Set file as system file if special file such as fifo and server
1329 * expecting SFU style and no Unix extensions.
1330 */
1331 if (create_options & CREATE_OPTION_SPECIAL)
1332 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1333 else
1334 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1335
1336 /*
1337 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1338 * sensitive checks for other servers such as Samba.
1339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001341 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Jeff Layton67750fb2008-05-09 22:28:02 +00001343 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001344 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001345
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001346 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1347 req->CreateDisposition = cpu_to_le32(disposition);
1348 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1349
Steve French09d1db52005-04-28 22:41:08 -07001350 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001351 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1352 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001355 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001357 req->ByteCount = cpu_to_le16(count);
1358 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1359 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001360 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001362 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001363 cifs_buf_release(req);
1364 if (rc == -EAGAIN)
1365 goto openRetry;
1366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001368
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001369 /* 1 byte no need to le_to_cpu */
1370 *oplock = rsp->OplockLevel;
1371 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001372 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001373
1374 /* Let caller know file was created so we can set the mode. */
1375 /* Do we care about the CreateAction in any other cases? */
1376 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1377 *oplock |= CIFS_CREATE_ACTION;
1378
1379 if (buf) {
1380 /* copy from CreationTime to Attributes */
1381 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1382 /* the file_info buf is endian converted by caller */
1383 buf->AllocationSize = rsp->AllocationSize;
1384 buf->EndOfFile = rsp->EndOfFile;
1385 buf->NumberOfLinks = cpu_to_le32(1);
1386 buf->DeletePending = 0;
1387 }
1388
1389 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return rc;
1391}
1392
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001393/*
1394 * Discard any remaining data in the current SMB. To do this, we borrow the
1395 * current bigbuf.
1396 */
1397static int
1398cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1399{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001400 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001401 int remaining = rfclen + 4 - server->total_read;
1402 struct cifs_readdata *rdata = mid->callback_data;
1403
1404 while (remaining > 0) {
1405 int length;
1406
1407 length = cifs_read_from_socket(server, server->bigbuf,
1408 min_t(unsigned int, remaining,
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001409 CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001410 if (length < 0)
1411 return length;
1412 server->total_read += length;
1413 remaining -= length;
1414 }
1415
1416 dequeue_mid(mid, rdata->result);
1417 return 0;
1418}
1419
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001420int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001421cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1422{
1423 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001424 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001425 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001426 char *buf = server->smallbuf;
1427 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001428
Joe Perchesf96637b2013-05-04 22:12:25 -05001429 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1430 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001431
1432 /*
1433 * read the rest of READ_RSP header (sans Data array), or whatever we
1434 * can if there's not enough data. At this point, we've read down to
1435 * the Mid.
1436 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001437 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001438 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001439
Jeff Layton58195752012-09-19 06:22:34 -07001440 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1441 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001442
Jeff Layton58195752012-09-19 06:22:34 -07001443 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001444 if (length < 0)
1445 return length;
1446 server->total_read += length;
1447
1448 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001449 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001450 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001451 cifs_dbg(FYI, "%s: server returned error %d\n",
1452 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001453 return cifs_readv_discard(server, mid);
1454 }
1455
1456 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001457 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001458 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1459 __func__, server->total_read,
1460 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001461 rdata->result = -EIO;
1462 return cifs_readv_discard(server, mid);
1463 }
1464
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001465 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001466 if (data_offset < server->total_read) {
1467 /*
1468 * win2k8 sometimes sends an offset of 0 when the read
1469 * is beyond the EOF. Treat it as if the data starts just after
1470 * the header.
1471 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001472 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1473 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001474 data_offset = server->total_read;
1475 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1476 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001477 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1478 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001479 rdata->result = -EIO;
1480 return cifs_readv_discard(server, mid);
1481 }
1482
Joe Perchesf96637b2013-05-04 22:12:25 -05001483 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1484 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001485
1486 len = data_offset - server->total_read;
1487 if (len > 0) {
1488 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001489 rdata->iov.iov_base = buf + server->total_read;
1490 rdata->iov.iov_len = len;
1491 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001492 if (length < 0)
1493 return length;
1494 server->total_read += length;
1495 }
1496
1497 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001498 rdata->iov.iov_base = buf;
1499 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001500 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1501 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001502
1503 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001504 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001505 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001506 /* data_len is corrupt -- discard frame */
1507 rdata->result = -EIO;
1508 return cifs_readv_discard(server, mid);
1509 }
1510
Jeff Layton8321fec2012-09-19 06:22:32 -07001511 length = rdata->read_into_pages(server, rdata, data_len);
1512 if (length < 0)
1513 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001514
Jeff Layton8321fec2012-09-19 06:22:32 -07001515 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001516
Joe Perchesf96637b2013-05-04 22:12:25 -05001517 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1518 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001519
1520 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001521 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001522 return cifs_readv_discard(server, mid);
1523
1524 dequeue_mid(mid, false);
1525 return length;
1526}
1527
1528static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001529cifs_readv_callback(struct mid_q_entry *mid)
1530{
1531 struct cifs_readdata *rdata = mid->callback_data;
1532 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1533 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001534 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1535 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001536 .rq_pages = rdata->pages,
1537 .rq_npages = rdata->nr_pages,
1538 .rq_pagesz = rdata->pagesz,
1539 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001540
Joe Perchesf96637b2013-05-04 22:12:25 -05001541 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1542 __func__, mid->mid, mid->mid_state, rdata->result,
1543 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001544
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001545 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001546 case MID_RESPONSE_RECEIVED:
1547 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001548 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001549 int rc = 0;
1550
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001551 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001552 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001553 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001554 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1555 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001556 }
1557 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001558 task_io_account_read(rdata->got_bytes);
1559 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001560 break;
1561 case MID_REQUEST_SUBMITTED:
1562 case MID_RETRY_NEEDED:
1563 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001564 if (server->sign && rdata->got_bytes)
1565 /* reset bytes number since we can not check a sign */
1566 rdata->got_bytes = 0;
1567 /* FIXME: should this be counted toward the initiating task? */
1568 task_io_account_read(rdata->got_bytes);
1569 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001570 break;
1571 default:
1572 rdata->result = -EIO;
1573 }
1574
Jeff Laytonda472fc2012-03-23 14:40:53 -04001575 queue_work(cifsiod_wq, &rdata->work);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001576 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001577 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001578}
1579
1580/* cifs_async_readv - send an async write, and set up mid to handle result */
1581int
1582cifs_async_readv(struct cifs_readdata *rdata)
1583{
1584 int rc;
1585 READ_REQ *smb = NULL;
1586 int wct;
1587 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001588 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001589 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001590
Joe Perchesf96637b2013-05-04 22:12:25 -05001591 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1592 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593
1594 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1595 wct = 12;
1596 else {
1597 wct = 10; /* old style read */
1598 if ((rdata->offset >> 32) > 0) {
1599 /* can not handle this big offset for old */
1600 return -EIO;
1601 }
1602 }
1603
1604 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1605 if (rc)
1606 return rc;
1607
1608 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1609 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1610
1611 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001612 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001613 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1614 if (wct == 12)
1615 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1616 smb->Remaining = 0;
1617 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1618 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1619 if (wct == 12)
1620 smb->ByteCount = 0;
1621 else {
1622 /* old style read */
1623 struct smb_com_readx_req *smbr =
1624 (struct smb_com_readx_req *)smb;
1625 smbr->ByteCount = 0;
1626 }
1627
1628 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001629 rdata->iov.iov_base = smb;
1630 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001631
Jeff Layton6993f742012-05-16 07:13:17 -04001632 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001633 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1634 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001635
1636 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001637 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001638 else
1639 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001640
1641 cifs_small_buf_release(smb);
1642 return rc;
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001646CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1647 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 int rc = -EACCES;
1650 READ_REQ *pSMB = NULL;
1651 READ_RSP *pSMBr = NULL;
1652 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001653 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001654 int resp_buf_type = 0;
1655 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001656 __u32 pid = io_parms->pid;
1657 __u16 netfid = io_parms->netfid;
1658 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001659 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001660 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Joe Perchesf96637b2013-05-04 22:12:25 -05001662 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001663 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001664 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001665 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001666 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001667 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001668 /* can not handle this big offset for old */
1669 return -EIO;
1670 }
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001674 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (rc)
1676 return rc;
1677
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001678 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1679 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /* tcon and ses pointer are checked in smb_init */
1682 if (tcon->ses->server == NULL)
1683 return -ECONNABORTED;
1684
Steve Frenchec637e32005-12-12 20:53:18 -08001685 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001687 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001688 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001689 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 pSMB->Remaining = 0;
1692 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1693 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001694 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001695 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1696 else {
1697 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001698 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001699 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001700 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001701 }
Steve Frenchec637e32005-12-12 20:53:18 -08001702
1703 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001704 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001705 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001706 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001707 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001708 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001710 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 } else {
1712 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1713 data_length = data_length << 16;
1714 data_length += le16_to_cpu(pSMBr->DataLength);
1715 *nbytes = data_length;
1716
1717 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001718 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001720 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001721 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 rc = -EIO;
1723 *nbytes = 0;
1724 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001725 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001726 le16_to_cpu(pSMBr->DataOffset);
1727/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001728 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001729 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001730 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001731 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001732 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Steve French4b8f9302006-02-26 16:41:18 +00001736/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001737 if (*buf) {
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01001738 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001739 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001740 /* return buffer to caller to free */
1741 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001742 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001743 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001744 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001745 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001746 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001747
1748 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 since file handle passed in no longer valid */
1750 return rc;
1751}
1752
Steve Frenchec637e32005-12-12 20:53:18 -08001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001755CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001756 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001757 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 int rc = -EACCES;
1760 WRITE_REQ *pSMB = NULL;
1761 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001762 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 __u32 bytes_sent;
1764 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001765 __u32 pid = io_parms->pid;
1766 __u16 netfid = io_parms->netfid;
1767 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001768 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001769 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Steve Frencha24e2d72010-04-03 17:20:21 +00001771 *nbytes = 0;
1772
Joe Perchesf96637b2013-05-04 22:12:25 -05001773 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001774 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001775 return -ECONNABORTED;
1776
Steve French790fe572007-07-07 19:25:05 +00001777 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001778 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001779 else {
Steve French1c955182005-08-30 20:58:07 -07001780 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001781 if ((offset >> 32) > 0) {
1782 /* can not handle big offset for old srv */
1783 return -EIO;
1784 }
1785 }
Steve French1c955182005-08-30 20:58:07 -07001786
1787 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 (void **) &pSMBr);
1789 if (rc)
1790 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001791
1792 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1793 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* tcon and ses pointer are checked in smb_init */
1796 if (tcon->ses->server == NULL)
1797 return -ECONNABORTED;
1798
1799 pSMB->AndXCommand = 0xFF; /* none */
1800 pSMB->Fid = netfid;
1801 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001802 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001803 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 pSMB->Reserved = 0xFFFFFFFF;
1806 pSMB->WriteMode = 0;
1807 pSMB->Remaining = 0;
1808
Steve French50c2f752007-07-13 00:33:32 +00001809 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 can send more if LARGE_WRITE_X capability returned by the server and if
1811 our buffer is big enough or if we convert to iovecs on socket writes
1812 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001813 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1815 } else {
1816 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1817 & ~0xFF;
1818 }
1819
1820 if (bytes_sent > count)
1821 bytes_sent = count;
1822 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001823 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001824 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001825 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001826 else if (ubuf) {
1827 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 cifs_buf_release(pSMB);
1829 return -EFAULT;
1830 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001831 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* No buffer */
1833 cifs_buf_release(pSMB);
1834 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001835 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001836 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001837 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001838 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001839 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1842 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001843 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001844
Steve French790fe572007-07-07 19:25:05 +00001845 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001846 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001847 else { /* old style write has byte count 4 bytes earlier
1848 so 4 bytes pad */
1849 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001850 (struct smb_com_writex_req *)pSMB;
1851 pSMBW->ByteCount = cpu_to_le16(byte_count);
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1855 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001856 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001858 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 } else {
1860 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1861 *nbytes = (*nbytes) << 16;
1862 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301863
1864 /*
1865 * Mask off high 16 bits when bytes written as returned by the
1866 * server is greater than bytes requested by the client. Some
1867 * OS/2 servers are known to set incorrect CountHigh values.
1868 */
1869 if (*nbytes > count)
1870 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
1873 cifs_buf_release(pSMB);
1874
Steve French50c2f752007-07-13 00:33:32 +00001875 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 since file handle passed in no longer valid */
1877
1878 return rc;
1879}
1880
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001881void
1882cifs_writedata_release(struct kref *refcount)
1883{
1884 struct cifs_writedata *wdata = container_of(refcount,
1885 struct cifs_writedata, refcount);
1886
1887 if (wdata->cfile)
1888 cifsFileInfo_put(wdata->cfile);
1889
1890 kfree(wdata);
1891}
1892
1893/*
1894 * Write failed with a retryable error. Resend the write request. It's also
1895 * possible that the page was redirtied so re-clean the page.
1896 */
1897static void
1898cifs_writev_requeue(struct cifs_writedata *wdata)
1899{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001900 int i, rc = 0;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001901 struct inode *inode = wdata->cfile->dentry->d_inode;
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001902 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001903 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001904
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001905 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1906 i = 0;
1907 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001908 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001909 struct cifs_writedata *wdata2;
1910 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001911
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001912 wsize = server->ops->wp_retry_size(inode);
1913 if (wsize < rest_len) {
1914 nr_pages = wsize / PAGE_CACHE_SIZE;
1915 if (!nr_pages) {
1916 rc = -ENOTSUPP;
1917 break;
1918 }
1919 cur_len = nr_pages * PAGE_CACHE_SIZE;
1920 tailsz = PAGE_CACHE_SIZE;
1921 } else {
1922 nr_pages = DIV_ROUND_UP(rest_len, PAGE_CACHE_SIZE);
1923 cur_len = rest_len;
1924 tailsz = rest_len - (nr_pages - 1) * PAGE_CACHE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001925 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001926
1927 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1928 if (!wdata2) {
1929 rc = -ENOMEM;
1930 break;
1931 }
1932
1933 for (j = 0; j < nr_pages; j++) {
1934 wdata2->pages[j] = wdata->pages[i + j];
1935 lock_page(wdata2->pages[j]);
1936 clear_page_dirty_for_io(wdata2->pages[j]);
1937 }
1938
1939 wdata2->sync_mode = wdata->sync_mode;
1940 wdata2->nr_pages = nr_pages;
1941 wdata2->offset = page_offset(wdata2->pages[0]);
1942 wdata2->pagesz = PAGE_CACHE_SIZE;
1943 wdata2->tailsz = tailsz;
1944 wdata2->bytes = cur_len;
1945
1946 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1947 if (!wdata2->cfile) {
1948 cifs_dbg(VFS, "No writable handles for inode\n");
1949 rc = -EBADF;
1950 break;
1951 }
1952 wdata2->pid = wdata2->cfile->pid;
1953 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1954
1955 for (j = 0; j < nr_pages; j++) {
1956 unlock_page(wdata2->pages[j]);
1957 if (rc != 0 && rc != -EAGAIN) {
1958 SetPageError(wdata2->pages[j]);
1959 end_page_writeback(wdata2->pages[j]);
1960 page_cache_release(wdata2->pages[j]);
1961 }
1962 }
1963
1964 if (rc) {
1965 kref_put(&wdata2->refcount, cifs_writedata_release);
1966 if (rc == -EAGAIN)
1967 continue;
1968 break;
1969 }
1970
1971 rest_len -= cur_len;
1972 i += nr_pages;
1973 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001974
1975 mapping_set_error(inode->i_mapping, rc);
1976 kref_put(&wdata->refcount, cifs_writedata_release);
1977}
1978
Jeff Laytonc2e87642012-03-23 14:40:55 -04001979void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001980cifs_writev_complete(struct work_struct *work)
1981{
1982 struct cifs_writedata *wdata = container_of(work,
1983 struct cifs_writedata, work);
1984 struct inode *inode = wdata->cfile->dentry->d_inode;
1985 int i = 0;
1986
1987 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04001988 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001989 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04001990 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001991 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
1992 wdata->bytes);
1993 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
1994 return cifs_writev_requeue(wdata);
1995
1996 for (i = 0; i < wdata->nr_pages; i++) {
1997 struct page *page = wdata->pages[i];
1998 if (wdata->result == -EAGAIN)
1999 __set_page_dirty_nobuffers(page);
2000 else if (wdata->result < 0)
2001 SetPageError(page);
2002 end_page_writeback(page);
2003 page_cache_release(page);
2004 }
2005 if (wdata->result != -EAGAIN)
2006 mapping_set_error(inode->i_mapping, wdata->result);
2007 kref_put(&wdata->refcount, cifs_writedata_release);
2008}
2009
2010struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002011cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002012{
2013 struct cifs_writedata *wdata;
2014
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002015 /* writedata + number of page pointers */
2016 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002017 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002018 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002019 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002020 INIT_LIST_HEAD(&wdata->list);
2021 init_completion(&wdata->done);
2022 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002023 }
2024 return wdata;
2025}
2026
2027/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002028 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002029 * workqueue completion task.
2030 */
2031static void
2032cifs_writev_callback(struct mid_q_entry *mid)
2033{
2034 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002035 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002036 unsigned int written;
2037 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2038
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002039 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002040 case MID_RESPONSE_RECEIVED:
2041 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2042 if (wdata->result != 0)
2043 break;
2044
2045 written = le16_to_cpu(smb->CountHigh);
2046 written <<= 16;
2047 written += le16_to_cpu(smb->Count);
2048 /*
2049 * Mask off high 16 bits when bytes written as returned
2050 * by the server is greater than bytes requested by the
2051 * client. OS/2 servers are known to set incorrect
2052 * CountHigh values.
2053 */
2054 if (written > wdata->bytes)
2055 written &= 0xFFFF;
2056
2057 if (written < wdata->bytes)
2058 wdata->result = -ENOSPC;
2059 else
2060 wdata->bytes = written;
2061 break;
2062 case MID_REQUEST_SUBMITTED:
2063 case MID_RETRY_NEEDED:
2064 wdata->result = -EAGAIN;
2065 break;
2066 default:
2067 wdata->result = -EIO;
2068 break;
2069 }
2070
Jeff Laytonda472fc2012-03-23 14:40:53 -04002071 queue_work(cifsiod_wq, &wdata->work);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002072 DeleteMidQEntry(mid);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002073 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002074}
2075
2076/* cifs_async_writev - send an async write, and set up mid to handle result */
2077int
Steve French4a5c80d2014-02-07 20:45:12 -06002078cifs_async_writev(struct cifs_writedata *wdata,
2079 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002080{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002081 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002082 WRITE_REQ *smb = NULL;
2083 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002084 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002085 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002086 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002087
2088 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2089 wct = 14;
2090 } else {
2091 wct = 12;
2092 if (wdata->offset >> 32 > 0) {
2093 /* can not handle big offset for old srv */
2094 return -EIO;
2095 }
2096 }
2097
2098 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2099 if (rc)
2100 goto async_writev_out;
2101
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002102 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2103 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002104
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002105 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002106 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002107 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2108 if (wct == 14)
2109 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2110 smb->Reserved = 0xFFFFFFFF;
2111 smb->WriteMode = 0;
2112 smb->Remaining = 0;
2113
2114 smb->DataOffset =
2115 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2116
2117 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002118 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2119 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002120
Jeff Laytoneddb0792012-09-18 16:20:35 -07002121 rqst.rq_iov = &iov;
2122 rqst.rq_nvec = 1;
2123 rqst.rq_pages = wdata->pages;
2124 rqst.rq_npages = wdata->nr_pages;
2125 rqst.rq_pagesz = wdata->pagesz;
2126 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002127
Joe Perchesf96637b2013-05-04 22:12:25 -05002128 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2129 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002130
2131 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2132 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2133
2134 if (wct == 14) {
2135 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2136 put_bcc(wdata->bytes + 1, &smb->hdr);
2137 } else {
2138 /* wct == 12 */
2139 struct smb_com_writex_req *smbw =
2140 (struct smb_com_writex_req *)smb;
2141 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2142 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002143 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002144 }
2145
2146 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002147 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2148 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002149
2150 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002151 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002152 else
Steve French4a5c80d2014-02-07 20:45:12 -06002153 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002154
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002155async_writev_out:
2156 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002157 return rc;
2158}
2159
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002160int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002161CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002162 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 int rc = -EACCES;
2165 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002166 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002167 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002168 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002169 __u32 pid = io_parms->pid;
2170 __u16 netfid = io_parms->netfid;
2171 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002172 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002173 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002175 *nbytes = 0;
2176
Joe Perchesf96637b2013-05-04 22:12:25 -05002177 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002178
Steve French4c3130e2008-12-09 00:28:16 +00002179 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002180 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002181 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002182 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002183 if ((offset >> 32) > 0) {
2184 /* can not handle big offset for old srv */
2185 return -EIO;
2186 }
2187 }
Steve French8cc64c62005-10-03 13:49:43 -07002188 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (rc)
2190 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002191
2192 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2193 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* tcon and ses pointer are checked in smb_init */
2196 if (tcon->ses->server == NULL)
2197 return -ECONNABORTED;
2198
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002199 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 pSMB->Fid = netfid;
2201 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002202 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002203 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 pSMB->Reserved = 0xFFFFFFFF;
2205 pSMB->WriteMode = 0;
2206 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002209 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Steve French3e844692005-10-03 13:37:24 -07002211 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2212 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002213 /* header + 1 byte pad */
2214 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002215 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002216 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002217 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002218 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002219 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002220 pSMB->ByteCount = cpu_to_le16(count + 1);
2221 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002222 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002223 (struct smb_com_writex_req *)pSMB;
2224 pSMBW->ByteCount = cpu_to_le16(count + 5);
2225 }
Steve French3e844692005-10-03 13:37:24 -07002226 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002227 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002228 iov[0].iov_len = smb_hdr_len + 4;
2229 else /* wct == 12 pad bigger by four bytes */
2230 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002231
Steve French3e844692005-10-03 13:37:24 -07002232
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002233 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002234 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002236 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002237 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002238 /* presumably this can not happen, but best to be safe */
2239 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002240 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002241 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002242 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2243 *nbytes = (*nbytes) << 16;
2244 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302245
2246 /*
2247 * Mask off high 16 bits when bytes written as returned by the
2248 * server is greater than bytes requested by the client. OS/2
2249 * servers are known to set incorrect CountHigh values.
2250 */
2251 if (*nbytes > count)
2252 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Steve French4b8f9302006-02-26 16:41:18 +00002255/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002256 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Steve French50c2f752007-07-13 00:33:32 +00002258 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 since file handle passed in no longer valid */
2260
2261 return rc;
2262}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002263
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002264int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2265 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002266 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2267{
2268 int rc = 0;
2269 LOCK_REQ *pSMB = NULL;
2270 struct kvec iov[2];
2271 int resp_buf_type;
2272 __u16 count;
2273
Joe Perchesf96637b2013-05-04 22:12:25 -05002274 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2275 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002276
2277 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2278 if (rc)
2279 return rc;
2280
2281 pSMB->Timeout = 0;
2282 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2283 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2284 pSMB->LockType = lock_type;
2285 pSMB->AndXCommand = 0xFF; /* none */
2286 pSMB->Fid = netfid; /* netfid stays le */
2287
2288 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2289 inc_rfc1001_len(pSMB, count);
2290 pSMB->ByteCount = cpu_to_le16(count);
2291
2292 iov[0].iov_base = (char *)pSMB;
2293 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2294 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2295 iov[1].iov_base = (char *)buf;
2296 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2297
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002298 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002299 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2300 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002301 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002302
2303 return rc;
2304}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002307CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002308 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002310 const __u32 numLock, const __u8 lockType,
2311 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 int rc = 0;
2314 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002315/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002317 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 __u16 count;
2319
Joe Perchesf96637b2013-05-04 22:12:25 -05002320 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2321 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002322 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (rc)
2325 return rc;
2326
Steve French790fe572007-07-07 19:25:05 +00002327 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002328 /* no response expected */
2329 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002331 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002332 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2334 } else {
2335 pSMB->Timeout = 0;
2336 }
2337
2338 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2339 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2340 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002341 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 pSMB->AndXCommand = 0xFF; /* none */
2343 pSMB->Fid = smb_file_id; /* netfid stays le */
2344
Steve French790fe572007-07-07 19:25:05 +00002345 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002346 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /* BB where to store pid high? */
2348 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2349 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2350 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2351 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2352 count = sizeof(LOCKING_ANDX_RANGE);
2353 } else {
2354 /* oplock break */
2355 count = 0;
2356 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002357 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 pSMB->ByteCount = cpu_to_le16(count);
2359
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002360 if (waitFlag) {
2361 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002362 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002363 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002364 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002365 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002366 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002367 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002368 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002369 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002370 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Steve French50c2f752007-07-13 00:33:32 +00002372 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 since file handle passed in no longer valid */
2374 return rc;
2375}
2376
2377int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002378CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002379 const __u16 smb_file_id, const __u32 netpid,
2380 const loff_t start_offset, const __u64 len,
2381 struct file_lock *pLockData, const __u16 lock_type,
2382 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002383{
2384 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2385 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002386 struct cifs_posix_lock *parm_data;
2387 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002388 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002389 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002390 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002391 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002392 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002393
Joe Perchesf96637b2013-05-04 22:12:25 -05002394 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002395
Steve French08547b02006-02-28 22:39:25 +00002396 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2397
2398 if (rc)
2399 return rc;
2400
2401 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2402
Steve French50c2f752007-07-13 00:33:32 +00002403 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002404 pSMB->MaxSetupCount = 0;
2405 pSMB->Reserved = 0;
2406 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002407 pSMB->Reserved2 = 0;
2408 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2409 offset = param_offset + params;
2410
Steve French08547b02006-02-28 22:39:25 +00002411 count = sizeof(struct cifs_posix_lock);
2412 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002413 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002414 pSMB->SetupCount = 1;
2415 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002416 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002417 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2418 else
2419 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2420 byte_count = 3 /* pad */ + params + count;
2421 pSMB->DataCount = cpu_to_le16(count);
2422 pSMB->ParameterCount = cpu_to_le16(params);
2423 pSMB->TotalDataCount = pSMB->DataCount;
2424 pSMB->TotalParameterCount = pSMB->ParameterCount;
2425 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002426 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002427 (((char *) &pSMB->hdr.Protocol) + offset);
2428
2429 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002430 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002431 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002432 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002433 pSMB->Timeout = cpu_to_le32(-1);
2434 } else
2435 pSMB->Timeout = 0;
2436
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002437 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002438 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002439 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002440
2441 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002442 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002443 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2444 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002445 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002446 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002447 if (waitFlag) {
2448 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2449 (struct smb_hdr *) pSMBr, &bytes_returned);
2450 } else {
Steve French133672e2007-11-13 22:41:37 +00002451 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002452 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002453 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2454 &resp_buf_type, timeout);
2455 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2456 not try to free it twice below on exit */
2457 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002458 }
2459
Steve French08547b02006-02-28 22:39:25 +00002460 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002461 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002462 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002463 /* lock structure can be returned on get */
2464 __u16 data_offset;
2465 __u16 data_count;
2466 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002467
Jeff Layton820a8032011-05-04 08:05:26 -04002468 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002469 rc = -EIO; /* bad smb */
2470 goto plk_err_exit;
2471 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002472 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2473 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002474 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002475 rc = -EIO;
2476 goto plk_err_exit;
2477 }
2478 parm_data = (struct cifs_posix_lock *)
2479 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002480 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002481 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002482 else {
2483 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002484 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002485 pLockData->fl_type = F_RDLCK;
2486 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002487 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002488 pLockData->fl_type = F_WRLCK;
2489
Steve French5443d132011-03-13 05:08:25 +00002490 pLockData->fl_start = le64_to_cpu(parm_data->start);
2491 pLockData->fl_end = pLockData->fl_start +
2492 le64_to_cpu(parm_data->length) - 1;
2493 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002494 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002495 }
Steve French50c2f752007-07-13 00:33:32 +00002496
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002497plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002498 if (pSMB)
2499 cifs_small_buf_release(pSMB);
2500
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002501 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French133672e2007-11-13 22:41:37 +00002502
Steve French08547b02006-02-28 22:39:25 +00002503 /* Note: On -EAGAIN error only caller can retry on handle based calls
2504 since file handle passed in no longer valid */
2505
2506 return rc;
2507}
2508
2509
2510int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002511CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 int rc = 0;
2514 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002515 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517/* do not retry on dead session on close */
2518 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002519 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return 0;
2521 if (rc)
2522 return rc;
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002525 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002527 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002528 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002530 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002532 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534 }
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002537 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 rc = 0;
2539
2540 return rc;
2541}
2542
2543int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002544CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002545{
2546 int rc = 0;
2547 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002548 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002549
2550 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2551 if (rc)
2552 return rc;
2553
2554 pSMB->FileID = (__u16) smb_file_id;
2555 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002556 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002557 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002558 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002559 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002560
2561 return rc;
2562}
2563
2564int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002565CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002566 const char *from_name, const char *to_name,
2567 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 int rc = 0;
2570 RENAME_REQ *pSMB = NULL;
2571 RENAME_RSP *pSMBr = NULL;
2572 int bytes_returned;
2573 int name_len, name_len2;
2574 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002575 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Joe Perchesf96637b2013-05-04 22:12:25 -05002577 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578renameRetry:
2579 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2580 (void **) &pSMBr);
2581 if (rc)
2582 return rc;
2583
2584 pSMB->BufferFormat = 0x04;
2585 pSMB->SearchAttributes =
2586 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2587 ATTR_DIRECTORY);
2588
2589 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002590 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2591 from_name, PATH_MAX,
2592 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 name_len++; /* trailing null */
2594 name_len *= 2;
2595 pSMB->OldFileName[name_len] = 0x04; /* pad */
2596 /* protocol requires ASCII signature byte on Unicode string */
2597 pSMB->OldFileName[name_len + 1] = 0x00;
2598 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002599 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002600 to_name, PATH_MAX, cifs_sb->local_nls,
2601 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2603 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002604 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002605 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002607 strncpy(pSMB->OldFileName, from_name, name_len);
2608 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 name_len2++; /* trailing null */
2610 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002611 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 name_len2++; /* trailing null */
2613 name_len2++; /* signature byte */
2614 }
2615
2616 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002617 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 pSMB->ByteCount = cpu_to_le16(count);
2619
2620 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2621 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002622 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002623 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002624 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 cifs_buf_release(pSMB);
2627
2628 if (rc == -EAGAIN)
2629 goto renameRetry;
2630
2631 return rc;
2632}
2633
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002634int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002635 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002636 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2639 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002640 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 char *data_offset;
2642 char dummy_string[30];
2643 int rc = 0;
2644 int bytes_returned = 0;
2645 int len_of_str;
2646 __u16 params, param_offset, offset, count, byte_count;
2647
Joe Perchesf96637b2013-05-04 22:12:25 -05002648 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2650 (void **) &pSMBr);
2651 if (rc)
2652 return rc;
2653
2654 params = 6;
2655 pSMB->MaxSetupCount = 0;
2656 pSMB->Reserved = 0;
2657 pSMB->Flags = 0;
2658 pSMB->Timeout = 0;
2659 pSMB->Reserved2 = 0;
2660 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2661 offset = param_offset + params;
2662
2663 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2664 rename_info = (struct set_file_rename *) data_offset;
2665 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002666 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 pSMB->SetupCount = 1;
2668 pSMB->Reserved3 = 0;
2669 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2670 byte_count = 3 /* pad */ + params;
2671 pSMB->ParameterCount = cpu_to_le16(params);
2672 pSMB->TotalParameterCount = pSMB->ParameterCount;
2673 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2674 pSMB->DataOffset = cpu_to_le16(offset);
2675 /* construct random name ".cifs_tmp<inodenum><mid>" */
2676 rename_info->overwrite = cpu_to_le32(1);
2677 rename_info->root_fid = 0;
2678 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002679 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002680 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002681 len_of_str =
2682 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002683 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002685 len_of_str =
2686 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002687 target_name, PATH_MAX, nls_codepage,
2688 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002691 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 byte_count += count;
2693 pSMB->DataCount = cpu_to_le16(count);
2694 pSMB->TotalDataCount = pSMB->DataCount;
2695 pSMB->Fid = netfid;
2696 pSMB->InformationLevel =
2697 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2698 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002699 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 pSMB->ByteCount = cpu_to_le16(byte_count);
2701 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002702 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002703 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002704 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002705 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2706 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 cifs_buf_release(pSMB);
2709
2710 /* Note: On -EAGAIN error only caller can retry on handle based calls
2711 since file handle passed in no longer valid */
2712
2713 return rc;
2714}
2715
2716int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002717CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2718 const char *fromName, const __u16 target_tid, const char *toName,
2719 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 int rc = 0;
2722 COPY_REQ *pSMB = NULL;
2723 COPY_RSP *pSMBr = NULL;
2724 int bytes_returned;
2725 int name_len, name_len2;
2726 __u16 count;
2727
Joe Perchesf96637b2013-05-04 22:12:25 -05002728 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729copyRetry:
2730 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2731 (void **) &pSMBr);
2732 if (rc)
2733 return rc;
2734
2735 pSMB->BufferFormat = 0x04;
2736 pSMB->Tid2 = target_tid;
2737
2738 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2739
2740 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002741 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2742 fromName, PATH_MAX, nls_codepage,
2743 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 name_len++; /* trailing null */
2745 name_len *= 2;
2746 pSMB->OldFileName[name_len] = 0x04; /* pad */
2747 /* protocol requires ASCII signature byte on Unicode string */
2748 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002749 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002750 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2751 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2753 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002754 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 name_len = strnlen(fromName, PATH_MAX);
2756 name_len++; /* trailing null */
2757 strncpy(pSMB->OldFileName, fromName, name_len);
2758 name_len2 = strnlen(toName, PATH_MAX);
2759 name_len2++; /* trailing null */
2760 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2761 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2762 name_len2++; /* trailing null */
2763 name_len2++; /* signature byte */
2764 }
2765
2766 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002767 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 pSMB->ByteCount = cpu_to_le16(count);
2769
2770 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2771 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2772 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002773 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2774 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Steve French0d817bc2008-05-22 02:02:03 +00002776 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 if (rc == -EAGAIN)
2779 goto copyRetry;
2780
2781 return rc;
2782}
2783
2784int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002785CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 const char *fromName, const char *toName,
2787 const struct nls_table *nls_codepage)
2788{
2789 TRANSACTION2_SPI_REQ *pSMB = NULL;
2790 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2791 char *data_offset;
2792 int name_len;
2793 int name_len_target;
2794 int rc = 0;
2795 int bytes_returned = 0;
2796 __u16 params, param_offset, offset, byte_count;
2797
Joe Perchesf96637b2013-05-04 22:12:25 -05002798 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799createSymLinkRetry:
2800 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2801 (void **) &pSMBr);
2802 if (rc)
2803 return rc;
2804
2805 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2806 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06002807 cifs_strtoUTF16((__le16 *) pSMB->FileName, fromName,
2808 /* find define for this maxpathcomponent */
2809 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 name_len++; /* trailing null */
2811 name_len *= 2;
2812
Steve French50c2f752007-07-13 00:33:32 +00002813 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 name_len = strnlen(fromName, PATH_MAX);
2815 name_len++; /* trailing null */
2816 strncpy(pSMB->FileName, fromName, name_len);
2817 }
2818 params = 6 + name_len;
2819 pSMB->MaxSetupCount = 0;
2820 pSMB->Reserved = 0;
2821 pSMB->Flags = 0;
2822 pSMB->Timeout = 0;
2823 pSMB->Reserved2 = 0;
2824 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002825 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 offset = param_offset + params;
2827
2828 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2829 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2830 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002831 cifs_strtoUTF16((__le16 *) data_offset, toName, PATH_MAX
2832 /* find define for this maxpathcomponent */
2833 , nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 name_len_target++; /* trailing null */
2835 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002836 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 name_len_target = strnlen(toName, PATH_MAX);
2838 name_len_target++; /* trailing null */
2839 strncpy(data_offset, toName, name_len_target);
2840 }
2841
2842 pSMB->MaxParameterCount = cpu_to_le16(2);
2843 /* BB find exact max on data count below from sess */
2844 pSMB->MaxDataCount = cpu_to_le16(1000);
2845 pSMB->SetupCount = 1;
2846 pSMB->Reserved3 = 0;
2847 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2848 byte_count = 3 /* pad */ + params + name_len_target;
2849 pSMB->DataCount = cpu_to_le16(name_len_target);
2850 pSMB->ParameterCount = cpu_to_le16(params);
2851 pSMB->TotalDataCount = pSMB->DataCount;
2852 pSMB->TotalParameterCount = pSMB->ParameterCount;
2853 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2854 pSMB->DataOffset = cpu_to_le16(offset);
2855 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2856 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002857 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 pSMB->ByteCount = cpu_to_le16(byte_count);
2859 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2860 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002861 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002862 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002863 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2864 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Steve French0d817bc2008-05-22 02:02:03 +00002866 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (rc == -EAGAIN)
2869 goto createSymLinkRetry;
2870
2871 return rc;
2872}
2873
2874int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002875CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002877 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
2879 TRANSACTION2_SPI_REQ *pSMB = NULL;
2880 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2881 char *data_offset;
2882 int name_len;
2883 int name_len_target;
2884 int rc = 0;
2885 int bytes_returned = 0;
2886 __u16 params, param_offset, offset, byte_count;
2887
Joe Perchesf96637b2013-05-04 22:12:25 -05002888 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889createHardLinkRetry:
2890 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2891 (void **) &pSMBr);
2892 if (rc)
2893 return rc;
2894
2895 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002896 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2897 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 name_len++; /* trailing null */
2899 name_len *= 2;
2900
Steve French50c2f752007-07-13 00:33:32 +00002901 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 name_len = strnlen(toName, PATH_MAX);
2903 name_len++; /* trailing null */
2904 strncpy(pSMB->FileName, toName, name_len);
2905 }
2906 params = 6 + name_len;
2907 pSMB->MaxSetupCount = 0;
2908 pSMB->Reserved = 0;
2909 pSMB->Flags = 0;
2910 pSMB->Timeout = 0;
2911 pSMB->Reserved2 = 0;
2912 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002913 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 offset = param_offset + params;
2915
2916 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2917 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2918 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002919 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2920 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 name_len_target++; /* trailing null */
2922 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002923 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 name_len_target = strnlen(fromName, PATH_MAX);
2925 name_len_target++; /* trailing null */
2926 strncpy(data_offset, fromName, name_len_target);
2927 }
2928
2929 pSMB->MaxParameterCount = cpu_to_le16(2);
2930 /* BB find exact max on data count below from sess*/
2931 pSMB->MaxDataCount = cpu_to_le16(1000);
2932 pSMB->SetupCount = 1;
2933 pSMB->Reserved3 = 0;
2934 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2935 byte_count = 3 /* pad */ + params + name_len_target;
2936 pSMB->ParameterCount = cpu_to_le16(params);
2937 pSMB->TotalParameterCount = pSMB->ParameterCount;
2938 pSMB->DataCount = cpu_to_le16(name_len_target);
2939 pSMB->TotalDataCount = pSMB->DataCount;
2940 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2941 pSMB->DataOffset = cpu_to_le16(offset);
2942 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2943 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002944 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 pSMB->ByteCount = cpu_to_le16(byte_count);
2946 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2947 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002948 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002949 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002950 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2951 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 cifs_buf_release(pSMB);
2954 if (rc == -EAGAIN)
2955 goto createHardLinkRetry;
2956
2957 return rc;
2958}
2959
2960int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002961CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002962 const char *from_name, const char *to_name,
2963 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
2965 int rc = 0;
2966 NT_RENAME_REQ *pSMB = NULL;
2967 RENAME_RSP *pSMBr = NULL;
2968 int bytes_returned;
2969 int name_len, name_len2;
2970 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002971 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Joe Perchesf96637b2013-05-04 22:12:25 -05002973 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974winCreateHardLinkRetry:
2975
2976 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2977 (void **) &pSMBr);
2978 if (rc)
2979 return rc;
2980
2981 pSMB->SearchAttributes =
2982 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2983 ATTR_DIRECTORY);
2984 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
2985 pSMB->ClusterCount = 0;
2986
2987 pSMB->BufferFormat = 0x04;
2988
2989 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2990 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07002991 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
2992 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 name_len++; /* trailing null */
2994 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05002995
2996 /* protocol specifies ASCII buffer format (0x04) for unicode */
2997 pSMB->OldFileName[name_len] = 0x04;
2998 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003000 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003001 to_name, PATH_MAX, cifs_sb->local_nls,
3002 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3004 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003005 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003006 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003008 strncpy(pSMB->OldFileName, from_name, name_len);
3009 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 name_len2++; /* trailing null */
3011 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003012 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 name_len2++; /* trailing null */
3014 name_len2++; /* signature byte */
3015 }
3016
3017 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003018 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 pSMB->ByteCount = cpu_to_le16(count);
3020
3021 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3022 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003023 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003024 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003025 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 cifs_buf_release(pSMB);
3028 if (rc == -EAGAIN)
3029 goto winCreateHardLinkRetry;
3030
3031 return rc;
3032}
3033
3034int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003035CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003036 const unsigned char *searchName, char **symlinkinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 const struct nls_table *nls_codepage)
3038{
3039/* SMB_QUERY_FILE_UNIX_LINK */
3040 TRANSACTION2_QPI_REQ *pSMB = NULL;
3041 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3042 int rc = 0;
3043 int bytes_returned;
3044 int name_len;
3045 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003046 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Joe Perchesf96637b2013-05-04 22:12:25 -05003048 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050querySymLinkRetry:
3051 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3052 (void **) &pSMBr);
3053 if (rc)
3054 return rc;
3055
3056 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3057 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003058 cifs_strtoUTF16((__le16 *) pSMB->FileName, searchName,
3059 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 name_len++; /* trailing null */
3061 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003062 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 name_len = strnlen(searchName, PATH_MAX);
3064 name_len++; /* trailing null */
3065 strncpy(pSMB->FileName, searchName, name_len);
3066 }
3067
3068 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3069 pSMB->TotalDataCount = 0;
3070 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003071 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 pSMB->MaxSetupCount = 0;
3073 pSMB->Reserved = 0;
3074 pSMB->Flags = 0;
3075 pSMB->Timeout = 0;
3076 pSMB->Reserved2 = 0;
3077 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003078 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 pSMB->DataCount = 0;
3080 pSMB->DataOffset = 0;
3081 pSMB->SetupCount = 1;
3082 pSMB->Reserved3 = 0;
3083 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3084 byte_count = params + 1 /* pad */ ;
3085 pSMB->TotalParameterCount = cpu_to_le16(params);
3086 pSMB->ParameterCount = pSMB->TotalParameterCount;
3087 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3088 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003089 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 pSMB->ByteCount = cpu_to_le16(byte_count);
3091
3092 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3093 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3094 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003095 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 } else {
3097 /* decode response */
3098
3099 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003101 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003102 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003104 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003105 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Jeff Layton460b9692009-04-30 07:17:56 -04003107 data_start = ((char *) &pSMBr->hdr.Protocol) +
3108 le16_to_cpu(pSMBr->t2.DataOffset);
3109
Steve French0e0d2cf2009-05-01 05:27:32 +00003110 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3111 is_unicode = true;
3112 else
3113 is_unicode = false;
3114
Steve French737b7582005-04-28 22:41:06 -07003115 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003116 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3117 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003118 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003119 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121 }
3122 cifs_buf_release(pSMB);
3123 if (rc == -EAGAIN)
3124 goto querySymLinkRetry;
3125 return rc;
3126}
3127
Steve Frenchc52a9552011-02-24 06:16:22 +00003128/*
3129 * Recent Windows versions now create symlinks more frequently
3130 * and they use the "reparse point" mechanism below. We can of course
3131 * do symlinks nicely to Samba and other servers which support the
3132 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3133 * "MF" symlinks optionally, but for recent Windows we really need to
3134 * reenable the code below and fix the cifs_symlink callers to handle this.
3135 * In the interim this code has been moved to its own config option so
3136 * it is not compiled in by default until callers fixed up and more tested.
3137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003139CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3140 __u16 fid, char **symlinkinfo,
3141 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
3143 int rc = 0;
3144 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003145 struct smb_com_transaction_ioctl_req *pSMB;
3146 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003147 bool is_unicode;
3148 unsigned int sub_len;
3149 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003150 struct reparse_symlink_data *reparse_buf;
3151 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003152 __u32 data_offset, data_count;
3153 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003155 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3157 (void **) &pSMBr);
3158 if (rc)
3159 return rc;
3160
3161 pSMB->TotalParameterCount = 0 ;
3162 pSMB->TotalDataCount = 0;
3163 pSMB->MaxParameterCount = cpu_to_le32(2);
3164 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003165 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 pSMB->MaxSetupCount = 4;
3167 pSMB->Reserved = 0;
3168 pSMB->ParameterOffset = 0;
3169 pSMB->DataCount = 0;
3170 pSMB->DataOffset = 0;
3171 pSMB->SetupCount = 4;
3172 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3173 pSMB->ParameterCount = pSMB->TotalParameterCount;
3174 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3175 pSMB->IsFsctl = 1; /* FSCTL */
3176 pSMB->IsRootFlag = 0;
3177 pSMB->Fid = fid; /* file handle always le */
3178 pSMB->ByteCount = 0;
3179
3180 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3181 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3182 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003183 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003184 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
Steve French989c7e52009-05-02 05:32:20 +00003186
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003187 data_offset = le32_to_cpu(pSMBr->DataOffset);
3188 data_count = le32_to_cpu(pSMBr->DataCount);
3189 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3190 /* BB also check enough total bytes returned */
3191 rc = -EIO; /* bad smb */
3192 goto qreparse_out;
3193 }
3194 if (!data_count || (data_count > 2048)) {
3195 rc = -EIO;
3196 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3197 goto qreparse_out;
3198 }
3199 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003200 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003201 ((char *)&pSMBr->hdr.Protocol + data_offset);
3202 if ((char *)reparse_buf >= end_of_smb) {
3203 rc = -EIO;
3204 goto qreparse_out;
3205 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003206 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3207 cifs_dbg(FYI, "NFS style reparse tag\n");
3208 posix_buf = (struct reparse_posix_data *)reparse_buf;
3209
3210 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3211 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3212 le64_to_cpu(posix_buf->InodeType));
3213 rc = -EOPNOTSUPP;
3214 goto qreparse_out;
3215 }
3216 is_unicode = true;
3217 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3218 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3219 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3220 rc = -EIO;
3221 goto qreparse_out;
3222 }
3223 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3224 sub_len, is_unicode, nls_codepage);
3225 goto qreparse_out;
3226 } else if (reparse_buf->ReparseTag !=
3227 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3228 rc = -EOPNOTSUPP;
3229 goto qreparse_out;
3230 }
3231
3232 /* Reparse tag is NTFS symlink */
3233 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3234 reparse_buf->PathBuffer;
3235 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3236 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003237 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3238 rc = -EIO;
3239 goto qreparse_out;
3240 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003241 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3242 is_unicode = true;
3243 else
3244 is_unicode = false;
3245
3246 /* BB FIXME investigate remapping reserved chars here */
3247 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3248 nls_codepage);
3249 if (!*symlinkinfo)
3250 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003252 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003254 /*
3255 * Note: On -EAGAIN error only caller can retry on handle based calls
3256 * since file handle passed in no longer valid.
3257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return rc;
3259}
3260
Steve Frenchc7f508a2013-10-14 15:27:32 -05003261int
3262CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3263 __u16 fid)
3264{
3265 int rc = 0;
3266 int bytes_returned;
3267 struct smb_com_transaction_compr_ioctl_req *pSMB;
3268 struct smb_com_transaction_ioctl_rsp *pSMBr;
3269
3270 cifs_dbg(FYI, "Set compression for %u\n", fid);
3271 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3272 (void **) &pSMBr);
3273 if (rc)
3274 return rc;
3275
3276 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3277
3278 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003279 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003280 pSMB->MaxParameterCount = 0;
3281 pSMB->MaxDataCount = 0;
3282 pSMB->MaxSetupCount = 4;
3283 pSMB->Reserved = 0;
3284 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003285 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003286 pSMB->DataOffset =
3287 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3288 compression_state) - 4); /* 84 */
3289 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003290 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003291 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003292 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003293 pSMB->IsFsctl = 1; /* FSCTL */
3294 pSMB->IsRootFlag = 0;
3295 pSMB->Fid = fid; /* file handle always le */
3296 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003297 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003298 inc_rfc1001_len(pSMB, 5);
3299
3300 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3301 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3302 if (rc)
3303 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3304
3305 cifs_buf_release(pSMB);
3306
3307 /*
3308 * Note: On -EAGAIN error only caller can retry on handle based calls
3309 * since file handle passed in no longer valid.
3310 */
3311 return rc;
3312}
3313
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315#ifdef CONFIG_CIFS_POSIX
3316
3317/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003318static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3319 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003322 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3323 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3324 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003325/*
3326 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3327 ace->e_perm, ace->e_tag, ace->e_id);
3328*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 return;
3331}
3332
3333/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003334static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3335 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 int size = 0;
3338 int i;
3339 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003340 struct cifs_posix_ace *pACE;
3341 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3342 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
3344 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3345 return -EOPNOTSUPP;
3346
Steve French790fe572007-07-07 19:25:05 +00003347 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 count = le16_to_cpu(cifs_acl->access_entry_count);
3349 pACE = &cifs_acl->ace_array[0];
3350 size = sizeof(struct cifs_posix_acl);
3351 size += sizeof(struct cifs_posix_ace) * count;
3352 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003353 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003354 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3355 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return -EINVAL;
3357 }
Steve French790fe572007-07-07 19:25:05 +00003358 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 count = le16_to_cpu(cifs_acl->access_entry_count);
3360 size = sizeof(struct cifs_posix_acl);
3361 size += sizeof(struct cifs_posix_ace) * count;
3362/* skip past access ACEs to get to default ACEs */
3363 pACE = &cifs_acl->ace_array[count];
3364 count = le16_to_cpu(cifs_acl->default_entry_count);
3365 size += sizeof(struct cifs_posix_ace) * count;
3366 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003367 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return -EINVAL;
3369 } else {
3370 /* illegal type */
3371 return -EINVAL;
3372 }
3373
3374 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003375 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003376 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003377 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return -ERANGE;
3379 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003380 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003381 for (i = 0; i < count ; i++) {
3382 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3383 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385 }
3386 return size;
3387}
3388
Steve French50c2f752007-07-13 00:33:32 +00003389static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3390 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 __u16 rc = 0; /* 0 = ACL converted ok */
3393
Steve Frenchff7feac2005-11-15 16:45:16 -08003394 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3395 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003397 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /* Probably no need to le convert -1 on any arch but can not hurt */
3399 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003400 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003401 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003402/*
3403 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3404 ace->e_perm, ace->e_tag, ace->e_id);
3405*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return rc;
3407}
3408
3409/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003410static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3411 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
3413 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003414 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3415 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 int count;
3417 int i;
3418
Steve French790fe572007-07-07 19:25:05 +00003419 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return 0;
3421
3422 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003423 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3424 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003425 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003426 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3427 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return 0;
3429 }
3430 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003431 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003432 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003433 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003434 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003435 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003436 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003437 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003438 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return 0;
3440 }
Steve French50c2f752007-07-13 00:33:32 +00003441 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3443 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003444 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 /* ACE not converted */
3446 break;
3447 }
3448 }
Steve French790fe572007-07-07 19:25:05 +00003449 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3451 rc += sizeof(struct cifs_posix_acl);
3452 /* BB add check to make sure ACL does not overflow SMB */
3453 }
3454 return rc;
3455}
3456
3457int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003458CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003459 const unsigned char *searchName,
3460 char *acl_inf, const int buflen, const int acl_type,
3461 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
3463/* SMB_QUERY_POSIX_ACL */
3464 TRANSACTION2_QPI_REQ *pSMB = NULL;
3465 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3466 int rc = 0;
3467 int bytes_returned;
3468 int name_len;
3469 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003470
Joe Perchesf96637b2013-05-04 22:12:25 -05003471 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
3473queryAclRetry:
3474 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3475 (void **) &pSMBr);
3476 if (rc)
3477 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3480 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003481 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3482 searchName, PATH_MAX, nls_codepage,
3483 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 name_len++; /* trailing null */
3485 name_len *= 2;
3486 pSMB->FileName[name_len] = 0;
3487 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003488 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 name_len = strnlen(searchName, PATH_MAX);
3490 name_len++; /* trailing null */
3491 strncpy(pSMB->FileName, searchName, name_len);
3492 }
3493
3494 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3495 pSMB->TotalDataCount = 0;
3496 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003497 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 pSMB->MaxDataCount = cpu_to_le16(4000);
3499 pSMB->MaxSetupCount = 0;
3500 pSMB->Reserved = 0;
3501 pSMB->Flags = 0;
3502 pSMB->Timeout = 0;
3503 pSMB->Reserved2 = 0;
3504 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003505 offsetof(struct smb_com_transaction2_qpi_req,
3506 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 pSMB->DataCount = 0;
3508 pSMB->DataOffset = 0;
3509 pSMB->SetupCount = 1;
3510 pSMB->Reserved3 = 0;
3511 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3512 byte_count = params + 1 /* pad */ ;
3513 pSMB->TotalParameterCount = cpu_to_le16(params);
3514 pSMB->ParameterCount = pSMB->TotalParameterCount;
3515 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3516 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003517 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 pSMB->ByteCount = cpu_to_le16(byte_count);
3519
3520 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3521 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003522 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003524 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 } else {
3526 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003530 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 rc = -EIO; /* bad smb */
3532 else {
3533 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3534 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3535 rc = cifs_copy_posix_acl(acl_inf,
3536 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003537 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
3539 }
3540 cifs_buf_release(pSMB);
3541 if (rc == -EAGAIN)
3542 goto queryAclRetry;
3543 return rc;
3544}
3545
3546int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003547CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003548 const unsigned char *fileName,
3549 const char *local_acl, const int buflen,
3550 const int acl_type,
3551 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
3553 struct smb_com_transaction2_spi_req *pSMB = NULL;
3554 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3555 char *parm_data;
3556 int name_len;
3557 int rc = 0;
3558 int bytes_returned = 0;
3559 __u16 params, byte_count, data_count, param_offset, offset;
3560
Joe Perchesf96637b2013-05-04 22:12:25 -05003561 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562setAclRetry:
3563 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003564 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 if (rc)
3566 return rc;
3567 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3568 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003569 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3570 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 name_len++; /* trailing null */
3572 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003573 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 name_len = strnlen(fileName, PATH_MAX);
3575 name_len++; /* trailing null */
3576 strncpy(pSMB->FileName, fileName, name_len);
3577 }
3578 params = 6 + name_len;
3579 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003580 /* BB find max SMB size from sess */
3581 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 pSMB->MaxSetupCount = 0;
3583 pSMB->Reserved = 0;
3584 pSMB->Flags = 0;
3585 pSMB->Timeout = 0;
3586 pSMB->Reserved2 = 0;
3587 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003588 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 offset = param_offset + params;
3590 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3591 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3592
3593 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003594 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Steve French790fe572007-07-07 19:25:05 +00003596 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 rc = -EOPNOTSUPP;
3598 goto setACLerrorExit;
3599 }
3600 pSMB->DataOffset = cpu_to_le16(offset);
3601 pSMB->SetupCount = 1;
3602 pSMB->Reserved3 = 0;
3603 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3604 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3605 byte_count = 3 /* pad */ + params + data_count;
3606 pSMB->DataCount = cpu_to_le16(data_count);
3607 pSMB->TotalDataCount = pSMB->DataCount;
3608 pSMB->ParameterCount = cpu_to_le16(params);
3609 pSMB->TotalParameterCount = pSMB->ParameterCount;
3610 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003611 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 pSMB->ByteCount = cpu_to_le16(byte_count);
3613 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003614 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003615 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003616 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618setACLerrorExit:
3619 cifs_buf_release(pSMB);
3620 if (rc == -EAGAIN)
3621 goto setAclRetry;
3622 return rc;
3623}
3624
Steve Frenchf654bac2005-04-28 22:41:04 -07003625/* BB fix tabs in this function FIXME BB */
3626int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003627CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003628 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003629{
Steve French50c2f752007-07-13 00:33:32 +00003630 int rc = 0;
3631 struct smb_t2_qfi_req *pSMB = NULL;
3632 struct smb_t2_qfi_rsp *pSMBr = NULL;
3633 int bytes_returned;
3634 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003635
Joe Perchesf96637b2013-05-04 22:12:25 -05003636 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003637 if (tcon == NULL)
3638 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003639
3640GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003641 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3642 (void **) &pSMBr);
3643 if (rc)
3644 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003645
Steve Frenchad7a2922008-02-07 23:25:02 +00003646 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003647 pSMB->t2.TotalDataCount = 0;
3648 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3649 /* BB find exact max data count below from sess structure BB */
3650 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3651 pSMB->t2.MaxSetupCount = 0;
3652 pSMB->t2.Reserved = 0;
3653 pSMB->t2.Flags = 0;
3654 pSMB->t2.Timeout = 0;
3655 pSMB->t2.Reserved2 = 0;
3656 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3657 Fid) - 4);
3658 pSMB->t2.DataCount = 0;
3659 pSMB->t2.DataOffset = 0;
3660 pSMB->t2.SetupCount = 1;
3661 pSMB->t2.Reserved3 = 0;
3662 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3663 byte_count = params + 1 /* pad */ ;
3664 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3665 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3666 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3667 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003668 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003669 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003670 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003671
Steve French790fe572007-07-07 19:25:05 +00003672 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3673 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3674 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003675 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003676 } else {
3677 /* decode response */
3678 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003679 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003680 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003681 /* If rc should we check for EOPNOSUPP and
3682 disable the srvino flag? or in caller? */
3683 rc = -EIO; /* bad smb */
3684 else {
3685 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3686 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3687 struct file_chattr_info *pfinfo;
3688 /* BB Do we need a cast or hash here ? */
3689 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003690 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003691 rc = -EIO;
3692 goto GetExtAttrOut;
3693 }
3694 pfinfo = (struct file_chattr_info *)
3695 (data_offset + (char *) &pSMBr->hdr.Protocol);
3696 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003697 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003698 }
3699 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003700GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003701 cifs_buf_release(pSMB);
3702 if (rc == -EAGAIN)
3703 goto GetExtAttrRetry;
3704 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003705}
3706
Steve Frenchf654bac2005-04-28 22:41:04 -07003707#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Jeff Layton79df1ba2010-12-06 12:52:08 -05003709#ifdef CONFIG_CIFS_ACL
3710/*
3711 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3712 * all NT TRANSACTS that we init here have total parm and data under about 400
3713 * bytes (to fit in small cifs buffer size), which is the case so far, it
3714 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3715 * returned setup area) and MaxParameterCount (returned parms size) must be set
3716 * by caller
3717 */
3718static int
3719smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003720 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003721 void **ret_buf)
3722{
3723 int rc;
3724 __u32 temp_offset;
3725 struct smb_com_ntransact_req *pSMB;
3726
3727 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3728 (void **)&pSMB);
3729 if (rc)
3730 return rc;
3731 *ret_buf = (void *)pSMB;
3732 pSMB->Reserved = 0;
3733 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3734 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003735 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003736 pSMB->ParameterCount = pSMB->TotalParameterCount;
3737 pSMB->DataCount = pSMB->TotalDataCount;
3738 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3739 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3740 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3741 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3742 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3743 pSMB->SubCommand = cpu_to_le16(sub_command);
3744 return 0;
3745}
3746
3747static int
3748validate_ntransact(char *buf, char **ppparm, char **ppdata,
3749 __u32 *pparmlen, __u32 *pdatalen)
3750{
3751 char *end_of_smb;
3752 __u32 data_count, data_offset, parm_count, parm_offset;
3753 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003754 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003755
3756 *pdatalen = 0;
3757 *pparmlen = 0;
3758
3759 if (buf == NULL)
3760 return -EINVAL;
3761
3762 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3763
Jeff Layton820a8032011-05-04 08:05:26 -04003764 bcc = get_bcc(&pSMBr->hdr);
3765 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003766 (char *)&pSMBr->ByteCount;
3767
3768 data_offset = le32_to_cpu(pSMBr->DataOffset);
3769 data_count = le32_to_cpu(pSMBr->DataCount);
3770 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3771 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3772
3773 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3774 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3775
3776 /* should we also check that parm and data areas do not overlap? */
3777 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003778 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003779 return -EINVAL;
3780 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003781 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003782 return -EINVAL;
3783 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003784 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003785 return -EINVAL;
3786 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003787 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3788 *ppdata, data_count, (data_count + *ppdata),
3789 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003790 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003791 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003792 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003793 return -EINVAL;
3794 }
3795 *pdatalen = data_count;
3796 *pparmlen = parm_count;
3797 return 0;
3798}
3799
Steve French0a4b92c2006-01-12 15:44:21 -08003800/* Get Security Descriptor (by handle) from remote server for a file or dir */
3801int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003802CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003803 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003804{
3805 int rc = 0;
3806 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003807 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003808 struct kvec iov[1];
3809
Joe Perchesf96637b2013-05-04 22:12:25 -05003810 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003811
Steve French630f3f0c2007-10-25 21:17:17 +00003812 *pbuflen = 0;
3813 *acl_inf = NULL;
3814
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003815 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003816 8 /* parm len */, tcon, (void **) &pSMB);
3817 if (rc)
3818 return rc;
3819
3820 pSMB->MaxParameterCount = cpu_to_le32(4);
3821 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3822 pSMB->MaxSetupCount = 0;
3823 pSMB->Fid = fid; /* file handle always le */
3824 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3825 CIFS_ACL_DACL);
3826 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003827 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003828 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003829 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003830
Steve Frencha761ac52007-10-18 21:45:27 +00003831 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003832 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003833 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003834 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003835 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003836 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003837 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003838 __u32 parm_len;
3839 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003840 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003841 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003842
3843/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003844 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003845 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003846 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003847 goto qsec_out;
3848 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3849
Joe Perchesf96637b2013-05-04 22:12:25 -05003850 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3851 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003852
3853 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3854 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003855 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003856 goto qsec_out;
3857 }
3858
3859/* BB check that data area is minimum length and as big as acl_len */
3860
Steve Frenchaf6f4612007-10-16 18:40:37 +00003861 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003862 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003863 cifs_dbg(VFS, "acl length %d does not match %d\n",
3864 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003865 if (*pbuflen > acl_len)
3866 *pbuflen = acl_len;
3867 }
Steve French0a4b92c2006-01-12 15:44:21 -08003868
Steve French630f3f0c2007-10-25 21:17:17 +00003869 /* check if buffer is big enough for the acl
3870 header followed by the smallest SID */
3871 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3872 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003873 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003874 rc = -EINVAL;
3875 *pbuflen = 0;
3876 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003877 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003878 if (*acl_inf == NULL) {
3879 *pbuflen = 0;
3880 rc = -ENOMEM;
3881 }
Steve French630f3f0c2007-10-25 21:17:17 +00003882 }
Steve French0a4b92c2006-01-12 15:44:21 -08003883 }
3884qsec_out:
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01003885 free_rsp_buf(buf_type, iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003886/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003887 return rc;
3888}
Steve French97837582007-12-31 07:47:21 +00003889
3890int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003891CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003892 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003893{
3894 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3895 int rc = 0;
3896 int bytes_returned = 0;
3897 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003898 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003899
3900setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003901 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003902 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003903 return rc;
Steve French97837582007-12-31 07:47:21 +00003904
3905 pSMB->MaxSetupCount = 0;
3906 pSMB->Reserved = 0;
3907
3908 param_count = 8;
3909 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3910 data_count = acllen;
3911 data_offset = param_offset + param_count;
3912 byte_count = 3 /* pad */ + param_count;
3913
3914 pSMB->DataCount = cpu_to_le32(data_count);
3915 pSMB->TotalDataCount = pSMB->DataCount;
3916 pSMB->MaxParameterCount = cpu_to_le32(4);
3917 pSMB->MaxDataCount = cpu_to_le32(16384);
3918 pSMB->ParameterCount = cpu_to_le32(param_count);
3919 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3920 pSMB->TotalParameterCount = pSMB->ParameterCount;
3921 pSMB->DataOffset = cpu_to_le32(data_offset);
3922 pSMB->SetupCount = 0;
3923 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3924 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3925
3926 pSMB->Fid = fid; /* file handle always le */
3927 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003928 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003929
3930 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003931 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3932 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003933 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003934 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003935 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003936
3937 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3938 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3939
Joe Perchesf96637b2013-05-04 22:12:25 -05003940 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3941 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003942 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003943 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003944 cifs_buf_release(pSMB);
3945
3946 if (rc == -EAGAIN)
3947 goto setCifsAclRetry;
3948
3949 return (rc);
3950}
3951
Jeff Layton79df1ba2010-12-06 12:52:08 -05003952#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003953
Steve French6b8edfe2005-08-23 20:26:03 -07003954/* Legacy Query Path Information call for lookup to old servers such
3955 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003956int
3957SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3958 const char *search_name, FILE_ALL_INFO *data,
3959 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003960{
Steve Frenchad7a2922008-02-07 23:25:02 +00003961 QUERY_INFORMATION_REQ *pSMB;
3962 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003963 int rc = 0;
3964 int bytes_returned;
3965 int name_len;
3966
Joe Perchesf96637b2013-05-04 22:12:25 -05003967 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003968QInfRetry:
3969 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003970 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003971 if (rc)
3972 return rc;
3973
3974 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3975 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003976 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003977 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003978 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003979 name_len++; /* trailing null */
3980 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003981 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003982 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003983 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003984 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003985 }
3986 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003987 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003988 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003989 pSMB->ByteCount = cpu_to_le16(name_len);
3990
3991 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003992 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003993 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003994 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003995 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003996 struct timespec ts;
3997 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003998
3999 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004000 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004001 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004002 ts.tv_nsec = 0;
4003 ts.tv_sec = time;
4004 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004005 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4006 data->LastWriteTime = data->ChangeTime;
4007 data->LastAccessTime = 0;
4008 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004009 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004010 data->EndOfFile = data->AllocationSize;
4011 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004012 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004013 } else
4014 rc = -EIO; /* bad buffer passed in */
4015
4016 cifs_buf_release(pSMB);
4017
4018 if (rc == -EAGAIN)
4019 goto QInfRetry;
4020
4021 return rc;
4022}
4023
Jeff Laytonbcd53572010-02-12 07:44:16 -05004024int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004025CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004026 u16 netfid, FILE_ALL_INFO *pFindData)
4027{
4028 struct smb_t2_qfi_req *pSMB = NULL;
4029 struct smb_t2_qfi_rsp *pSMBr = NULL;
4030 int rc = 0;
4031 int bytes_returned;
4032 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004033
Jeff Laytonbcd53572010-02-12 07:44:16 -05004034QFileInfoRetry:
4035 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4036 (void **) &pSMBr);
4037 if (rc)
4038 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004039
Jeff Laytonbcd53572010-02-12 07:44:16 -05004040 params = 2 /* level */ + 2 /* fid */;
4041 pSMB->t2.TotalDataCount = 0;
4042 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4043 /* BB find exact max data count below from sess structure BB */
4044 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4045 pSMB->t2.MaxSetupCount = 0;
4046 pSMB->t2.Reserved = 0;
4047 pSMB->t2.Flags = 0;
4048 pSMB->t2.Timeout = 0;
4049 pSMB->t2.Reserved2 = 0;
4050 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4051 Fid) - 4);
4052 pSMB->t2.DataCount = 0;
4053 pSMB->t2.DataOffset = 0;
4054 pSMB->t2.SetupCount = 1;
4055 pSMB->t2.Reserved3 = 0;
4056 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4057 byte_count = params + 1 /* pad */ ;
4058 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4059 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4060 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4061 pSMB->Pad = 0;
4062 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004063 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004064 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004065
4066 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4067 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4068 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004069 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004070 } else { /* decode response */
4071 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4072
4073 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4074 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004075 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004076 rc = -EIO; /* bad smb */
4077 else if (pFindData) {
4078 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4079 memcpy((char *) pFindData,
4080 (char *) &pSMBr->hdr.Protocol +
4081 data_offset, sizeof(FILE_ALL_INFO));
4082 } else
4083 rc = -ENOMEM;
4084 }
4085 cifs_buf_release(pSMB);
4086 if (rc == -EAGAIN)
4087 goto QFileInfoRetry;
4088
4089 return rc;
4090}
Steve French6b8edfe2005-08-23 20:26:03 -07004091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004093CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004094 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004095 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004096 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004098 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 TRANSACTION2_QPI_REQ *pSMB = NULL;
4100 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4101 int rc = 0;
4102 int bytes_returned;
4103 int name_len;
4104 __u16 params, byte_count;
4105
Joe Perchesf96637b2013-05-04 22:12:25 -05004106 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107QPathInfoRetry:
4108 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4109 (void **) &pSMBr);
4110 if (rc)
4111 return rc;
4112
4113 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4114 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004115 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004116 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 name_len++; /* trailing null */
4118 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004119 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004120 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004122 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124
Steve French50c2f752007-07-13 00:33:32 +00004125 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 pSMB->TotalDataCount = 0;
4127 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004128 /* BB find exact max SMB PDU from sess structure BB */
4129 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 pSMB->MaxSetupCount = 0;
4131 pSMB->Reserved = 0;
4132 pSMB->Flags = 0;
4133 pSMB->Timeout = 0;
4134 pSMB->Reserved2 = 0;
4135 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004136 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 pSMB->DataCount = 0;
4138 pSMB->DataOffset = 0;
4139 pSMB->SetupCount = 1;
4140 pSMB->Reserved3 = 0;
4141 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4142 byte_count = params + 1 /* pad */ ;
4143 pSMB->TotalParameterCount = cpu_to_le16(params);
4144 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004145 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004146 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4147 else
4148 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004150 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 pSMB->ByteCount = cpu_to_le16(byte_count);
4152
4153 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4154 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4155 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004156 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 } else { /* decode response */
4158 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4159
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004160 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4161 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004162 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004164 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004165 rc = -EIO; /* 24 or 26 expected but we do not read
4166 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004167 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004168 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004170
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004171 /*
4172 * On legacy responses we do not read the last field,
4173 * EAsize, fortunately since it varies by subdialect and
4174 * also note it differs on Set vs Get, ie two bytes or 4
4175 * bytes depending but we don't care here.
4176 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004177 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004178 size = sizeof(FILE_INFO_STANDARD);
4179 else
4180 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004181 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004182 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 } else
4184 rc = -ENOMEM;
4185 }
4186 cifs_buf_release(pSMB);
4187 if (rc == -EAGAIN)
4188 goto QPathInfoRetry;
4189
4190 return rc;
4191}
4192
4193int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004194CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004195 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4196{
4197 struct smb_t2_qfi_req *pSMB = NULL;
4198 struct smb_t2_qfi_rsp *pSMBr = NULL;
4199 int rc = 0;
4200 int bytes_returned;
4201 __u16 params, byte_count;
4202
4203UnixQFileInfoRetry:
4204 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4205 (void **) &pSMBr);
4206 if (rc)
4207 return rc;
4208
4209 params = 2 /* level */ + 2 /* fid */;
4210 pSMB->t2.TotalDataCount = 0;
4211 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4212 /* BB find exact max data count below from sess structure BB */
4213 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4214 pSMB->t2.MaxSetupCount = 0;
4215 pSMB->t2.Reserved = 0;
4216 pSMB->t2.Flags = 0;
4217 pSMB->t2.Timeout = 0;
4218 pSMB->t2.Reserved2 = 0;
4219 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4220 Fid) - 4);
4221 pSMB->t2.DataCount = 0;
4222 pSMB->t2.DataOffset = 0;
4223 pSMB->t2.SetupCount = 1;
4224 pSMB->t2.Reserved3 = 0;
4225 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4226 byte_count = params + 1 /* pad */ ;
4227 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4228 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4229 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4230 pSMB->Pad = 0;
4231 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004232 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004233 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004234
4235 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4236 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4237 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004238 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004239 } else { /* decode response */
4240 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4241
Jeff Layton820a8032011-05-04 08:05:26 -04004242 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004243 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 -05004244 rc = -EIO; /* bad smb */
4245 } else {
4246 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4247 memcpy((char *) pFindData,
4248 (char *) &pSMBr->hdr.Protocol +
4249 data_offset,
4250 sizeof(FILE_UNIX_BASIC_INFO));
4251 }
4252 }
4253
4254 cifs_buf_release(pSMB);
4255 if (rc == -EAGAIN)
4256 goto UnixQFileInfoRetry;
4257
4258 return rc;
4259}
4260
4261int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004262CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004264 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004265 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
4267/* SMB_QUERY_FILE_UNIX_BASIC */
4268 TRANSACTION2_QPI_REQ *pSMB = NULL;
4269 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4270 int rc = 0;
4271 int bytes_returned = 0;
4272 int name_len;
4273 __u16 params, byte_count;
4274
Joe Perchesf96637b2013-05-04 22:12:25 -05004275 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276UnixQPathInfoRetry:
4277 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4278 (void **) &pSMBr);
4279 if (rc)
4280 return rc;
4281
4282 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4283 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004284 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4285 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 name_len++; /* trailing null */
4287 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004288 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 name_len = strnlen(searchName, PATH_MAX);
4290 name_len++; /* trailing null */
4291 strncpy(pSMB->FileName, searchName, name_len);
4292 }
4293
Steve French50c2f752007-07-13 00:33:32 +00004294 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 pSMB->TotalDataCount = 0;
4296 pSMB->MaxParameterCount = cpu_to_le16(2);
4297 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004298 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 pSMB->MaxSetupCount = 0;
4300 pSMB->Reserved = 0;
4301 pSMB->Flags = 0;
4302 pSMB->Timeout = 0;
4303 pSMB->Reserved2 = 0;
4304 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004305 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 pSMB->DataCount = 0;
4307 pSMB->DataOffset = 0;
4308 pSMB->SetupCount = 1;
4309 pSMB->Reserved3 = 0;
4310 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4311 byte_count = params + 1 /* pad */ ;
4312 pSMB->TotalParameterCount = cpu_to_le16(params);
4313 pSMB->ParameterCount = pSMB->TotalParameterCount;
4314 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4315 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004316 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 pSMB->ByteCount = cpu_to_le16(byte_count);
4318
4319 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4320 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4321 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004322 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 } else { /* decode response */
4324 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4325
Jeff Layton820a8032011-05-04 08:05:26 -04004326 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004327 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 -07004328 rc = -EIO; /* bad smb */
4329 } else {
4330 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4331 memcpy((char *) pFindData,
4332 (char *) &pSMBr->hdr.Protocol +
4333 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004334 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336 }
4337 cifs_buf_release(pSMB);
4338 if (rc == -EAGAIN)
4339 goto UnixQPathInfoRetry;
4340
4341 return rc;
4342}
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344/* xid, tcon, searchName and codepage are input parms, rest are returned */
4345int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004346CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004347 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004348 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004349 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
4351/* level 257 SMB_ */
4352 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4353 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004354 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 int rc = 0;
4356 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004357 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004359 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Joe Perchesf96637b2013-05-04 22:12:25 -05004361 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363findFirstRetry:
4364 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4365 (void **) &pSMBr);
4366 if (rc)
4367 return rc;
4368
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004369 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004370 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4373 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004374 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4375 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004376 /* We can not add the asterik earlier in case
4377 it got remapped to 0xF03A as if it were part of the
4378 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004380 if (msearch) {
4381 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4382 pSMB->FileName[name_len+1] = 0;
4383 pSMB->FileName[name_len+2] = '*';
4384 pSMB->FileName[name_len+3] = 0;
4385 name_len += 4; /* now the trailing null */
4386 /* null terminate just in case */
4387 pSMB->FileName[name_len] = 0;
4388 pSMB->FileName[name_len+1] = 0;
4389 name_len += 2;
4390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 } else { /* BB add check for overrun of SMB buf BB */
4392 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004394 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 free buffer exit; BB */
4396 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004397 if (msearch) {
4398 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4399 pSMB->FileName[name_len+1] = '*';
4400 pSMB->FileName[name_len+2] = 0;
4401 name_len += 3;
4402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 }
4404
4405 params = 12 + name_len /* includes null */ ;
4406 pSMB->TotalDataCount = 0; /* no EAs */
4407 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004408 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 pSMB->MaxSetupCount = 0;
4410 pSMB->Reserved = 0;
4411 pSMB->Flags = 0;
4412 pSMB->Timeout = 0;
4413 pSMB->Reserved2 = 0;
4414 byte_count = params + 1 /* pad */ ;
4415 pSMB->TotalParameterCount = cpu_to_le16(params);
4416 pSMB->ParameterCount = pSMB->TotalParameterCount;
4417 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004418 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4419 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 pSMB->DataCount = 0;
4421 pSMB->DataOffset = 0;
4422 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4423 pSMB->Reserved3 = 0;
4424 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4425 pSMB->SearchAttributes =
4426 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4427 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004428 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004429 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4431
4432 /* BB what should we set StorageType to? Does it matter? BB */
4433 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004434 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 pSMB->ByteCount = cpu_to_le16(byte_count);
4436
4437 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4438 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004439 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Steve French88274812006-03-09 22:21:45 +00004441 if (rc) {/* BB add logic to retry regular search if Unix search
4442 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004444 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004445
Steve French88274812006-03-09 22:21:45 +00004446 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 /* BB eventually could optimize out free and realloc of buf */
4449 /* for this case */
4450 if (rc == -EAGAIN)
4451 goto findFirstRetry;
4452 } else { /* decode response */
4453 /* BB remember to free buffer if error BB */
4454 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004455 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004456 unsigned int lnoff;
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004459 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 else
Steve French4b18f2a2008-04-29 00:06:05 +00004461 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004464 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004465 psrch_inf->srch_entries_start =
4466 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4469 le16_to_cpu(pSMBr->t2.ParameterOffset));
4470
Steve French790fe572007-07-07 19:25:05 +00004471 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004472 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 else
Steve French4b18f2a2008-04-29 00:06:05 +00004474 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Steve French50c2f752007-07-13 00:33:32 +00004476 psrch_inf->entries_in_buffer =
4477 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004478 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004480 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004481 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004482 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004483 psrch_inf->last_entry = NULL;
4484 return rc;
4485 }
4486
Steve French0752f152008-10-07 20:03:33 +00004487 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004488 lnoff;
4489
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004490 if (pnetfid)
4491 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 } else {
4493 cifs_buf_release(pSMB);
4494 }
4495 }
4496
4497 return rc;
4498}
4499
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004500int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4501 __u16 searchHandle, __u16 search_flags,
4502 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
4504 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4505 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004506 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 char *response_data;
4508 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004509 int bytes_returned;
4510 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 __u16 params, byte_count;
4512
Joe Perchesf96637b2013-05-04 22:12:25 -05004513 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Steve French4b18f2a2008-04-29 00:06:05 +00004515 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return -ENOENT;
4517
4518 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4519 (void **) &pSMBr);
4520 if (rc)
4521 return rc;
4522
Steve French50c2f752007-07-13 00:33:32 +00004523 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 byte_count = 0;
4525 pSMB->TotalDataCount = 0; /* no EAs */
4526 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004527 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 pSMB->MaxSetupCount = 0;
4529 pSMB->Reserved = 0;
4530 pSMB->Flags = 0;
4531 pSMB->Timeout = 0;
4532 pSMB->Reserved2 = 0;
4533 pSMB->ParameterOffset = cpu_to_le16(
4534 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4535 pSMB->DataCount = 0;
4536 pSMB->DataOffset = 0;
4537 pSMB->SetupCount = 1;
4538 pSMB->Reserved3 = 0;
4539 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4540 pSMB->SearchHandle = searchHandle; /* always kept as le */
4541 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004542 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4544 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004545 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 name_len = psrch_inf->resume_name_len;
4548 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004549 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4551 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004552 /* 14 byte parm len above enough for 2 byte null terminator */
4553 pSMB->ResumeFileName[name_len] = 0;
4554 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 } else {
4556 rc = -EINVAL;
4557 goto FNext2_err_exit;
4558 }
4559 byte_count = params + 1 /* pad */ ;
4560 pSMB->TotalParameterCount = cpu_to_le16(params);
4561 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004562 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4566 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004567 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (rc) {
4569 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004570 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004571 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004572 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004574 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 } else { /* decode response */
4576 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004577
Steve French790fe572007-07-07 19:25:05 +00004578 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004579 unsigned int lnoff;
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 /* BB fixme add lock for file (srch_info) struct here */
4582 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004583 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 else
Steve French4b18f2a2008-04-29 00:06:05 +00004585 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 response_data = (char *) &pSMBr->hdr.Protocol +
4587 le16_to_cpu(pSMBr->t2.ParameterOffset);
4588 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4589 response_data = (char *)&pSMBr->hdr.Protocol +
4590 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004591 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004592 cifs_small_buf_release(
4593 psrch_inf->ntwrk_buf_start);
4594 else
4595 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 psrch_inf->srch_entries_start = response_data;
4597 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004598 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004599 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004600 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 else
Steve French4b18f2a2008-04-29 00:06:05 +00004602 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004603 psrch_inf->entries_in_buffer =
4604 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 psrch_inf->index_of_last_entry +=
4606 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004607 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004608 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004609 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004610 psrch_inf->last_entry = NULL;
4611 return rc;
4612 } else
4613 psrch_inf->last_entry =
4614 psrch_inf->srch_entries_start + lnoff;
4615
Joe Perchesf96637b2013-05-04 22:12:25 -05004616/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4617 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
4619 /* BB fixme add unlock here */
4620 }
4621
4622 }
4623
4624 /* BB On error, should we leave previous search buf (and count and
4625 last entry fields) intact or free the previous one? */
4626
4627 /* Note: On -EAGAIN error only caller can retry on handle based calls
4628 since file handle passed in no longer valid */
4629FNext2_err_exit:
4630 if (rc != 0)
4631 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 return rc;
4633}
4634
4635int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004636CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004637 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
4639 int rc = 0;
4640 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Joe Perchesf96637b2013-05-04 22:12:25 -05004642 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4644
4645 /* no sense returning error if session restarted
4646 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004647 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return 0;
4649 if (rc)
4650 return rc;
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 pSMB->FileID = searchHandle;
4653 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004654 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004655 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004656 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004657
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004658 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
4660 /* Since session is dead, search handle closed on server already */
4661 if (rc == -EAGAIN)
4662 rc = 0;
4663
4664 return rc;
4665}
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004668CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004669 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004670 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
4672 int rc = 0;
4673 TRANSACTION2_QPI_REQ *pSMB = NULL;
4674 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4675 int name_len, bytes_returned;
4676 __u16 params, byte_count;
4677
Joe Perchesf96637b2013-05-04 22:12:25 -05004678 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004679 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004680 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682GetInodeNumberRetry:
4683 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004684 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 if (rc)
4686 return rc;
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4689 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004690 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004691 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004692 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 name_len++; /* trailing null */
4694 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004695 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004696 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004698 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 }
4700
4701 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4702 pSMB->TotalDataCount = 0;
4703 pSMB->MaxParameterCount = cpu_to_le16(2);
4704 /* BB find exact max data count below from sess structure BB */
4705 pSMB->MaxDataCount = cpu_to_le16(4000);
4706 pSMB->MaxSetupCount = 0;
4707 pSMB->Reserved = 0;
4708 pSMB->Flags = 0;
4709 pSMB->Timeout = 0;
4710 pSMB->Reserved2 = 0;
4711 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004712 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 pSMB->DataCount = 0;
4714 pSMB->DataOffset = 0;
4715 pSMB->SetupCount = 1;
4716 pSMB->Reserved3 = 0;
4717 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4718 byte_count = params + 1 /* pad */ ;
4719 pSMB->TotalParameterCount = cpu_to_le16(params);
4720 pSMB->ParameterCount = pSMB->TotalParameterCount;
4721 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4722 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004723 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 pSMB->ByteCount = cpu_to_le16(byte_count);
4725
4726 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4727 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4728 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004729 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 } else {
4731 /* decode response */
4732 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004734 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 /* If rc should we check for EOPNOSUPP and
4736 disable the srvino flag? or in caller? */
4737 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004738 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4740 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004741 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004743 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004744 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 rc = -EIO;
4746 goto GetInodeNumOut;
4747 }
4748 pfinfo = (struct file_internal_info *)
4749 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004750 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 }
4752 }
4753GetInodeNumOut:
4754 cifs_buf_release(pSMB);
4755 if (rc == -EAGAIN)
4756 goto GetInodeNumberRetry;
4757 return rc;
4758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Igor Mammedovfec45852008-05-16 13:06:30 +04004760/* parses DFS refferal V3 structure
4761 * caller is responsible for freeing target_nodes
4762 * returns:
4763 * on success - 0
4764 * on failure - errno
4765 */
4766static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004767parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004768 unsigned int *num_of_nodes,
4769 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004770 const struct nls_table *nls_codepage, int remap,
4771 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004772{
4773 int i, rc = 0;
4774 char *data_end;
4775 bool is_unicode;
4776 struct dfs_referral_level_3 *ref;
4777
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004778 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4779 is_unicode = true;
4780 else
4781 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004782 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4783
4784 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004785 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4786 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004787 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004788 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004789 }
4790
4791 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004792 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004793 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4794 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004795 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004796 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004797 }
4798
4799 /* get the upper boundary of the resp buffer */
4800 data_end = (char *)(&(pSMBr->PathConsumed)) +
4801 le16_to_cpu(pSMBr->t2.DataCount);
4802
Joe Perchesf96637b2013-05-04 22:12:25 -05004803 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4804 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004805
Joe Perchesf96637b2013-05-04 22:12:25 -05004806 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4807 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004808 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004809 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004810 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004811 }
4812
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004813 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004814 for (i = 0; i < *num_of_nodes; i++) {
4815 char *temp;
4816 int max_len;
4817 struct dfs_info3_param *node = (*target_nodes)+i;
4818
Steve French0e0d2cf2009-05-01 05:27:32 +00004819 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004820 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004821 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4822 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004823 if (tmp == NULL) {
4824 rc = -ENOMEM;
4825 goto parse_DFS_referrals_exit;
4826 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004827 cifsConvertToUTF16((__le16 *) tmp, searchName,
4828 PATH_MAX, nls_codepage, remap);
4829 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004830 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004831 nls_codepage);
4832 kfree(tmp);
4833 } else
4834 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4835
Igor Mammedovfec45852008-05-16 13:06:30 +04004836 node->server_type = le16_to_cpu(ref->ServerType);
4837 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4838
4839 /* copy DfsPath */
4840 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4841 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004842 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4843 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004844 if (!node->path_name) {
4845 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004846 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004847 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004848
4849 /* copy link target UNC */
4850 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4851 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004852 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4853 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004854 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004855 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004856 goto parse_DFS_referrals_exit;
4857 }
4858
4859 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004860 }
4861
Steve Frencha1fe78f2008-05-16 18:48:38 +00004862parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004863 if (rc) {
4864 free_dfs_info_array(*target_nodes, *num_of_nodes);
4865 *target_nodes = NULL;
4866 *num_of_nodes = 0;
4867 }
4868 return rc;
4869}
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004872CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004873 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004874 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004875 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
4877/* TRANS2_GET_DFS_REFERRAL */
4878 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4879 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 int rc = 0;
4881 int bytes_returned;
4882 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004884 *num_of_nodes = 0;
4885 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Joe Perchesf96637b2013-05-04 22:12:25 -05004887 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (ses == NULL)
4889 return -ENODEV;
4890getDFSRetry:
4891 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4892 (void **) &pSMBr);
4893 if (rc)
4894 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004895
4896 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004897 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004898 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 pSMB->hdr.Tid = ses->ipc_tid;
4900 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004901 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004903 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
4906 if (ses->capabilities & CAP_UNICODE) {
4907 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4908 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004909 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004910 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004911 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 name_len++; /* trailing null */
4913 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004914 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004915 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004917 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 }
4919
Jeff Layton38d77c52013-05-26 07:01:00 -04004920 if (ses->server && ses->server->sign)
4921 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004922
Steve French50c2f752007-07-13 00:33:32 +00004923 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 params = 2 /* level */ + name_len /*includes null */ ;
4926 pSMB->TotalDataCount = 0;
4927 pSMB->DataCount = 0;
4928 pSMB->DataOffset = 0;
4929 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004930 /* BB find exact max SMB PDU from sess structure BB */
4931 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 pSMB->MaxSetupCount = 0;
4933 pSMB->Reserved = 0;
4934 pSMB->Flags = 0;
4935 pSMB->Timeout = 0;
4936 pSMB->Reserved2 = 0;
4937 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004938 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 pSMB->SetupCount = 1;
4940 pSMB->Reserved3 = 0;
4941 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4942 byte_count = params + 3 /* pad */ ;
4943 pSMB->ParameterCount = cpu_to_le16(params);
4944 pSMB->TotalParameterCount = pSMB->ParameterCount;
4945 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004946 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 pSMB->ByteCount = cpu_to_le16(byte_count);
4948
4949 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4950 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4951 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004952 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004953 goto GetDFSRefExit;
4954 }
4955 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004957 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004958 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004959 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004960 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004962
Joe Perchesf96637b2013-05-04 22:12:25 -05004963 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4964 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004965
4966 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004967 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004968 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004969 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004972 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 if (rc == -EAGAIN)
4975 goto getDFSRetry;
4976
4977 return rc;
4978}
4979
Steve French20962432005-09-21 22:05:57 -07004980/* Query File System Info such as free space to old servers such as Win 9x */
4981int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004982SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4983 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004984{
4985/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4986 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4987 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4988 FILE_SYSTEM_ALLOC_INFO *response_data;
4989 int rc = 0;
4990 int bytes_returned = 0;
4991 __u16 params, byte_count;
4992
Joe Perchesf96637b2013-05-04 22:12:25 -05004993 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004994oldQFSInfoRetry:
4995 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4996 (void **) &pSMBr);
4997 if (rc)
4998 return rc;
Steve French20962432005-09-21 22:05:57 -07004999
5000 params = 2; /* level */
5001 pSMB->TotalDataCount = 0;
5002 pSMB->MaxParameterCount = cpu_to_le16(2);
5003 pSMB->MaxDataCount = cpu_to_le16(1000);
5004 pSMB->MaxSetupCount = 0;
5005 pSMB->Reserved = 0;
5006 pSMB->Flags = 0;
5007 pSMB->Timeout = 0;
5008 pSMB->Reserved2 = 0;
5009 byte_count = params + 1 /* pad */ ;
5010 pSMB->TotalParameterCount = cpu_to_le16(params);
5011 pSMB->ParameterCount = pSMB->TotalParameterCount;
5012 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5013 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5014 pSMB->DataCount = 0;
5015 pSMB->DataOffset = 0;
5016 pSMB->SetupCount = 1;
5017 pSMB->Reserved3 = 0;
5018 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5019 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005020 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005021 pSMB->ByteCount = cpu_to_le16(byte_count);
5022
5023 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5024 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5025 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005026 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005027 } else { /* decode response */
5028 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5029
Jeff Layton820a8032011-05-04 08:05:26 -04005030 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005031 rc = -EIO; /* bad smb */
5032 else {
5033 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005034 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005035 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005036
Steve French50c2f752007-07-13 00:33:32 +00005037 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005038 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5039 FSData->f_bsize =
5040 le16_to_cpu(response_data->BytesPerSector) *
5041 le32_to_cpu(response_data->
5042 SectorsPerAllocationUnit);
5043 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005044 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005045 FSData->f_bfree = FSData->f_bavail =
5046 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005047 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5048 (unsigned long long)FSData->f_blocks,
5049 (unsigned long long)FSData->f_bfree,
5050 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005051 }
5052 }
5053 cifs_buf_release(pSMB);
5054
5055 if (rc == -EAGAIN)
5056 goto oldQFSInfoRetry;
5057
5058 return rc;
5059}
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005062CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5063 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
5065/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5066 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5067 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5068 FILE_SYSTEM_INFO *response_data;
5069 int rc = 0;
5070 int bytes_returned = 0;
5071 __u16 params, byte_count;
5072
Joe Perchesf96637b2013-05-04 22:12:25 -05005073 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074QFSInfoRetry:
5075 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5076 (void **) &pSMBr);
5077 if (rc)
5078 return rc;
5079
5080 params = 2; /* level */
5081 pSMB->TotalDataCount = 0;
5082 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005083 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 pSMB->MaxSetupCount = 0;
5085 pSMB->Reserved = 0;
5086 pSMB->Flags = 0;
5087 pSMB->Timeout = 0;
5088 pSMB->Reserved2 = 0;
5089 byte_count = params + 1 /* pad */ ;
5090 pSMB->TotalParameterCount = cpu_to_le16(params);
5091 pSMB->ParameterCount = pSMB->TotalParameterCount;
5092 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005093 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 pSMB->DataCount = 0;
5095 pSMB->DataOffset = 0;
5096 pSMB->SetupCount = 1;
5097 pSMB->Reserved3 = 0;
5098 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5099 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005100 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 pSMB->ByteCount = cpu_to_le16(byte_count);
5102
5103 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5104 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5105 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005106 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005108 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Jeff Layton820a8032011-05-04 08:05:26 -04005110 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 rc = -EIO; /* bad smb */
5112 else {
5113 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
5115 response_data =
5116 (FILE_SYSTEM_INFO
5117 *) (((char *) &pSMBr->hdr.Protocol) +
5118 data_offset);
5119 FSData->f_bsize =
5120 le32_to_cpu(response_data->BytesPerSector) *
5121 le32_to_cpu(response_data->
5122 SectorsPerAllocationUnit);
5123 FSData->f_blocks =
5124 le64_to_cpu(response_data->TotalAllocationUnits);
5125 FSData->f_bfree = FSData->f_bavail =
5126 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005127 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5128 (unsigned long long)FSData->f_blocks,
5129 (unsigned long long)FSData->f_bfree,
5130 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 }
5132 }
5133 cifs_buf_release(pSMB);
5134
5135 if (rc == -EAGAIN)
5136 goto QFSInfoRetry;
5137
5138 return rc;
5139}
5140
5141int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005142CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143{
5144/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5145 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5146 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5147 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5148 int rc = 0;
5149 int bytes_returned = 0;
5150 __u16 params, byte_count;
5151
Joe Perchesf96637b2013-05-04 22:12:25 -05005152 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153QFSAttributeRetry:
5154 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5155 (void **) &pSMBr);
5156 if (rc)
5157 return rc;
5158
5159 params = 2; /* level */
5160 pSMB->TotalDataCount = 0;
5161 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005162 /* BB find exact max SMB PDU from sess structure BB */
5163 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 pSMB->MaxSetupCount = 0;
5165 pSMB->Reserved = 0;
5166 pSMB->Flags = 0;
5167 pSMB->Timeout = 0;
5168 pSMB->Reserved2 = 0;
5169 byte_count = params + 1 /* pad */ ;
5170 pSMB->TotalParameterCount = cpu_to_le16(params);
5171 pSMB->ParameterCount = pSMB->TotalParameterCount;
5172 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005173 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 pSMB->DataCount = 0;
5175 pSMB->DataOffset = 0;
5176 pSMB->SetupCount = 1;
5177 pSMB->Reserved3 = 0;
5178 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5179 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005180 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 pSMB->ByteCount = cpu_to_le16(byte_count);
5182
5183 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5184 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5185 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005186 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 } else { /* decode response */
5188 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5189
Jeff Layton820a8032011-05-04 08:05:26 -04005190 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005191 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 rc = -EIO; /* bad smb */
5193 } else {
5194 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5195 response_data =
5196 (FILE_SYSTEM_ATTRIBUTE_INFO
5197 *) (((char *) &pSMBr->hdr.Protocol) +
5198 data_offset);
5199 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005200 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 }
5202 }
5203 cifs_buf_release(pSMB);
5204
5205 if (rc == -EAGAIN)
5206 goto QFSAttributeRetry;
5207
5208 return rc;
5209}
5210
5211int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005212CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213{
5214/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5215 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5216 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5217 FILE_SYSTEM_DEVICE_INFO *response_data;
5218 int rc = 0;
5219 int bytes_returned = 0;
5220 __u16 params, byte_count;
5221
Joe Perchesf96637b2013-05-04 22:12:25 -05005222 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223QFSDeviceRetry:
5224 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5225 (void **) &pSMBr);
5226 if (rc)
5227 return rc;
5228
5229 params = 2; /* level */
5230 pSMB->TotalDataCount = 0;
5231 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005232 /* BB find exact max SMB PDU from sess structure BB */
5233 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 pSMB->MaxSetupCount = 0;
5235 pSMB->Reserved = 0;
5236 pSMB->Flags = 0;
5237 pSMB->Timeout = 0;
5238 pSMB->Reserved2 = 0;
5239 byte_count = params + 1 /* pad */ ;
5240 pSMB->TotalParameterCount = cpu_to_le16(params);
5241 pSMB->ParameterCount = pSMB->TotalParameterCount;
5242 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005243 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
5245 pSMB->DataCount = 0;
5246 pSMB->DataOffset = 0;
5247 pSMB->SetupCount = 1;
5248 pSMB->Reserved3 = 0;
5249 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5250 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005251 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 pSMB->ByteCount = cpu_to_le16(byte_count);
5253
5254 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5255 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5256 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005257 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 } else { /* decode response */
5259 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5260
Jeff Layton820a8032011-05-04 08:05:26 -04005261 if (rc || get_bcc(&pSMBr->hdr) <
5262 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 rc = -EIO; /* bad smb */
5264 else {
5265 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5266 response_data =
Steve French737b7582005-04-28 22:41:06 -07005267 (FILE_SYSTEM_DEVICE_INFO *)
5268 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 data_offset);
5270 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005271 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 }
5273 }
5274 cifs_buf_release(pSMB);
5275
5276 if (rc == -EAGAIN)
5277 goto QFSDeviceRetry;
5278
5279 return rc;
5280}
5281
5282int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005283CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
5285/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5286 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5287 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5288 FILE_SYSTEM_UNIX_INFO *response_data;
5289 int rc = 0;
5290 int bytes_returned = 0;
5291 __u16 params, byte_count;
5292
Joe Perchesf96637b2013-05-04 22:12:25 -05005293 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005295 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5296 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 if (rc)
5298 return rc;
5299
5300 params = 2; /* level */
5301 pSMB->TotalDataCount = 0;
5302 pSMB->DataCount = 0;
5303 pSMB->DataOffset = 0;
5304 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005305 /* BB find exact max SMB PDU from sess structure BB */
5306 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 pSMB->MaxSetupCount = 0;
5308 pSMB->Reserved = 0;
5309 pSMB->Flags = 0;
5310 pSMB->Timeout = 0;
5311 pSMB->Reserved2 = 0;
5312 byte_count = params + 1 /* pad */ ;
5313 pSMB->ParameterCount = cpu_to_le16(params);
5314 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005315 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5316 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 pSMB->SetupCount = 1;
5318 pSMB->Reserved3 = 0;
5319 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5320 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005321 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 pSMB->ByteCount = cpu_to_le16(byte_count);
5323
5324 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5325 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5326 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005327 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 } else { /* decode response */
5329 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5330
Jeff Layton820a8032011-05-04 08:05:26 -04005331 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 rc = -EIO; /* bad smb */
5333 } else {
5334 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5335 response_data =
5336 (FILE_SYSTEM_UNIX_INFO
5337 *) (((char *) &pSMBr->hdr.Protocol) +
5338 data_offset);
5339 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005340 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
5342 }
5343 cifs_buf_release(pSMB);
5344
5345 if (rc == -EAGAIN)
5346 goto QFSUnixRetry;
5347
5348
5349 return rc;
5350}
5351
Jeremy Allisonac670552005-06-22 17:26:35 -07005352int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005353CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005354{
5355/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5356 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5357 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5358 int rc = 0;
5359 int bytes_returned = 0;
5360 __u16 params, param_offset, offset, byte_count;
5361
Joe Perchesf96637b2013-05-04 22:12:25 -05005362 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005363SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005364 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005365 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5366 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005367 if (rc)
5368 return rc;
5369
5370 params = 4; /* 2 bytes zero followed by info level. */
5371 pSMB->MaxSetupCount = 0;
5372 pSMB->Reserved = 0;
5373 pSMB->Flags = 0;
5374 pSMB->Timeout = 0;
5375 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005376 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5377 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005378 offset = param_offset + params;
5379
5380 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005381 /* BB find exact max SMB PDU from sess structure BB */
5382 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005383 pSMB->SetupCount = 1;
5384 pSMB->Reserved3 = 0;
5385 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5386 byte_count = 1 /* pad */ + params + 12;
5387
5388 pSMB->DataCount = cpu_to_le16(12);
5389 pSMB->ParameterCount = cpu_to_le16(params);
5390 pSMB->TotalDataCount = pSMB->DataCount;
5391 pSMB->TotalParameterCount = pSMB->ParameterCount;
5392 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5393 pSMB->DataOffset = cpu_to_le16(offset);
5394
5395 /* Params. */
5396 pSMB->FileNum = 0;
5397 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5398
5399 /* Data. */
5400 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5401 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5402 pSMB->ClientUnixCap = cpu_to_le64(cap);
5403
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005404 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005405 pSMB->ByteCount = cpu_to_le16(byte_count);
5406
5407 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5408 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5409 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005410 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005411 } else { /* decode response */
5412 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005413 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005414 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005415 }
5416 cifs_buf_release(pSMB);
5417
5418 if (rc == -EAGAIN)
5419 goto SETFSUnixRetry;
5420
5421 return rc;
5422}
5423
5424
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005427CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005428 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429{
5430/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5431 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5432 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5433 FILE_SYSTEM_POSIX_INFO *response_data;
5434 int rc = 0;
5435 int bytes_returned = 0;
5436 __u16 params, byte_count;
5437
Joe Perchesf96637b2013-05-04 22:12:25 -05005438 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439QFSPosixRetry:
5440 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5441 (void **) &pSMBr);
5442 if (rc)
5443 return rc;
5444
5445 params = 2; /* level */
5446 pSMB->TotalDataCount = 0;
5447 pSMB->DataCount = 0;
5448 pSMB->DataOffset = 0;
5449 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005450 /* BB find exact max SMB PDU from sess structure BB */
5451 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 pSMB->MaxSetupCount = 0;
5453 pSMB->Reserved = 0;
5454 pSMB->Flags = 0;
5455 pSMB->Timeout = 0;
5456 pSMB->Reserved2 = 0;
5457 byte_count = params + 1 /* pad */ ;
5458 pSMB->ParameterCount = cpu_to_le16(params);
5459 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005460 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5461 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 pSMB->SetupCount = 1;
5463 pSMB->Reserved3 = 0;
5464 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5465 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005466 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 pSMB->ByteCount = cpu_to_le16(byte_count);
5468
5469 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5470 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5471 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005472 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 } else { /* decode response */
5474 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5475
Jeff Layton820a8032011-05-04 08:05:26 -04005476 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 rc = -EIO; /* bad smb */
5478 } else {
5479 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5480 response_data =
5481 (FILE_SYSTEM_POSIX_INFO
5482 *) (((char *) &pSMBr->hdr.Protocol) +
5483 data_offset);
5484 FSData->f_bsize =
5485 le32_to_cpu(response_data->BlockSize);
5486 FSData->f_blocks =
5487 le64_to_cpu(response_data->TotalBlocks);
5488 FSData->f_bfree =
5489 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005490 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 FSData->f_bavail = FSData->f_bfree;
5492 } else {
5493 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005494 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
Steve French790fe572007-07-07 19:25:05 +00005496 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005498 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005499 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005501 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503 }
5504 cifs_buf_release(pSMB);
5505
5506 if (rc == -EAGAIN)
5507 goto QFSPosixRetry;
5508
5509 return rc;
5510}
5511
5512
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005513/*
5514 * We can not use write of zero bytes trick to set file size due to need for
5515 * large file support. Also note that this SetPathInfo is preferred to
5516 * SetFileInfo based method in next routine which is only needed to work around
5517 * a sharing violation bugin Samba which this routine can run into.
5518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005520CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005521 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5522 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
5524 struct smb_com_transaction2_spi_req *pSMB = NULL;
5525 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5526 struct file_end_of_file_info *parm_data;
5527 int name_len;
5528 int rc = 0;
5529 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005530 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 __u16 params, byte_count, data_count, param_offset, offset;
5533
Joe Perchesf96637b2013-05-04 22:12:25 -05005534 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535SetEOFRetry:
5536 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5537 (void **) &pSMBr);
5538 if (rc)
5539 return rc;
5540
5541 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5542 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005543 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5544 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 name_len++; /* trailing null */
5546 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005547 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005548 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005550 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 }
5552 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005553 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005555 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 pSMB->MaxSetupCount = 0;
5557 pSMB->Reserved = 0;
5558 pSMB->Flags = 0;
5559 pSMB->Timeout = 0;
5560 pSMB->Reserved2 = 0;
5561 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005562 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005564 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005565 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5566 pSMB->InformationLevel =
5567 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5568 else
5569 pSMB->InformationLevel =
5570 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5571 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5573 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005574 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 else
5576 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005577 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
5579
5580 parm_data =
5581 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5582 offset);
5583 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5584 pSMB->DataOffset = cpu_to_le16(offset);
5585 pSMB->SetupCount = 1;
5586 pSMB->Reserved3 = 0;
5587 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5588 byte_count = 3 /* pad */ + params + data_count;
5589 pSMB->DataCount = cpu_to_le16(data_count);
5590 pSMB->TotalDataCount = pSMB->DataCount;
5591 pSMB->ParameterCount = cpu_to_le16(params);
5592 pSMB->TotalParameterCount = pSMB->ParameterCount;
5593 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005594 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 parm_data->FileSize = cpu_to_le64(size);
5596 pSMB->ByteCount = cpu_to_le16(byte_count);
5597 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5598 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005599 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005600 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 cifs_buf_release(pSMB);
5603
5604 if (rc == -EAGAIN)
5605 goto SetEOFRetry;
5606
5607 return rc;
5608}
5609
5610int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005611CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5612 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613{
5614 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 struct file_end_of_file_info *parm_data;
5616 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 __u16 params, param_offset, offset, byte_count, count;
5618
Joe Perchesf96637b2013-05-04 22:12:25 -05005619 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5620 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005621 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 if (rc)
5624 return rc;
5625
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005626 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5627 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 params = 6;
5630 pSMB->MaxSetupCount = 0;
5631 pSMB->Reserved = 0;
5632 pSMB->Flags = 0;
5633 pSMB->Timeout = 0;
5634 pSMB->Reserved2 = 0;
5635 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5636 offset = param_offset + params;
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 count = sizeof(struct file_end_of_file_info);
5639 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005640 /* BB find exact max SMB PDU from sess structure BB */
5641 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 pSMB->SetupCount = 1;
5643 pSMB->Reserved3 = 0;
5644 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5645 byte_count = 3 /* pad */ + params + count;
5646 pSMB->DataCount = cpu_to_le16(count);
5647 pSMB->ParameterCount = cpu_to_le16(params);
5648 pSMB->TotalDataCount = pSMB->DataCount;
5649 pSMB->TotalParameterCount = pSMB->ParameterCount;
5650 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5651 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005652 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5653 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 pSMB->DataOffset = cpu_to_le16(offset);
5655 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005656 pSMB->Fid = cfile->fid.netfid;
5657 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5659 pSMB->InformationLevel =
5660 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5661 else
5662 pSMB->InformationLevel =
5663 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005664 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5666 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005667 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 else
5669 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005670 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005673 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005677 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5678 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680
Steve French50c2f752007-07-13 00:33:32 +00005681 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 since file handle passed in no longer valid */
5683
5684 return rc;
5685}
5686
Steve French50c2f752007-07-13 00:33:32 +00005687/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 an open handle, rather than by pathname - this is awkward due to
5689 potential access conflicts on the open, but it is unavoidable for these
5690 old servers since the only other choice is to go from 100 nanosecond DCE
5691 time and resort to the original setpathinfo level which takes the ancient
5692 DOS time format with 2 second granularity */
5693int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005694CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005695 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696{
5697 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 char *data_offset;
5699 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 __u16 params, param_offset, offset, byte_count, count;
5701
Joe Perchesf96637b2013-05-04 22:12:25 -05005702 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005703 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 if (rc)
5706 return rc;
5707
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005708 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5709 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 params = 6;
5712 pSMB->MaxSetupCount = 0;
5713 pSMB->Reserved = 0;
5714 pSMB->Flags = 0;
5715 pSMB->Timeout = 0;
5716 pSMB->Reserved2 = 0;
5717 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5718 offset = param_offset + params;
5719
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005720 data_offset = (char *)pSMB +
5721 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Steve French26f57362007-08-30 22:09:15 +00005723 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005725 /* BB find max SMB PDU from sess */
5726 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 pSMB->SetupCount = 1;
5728 pSMB->Reserved3 = 0;
5729 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5730 byte_count = 3 /* pad */ + params + count;
5731 pSMB->DataCount = cpu_to_le16(count);
5732 pSMB->ParameterCount = cpu_to_le16(params);
5733 pSMB->TotalDataCount = pSMB->DataCount;
5734 pSMB->TotalParameterCount = pSMB->ParameterCount;
5735 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5736 pSMB->DataOffset = cpu_to_le16(offset);
5737 pSMB->Fid = fid;
5738 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5739 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5740 else
5741 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5742 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005743 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005745 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005746 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005747 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005748 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5749 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Steve French50c2f752007-07-13 00:33:32 +00005751 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 since file handle passed in no longer valid */
5753
5754 return rc;
5755}
5756
Jeff Layton6d22f092008-09-23 11:48:35 -04005757int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005758CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005759 bool delete_file, __u16 fid, __u32 pid_of_opener)
5760{
5761 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5762 char *data_offset;
5763 int rc = 0;
5764 __u16 params, param_offset, offset, byte_count, count;
5765
Joe Perchesf96637b2013-05-04 22:12:25 -05005766 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005767 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5768
5769 if (rc)
5770 return rc;
5771
5772 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5773 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5774
5775 params = 6;
5776 pSMB->MaxSetupCount = 0;
5777 pSMB->Reserved = 0;
5778 pSMB->Flags = 0;
5779 pSMB->Timeout = 0;
5780 pSMB->Reserved2 = 0;
5781 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5782 offset = param_offset + params;
5783
5784 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5785
5786 count = 1;
5787 pSMB->MaxParameterCount = cpu_to_le16(2);
5788 /* BB find max SMB PDU from sess */
5789 pSMB->MaxDataCount = cpu_to_le16(1000);
5790 pSMB->SetupCount = 1;
5791 pSMB->Reserved3 = 0;
5792 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5793 byte_count = 3 /* pad */ + params + count;
5794 pSMB->DataCount = cpu_to_le16(count);
5795 pSMB->ParameterCount = cpu_to_le16(params);
5796 pSMB->TotalDataCount = pSMB->DataCount;
5797 pSMB->TotalParameterCount = pSMB->ParameterCount;
5798 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5799 pSMB->DataOffset = cpu_to_le16(offset);
5800 pSMB->Fid = fid;
5801 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5802 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005803 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005804 pSMB->ByteCount = cpu_to_le16(byte_count);
5805 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005806 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005807 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005808 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005809
5810 return rc;
5811}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812
5813int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005814CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005815 const char *fileName, const FILE_BASIC_INFO *data,
5816 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817{
5818 TRANSACTION2_SPI_REQ *pSMB = NULL;
5819 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5820 int name_len;
5821 int rc = 0;
5822 int bytes_returned = 0;
5823 char *data_offset;
5824 __u16 params, param_offset, offset, byte_count, count;
5825
Joe Perchesf96637b2013-05-04 22:12:25 -05005826 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828SetTimesRetry:
5829 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5830 (void **) &pSMBr);
5831 if (rc)
5832 return rc;
5833
5834 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5835 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005836 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5837 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 name_len++; /* trailing null */
5839 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005840 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 name_len = strnlen(fileName, PATH_MAX);
5842 name_len++; /* trailing null */
5843 strncpy(pSMB->FileName, fileName, name_len);
5844 }
5845
5846 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005847 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005849 /* BB find max SMB PDU from sess structure BB */
5850 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 pSMB->MaxSetupCount = 0;
5852 pSMB->Reserved = 0;
5853 pSMB->Flags = 0;
5854 pSMB->Timeout = 0;
5855 pSMB->Reserved2 = 0;
5856 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005857 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 offset = param_offset + params;
5859 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5860 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5861 pSMB->DataOffset = cpu_to_le16(offset);
5862 pSMB->SetupCount = 1;
5863 pSMB->Reserved3 = 0;
5864 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5865 byte_count = 3 /* pad */ + params + count;
5866
5867 pSMB->DataCount = cpu_to_le16(count);
5868 pSMB->ParameterCount = cpu_to_le16(params);
5869 pSMB->TotalDataCount = pSMB->DataCount;
5870 pSMB->TotalParameterCount = pSMB->ParameterCount;
5871 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5872 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5873 else
5874 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5875 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005876 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005877 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 pSMB->ByteCount = cpu_to_le16(byte_count);
5879 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5880 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005881 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005882 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
5884 cifs_buf_release(pSMB);
5885
5886 if (rc == -EAGAIN)
5887 goto SetTimesRetry;
5888
5889 return rc;
5890}
5891
5892/* Can not be used to set time stamps yet (due to old DOS time format) */
5893/* Can be used to set attributes */
5894#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5895 handling it anyway and NT4 was what we thought it would be needed for
5896 Do not delete it until we prove whether needed for Win9x though */
5897int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005898CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 __u16 dos_attrs, const struct nls_table *nls_codepage)
5900{
5901 SETATTR_REQ *pSMB = NULL;
5902 SETATTR_RSP *pSMBr = NULL;
5903 int rc = 0;
5904 int bytes_returned;
5905 int name_len;
5906
Joe Perchesf96637b2013-05-04 22:12:25 -05005907 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909SetAttrLgcyRetry:
5910 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5911 (void **) &pSMBr);
5912 if (rc)
5913 return rc;
5914
5915 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5916 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005917 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5918 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 name_len++; /* trailing null */
5920 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005921 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 name_len = strnlen(fileName, PATH_MAX);
5923 name_len++; /* trailing null */
5924 strncpy(pSMB->fileName, fileName, name_len);
5925 }
5926 pSMB->attr = cpu_to_le16(dos_attrs);
5927 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005928 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5930 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5931 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005932 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005933 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
5935 cifs_buf_release(pSMB);
5936
5937 if (rc == -EAGAIN)
5938 goto SetAttrLgcyRetry;
5939
5940 return rc;
5941}
5942#endif /* temporarily unneeded SetAttr legacy function */
5943
Jeff Layton654cf142009-07-09 20:02:49 -04005944static void
5945cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5946 const struct cifs_unix_set_info_args *args)
5947{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005948 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005949 u64 mode = args->mode;
5950
Eric W. Biederman49418b22013-02-06 00:57:56 -08005951 if (uid_valid(args->uid))
5952 uid = from_kuid(&init_user_ns, args->uid);
5953 if (gid_valid(args->gid))
5954 gid = from_kgid(&init_user_ns, args->gid);
5955
Jeff Layton654cf142009-07-09 20:02:49 -04005956 /*
5957 * Samba server ignores set of file size to zero due to bugs in some
5958 * older clients, but we should be precise - we use SetFileSize to
5959 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005960 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005961 * zero instead of -1 here
5962 */
5963 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5964 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5965 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5966 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5967 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005968 data_offset->Uid = cpu_to_le64(uid);
5969 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005970 /* better to leave device as zero when it is */
5971 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5972 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5973 data_offset->Permissions = cpu_to_le64(mode);
5974
5975 if (S_ISREG(mode))
5976 data_offset->Type = cpu_to_le32(UNIX_FILE);
5977 else if (S_ISDIR(mode))
5978 data_offset->Type = cpu_to_le32(UNIX_DIR);
5979 else if (S_ISLNK(mode))
5980 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5981 else if (S_ISCHR(mode))
5982 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5983 else if (S_ISBLK(mode))
5984 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5985 else if (S_ISFIFO(mode))
5986 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5987 else if (S_ISSOCK(mode))
5988 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5989}
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005992CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005993 const struct cifs_unix_set_info_args *args,
5994 u16 fid, u32 pid_of_opener)
5995{
5996 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005997 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005998 int rc = 0;
5999 u16 params, param_offset, offset, byte_count, count;
6000
Joe Perchesf96637b2013-05-04 22:12:25 -05006001 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006002 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6003
6004 if (rc)
6005 return rc;
6006
6007 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6008 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6009
6010 params = 6;
6011 pSMB->MaxSetupCount = 0;
6012 pSMB->Reserved = 0;
6013 pSMB->Flags = 0;
6014 pSMB->Timeout = 0;
6015 pSMB->Reserved2 = 0;
6016 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6017 offset = param_offset + params;
6018
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006019 data_offset = (char *)pSMB +
6020 offsetof(struct smb_hdr, Protocol) + offset;
6021
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006022 count = sizeof(FILE_UNIX_BASIC_INFO);
6023
6024 pSMB->MaxParameterCount = cpu_to_le16(2);
6025 /* BB find max SMB PDU from sess */
6026 pSMB->MaxDataCount = cpu_to_le16(1000);
6027 pSMB->SetupCount = 1;
6028 pSMB->Reserved3 = 0;
6029 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6030 byte_count = 3 /* pad */ + params + count;
6031 pSMB->DataCount = cpu_to_le16(count);
6032 pSMB->ParameterCount = cpu_to_le16(params);
6033 pSMB->TotalDataCount = pSMB->DataCount;
6034 pSMB->TotalParameterCount = pSMB->ParameterCount;
6035 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6036 pSMB->DataOffset = cpu_to_le16(offset);
6037 pSMB->Fid = fid;
6038 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6039 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006040 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006041 pSMB->ByteCount = cpu_to_le16(byte_count);
6042
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006043 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006044
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006045 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006046 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006047 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6048 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006049
6050 /* Note: On -EAGAIN error only caller can retry on handle based calls
6051 since file handle passed in no longer valid */
6052
6053 return rc;
6054}
6055
6056int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006057CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006058 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006059 const struct cifs_unix_set_info_args *args,
6060 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061{
6062 TRANSACTION2_SPI_REQ *pSMB = NULL;
6063 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6064 int name_len;
6065 int rc = 0;
6066 int bytes_returned = 0;
6067 FILE_UNIX_BASIC_INFO *data_offset;
6068 __u16 params, param_offset, offset, count, byte_count;
6069
Joe Perchesf96637b2013-05-04 22:12:25 -05006070 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071setPermsRetry:
6072 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6073 (void **) &pSMBr);
6074 if (rc)
6075 return rc;
6076
6077 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6078 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006079 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006080 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 name_len++; /* trailing null */
6082 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006083 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006084 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006086 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 }
6088
6089 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006090 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006092 /* BB find max SMB PDU from sess structure BB */
6093 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 pSMB->MaxSetupCount = 0;
6095 pSMB->Reserved = 0;
6096 pSMB->Flags = 0;
6097 pSMB->Timeout = 0;
6098 pSMB->Reserved2 = 0;
6099 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006100 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 offset = param_offset + params;
6102 data_offset =
6103 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6104 offset);
6105 memset(data_offset, 0, count);
6106 pSMB->DataOffset = cpu_to_le16(offset);
6107 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6108 pSMB->SetupCount = 1;
6109 pSMB->Reserved3 = 0;
6110 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6111 byte_count = 3 /* pad */ + params + count;
6112 pSMB->ParameterCount = cpu_to_le16(params);
6113 pSMB->DataCount = cpu_to_le16(count);
6114 pSMB->TotalParameterCount = pSMB->ParameterCount;
6115 pSMB->TotalDataCount = pSMB->DataCount;
6116 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6117 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006118 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006119
Jeff Layton654cf142009-07-09 20:02:49 -04006120 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
6122 pSMB->ByteCount = cpu_to_le16(byte_count);
6123 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6124 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006125 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006126 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Steve French0d817bc2008-05-22 02:02:03 +00006128 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 if (rc == -EAGAIN)
6130 goto setPermsRetry;
6131 return rc;
6132}
6133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006135/*
6136 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6137 * function used by listxattr and getxattr type calls. When ea_name is set,
6138 * it looks for that attribute name and stuffs that value into the EAData
6139 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6140 * buffer. In both cases, the return value is either the length of the
6141 * resulting data or a negative error code. If EAData is a NULL pointer then
6142 * the data isn't copied to it, but the length is returned.
6143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006145CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006146 const unsigned char *searchName, const unsigned char *ea_name,
6147 char *EAData, size_t buf_size,
6148 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149{
6150 /* BB assumes one setup word */
6151 TRANSACTION2_QPI_REQ *pSMB = NULL;
6152 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6153 int rc = 0;
6154 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006155 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006156 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006157 struct fea *temp_fea;
6158 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006159 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006160 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006161 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Joe Perchesf96637b2013-05-04 22:12:25 -05006163 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164QAllEAsRetry:
6165 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6166 (void **) &pSMBr);
6167 if (rc)
6168 return rc;
6169
6170 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006171 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006172 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6173 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006174 list_len++; /* trailing null */
6175 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006177 list_len = strnlen(searchName, PATH_MAX);
6178 list_len++; /* trailing null */
6179 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 }
6181
Jeff Layton6e462b92010-02-10 16:18:26 -05006182 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 pSMB->TotalDataCount = 0;
6184 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006185 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006186 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 pSMB->MaxSetupCount = 0;
6188 pSMB->Reserved = 0;
6189 pSMB->Flags = 0;
6190 pSMB->Timeout = 0;
6191 pSMB->Reserved2 = 0;
6192 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006193 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 pSMB->DataCount = 0;
6195 pSMB->DataOffset = 0;
6196 pSMB->SetupCount = 1;
6197 pSMB->Reserved3 = 0;
6198 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6199 byte_count = params + 1 /* pad */ ;
6200 pSMB->TotalParameterCount = cpu_to_le16(params);
6201 pSMB->ParameterCount = pSMB->TotalParameterCount;
6202 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6203 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006204 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 pSMB->ByteCount = cpu_to_le16(byte_count);
6206
6207 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6208 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6209 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006210 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006211 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006213
6214
6215 /* BB also check enough total bytes returned */
6216 /* BB we need to improve the validity checking
6217 of these trans2 responses */
6218
6219 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006220 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006221 rc = -EIO; /* bad smb */
6222 goto QAllEAsOut;
6223 }
6224
6225 /* check that length of list is not more than bcc */
6226 /* check that each entry does not go beyond length
6227 of list */
6228 /* check that each element of each entry does not
6229 go beyond end of list */
6230 /* validate_trans2_offsets() */
6231 /* BB check if start of smb + data_offset > &bcc+ bcc */
6232
6233 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6234 ea_response_data = (struct fealist *)
6235 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6236
Jeff Layton6e462b92010-02-10 16:18:26 -05006237 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006238 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006239 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006240 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006241 /* didn't find the named attribute */
6242 if (ea_name)
6243 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006244 goto QAllEAsOut;
6245 }
6246
Jeff Layton0cd126b2010-02-10 16:18:26 -05006247 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006248 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006249 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006250 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006251 rc = -EIO;
6252 goto QAllEAsOut;
6253 }
6254
Jeff Laytonf0d38682010-02-10 16:18:26 -05006255 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006256 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006257 temp_fea = ea_response_data->list;
6258 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006259 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006260 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006261 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006262
Jeff Layton6e462b92010-02-10 16:18:26 -05006263 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006264 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006265 /* make sure we can read name_len and value_len */
6266 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006267 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006268 rc = -EIO;
6269 goto QAllEAsOut;
6270 }
6271
6272 name_len = temp_fea->name_len;
6273 value_len = le16_to_cpu(temp_fea->value_len);
6274 list_len -= name_len + 1 + value_len;
6275 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006276 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006277 rc = -EIO;
6278 goto QAllEAsOut;
6279 }
6280
Jeff Layton31c05192010-02-10 16:18:26 -05006281 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006282 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006283 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006284 temp_ptr += name_len + 1;
6285 rc = value_len;
6286 if (buf_size == 0)
6287 goto QAllEAsOut;
6288 if ((size_t)value_len > buf_size) {
6289 rc = -ERANGE;
6290 goto QAllEAsOut;
6291 }
6292 memcpy(EAData, temp_ptr, value_len);
6293 goto QAllEAsOut;
6294 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006295 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006296 /* account for prefix user. and trailing null */
6297 rc += (5 + 1 + name_len);
6298 if (rc < (int) buf_size) {
6299 memcpy(EAData, "user.", 5);
6300 EAData += 5;
6301 memcpy(EAData, temp_ptr, name_len);
6302 EAData += name_len;
6303 /* null terminate name */
6304 *EAData = 0;
6305 ++EAData;
6306 } else if (buf_size == 0) {
6307 /* skip copy - calc size only */
6308 } else {
6309 /* stop before overrun buffer */
6310 rc = -ERANGE;
6311 break;
6312 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006313 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006314 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006315 temp_fea = (struct fea *)temp_ptr;
6316 }
6317
Jeff Layton31c05192010-02-10 16:18:26 -05006318 /* didn't find the named attribute */
6319 if (ea_name)
6320 rc = -ENODATA;
6321
Jeff Laytonf0d38682010-02-10 16:18:26 -05006322QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006323 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 if (rc == -EAGAIN)
6325 goto QAllEAsRetry;
6326
6327 return (ssize_t)rc;
6328}
6329
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006331CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6332 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006333 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6334 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335{
6336 struct smb_com_transaction2_spi_req *pSMB = NULL;
6337 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6338 struct fealist *parm_data;
6339 int name_len;
6340 int rc = 0;
6341 int bytes_returned = 0;
6342 __u16 params, param_offset, byte_count, offset, count;
6343
Joe Perchesf96637b2013-05-04 22:12:25 -05006344 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345SetEARetry:
6346 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6347 (void **) &pSMBr);
6348 if (rc)
6349 return rc;
6350
6351 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6352 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006353 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6354 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 name_len++; /* trailing null */
6356 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006357 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 name_len = strnlen(fileName, PATH_MAX);
6359 name_len++; /* trailing null */
6360 strncpy(pSMB->FileName, fileName, name_len);
6361 }
6362
6363 params = 6 + name_len;
6364
6365 /* done calculating parms using name_len of file name,
6366 now use name_len to calculate length of ea name
6367 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006368 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 name_len = 0;
6370 else
Steve French50c2f752007-07-13 00:33:32 +00006371 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006373 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006375 /* BB find max SMB PDU from sess */
6376 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 pSMB->MaxSetupCount = 0;
6378 pSMB->Reserved = 0;
6379 pSMB->Flags = 0;
6380 pSMB->Timeout = 0;
6381 pSMB->Reserved2 = 0;
6382 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006383 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 offset = param_offset + params;
6385 pSMB->InformationLevel =
6386 cpu_to_le16(SMB_SET_FILE_EA);
6387
6388 parm_data =
6389 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6390 offset);
6391 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6392 pSMB->DataOffset = cpu_to_le16(offset);
6393 pSMB->SetupCount = 1;
6394 pSMB->Reserved3 = 0;
6395 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6396 byte_count = 3 /* pad */ + params + count;
6397 pSMB->DataCount = cpu_to_le16(count);
6398 parm_data->list_len = cpu_to_le32(count);
6399 parm_data->list[0].EA_flags = 0;
6400 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006401 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006403 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006404 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 parm_data->list[0].name[name_len] = 0;
6406 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6407 /* caller ensures that ea_value_len is less than 64K but
6408 we need to ensure that it fits within the smb */
6409
Steve French50c2f752007-07-13 00:33:32 +00006410 /*BB add length check to see if it would fit in
6411 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006412 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6413 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006414 memcpy(parm_data->list[0].name+name_len+1,
6415 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
6417 pSMB->TotalDataCount = pSMB->DataCount;
6418 pSMB->ParameterCount = cpu_to_le16(params);
6419 pSMB->TotalParameterCount = pSMB->ParameterCount;
6420 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006421 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 pSMB->ByteCount = cpu_to_le16(byte_count);
6423 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6424 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006425 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006426 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
6428 cifs_buf_release(pSMB);
6429
6430 if (rc == -EAGAIN)
6431 goto SetEARetry;
6432
6433 return rc;
6434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435#endif
Steve French0eff0e22011-02-24 05:39:23 +00006436
6437#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6438/*
6439 * Years ago the kernel added a "dnotify" function for Samba server,
6440 * to allow network clients (such as Windows) to display updated
6441 * lists of files in directory listings automatically when
6442 * files are added by one user when another user has the
6443 * same directory open on their desktop. The Linux cifs kernel
6444 * client hooked into the kernel side of this interface for
6445 * the same reason, but ironically when the VFS moved from
6446 * "dnotify" to "inotify" it became harder to plug in Linux
6447 * network file system clients (the most obvious use case
6448 * for notify interfaces is when multiple users can update
6449 * the contents of the same directory - exactly what network
6450 * file systems can do) although the server (Samba) could
6451 * still use it. For the short term we leave the worker
6452 * function ifdeffed out (below) until inotify is fixed
6453 * in the VFS to make it easier to plug in network file
6454 * system clients. If inotify turns out to be permanently
6455 * incompatible for network fs clients, we could instead simply
6456 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6457 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006458int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006459 const int notify_subdirs, const __u16 netfid,
6460 __u32 filter, struct file *pfile, int multishot,
6461 const struct nls_table *nls_codepage)
6462{
6463 int rc = 0;
6464 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6465 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6466 struct dir_notify_req *dnotify_req;
6467 int bytes_returned;
6468
Joe Perchesf96637b2013-05-04 22:12:25 -05006469 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006470 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6471 (void **) &pSMBr);
6472 if (rc)
6473 return rc;
6474
6475 pSMB->TotalParameterCount = 0 ;
6476 pSMB->TotalDataCount = 0;
6477 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006478 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006479 pSMB->MaxSetupCount = 4;
6480 pSMB->Reserved = 0;
6481 pSMB->ParameterOffset = 0;
6482 pSMB->DataCount = 0;
6483 pSMB->DataOffset = 0;
6484 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6485 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6486 pSMB->ParameterCount = pSMB->TotalParameterCount;
6487 if (notify_subdirs)
6488 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6489 pSMB->Reserved2 = 0;
6490 pSMB->CompletionFilter = cpu_to_le32(filter);
6491 pSMB->Fid = netfid; /* file handle always le */
6492 pSMB->ByteCount = 0;
6493
6494 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6495 (struct smb_hdr *)pSMBr, &bytes_returned,
6496 CIFS_ASYNC_OP);
6497 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006498 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006499 } else {
6500 /* Add file to outstanding requests */
6501 /* BB change to kmem cache alloc */
6502 dnotify_req = kmalloc(
6503 sizeof(struct dir_notify_req),
6504 GFP_KERNEL);
6505 if (dnotify_req) {
6506 dnotify_req->Pid = pSMB->hdr.Pid;
6507 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6508 dnotify_req->Mid = pSMB->hdr.Mid;
6509 dnotify_req->Tid = pSMB->hdr.Tid;
6510 dnotify_req->Uid = pSMB->hdr.Uid;
6511 dnotify_req->netfid = netfid;
6512 dnotify_req->pfile = pfile;
6513 dnotify_req->filter = filter;
6514 dnotify_req->multishot = multishot;
6515 spin_lock(&GlobalMid_Lock);
6516 list_add_tail(&dnotify_req->lhead,
6517 &GlobalDnotifyReqList);
6518 spin_unlock(&GlobalMid_Lock);
6519 } else
6520 rc = -ENOMEM;
6521 }
6522 cifs_buf_release(pSMB);
6523 return rc;
6524}
6525#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */