blob: f26ffbfc64d8b4eca26b8e8101f705043fc7a4a0 [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;
David Howells2b0143b2015-03-17 22:25:59 +00001901 struct inode *inode = d_inode(wdata->cfile->dentry);
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);
David Howells2b0143b2015-03-17 22:25:59 +00001984 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001985 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,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002787 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
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 =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002807 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2808 /* find define for this maxpathcomponent */
2809 PATH_MAX, nls_codepage, remap);
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 =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002831 cifsConvertToUTF16((__le16 *) data_offset, toName,
2832 /* find define for this maxpathcomponent */
2833 PATH_MAX, nls_codepage, remap);
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,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003037 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
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 =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003058 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3059 searchName, PATH_MAX, nls_codepage,
3060 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 name_len++; /* trailing null */
3062 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003063 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 name_len = strnlen(searchName, PATH_MAX);
3065 name_len++; /* trailing null */
3066 strncpy(pSMB->FileName, searchName, name_len);
3067 }
3068
3069 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3070 pSMB->TotalDataCount = 0;
3071 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003072 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 pSMB->MaxSetupCount = 0;
3074 pSMB->Reserved = 0;
3075 pSMB->Flags = 0;
3076 pSMB->Timeout = 0;
3077 pSMB->Reserved2 = 0;
3078 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003079 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 pSMB->DataCount = 0;
3081 pSMB->DataOffset = 0;
3082 pSMB->SetupCount = 1;
3083 pSMB->Reserved3 = 0;
3084 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3085 byte_count = params + 1 /* pad */ ;
3086 pSMB->TotalParameterCount = cpu_to_le16(params);
3087 pSMB->ParameterCount = pSMB->TotalParameterCount;
3088 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3089 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003090 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 pSMB->ByteCount = cpu_to_le16(byte_count);
3092
3093 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3094 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3095 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003096 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 } else {
3098 /* decode response */
3099
3100 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003102 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003103 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003105 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003106 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Jeff Layton460b9692009-04-30 07:17:56 -04003108 data_start = ((char *) &pSMBr->hdr.Protocol) +
3109 le16_to_cpu(pSMBr->t2.DataOffset);
3110
Steve French0e0d2cf2009-05-01 05:27:32 +00003111 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3112 is_unicode = true;
3113 else
3114 is_unicode = false;
3115
Steve French737b7582005-04-28 22:41:06 -07003116 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003117 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3118 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003119 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003120 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
3122 }
3123 cifs_buf_release(pSMB);
3124 if (rc == -EAGAIN)
3125 goto querySymLinkRetry;
3126 return rc;
3127}
3128
Steve Frenchc52a9552011-02-24 06:16:22 +00003129/*
3130 * Recent Windows versions now create symlinks more frequently
3131 * and they use the "reparse point" mechanism below. We can of course
3132 * do symlinks nicely to Samba and other servers which support the
3133 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3134 * "MF" symlinks optionally, but for recent Windows we really need to
3135 * reenable the code below and fix the cifs_symlink callers to handle this.
3136 * In the interim this code has been moved to its own config option so
3137 * it is not compiled in by default until callers fixed up and more tested.
3138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003140CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3141 __u16 fid, char **symlinkinfo,
3142 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 int rc = 0;
3145 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003146 struct smb_com_transaction_ioctl_req *pSMB;
3147 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003148 bool is_unicode;
3149 unsigned int sub_len;
3150 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003151 struct reparse_symlink_data *reparse_buf;
3152 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003153 __u32 data_offset, data_count;
3154 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003156 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3158 (void **) &pSMBr);
3159 if (rc)
3160 return rc;
3161
3162 pSMB->TotalParameterCount = 0 ;
3163 pSMB->TotalDataCount = 0;
3164 pSMB->MaxParameterCount = cpu_to_le32(2);
3165 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003166 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 pSMB->MaxSetupCount = 4;
3168 pSMB->Reserved = 0;
3169 pSMB->ParameterOffset = 0;
3170 pSMB->DataCount = 0;
3171 pSMB->DataOffset = 0;
3172 pSMB->SetupCount = 4;
3173 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3174 pSMB->ParameterCount = pSMB->TotalParameterCount;
3175 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3176 pSMB->IsFsctl = 1; /* FSCTL */
3177 pSMB->IsRootFlag = 0;
3178 pSMB->Fid = fid; /* file handle always le */
3179 pSMB->ByteCount = 0;
3180
3181 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3182 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3183 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003184 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003185 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
Steve French989c7e52009-05-02 05:32:20 +00003187
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003188 data_offset = le32_to_cpu(pSMBr->DataOffset);
3189 data_count = le32_to_cpu(pSMBr->DataCount);
3190 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3191 /* BB also check enough total bytes returned */
3192 rc = -EIO; /* bad smb */
3193 goto qreparse_out;
3194 }
3195 if (!data_count || (data_count > 2048)) {
3196 rc = -EIO;
3197 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3198 goto qreparse_out;
3199 }
3200 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003201 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003202 ((char *)&pSMBr->hdr.Protocol + data_offset);
3203 if ((char *)reparse_buf >= end_of_smb) {
3204 rc = -EIO;
3205 goto qreparse_out;
3206 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003207 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3208 cifs_dbg(FYI, "NFS style reparse tag\n");
3209 posix_buf = (struct reparse_posix_data *)reparse_buf;
3210
3211 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3212 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3213 le64_to_cpu(posix_buf->InodeType));
3214 rc = -EOPNOTSUPP;
3215 goto qreparse_out;
3216 }
3217 is_unicode = true;
3218 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3219 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3220 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3221 rc = -EIO;
3222 goto qreparse_out;
3223 }
3224 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3225 sub_len, is_unicode, nls_codepage);
3226 goto qreparse_out;
3227 } else if (reparse_buf->ReparseTag !=
3228 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3229 rc = -EOPNOTSUPP;
3230 goto qreparse_out;
3231 }
3232
3233 /* Reparse tag is NTFS symlink */
3234 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3235 reparse_buf->PathBuffer;
3236 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3237 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003238 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3239 rc = -EIO;
3240 goto qreparse_out;
3241 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003242 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3243 is_unicode = true;
3244 else
3245 is_unicode = false;
3246
3247 /* BB FIXME investigate remapping reserved chars here */
3248 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3249 nls_codepage);
3250 if (!*symlinkinfo)
3251 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003253 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003255 /*
3256 * Note: On -EAGAIN error only caller can retry on handle based calls
3257 * since file handle passed in no longer valid.
3258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return rc;
3260}
3261
Steve Frenchc7f508a2013-10-14 15:27:32 -05003262int
3263CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3264 __u16 fid)
3265{
3266 int rc = 0;
3267 int bytes_returned;
3268 struct smb_com_transaction_compr_ioctl_req *pSMB;
3269 struct smb_com_transaction_ioctl_rsp *pSMBr;
3270
3271 cifs_dbg(FYI, "Set compression for %u\n", fid);
3272 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3273 (void **) &pSMBr);
3274 if (rc)
3275 return rc;
3276
3277 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3278
3279 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003280 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003281 pSMB->MaxParameterCount = 0;
3282 pSMB->MaxDataCount = 0;
3283 pSMB->MaxSetupCount = 4;
3284 pSMB->Reserved = 0;
3285 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003286 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003287 pSMB->DataOffset =
3288 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3289 compression_state) - 4); /* 84 */
3290 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003291 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003292 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003293 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003294 pSMB->IsFsctl = 1; /* FSCTL */
3295 pSMB->IsRootFlag = 0;
3296 pSMB->Fid = fid; /* file handle always le */
3297 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003298 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003299 inc_rfc1001_len(pSMB, 5);
3300
3301 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3302 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3303 if (rc)
3304 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3305
3306 cifs_buf_release(pSMB);
3307
3308 /*
3309 * Note: On -EAGAIN error only caller can retry on handle based calls
3310 * since file handle passed in no longer valid.
3311 */
3312 return rc;
3313}
3314
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316#ifdef CONFIG_CIFS_POSIX
3317
3318/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003319static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3320 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
3322 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003323 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3324 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3325 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003326/*
3327 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3328 ace->e_perm, ace->e_tag, ace->e_id);
3329*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 return;
3332}
3333
3334/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003335static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3336 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
3338 int size = 0;
3339 int i;
3340 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003341 struct cifs_posix_ace *pACE;
3342 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3343 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
3345 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3346 return -EOPNOTSUPP;
3347
Steve French790fe572007-07-07 19:25:05 +00003348 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 count = le16_to_cpu(cifs_acl->access_entry_count);
3350 pACE = &cifs_acl->ace_array[0];
3351 size = sizeof(struct cifs_posix_acl);
3352 size += sizeof(struct cifs_posix_ace) * count;
3353 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003354 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003355 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3356 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 return -EINVAL;
3358 }
Steve French790fe572007-07-07 19:25:05 +00003359 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 count = le16_to_cpu(cifs_acl->access_entry_count);
3361 size = sizeof(struct cifs_posix_acl);
3362 size += sizeof(struct cifs_posix_ace) * count;
3363/* skip past access ACEs to get to default ACEs */
3364 pACE = &cifs_acl->ace_array[count];
3365 count = le16_to_cpu(cifs_acl->default_entry_count);
3366 size += sizeof(struct cifs_posix_ace) * count;
3367 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003368 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return -EINVAL;
3370 } else {
3371 /* illegal type */
3372 return -EINVAL;
3373 }
3374
3375 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003376 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003377 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003378 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return -ERANGE;
3380 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003381 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003382 for (i = 0; i < count ; i++) {
3383 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3384 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386 }
3387 return size;
3388}
3389
Steve French50c2f752007-07-13 00:33:32 +00003390static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3391 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 __u16 rc = 0; /* 0 = ACL converted ok */
3394
Steve Frenchff7feac2005-11-15 16:45:16 -08003395 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3396 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003398 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 /* Probably no need to le convert -1 on any arch but can not hurt */
3400 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003401 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003402 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003403/*
3404 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3405 ace->e_perm, ace->e_tag, ace->e_id);
3406*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return rc;
3408}
3409
3410/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003411static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3412 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
3414 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003415 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3416 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 int count;
3418 int i;
3419
Steve French790fe572007-07-07 19:25:05 +00003420 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return 0;
3422
3423 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003424 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3425 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003426 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003427 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3428 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return 0;
3430 }
3431 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003432 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003433 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003434 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003435 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003436 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003437 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003438 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003439 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return 0;
3441 }
Steve French50c2f752007-07-13 00:33:32 +00003442 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3444 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003445 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /* ACE not converted */
3447 break;
3448 }
3449 }
Steve French790fe572007-07-07 19:25:05 +00003450 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3452 rc += sizeof(struct cifs_posix_acl);
3453 /* BB add check to make sure ACL does not overflow SMB */
3454 }
3455 return rc;
3456}
3457
3458int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003459CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003460 const unsigned char *searchName,
3461 char *acl_inf, const int buflen, const int acl_type,
3462 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464/* SMB_QUERY_POSIX_ACL */
3465 TRANSACTION2_QPI_REQ *pSMB = NULL;
3466 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3467 int rc = 0;
3468 int bytes_returned;
3469 int name_len;
3470 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003471
Joe Perchesf96637b2013-05-04 22:12:25 -05003472 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474queryAclRetry:
3475 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3476 (void **) &pSMBr);
3477 if (rc)
3478 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3481 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003482 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3483 searchName, PATH_MAX, nls_codepage,
3484 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 name_len++; /* trailing null */
3486 name_len *= 2;
3487 pSMB->FileName[name_len] = 0;
3488 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003489 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 name_len = strnlen(searchName, PATH_MAX);
3491 name_len++; /* trailing null */
3492 strncpy(pSMB->FileName, searchName, name_len);
3493 }
3494
3495 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3496 pSMB->TotalDataCount = 0;
3497 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003498 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 pSMB->MaxDataCount = cpu_to_le16(4000);
3500 pSMB->MaxSetupCount = 0;
3501 pSMB->Reserved = 0;
3502 pSMB->Flags = 0;
3503 pSMB->Timeout = 0;
3504 pSMB->Reserved2 = 0;
3505 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003506 offsetof(struct smb_com_transaction2_qpi_req,
3507 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 pSMB->DataCount = 0;
3509 pSMB->DataOffset = 0;
3510 pSMB->SetupCount = 1;
3511 pSMB->Reserved3 = 0;
3512 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3513 byte_count = params + 1 /* pad */ ;
3514 pSMB->TotalParameterCount = cpu_to_le16(params);
3515 pSMB->ParameterCount = pSMB->TotalParameterCount;
3516 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3517 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003518 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 pSMB->ByteCount = cpu_to_le16(byte_count);
3520
3521 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3522 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003523 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003525 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 } else {
3527 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003531 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 rc = -EIO; /* bad smb */
3533 else {
3534 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3535 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3536 rc = cifs_copy_posix_acl(acl_inf,
3537 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003538 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
3540 }
3541 cifs_buf_release(pSMB);
3542 if (rc == -EAGAIN)
3543 goto queryAclRetry;
3544 return rc;
3545}
3546
3547int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003548CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003549 const unsigned char *fileName,
3550 const char *local_acl, const int buflen,
3551 const int acl_type,
3552 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
3554 struct smb_com_transaction2_spi_req *pSMB = NULL;
3555 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3556 char *parm_data;
3557 int name_len;
3558 int rc = 0;
3559 int bytes_returned = 0;
3560 __u16 params, byte_count, data_count, param_offset, offset;
3561
Joe Perchesf96637b2013-05-04 22:12:25 -05003562 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563setAclRetry:
3564 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003565 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (rc)
3567 return rc;
3568 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3569 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003570 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3571 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 name_len++; /* trailing null */
3573 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003574 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 name_len = strnlen(fileName, PATH_MAX);
3576 name_len++; /* trailing null */
3577 strncpy(pSMB->FileName, fileName, name_len);
3578 }
3579 params = 6 + name_len;
3580 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003581 /* BB find max SMB size from sess */
3582 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 pSMB->MaxSetupCount = 0;
3584 pSMB->Reserved = 0;
3585 pSMB->Flags = 0;
3586 pSMB->Timeout = 0;
3587 pSMB->Reserved2 = 0;
3588 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003589 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 offset = param_offset + params;
3591 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3592 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3593
3594 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003595 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Steve French790fe572007-07-07 19:25:05 +00003597 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 rc = -EOPNOTSUPP;
3599 goto setACLerrorExit;
3600 }
3601 pSMB->DataOffset = cpu_to_le16(offset);
3602 pSMB->SetupCount = 1;
3603 pSMB->Reserved3 = 0;
3604 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3605 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3606 byte_count = 3 /* pad */ + params + data_count;
3607 pSMB->DataCount = cpu_to_le16(data_count);
3608 pSMB->TotalDataCount = pSMB->DataCount;
3609 pSMB->ParameterCount = cpu_to_le16(params);
3610 pSMB->TotalParameterCount = pSMB->ParameterCount;
3611 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003612 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 pSMB->ByteCount = cpu_to_le16(byte_count);
3614 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003615 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003616 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003617 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619setACLerrorExit:
3620 cifs_buf_release(pSMB);
3621 if (rc == -EAGAIN)
3622 goto setAclRetry;
3623 return rc;
3624}
3625
Steve Frenchf654bac2005-04-28 22:41:04 -07003626/* BB fix tabs in this function FIXME BB */
3627int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003628CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003629 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003630{
Steve French50c2f752007-07-13 00:33:32 +00003631 int rc = 0;
3632 struct smb_t2_qfi_req *pSMB = NULL;
3633 struct smb_t2_qfi_rsp *pSMBr = NULL;
3634 int bytes_returned;
3635 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003636
Joe Perchesf96637b2013-05-04 22:12:25 -05003637 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003638 if (tcon == NULL)
3639 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003640
3641GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003642 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3643 (void **) &pSMBr);
3644 if (rc)
3645 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003646
Steve Frenchad7a2922008-02-07 23:25:02 +00003647 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003648 pSMB->t2.TotalDataCount = 0;
3649 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3650 /* BB find exact max data count below from sess structure BB */
3651 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3652 pSMB->t2.MaxSetupCount = 0;
3653 pSMB->t2.Reserved = 0;
3654 pSMB->t2.Flags = 0;
3655 pSMB->t2.Timeout = 0;
3656 pSMB->t2.Reserved2 = 0;
3657 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3658 Fid) - 4);
3659 pSMB->t2.DataCount = 0;
3660 pSMB->t2.DataOffset = 0;
3661 pSMB->t2.SetupCount = 1;
3662 pSMB->t2.Reserved3 = 0;
3663 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3664 byte_count = params + 1 /* pad */ ;
3665 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3666 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3667 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3668 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003669 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003670 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003671 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003672
Steve French790fe572007-07-07 19:25:05 +00003673 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3674 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3675 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003676 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003677 } else {
3678 /* decode response */
3679 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003680 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003681 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003682 /* If rc should we check for EOPNOSUPP and
3683 disable the srvino flag? or in caller? */
3684 rc = -EIO; /* bad smb */
3685 else {
3686 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3687 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3688 struct file_chattr_info *pfinfo;
3689 /* BB Do we need a cast or hash here ? */
3690 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003691 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003692 rc = -EIO;
3693 goto GetExtAttrOut;
3694 }
3695 pfinfo = (struct file_chattr_info *)
3696 (data_offset + (char *) &pSMBr->hdr.Protocol);
3697 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003698 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003699 }
3700 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003701GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003702 cifs_buf_release(pSMB);
3703 if (rc == -EAGAIN)
3704 goto GetExtAttrRetry;
3705 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003706}
3707
Steve Frenchf654bac2005-04-28 22:41:04 -07003708#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Jeff Layton79df1ba2010-12-06 12:52:08 -05003710#ifdef CONFIG_CIFS_ACL
3711/*
3712 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3713 * all NT TRANSACTS that we init here have total parm and data under about 400
3714 * bytes (to fit in small cifs buffer size), which is the case so far, it
3715 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3716 * returned setup area) and MaxParameterCount (returned parms size) must be set
3717 * by caller
3718 */
3719static int
3720smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003721 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003722 void **ret_buf)
3723{
3724 int rc;
3725 __u32 temp_offset;
3726 struct smb_com_ntransact_req *pSMB;
3727
3728 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3729 (void **)&pSMB);
3730 if (rc)
3731 return rc;
3732 *ret_buf = (void *)pSMB;
3733 pSMB->Reserved = 0;
3734 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3735 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003736 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003737 pSMB->ParameterCount = pSMB->TotalParameterCount;
3738 pSMB->DataCount = pSMB->TotalDataCount;
3739 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3740 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3741 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3742 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3743 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3744 pSMB->SubCommand = cpu_to_le16(sub_command);
3745 return 0;
3746}
3747
3748static int
3749validate_ntransact(char *buf, char **ppparm, char **ppdata,
3750 __u32 *pparmlen, __u32 *pdatalen)
3751{
3752 char *end_of_smb;
3753 __u32 data_count, data_offset, parm_count, parm_offset;
3754 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003755 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003756
3757 *pdatalen = 0;
3758 *pparmlen = 0;
3759
3760 if (buf == NULL)
3761 return -EINVAL;
3762
3763 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3764
Jeff Layton820a8032011-05-04 08:05:26 -04003765 bcc = get_bcc(&pSMBr->hdr);
3766 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003767 (char *)&pSMBr->ByteCount;
3768
3769 data_offset = le32_to_cpu(pSMBr->DataOffset);
3770 data_count = le32_to_cpu(pSMBr->DataCount);
3771 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3772 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3773
3774 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3775 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3776
3777 /* should we also check that parm and data areas do not overlap? */
3778 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003779 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003780 return -EINVAL;
3781 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003782 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003783 return -EINVAL;
3784 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003785 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003786 return -EINVAL;
3787 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003788 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3789 *ppdata, data_count, (data_count + *ppdata),
3790 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003791 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003792 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003793 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003794 return -EINVAL;
3795 }
3796 *pdatalen = data_count;
3797 *pparmlen = parm_count;
3798 return 0;
3799}
3800
Steve French0a4b92c2006-01-12 15:44:21 -08003801/* Get Security Descriptor (by handle) from remote server for a file or dir */
3802int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003803CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003804 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003805{
3806 int rc = 0;
3807 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003808 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003809 struct kvec iov[1];
3810
Joe Perchesf96637b2013-05-04 22:12:25 -05003811 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003812
Steve French630f3f0c2007-10-25 21:17:17 +00003813 *pbuflen = 0;
3814 *acl_inf = NULL;
3815
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003816 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003817 8 /* parm len */, tcon, (void **) &pSMB);
3818 if (rc)
3819 return rc;
3820
3821 pSMB->MaxParameterCount = cpu_to_le32(4);
3822 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3823 pSMB->MaxSetupCount = 0;
3824 pSMB->Fid = fid; /* file handle always le */
3825 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3826 CIFS_ACL_DACL);
3827 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003828 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003829 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003830 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003831
Steve Frencha761ac52007-10-18 21:45:27 +00003832 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003833 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003834 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003835 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003836 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003837 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003838 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003839 __u32 parm_len;
3840 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003841 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003842 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003843
3844/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003845 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003846 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003847 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003848 goto qsec_out;
3849 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3850
Joe Perchesf96637b2013-05-04 22:12:25 -05003851 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3852 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003853
3854 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3855 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003856 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003857 goto qsec_out;
3858 }
3859
3860/* BB check that data area is minimum length and as big as acl_len */
3861
Steve Frenchaf6f4612007-10-16 18:40:37 +00003862 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003863 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003864 cifs_dbg(VFS, "acl length %d does not match %d\n",
3865 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003866 if (*pbuflen > acl_len)
3867 *pbuflen = acl_len;
3868 }
Steve French0a4b92c2006-01-12 15:44:21 -08003869
Steve French630f3f0c2007-10-25 21:17:17 +00003870 /* check if buffer is big enough for the acl
3871 header followed by the smallest SID */
3872 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3873 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003874 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003875 rc = -EINVAL;
3876 *pbuflen = 0;
3877 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003878 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003879 if (*acl_inf == NULL) {
3880 *pbuflen = 0;
3881 rc = -ENOMEM;
3882 }
Steve French630f3f0c2007-10-25 21:17:17 +00003883 }
Steve French0a4b92c2006-01-12 15:44:21 -08003884 }
3885qsec_out:
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01003886 free_rsp_buf(buf_type, iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003887/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003888 return rc;
3889}
Steve French97837582007-12-31 07:47:21 +00003890
3891int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003892CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003893 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003894{
3895 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3896 int rc = 0;
3897 int bytes_returned = 0;
3898 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003899 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003900
3901setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003902 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003903 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003904 return rc;
Steve French97837582007-12-31 07:47:21 +00003905
3906 pSMB->MaxSetupCount = 0;
3907 pSMB->Reserved = 0;
3908
3909 param_count = 8;
3910 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3911 data_count = acllen;
3912 data_offset = param_offset + param_count;
3913 byte_count = 3 /* pad */ + param_count;
3914
3915 pSMB->DataCount = cpu_to_le32(data_count);
3916 pSMB->TotalDataCount = pSMB->DataCount;
3917 pSMB->MaxParameterCount = cpu_to_le32(4);
3918 pSMB->MaxDataCount = cpu_to_le32(16384);
3919 pSMB->ParameterCount = cpu_to_le32(param_count);
3920 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3921 pSMB->TotalParameterCount = pSMB->ParameterCount;
3922 pSMB->DataOffset = cpu_to_le32(data_offset);
3923 pSMB->SetupCount = 0;
3924 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3925 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3926
3927 pSMB->Fid = fid; /* file handle always le */
3928 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003929 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003930
3931 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003932 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3933 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003934 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003935 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003936 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003937
3938 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3939 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3940
Joe Perchesf96637b2013-05-04 22:12:25 -05003941 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3942 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003943 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003944 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003945 cifs_buf_release(pSMB);
3946
3947 if (rc == -EAGAIN)
3948 goto setCifsAclRetry;
3949
3950 return (rc);
3951}
3952
Jeff Layton79df1ba2010-12-06 12:52:08 -05003953#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003954
Steve French6b8edfe2005-08-23 20:26:03 -07003955/* Legacy Query Path Information call for lookup to old servers such
3956 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003957int
3958SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3959 const char *search_name, FILE_ALL_INFO *data,
3960 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003961{
Steve Frenchad7a2922008-02-07 23:25:02 +00003962 QUERY_INFORMATION_REQ *pSMB;
3963 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003964 int rc = 0;
3965 int bytes_returned;
3966 int name_len;
3967
Joe Perchesf96637b2013-05-04 22:12:25 -05003968 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003969QInfRetry:
3970 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003971 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003972 if (rc)
3973 return rc;
3974
3975 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3976 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003977 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003978 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06003979 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07003980 name_len++; /* trailing null */
3981 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003982 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003983 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07003984 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003985 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003986 }
3987 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00003988 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003989 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07003990 pSMB->ByteCount = cpu_to_le16(name_len);
3991
3992 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003993 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07003994 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003995 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003996 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00003997 struct timespec ts;
3998 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00003999
4000 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004001 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004002 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004003 ts.tv_nsec = 0;
4004 ts.tv_sec = time;
4005 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004006 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4007 data->LastWriteTime = data->ChangeTime;
4008 data->LastAccessTime = 0;
4009 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004010 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004011 data->EndOfFile = data->AllocationSize;
4012 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004013 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004014 } else
4015 rc = -EIO; /* bad buffer passed in */
4016
4017 cifs_buf_release(pSMB);
4018
4019 if (rc == -EAGAIN)
4020 goto QInfRetry;
4021
4022 return rc;
4023}
4024
Jeff Laytonbcd53572010-02-12 07:44:16 -05004025int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004026CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004027 u16 netfid, FILE_ALL_INFO *pFindData)
4028{
4029 struct smb_t2_qfi_req *pSMB = NULL;
4030 struct smb_t2_qfi_rsp *pSMBr = NULL;
4031 int rc = 0;
4032 int bytes_returned;
4033 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004034
Jeff Laytonbcd53572010-02-12 07:44:16 -05004035QFileInfoRetry:
4036 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4037 (void **) &pSMBr);
4038 if (rc)
4039 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004040
Jeff Laytonbcd53572010-02-12 07:44:16 -05004041 params = 2 /* level */ + 2 /* fid */;
4042 pSMB->t2.TotalDataCount = 0;
4043 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4044 /* BB find exact max data count below from sess structure BB */
4045 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4046 pSMB->t2.MaxSetupCount = 0;
4047 pSMB->t2.Reserved = 0;
4048 pSMB->t2.Flags = 0;
4049 pSMB->t2.Timeout = 0;
4050 pSMB->t2.Reserved2 = 0;
4051 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4052 Fid) - 4);
4053 pSMB->t2.DataCount = 0;
4054 pSMB->t2.DataOffset = 0;
4055 pSMB->t2.SetupCount = 1;
4056 pSMB->t2.Reserved3 = 0;
4057 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4058 byte_count = params + 1 /* pad */ ;
4059 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4060 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4061 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4062 pSMB->Pad = 0;
4063 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004064 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004065 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004066
4067 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4068 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4069 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004070 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004071 } else { /* decode response */
4072 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4073
4074 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4075 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004076 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004077 rc = -EIO; /* bad smb */
4078 else if (pFindData) {
4079 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4080 memcpy((char *) pFindData,
4081 (char *) &pSMBr->hdr.Protocol +
4082 data_offset, sizeof(FILE_ALL_INFO));
4083 } else
4084 rc = -ENOMEM;
4085 }
4086 cifs_buf_release(pSMB);
4087 if (rc == -EAGAIN)
4088 goto QFileInfoRetry;
4089
4090 return rc;
4091}
Steve French6b8edfe2005-08-23 20:26:03 -07004092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004094CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004095 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004096 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004097 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004099 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 TRANSACTION2_QPI_REQ *pSMB = NULL;
4101 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4102 int rc = 0;
4103 int bytes_returned;
4104 int name_len;
4105 __u16 params, byte_count;
4106
Joe Perchesf96637b2013-05-04 22:12:25 -05004107 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108QPathInfoRetry:
4109 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4110 (void **) &pSMBr);
4111 if (rc)
4112 return rc;
4113
4114 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4115 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004116 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004117 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 name_len++; /* trailing null */
4119 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004120 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004121 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004123 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125
Steve French50c2f752007-07-13 00:33:32 +00004126 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 pSMB->TotalDataCount = 0;
4128 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004129 /* BB find exact max SMB PDU from sess structure BB */
4130 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 pSMB->MaxSetupCount = 0;
4132 pSMB->Reserved = 0;
4133 pSMB->Flags = 0;
4134 pSMB->Timeout = 0;
4135 pSMB->Reserved2 = 0;
4136 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004137 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 pSMB->DataCount = 0;
4139 pSMB->DataOffset = 0;
4140 pSMB->SetupCount = 1;
4141 pSMB->Reserved3 = 0;
4142 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4143 byte_count = params + 1 /* pad */ ;
4144 pSMB->TotalParameterCount = cpu_to_le16(params);
4145 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004146 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004147 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4148 else
4149 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004151 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 pSMB->ByteCount = cpu_to_le16(byte_count);
4153
4154 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4155 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4156 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004157 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 } else { /* decode response */
4159 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4160
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004161 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4162 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004163 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004165 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004166 rc = -EIO; /* 24 or 26 expected but we do not read
4167 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004168 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004169 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004171
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004172 /*
4173 * On legacy responses we do not read the last field,
4174 * EAsize, fortunately since it varies by subdialect and
4175 * also note it differs on Set vs Get, ie two bytes or 4
4176 * bytes depending but we don't care here.
4177 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004178 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004179 size = sizeof(FILE_INFO_STANDARD);
4180 else
4181 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004182 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004183 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 } else
4185 rc = -ENOMEM;
4186 }
4187 cifs_buf_release(pSMB);
4188 if (rc == -EAGAIN)
4189 goto QPathInfoRetry;
4190
4191 return rc;
4192}
4193
4194int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004195CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004196 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4197{
4198 struct smb_t2_qfi_req *pSMB = NULL;
4199 struct smb_t2_qfi_rsp *pSMBr = NULL;
4200 int rc = 0;
4201 int bytes_returned;
4202 __u16 params, byte_count;
4203
4204UnixQFileInfoRetry:
4205 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4206 (void **) &pSMBr);
4207 if (rc)
4208 return rc;
4209
4210 params = 2 /* level */ + 2 /* fid */;
4211 pSMB->t2.TotalDataCount = 0;
4212 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4213 /* BB find exact max data count below from sess structure BB */
4214 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4215 pSMB->t2.MaxSetupCount = 0;
4216 pSMB->t2.Reserved = 0;
4217 pSMB->t2.Flags = 0;
4218 pSMB->t2.Timeout = 0;
4219 pSMB->t2.Reserved2 = 0;
4220 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4221 Fid) - 4);
4222 pSMB->t2.DataCount = 0;
4223 pSMB->t2.DataOffset = 0;
4224 pSMB->t2.SetupCount = 1;
4225 pSMB->t2.Reserved3 = 0;
4226 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4227 byte_count = params + 1 /* pad */ ;
4228 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4229 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4230 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4231 pSMB->Pad = 0;
4232 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004233 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004234 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004235
4236 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4237 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4238 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004239 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004240 } else { /* decode response */
4241 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4242
Jeff Layton820a8032011-05-04 08:05:26 -04004243 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004244 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 -05004245 rc = -EIO; /* bad smb */
4246 } else {
4247 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4248 memcpy((char *) pFindData,
4249 (char *) &pSMBr->hdr.Protocol +
4250 data_offset,
4251 sizeof(FILE_UNIX_BASIC_INFO));
4252 }
4253 }
4254
4255 cifs_buf_release(pSMB);
4256 if (rc == -EAGAIN)
4257 goto UnixQFileInfoRetry;
4258
4259 return rc;
4260}
4261
4262int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004263CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004265 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004266 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268/* SMB_QUERY_FILE_UNIX_BASIC */
4269 TRANSACTION2_QPI_REQ *pSMB = NULL;
4270 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4271 int rc = 0;
4272 int bytes_returned = 0;
4273 int name_len;
4274 __u16 params, byte_count;
4275
Joe Perchesf96637b2013-05-04 22:12:25 -05004276 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277UnixQPathInfoRetry:
4278 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4279 (void **) &pSMBr);
4280 if (rc)
4281 return rc;
4282
4283 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4284 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004285 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4286 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 name_len++; /* trailing null */
4288 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004289 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 name_len = strnlen(searchName, PATH_MAX);
4291 name_len++; /* trailing null */
4292 strncpy(pSMB->FileName, searchName, name_len);
4293 }
4294
Steve French50c2f752007-07-13 00:33:32 +00004295 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 pSMB->TotalDataCount = 0;
4297 pSMB->MaxParameterCount = cpu_to_le16(2);
4298 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004299 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 pSMB->MaxSetupCount = 0;
4301 pSMB->Reserved = 0;
4302 pSMB->Flags = 0;
4303 pSMB->Timeout = 0;
4304 pSMB->Reserved2 = 0;
4305 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004306 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 pSMB->DataCount = 0;
4308 pSMB->DataOffset = 0;
4309 pSMB->SetupCount = 1;
4310 pSMB->Reserved3 = 0;
4311 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4312 byte_count = params + 1 /* pad */ ;
4313 pSMB->TotalParameterCount = cpu_to_le16(params);
4314 pSMB->ParameterCount = pSMB->TotalParameterCount;
4315 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4316 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004317 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 pSMB->ByteCount = cpu_to_le16(byte_count);
4319
4320 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4321 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4322 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004323 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 } else { /* decode response */
4325 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4326
Jeff Layton820a8032011-05-04 08:05:26 -04004327 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004328 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 -07004329 rc = -EIO; /* bad smb */
4330 } else {
4331 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4332 memcpy((char *) pFindData,
4333 (char *) &pSMBr->hdr.Protocol +
4334 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004335 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
4337 }
4338 cifs_buf_release(pSMB);
4339 if (rc == -EAGAIN)
4340 goto UnixQPathInfoRetry;
4341
4342 return rc;
4343}
4344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345/* xid, tcon, searchName and codepage are input parms, rest are returned */
4346int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004347CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004348 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004349 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004350 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
4352/* level 257 SMB_ */
4353 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4354 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004355 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 int rc = 0;
4357 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004358 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004360 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Joe Perchesf96637b2013-05-04 22:12:25 -05004362 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364findFirstRetry:
4365 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4366 (void **) &pSMBr);
4367 if (rc)
4368 return rc;
4369
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004370 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004371 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4374 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004375 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4376 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004377 /* We can not add the asterik earlier in case
4378 it got remapped to 0xF03A as if it were part of the
4379 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004381 if (msearch) {
4382 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4383 pSMB->FileName[name_len+1] = 0;
4384 pSMB->FileName[name_len+2] = '*';
4385 pSMB->FileName[name_len+3] = 0;
4386 name_len += 4; /* now the trailing null */
4387 /* null terminate just in case */
4388 pSMB->FileName[name_len] = 0;
4389 pSMB->FileName[name_len+1] = 0;
4390 name_len += 2;
4391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 } else { /* BB add check for overrun of SMB buf BB */
4393 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004395 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 free buffer exit; BB */
4397 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004398 if (msearch) {
4399 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4400 pSMB->FileName[name_len+1] = '*';
4401 pSMB->FileName[name_len+2] = 0;
4402 name_len += 3;
4403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
4405
4406 params = 12 + name_len /* includes null */ ;
4407 pSMB->TotalDataCount = 0; /* no EAs */
4408 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004409 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 pSMB->MaxSetupCount = 0;
4411 pSMB->Reserved = 0;
4412 pSMB->Flags = 0;
4413 pSMB->Timeout = 0;
4414 pSMB->Reserved2 = 0;
4415 byte_count = params + 1 /* pad */ ;
4416 pSMB->TotalParameterCount = cpu_to_le16(params);
4417 pSMB->ParameterCount = pSMB->TotalParameterCount;
4418 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004419 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4420 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 pSMB->DataCount = 0;
4422 pSMB->DataOffset = 0;
4423 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4424 pSMB->Reserved3 = 0;
4425 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4426 pSMB->SearchAttributes =
4427 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4428 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004429 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004430 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4432
4433 /* BB what should we set StorageType to? Does it matter? BB */
4434 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004435 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 pSMB->ByteCount = cpu_to_le16(byte_count);
4437
4438 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4439 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004440 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Steve French88274812006-03-09 22:21:45 +00004442 if (rc) {/* BB add logic to retry regular search if Unix search
4443 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004445 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004446
Steve French88274812006-03-09 22:21:45 +00004447 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 /* BB eventually could optimize out free and realloc of buf */
4450 /* for this case */
4451 if (rc == -EAGAIN)
4452 goto findFirstRetry;
4453 } else { /* decode response */
4454 /* BB remember to free buffer if error BB */
4455 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004456 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004457 unsigned int lnoff;
4458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004460 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 else
Steve French4b18f2a2008-04-29 00:06:05 +00004462 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004465 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004466 psrch_inf->srch_entries_start =
4467 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4470 le16_to_cpu(pSMBr->t2.ParameterOffset));
4471
Steve French790fe572007-07-07 19:25:05 +00004472 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004473 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 else
Steve French4b18f2a2008-04-29 00:06:05 +00004475 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Steve French50c2f752007-07-13 00:33:32 +00004477 psrch_inf->entries_in_buffer =
4478 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004479 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004481 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004482 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004483 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004484 psrch_inf->last_entry = NULL;
4485 return rc;
4486 }
4487
Steve French0752f152008-10-07 20:03:33 +00004488 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004489 lnoff;
4490
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004491 if (pnetfid)
4492 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 } else {
4494 cifs_buf_release(pSMB);
4495 }
4496 }
4497
4498 return rc;
4499}
4500
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004501int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4502 __u16 searchHandle, __u16 search_flags,
4503 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
4505 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4506 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004507 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 char *response_data;
4509 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004510 int bytes_returned;
4511 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 __u16 params, byte_count;
4513
Joe Perchesf96637b2013-05-04 22:12:25 -05004514 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Steve French4b18f2a2008-04-29 00:06:05 +00004516 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 return -ENOENT;
4518
4519 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4520 (void **) &pSMBr);
4521 if (rc)
4522 return rc;
4523
Steve French50c2f752007-07-13 00:33:32 +00004524 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 byte_count = 0;
4526 pSMB->TotalDataCount = 0; /* no EAs */
4527 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004528 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 pSMB->MaxSetupCount = 0;
4530 pSMB->Reserved = 0;
4531 pSMB->Flags = 0;
4532 pSMB->Timeout = 0;
4533 pSMB->Reserved2 = 0;
4534 pSMB->ParameterOffset = cpu_to_le16(
4535 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4536 pSMB->DataCount = 0;
4537 pSMB->DataOffset = 0;
4538 pSMB->SetupCount = 1;
4539 pSMB->Reserved3 = 0;
4540 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4541 pSMB->SearchHandle = searchHandle; /* always kept as le */
4542 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004543 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4545 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004546 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 name_len = psrch_inf->resume_name_len;
4549 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004550 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4552 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004553 /* 14 byte parm len above enough for 2 byte null terminator */
4554 pSMB->ResumeFileName[name_len] = 0;
4555 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 } else {
4557 rc = -EINVAL;
4558 goto FNext2_err_exit;
4559 }
4560 byte_count = params + 1 /* pad */ ;
4561 pSMB->TotalParameterCount = cpu_to_le16(params);
4562 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004563 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4567 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004568 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 if (rc) {
4570 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004571 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004572 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004573 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004575 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 } else { /* decode response */
4577 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004578
Steve French790fe572007-07-07 19:25:05 +00004579 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004580 unsigned int lnoff;
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 /* BB fixme add lock for file (srch_info) struct here */
4583 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004584 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 else
Steve French4b18f2a2008-04-29 00:06:05 +00004586 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 response_data = (char *) &pSMBr->hdr.Protocol +
4588 le16_to_cpu(pSMBr->t2.ParameterOffset);
4589 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4590 response_data = (char *)&pSMBr->hdr.Protocol +
4591 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004592 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004593 cifs_small_buf_release(
4594 psrch_inf->ntwrk_buf_start);
4595 else
4596 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 psrch_inf->srch_entries_start = response_data;
4598 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004599 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004600 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004601 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 else
Steve French4b18f2a2008-04-29 00:06:05 +00004603 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004604 psrch_inf->entries_in_buffer =
4605 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 psrch_inf->index_of_last_entry +=
4607 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004608 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004609 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004610 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004611 psrch_inf->last_entry = NULL;
4612 return rc;
4613 } else
4614 psrch_inf->last_entry =
4615 psrch_inf->srch_entries_start + lnoff;
4616
Joe Perchesf96637b2013-05-04 22:12:25 -05004617/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4618 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 /* BB fixme add unlock here */
4621 }
4622
4623 }
4624
4625 /* BB On error, should we leave previous search buf (and count and
4626 last entry fields) intact or free the previous one? */
4627
4628 /* Note: On -EAGAIN error only caller can retry on handle based calls
4629 since file handle passed in no longer valid */
4630FNext2_err_exit:
4631 if (rc != 0)
4632 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 return rc;
4634}
4635
4636int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004637CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004638 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
4640 int rc = 0;
4641 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Joe Perchesf96637b2013-05-04 22:12:25 -05004643 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4645
4646 /* no sense returning error if session restarted
4647 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004648 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return 0;
4650 if (rc)
4651 return rc;
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 pSMB->FileID = searchHandle;
4654 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004655 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004656 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004657 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004658
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004659 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 /* Since session is dead, search handle closed on server already */
4662 if (rc == -EAGAIN)
4663 rc = 0;
4664
4665 return rc;
4666}
4667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004669CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004670 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004671 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
4673 int rc = 0;
4674 TRANSACTION2_QPI_REQ *pSMB = NULL;
4675 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4676 int name_len, bytes_returned;
4677 __u16 params, byte_count;
4678
Joe Perchesf96637b2013-05-04 22:12:25 -05004679 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004680 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004681 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683GetInodeNumberRetry:
4684 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004685 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 if (rc)
4687 return rc;
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4690 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004691 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004692 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004693 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 name_len++; /* trailing null */
4695 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004696 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004697 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004699 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
4702 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4703 pSMB->TotalDataCount = 0;
4704 pSMB->MaxParameterCount = cpu_to_le16(2);
4705 /* BB find exact max data count below from sess structure BB */
4706 pSMB->MaxDataCount = cpu_to_le16(4000);
4707 pSMB->MaxSetupCount = 0;
4708 pSMB->Reserved = 0;
4709 pSMB->Flags = 0;
4710 pSMB->Timeout = 0;
4711 pSMB->Reserved2 = 0;
4712 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004713 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 pSMB->DataCount = 0;
4715 pSMB->DataOffset = 0;
4716 pSMB->SetupCount = 1;
4717 pSMB->Reserved3 = 0;
4718 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4719 byte_count = params + 1 /* pad */ ;
4720 pSMB->TotalParameterCount = cpu_to_le16(params);
4721 pSMB->ParameterCount = pSMB->TotalParameterCount;
4722 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4723 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004724 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 pSMB->ByteCount = cpu_to_le16(byte_count);
4726
4727 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4728 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4729 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004730 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 } else {
4732 /* decode response */
4733 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004735 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 /* If rc should we check for EOPNOSUPP and
4737 disable the srvino flag? or in caller? */
4738 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004739 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4741 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004742 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004744 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004745 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 rc = -EIO;
4747 goto GetInodeNumOut;
4748 }
4749 pfinfo = (struct file_internal_info *)
4750 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004751 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 }
4753 }
4754GetInodeNumOut:
4755 cifs_buf_release(pSMB);
4756 if (rc == -EAGAIN)
4757 goto GetInodeNumberRetry;
4758 return rc;
4759}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Igor Mammedovfec45852008-05-16 13:06:30 +04004761/* parses DFS refferal V3 structure
4762 * caller is responsible for freeing target_nodes
4763 * returns:
4764 * on success - 0
4765 * on failure - errno
4766 */
4767static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004768parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004769 unsigned int *num_of_nodes,
4770 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004771 const struct nls_table *nls_codepage, int remap,
4772 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004773{
4774 int i, rc = 0;
4775 char *data_end;
4776 bool is_unicode;
4777 struct dfs_referral_level_3 *ref;
4778
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004779 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4780 is_unicode = true;
4781 else
4782 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004783 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4784
4785 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004786 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4787 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004788 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004789 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004790 }
4791
4792 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004793 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004794 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4795 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004796 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004797 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004798 }
4799
4800 /* get the upper boundary of the resp buffer */
4801 data_end = (char *)(&(pSMBr->PathConsumed)) +
4802 le16_to_cpu(pSMBr->t2.DataCount);
4803
Joe Perchesf96637b2013-05-04 22:12:25 -05004804 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4805 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004806
Joe Perchesf96637b2013-05-04 22:12:25 -05004807 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4808 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004809 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004810 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004811 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004812 }
4813
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004814 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004815 for (i = 0; i < *num_of_nodes; i++) {
4816 char *temp;
4817 int max_len;
4818 struct dfs_info3_param *node = (*target_nodes)+i;
4819
Steve French0e0d2cf2009-05-01 05:27:32 +00004820 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004821 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004822 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4823 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004824 if (tmp == NULL) {
4825 rc = -ENOMEM;
4826 goto parse_DFS_referrals_exit;
4827 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004828 cifsConvertToUTF16((__le16 *) tmp, searchName,
4829 PATH_MAX, nls_codepage, remap);
4830 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004831 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004832 nls_codepage);
4833 kfree(tmp);
4834 } else
4835 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4836
Igor Mammedovfec45852008-05-16 13:06:30 +04004837 node->server_type = le16_to_cpu(ref->ServerType);
4838 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4839
4840 /* copy DfsPath */
4841 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4842 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004843 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4844 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004845 if (!node->path_name) {
4846 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004847 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004848 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004849
4850 /* copy link target UNC */
4851 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4852 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004853 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4854 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004855 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004856 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004857 goto parse_DFS_referrals_exit;
4858 }
4859
4860 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004861 }
4862
Steve Frencha1fe78f2008-05-16 18:48:38 +00004863parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004864 if (rc) {
4865 free_dfs_info_array(*target_nodes, *num_of_nodes);
4866 *target_nodes = NULL;
4867 *num_of_nodes = 0;
4868 }
4869 return rc;
4870}
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004873CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004874 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004875 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004876 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
4878/* TRANS2_GET_DFS_REFERRAL */
4879 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4880 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 int rc = 0;
4882 int bytes_returned;
4883 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004885 *num_of_nodes = 0;
4886 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Joe Perchesf96637b2013-05-04 22:12:25 -05004888 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (ses == NULL)
4890 return -ENODEV;
4891getDFSRetry:
4892 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4893 (void **) &pSMBr);
4894 if (rc)
4895 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004896
4897 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004898 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004899 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 pSMB->hdr.Tid = ses->ipc_tid;
4901 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004902 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004904 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 if (ses->capabilities & CAP_UNICODE) {
4908 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4909 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004910 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004911 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004912 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 name_len++; /* trailing null */
4914 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004915 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004916 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004918 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920
Dan Carpenter65c3b202015-04-30 17:30:24 +03004921 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004922 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004923
Steve French50c2f752007-07-13 00:33:32 +00004924 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 params = 2 /* level */ + name_len /*includes null */ ;
4927 pSMB->TotalDataCount = 0;
4928 pSMB->DataCount = 0;
4929 pSMB->DataOffset = 0;
4930 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004931 /* BB find exact max SMB PDU from sess structure BB */
4932 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 pSMB->MaxSetupCount = 0;
4934 pSMB->Reserved = 0;
4935 pSMB->Flags = 0;
4936 pSMB->Timeout = 0;
4937 pSMB->Reserved2 = 0;
4938 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004939 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 pSMB->SetupCount = 1;
4941 pSMB->Reserved3 = 0;
4942 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4943 byte_count = params + 3 /* pad */ ;
4944 pSMB->ParameterCount = cpu_to_le16(params);
4945 pSMB->TotalParameterCount = pSMB->ParameterCount;
4946 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004947 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 pSMB->ByteCount = cpu_to_le16(byte_count);
4949
4950 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4951 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4952 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004953 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004954 goto GetDFSRefExit;
4955 }
4956 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004958 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004959 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004960 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004961 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004963
Joe Perchesf96637b2013-05-04 22:12:25 -05004964 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4965 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004966
4967 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004968 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004969 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004970 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004971
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004973 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975 if (rc == -EAGAIN)
4976 goto getDFSRetry;
4977
4978 return rc;
4979}
4980
Steve French20962432005-09-21 22:05:57 -07004981/* Query File System Info such as free space to old servers such as Win 9x */
4982int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004983SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
4984 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07004985{
4986/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
4987 TRANSACTION2_QFSI_REQ *pSMB = NULL;
4988 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
4989 FILE_SYSTEM_ALLOC_INFO *response_data;
4990 int rc = 0;
4991 int bytes_returned = 0;
4992 __u16 params, byte_count;
4993
Joe Perchesf96637b2013-05-04 22:12:25 -05004994 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07004995oldQFSInfoRetry:
4996 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4997 (void **) &pSMBr);
4998 if (rc)
4999 return rc;
Steve French20962432005-09-21 22:05:57 -07005000
5001 params = 2; /* level */
5002 pSMB->TotalDataCount = 0;
5003 pSMB->MaxParameterCount = cpu_to_le16(2);
5004 pSMB->MaxDataCount = cpu_to_le16(1000);
5005 pSMB->MaxSetupCount = 0;
5006 pSMB->Reserved = 0;
5007 pSMB->Flags = 0;
5008 pSMB->Timeout = 0;
5009 pSMB->Reserved2 = 0;
5010 byte_count = params + 1 /* pad */ ;
5011 pSMB->TotalParameterCount = cpu_to_le16(params);
5012 pSMB->ParameterCount = pSMB->TotalParameterCount;
5013 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5014 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5015 pSMB->DataCount = 0;
5016 pSMB->DataOffset = 0;
5017 pSMB->SetupCount = 1;
5018 pSMB->Reserved3 = 0;
5019 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5020 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005021 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005022 pSMB->ByteCount = cpu_to_le16(byte_count);
5023
5024 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5025 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5026 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005027 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005028 } else { /* decode response */
5029 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5030
Jeff Layton820a8032011-05-04 08:05:26 -04005031 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005032 rc = -EIO; /* bad smb */
5033 else {
5034 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005035 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005036 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005037
Steve French50c2f752007-07-13 00:33:32 +00005038 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005039 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5040 FSData->f_bsize =
5041 le16_to_cpu(response_data->BytesPerSector) *
5042 le32_to_cpu(response_data->
5043 SectorsPerAllocationUnit);
5044 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005045 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005046 FSData->f_bfree = FSData->f_bavail =
5047 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005048 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5049 (unsigned long long)FSData->f_blocks,
5050 (unsigned long long)FSData->f_bfree,
5051 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005052 }
5053 }
5054 cifs_buf_release(pSMB);
5055
5056 if (rc == -EAGAIN)
5057 goto oldQFSInfoRetry;
5058
5059 return rc;
5060}
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005063CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5064 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
5066/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5067 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5068 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5069 FILE_SYSTEM_INFO *response_data;
5070 int rc = 0;
5071 int bytes_returned = 0;
5072 __u16 params, byte_count;
5073
Joe Perchesf96637b2013-05-04 22:12:25 -05005074 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075QFSInfoRetry:
5076 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5077 (void **) &pSMBr);
5078 if (rc)
5079 return rc;
5080
5081 params = 2; /* level */
5082 pSMB->TotalDataCount = 0;
5083 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005084 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 pSMB->MaxSetupCount = 0;
5086 pSMB->Reserved = 0;
5087 pSMB->Flags = 0;
5088 pSMB->Timeout = 0;
5089 pSMB->Reserved2 = 0;
5090 byte_count = params + 1 /* pad */ ;
5091 pSMB->TotalParameterCount = cpu_to_le16(params);
5092 pSMB->ParameterCount = pSMB->TotalParameterCount;
5093 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005094 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 pSMB->DataCount = 0;
5096 pSMB->DataOffset = 0;
5097 pSMB->SetupCount = 1;
5098 pSMB->Reserved3 = 0;
5099 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5100 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005101 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 pSMB->ByteCount = cpu_to_le16(byte_count);
5103
5104 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5105 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5106 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005107 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005109 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Jeff Layton820a8032011-05-04 08:05:26 -04005111 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 rc = -EIO; /* bad smb */
5113 else {
5114 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
5116 response_data =
5117 (FILE_SYSTEM_INFO
5118 *) (((char *) &pSMBr->hdr.Protocol) +
5119 data_offset);
5120 FSData->f_bsize =
5121 le32_to_cpu(response_data->BytesPerSector) *
5122 le32_to_cpu(response_data->
5123 SectorsPerAllocationUnit);
5124 FSData->f_blocks =
5125 le64_to_cpu(response_data->TotalAllocationUnits);
5126 FSData->f_bfree = FSData->f_bavail =
5127 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005128 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5129 (unsigned long long)FSData->f_blocks,
5130 (unsigned long long)FSData->f_bfree,
5131 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 }
5133 }
5134 cifs_buf_release(pSMB);
5135
5136 if (rc == -EAGAIN)
5137 goto QFSInfoRetry;
5138
5139 return rc;
5140}
5141
5142int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005143CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
5145/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5146 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5147 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5148 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5149 int rc = 0;
5150 int bytes_returned = 0;
5151 __u16 params, byte_count;
5152
Joe Perchesf96637b2013-05-04 22:12:25 -05005153 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154QFSAttributeRetry:
5155 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5156 (void **) &pSMBr);
5157 if (rc)
5158 return rc;
5159
5160 params = 2; /* level */
5161 pSMB->TotalDataCount = 0;
5162 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005163 /* BB find exact max SMB PDU from sess structure BB */
5164 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 pSMB->MaxSetupCount = 0;
5166 pSMB->Reserved = 0;
5167 pSMB->Flags = 0;
5168 pSMB->Timeout = 0;
5169 pSMB->Reserved2 = 0;
5170 byte_count = params + 1 /* pad */ ;
5171 pSMB->TotalParameterCount = cpu_to_le16(params);
5172 pSMB->ParameterCount = pSMB->TotalParameterCount;
5173 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005174 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 pSMB->DataCount = 0;
5176 pSMB->DataOffset = 0;
5177 pSMB->SetupCount = 1;
5178 pSMB->Reserved3 = 0;
5179 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5180 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005181 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 pSMB->ByteCount = cpu_to_le16(byte_count);
5183
5184 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5185 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5186 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005187 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 } else { /* decode response */
5189 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5190
Jeff Layton820a8032011-05-04 08:05:26 -04005191 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005192 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 rc = -EIO; /* bad smb */
5194 } else {
5195 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5196 response_data =
5197 (FILE_SYSTEM_ATTRIBUTE_INFO
5198 *) (((char *) &pSMBr->hdr.Protocol) +
5199 data_offset);
5200 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005201 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203 }
5204 cifs_buf_release(pSMB);
5205
5206 if (rc == -EAGAIN)
5207 goto QFSAttributeRetry;
5208
5209 return rc;
5210}
5211
5212int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005213CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214{
5215/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5216 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5217 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5218 FILE_SYSTEM_DEVICE_INFO *response_data;
5219 int rc = 0;
5220 int bytes_returned = 0;
5221 __u16 params, byte_count;
5222
Joe Perchesf96637b2013-05-04 22:12:25 -05005223 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224QFSDeviceRetry:
5225 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5226 (void **) &pSMBr);
5227 if (rc)
5228 return rc;
5229
5230 params = 2; /* level */
5231 pSMB->TotalDataCount = 0;
5232 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005233 /* BB find exact max SMB PDU from sess structure BB */
5234 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 pSMB->MaxSetupCount = 0;
5236 pSMB->Reserved = 0;
5237 pSMB->Flags = 0;
5238 pSMB->Timeout = 0;
5239 pSMB->Reserved2 = 0;
5240 byte_count = params + 1 /* pad */ ;
5241 pSMB->TotalParameterCount = cpu_to_le16(params);
5242 pSMB->ParameterCount = pSMB->TotalParameterCount;
5243 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005244 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
5246 pSMB->DataCount = 0;
5247 pSMB->DataOffset = 0;
5248 pSMB->SetupCount = 1;
5249 pSMB->Reserved3 = 0;
5250 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5251 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005252 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 pSMB->ByteCount = cpu_to_le16(byte_count);
5254
5255 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5256 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5257 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005258 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 } else { /* decode response */
5260 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5261
Jeff Layton820a8032011-05-04 08:05:26 -04005262 if (rc || get_bcc(&pSMBr->hdr) <
5263 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 rc = -EIO; /* bad smb */
5265 else {
5266 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5267 response_data =
Steve French737b7582005-04-28 22:41:06 -07005268 (FILE_SYSTEM_DEVICE_INFO *)
5269 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 data_offset);
5271 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005272 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 }
5274 }
5275 cifs_buf_release(pSMB);
5276
5277 if (rc == -EAGAIN)
5278 goto QFSDeviceRetry;
5279
5280 return rc;
5281}
5282
5283int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005284CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285{
5286/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5287 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5288 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5289 FILE_SYSTEM_UNIX_INFO *response_data;
5290 int rc = 0;
5291 int bytes_returned = 0;
5292 __u16 params, byte_count;
5293
Joe Perchesf96637b2013-05-04 22:12:25 -05005294 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005296 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5297 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 if (rc)
5299 return rc;
5300
5301 params = 2; /* level */
5302 pSMB->TotalDataCount = 0;
5303 pSMB->DataCount = 0;
5304 pSMB->DataOffset = 0;
5305 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005306 /* BB find exact max SMB PDU from sess structure BB */
5307 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 pSMB->MaxSetupCount = 0;
5309 pSMB->Reserved = 0;
5310 pSMB->Flags = 0;
5311 pSMB->Timeout = 0;
5312 pSMB->Reserved2 = 0;
5313 byte_count = params + 1 /* pad */ ;
5314 pSMB->ParameterCount = cpu_to_le16(params);
5315 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005316 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5317 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 pSMB->SetupCount = 1;
5319 pSMB->Reserved3 = 0;
5320 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5321 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005322 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 pSMB->ByteCount = cpu_to_le16(byte_count);
5324
5325 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5326 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5327 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005328 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 } else { /* decode response */
5330 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5331
Jeff Layton820a8032011-05-04 08:05:26 -04005332 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 rc = -EIO; /* bad smb */
5334 } else {
5335 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5336 response_data =
5337 (FILE_SYSTEM_UNIX_INFO
5338 *) (((char *) &pSMBr->hdr.Protocol) +
5339 data_offset);
5340 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005341 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 }
5343 }
5344 cifs_buf_release(pSMB);
5345
5346 if (rc == -EAGAIN)
5347 goto QFSUnixRetry;
5348
5349
5350 return rc;
5351}
5352
Jeremy Allisonac670552005-06-22 17:26:35 -07005353int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005354CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005355{
5356/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5357 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5358 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5359 int rc = 0;
5360 int bytes_returned = 0;
5361 __u16 params, param_offset, offset, byte_count;
5362
Joe Perchesf96637b2013-05-04 22:12:25 -05005363 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005364SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005365 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005366 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5367 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005368 if (rc)
5369 return rc;
5370
5371 params = 4; /* 2 bytes zero followed by info level. */
5372 pSMB->MaxSetupCount = 0;
5373 pSMB->Reserved = 0;
5374 pSMB->Flags = 0;
5375 pSMB->Timeout = 0;
5376 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005377 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5378 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005379 offset = param_offset + params;
5380
5381 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005382 /* BB find exact max SMB PDU from sess structure BB */
5383 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005384 pSMB->SetupCount = 1;
5385 pSMB->Reserved3 = 0;
5386 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5387 byte_count = 1 /* pad */ + params + 12;
5388
5389 pSMB->DataCount = cpu_to_le16(12);
5390 pSMB->ParameterCount = cpu_to_le16(params);
5391 pSMB->TotalDataCount = pSMB->DataCount;
5392 pSMB->TotalParameterCount = pSMB->ParameterCount;
5393 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5394 pSMB->DataOffset = cpu_to_le16(offset);
5395
5396 /* Params. */
5397 pSMB->FileNum = 0;
5398 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5399
5400 /* Data. */
5401 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5402 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5403 pSMB->ClientUnixCap = cpu_to_le64(cap);
5404
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005405 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005406 pSMB->ByteCount = cpu_to_le16(byte_count);
5407
5408 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5409 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5410 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005411 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005412 } else { /* decode response */
5413 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005414 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005415 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005416 }
5417 cifs_buf_release(pSMB);
5418
5419 if (rc == -EAGAIN)
5420 goto SETFSUnixRetry;
5421
5422 return rc;
5423}
5424
5425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005428CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005429 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430{
5431/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5432 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5433 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5434 FILE_SYSTEM_POSIX_INFO *response_data;
5435 int rc = 0;
5436 int bytes_returned = 0;
5437 __u16 params, byte_count;
5438
Joe Perchesf96637b2013-05-04 22:12:25 -05005439 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440QFSPosixRetry:
5441 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5442 (void **) &pSMBr);
5443 if (rc)
5444 return rc;
5445
5446 params = 2; /* level */
5447 pSMB->TotalDataCount = 0;
5448 pSMB->DataCount = 0;
5449 pSMB->DataOffset = 0;
5450 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005451 /* BB find exact max SMB PDU from sess structure BB */
5452 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 pSMB->MaxSetupCount = 0;
5454 pSMB->Reserved = 0;
5455 pSMB->Flags = 0;
5456 pSMB->Timeout = 0;
5457 pSMB->Reserved2 = 0;
5458 byte_count = params + 1 /* pad */ ;
5459 pSMB->ParameterCount = cpu_to_le16(params);
5460 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005461 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5462 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 pSMB->SetupCount = 1;
5464 pSMB->Reserved3 = 0;
5465 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5466 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005467 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 pSMB->ByteCount = cpu_to_le16(byte_count);
5469
5470 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5471 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5472 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005473 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 } else { /* decode response */
5475 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5476
Jeff Layton820a8032011-05-04 08:05:26 -04005477 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 rc = -EIO; /* bad smb */
5479 } else {
5480 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5481 response_data =
5482 (FILE_SYSTEM_POSIX_INFO
5483 *) (((char *) &pSMBr->hdr.Protocol) +
5484 data_offset);
5485 FSData->f_bsize =
5486 le32_to_cpu(response_data->BlockSize);
5487 FSData->f_blocks =
5488 le64_to_cpu(response_data->TotalBlocks);
5489 FSData->f_bfree =
5490 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005491 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 FSData->f_bavail = FSData->f_bfree;
5493 } else {
5494 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005495 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 }
Steve French790fe572007-07-07 19:25:05 +00005497 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005499 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005500 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005502 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 }
5504 }
5505 cifs_buf_release(pSMB);
5506
5507 if (rc == -EAGAIN)
5508 goto QFSPosixRetry;
5509
5510 return rc;
5511}
5512
5513
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005514/*
5515 * We can not use write of zero bytes trick to set file size due to need for
5516 * large file support. Also note that this SetPathInfo is preferred to
5517 * SetFileInfo based method in next routine which is only needed to work around
5518 * a sharing violation bugin Samba which this routine can run into.
5519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005521CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005522 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5523 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
5525 struct smb_com_transaction2_spi_req *pSMB = NULL;
5526 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5527 struct file_end_of_file_info *parm_data;
5528 int name_len;
5529 int rc = 0;
5530 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005531 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005532
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 __u16 params, byte_count, data_count, param_offset, offset;
5534
Joe Perchesf96637b2013-05-04 22:12:25 -05005535 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536SetEOFRetry:
5537 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5538 (void **) &pSMBr);
5539 if (rc)
5540 return rc;
5541
5542 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5543 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005544 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5545 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 name_len++; /* trailing null */
5547 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005548 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005549 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005551 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 }
5553 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005554 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005556 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 pSMB->MaxSetupCount = 0;
5558 pSMB->Reserved = 0;
5559 pSMB->Flags = 0;
5560 pSMB->Timeout = 0;
5561 pSMB->Reserved2 = 0;
5562 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005563 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005565 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005566 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5567 pSMB->InformationLevel =
5568 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5569 else
5570 pSMB->InformationLevel =
5571 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5572 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5574 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005575 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 else
5577 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005578 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 }
5580
5581 parm_data =
5582 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5583 offset);
5584 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5585 pSMB->DataOffset = cpu_to_le16(offset);
5586 pSMB->SetupCount = 1;
5587 pSMB->Reserved3 = 0;
5588 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5589 byte_count = 3 /* pad */ + params + data_count;
5590 pSMB->DataCount = cpu_to_le16(data_count);
5591 pSMB->TotalDataCount = pSMB->DataCount;
5592 pSMB->ParameterCount = cpu_to_le16(params);
5593 pSMB->TotalParameterCount = pSMB->ParameterCount;
5594 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005595 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 parm_data->FileSize = cpu_to_le64(size);
5597 pSMB->ByteCount = cpu_to_le16(byte_count);
5598 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5599 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005600 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005601 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
5603 cifs_buf_release(pSMB);
5604
5605 if (rc == -EAGAIN)
5606 goto SetEOFRetry;
5607
5608 return rc;
5609}
5610
5611int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005612CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5613 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614{
5615 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 struct file_end_of_file_info *parm_data;
5617 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 __u16 params, param_offset, offset, byte_count, count;
5619
Joe Perchesf96637b2013-05-04 22:12:25 -05005620 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5621 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005622 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5623
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 if (rc)
5625 return rc;
5626
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005627 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5628 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 params = 6;
5631 pSMB->MaxSetupCount = 0;
5632 pSMB->Reserved = 0;
5633 pSMB->Flags = 0;
5634 pSMB->Timeout = 0;
5635 pSMB->Reserved2 = 0;
5636 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5637 offset = param_offset + params;
5638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 count = sizeof(struct file_end_of_file_info);
5640 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005641 /* BB find exact max SMB PDU from sess structure BB */
5642 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 pSMB->SetupCount = 1;
5644 pSMB->Reserved3 = 0;
5645 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5646 byte_count = 3 /* pad */ + params + count;
5647 pSMB->DataCount = cpu_to_le16(count);
5648 pSMB->ParameterCount = cpu_to_le16(params);
5649 pSMB->TotalDataCount = pSMB->DataCount;
5650 pSMB->TotalParameterCount = pSMB->ParameterCount;
5651 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5652 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005653 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5654 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 pSMB->DataOffset = cpu_to_le16(offset);
5656 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005657 pSMB->Fid = cfile->fid.netfid;
5658 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5660 pSMB->InformationLevel =
5661 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5662 else
5663 pSMB->InformationLevel =
5664 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005665 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5667 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005668 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 else
5670 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005671 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 }
5673 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005674 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005676 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005678 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5679 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
5681
Steve French50c2f752007-07-13 00:33:32 +00005682 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 since file handle passed in no longer valid */
5684
5685 return rc;
5686}
5687
Steve French50c2f752007-07-13 00:33:32 +00005688/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 an open handle, rather than by pathname - this is awkward due to
5690 potential access conflicts on the open, but it is unavoidable for these
5691 old servers since the only other choice is to go from 100 nanosecond DCE
5692 time and resort to the original setpathinfo level which takes the ancient
5693 DOS time format with 2 second granularity */
5694int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005695CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005696 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697{
5698 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 char *data_offset;
5700 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 __u16 params, param_offset, offset, byte_count, count;
5702
Joe Perchesf96637b2013-05-04 22:12:25 -05005703 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005704 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5705
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 if (rc)
5707 return rc;
5708
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005709 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5710 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 params = 6;
5713 pSMB->MaxSetupCount = 0;
5714 pSMB->Reserved = 0;
5715 pSMB->Flags = 0;
5716 pSMB->Timeout = 0;
5717 pSMB->Reserved2 = 0;
5718 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5719 offset = param_offset + params;
5720
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005721 data_offset = (char *)pSMB +
5722 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Steve French26f57362007-08-30 22:09:15 +00005724 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005726 /* BB find max SMB PDU from sess */
5727 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 pSMB->SetupCount = 1;
5729 pSMB->Reserved3 = 0;
5730 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5731 byte_count = 3 /* pad */ + params + count;
5732 pSMB->DataCount = cpu_to_le16(count);
5733 pSMB->ParameterCount = cpu_to_le16(params);
5734 pSMB->TotalDataCount = pSMB->DataCount;
5735 pSMB->TotalParameterCount = pSMB->ParameterCount;
5736 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5737 pSMB->DataOffset = cpu_to_le16(offset);
5738 pSMB->Fid = fid;
5739 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5740 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5741 else
5742 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5743 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005744 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005746 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005747 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005748 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005749 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5750 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Steve French50c2f752007-07-13 00:33:32 +00005752 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 since file handle passed in no longer valid */
5754
5755 return rc;
5756}
5757
Jeff Layton6d22f092008-09-23 11:48:35 -04005758int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005759CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005760 bool delete_file, __u16 fid, __u32 pid_of_opener)
5761{
5762 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5763 char *data_offset;
5764 int rc = 0;
5765 __u16 params, param_offset, offset, byte_count, count;
5766
Joe Perchesf96637b2013-05-04 22:12:25 -05005767 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005768 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5769
5770 if (rc)
5771 return rc;
5772
5773 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5774 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5775
5776 params = 6;
5777 pSMB->MaxSetupCount = 0;
5778 pSMB->Reserved = 0;
5779 pSMB->Flags = 0;
5780 pSMB->Timeout = 0;
5781 pSMB->Reserved2 = 0;
5782 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5783 offset = param_offset + params;
5784
5785 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5786
5787 count = 1;
5788 pSMB->MaxParameterCount = cpu_to_le16(2);
5789 /* BB find max SMB PDU from sess */
5790 pSMB->MaxDataCount = cpu_to_le16(1000);
5791 pSMB->SetupCount = 1;
5792 pSMB->Reserved3 = 0;
5793 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5794 byte_count = 3 /* pad */ + params + count;
5795 pSMB->DataCount = cpu_to_le16(count);
5796 pSMB->ParameterCount = cpu_to_le16(params);
5797 pSMB->TotalDataCount = pSMB->DataCount;
5798 pSMB->TotalParameterCount = pSMB->ParameterCount;
5799 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5800 pSMB->DataOffset = cpu_to_le16(offset);
5801 pSMB->Fid = fid;
5802 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5803 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005804 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005805 pSMB->ByteCount = cpu_to_le16(byte_count);
5806 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005807 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005808 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005809 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005810
5811 return rc;
5812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005815CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005816 const char *fileName, const FILE_BASIC_INFO *data,
5817 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818{
5819 TRANSACTION2_SPI_REQ *pSMB = NULL;
5820 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5821 int name_len;
5822 int rc = 0;
5823 int bytes_returned = 0;
5824 char *data_offset;
5825 __u16 params, param_offset, offset, byte_count, count;
5826
Joe Perchesf96637b2013-05-04 22:12:25 -05005827 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
5829SetTimesRetry:
5830 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5831 (void **) &pSMBr);
5832 if (rc)
5833 return rc;
5834
5835 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5836 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005837 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5838 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 name_len++; /* trailing null */
5840 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005841 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 name_len = strnlen(fileName, PATH_MAX);
5843 name_len++; /* trailing null */
5844 strncpy(pSMB->FileName, fileName, name_len);
5845 }
5846
5847 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005848 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005850 /* BB find max SMB PDU from sess structure BB */
5851 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 pSMB->MaxSetupCount = 0;
5853 pSMB->Reserved = 0;
5854 pSMB->Flags = 0;
5855 pSMB->Timeout = 0;
5856 pSMB->Reserved2 = 0;
5857 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005858 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 offset = param_offset + params;
5860 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5861 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5862 pSMB->DataOffset = cpu_to_le16(offset);
5863 pSMB->SetupCount = 1;
5864 pSMB->Reserved3 = 0;
5865 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5866 byte_count = 3 /* pad */ + params + count;
5867
5868 pSMB->DataCount = cpu_to_le16(count);
5869 pSMB->ParameterCount = cpu_to_le16(params);
5870 pSMB->TotalDataCount = pSMB->DataCount;
5871 pSMB->TotalParameterCount = pSMB->ParameterCount;
5872 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5873 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5874 else
5875 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5876 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005877 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005878 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 pSMB->ByteCount = cpu_to_le16(byte_count);
5880 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5881 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005882 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005883 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
5885 cifs_buf_release(pSMB);
5886
5887 if (rc == -EAGAIN)
5888 goto SetTimesRetry;
5889
5890 return rc;
5891}
5892
5893/* Can not be used to set time stamps yet (due to old DOS time format) */
5894/* Can be used to set attributes */
5895#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5896 handling it anyway and NT4 was what we thought it would be needed for
5897 Do not delete it until we prove whether needed for Win9x though */
5898int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005899CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 __u16 dos_attrs, const struct nls_table *nls_codepage)
5901{
5902 SETATTR_REQ *pSMB = NULL;
5903 SETATTR_RSP *pSMBr = NULL;
5904 int rc = 0;
5905 int bytes_returned;
5906 int name_len;
5907
Joe Perchesf96637b2013-05-04 22:12:25 -05005908 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
5910SetAttrLgcyRetry:
5911 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5912 (void **) &pSMBr);
5913 if (rc)
5914 return rc;
5915
5916 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5917 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005918 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5919 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 name_len++; /* trailing null */
5921 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005922 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 name_len = strnlen(fileName, PATH_MAX);
5924 name_len++; /* trailing null */
5925 strncpy(pSMB->fileName, fileName, name_len);
5926 }
5927 pSMB->attr = cpu_to_le16(dos_attrs);
5928 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005929 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5931 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5932 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005933 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005934 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
5936 cifs_buf_release(pSMB);
5937
5938 if (rc == -EAGAIN)
5939 goto SetAttrLgcyRetry;
5940
5941 return rc;
5942}
5943#endif /* temporarily unneeded SetAttr legacy function */
5944
Jeff Layton654cf142009-07-09 20:02:49 -04005945static void
5946cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5947 const struct cifs_unix_set_info_args *args)
5948{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005949 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005950 u64 mode = args->mode;
5951
Eric W. Biederman49418b22013-02-06 00:57:56 -08005952 if (uid_valid(args->uid))
5953 uid = from_kuid(&init_user_ns, args->uid);
5954 if (gid_valid(args->gid))
5955 gid = from_kgid(&init_user_ns, args->gid);
5956
Jeff Layton654cf142009-07-09 20:02:49 -04005957 /*
5958 * Samba server ignores set of file size to zero due to bugs in some
5959 * older clients, but we should be precise - we use SetFileSize to
5960 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005961 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005962 * zero instead of -1 here
5963 */
5964 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5965 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5966 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5967 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5968 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005969 data_offset->Uid = cpu_to_le64(uid);
5970 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005971 /* better to leave device as zero when it is */
5972 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5973 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5974 data_offset->Permissions = cpu_to_le64(mode);
5975
5976 if (S_ISREG(mode))
5977 data_offset->Type = cpu_to_le32(UNIX_FILE);
5978 else if (S_ISDIR(mode))
5979 data_offset->Type = cpu_to_le32(UNIX_DIR);
5980 else if (S_ISLNK(mode))
5981 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
5982 else if (S_ISCHR(mode))
5983 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
5984 else if (S_ISBLK(mode))
5985 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
5986 else if (S_ISFIFO(mode))
5987 data_offset->Type = cpu_to_le32(UNIX_FIFO);
5988 else if (S_ISSOCK(mode))
5989 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
5990}
5991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005993CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005994 const struct cifs_unix_set_info_args *args,
5995 u16 fid, u32 pid_of_opener)
5996{
5997 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005998 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04005999 int rc = 0;
6000 u16 params, param_offset, offset, byte_count, count;
6001
Joe Perchesf96637b2013-05-04 22:12:25 -05006002 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006003 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6004
6005 if (rc)
6006 return rc;
6007
6008 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6009 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6010
6011 params = 6;
6012 pSMB->MaxSetupCount = 0;
6013 pSMB->Reserved = 0;
6014 pSMB->Flags = 0;
6015 pSMB->Timeout = 0;
6016 pSMB->Reserved2 = 0;
6017 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6018 offset = param_offset + params;
6019
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006020 data_offset = (char *)pSMB +
6021 offsetof(struct smb_hdr, Protocol) + offset;
6022
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006023 count = sizeof(FILE_UNIX_BASIC_INFO);
6024
6025 pSMB->MaxParameterCount = cpu_to_le16(2);
6026 /* BB find max SMB PDU from sess */
6027 pSMB->MaxDataCount = cpu_to_le16(1000);
6028 pSMB->SetupCount = 1;
6029 pSMB->Reserved3 = 0;
6030 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6031 byte_count = 3 /* pad */ + params + count;
6032 pSMB->DataCount = cpu_to_le16(count);
6033 pSMB->ParameterCount = cpu_to_le16(params);
6034 pSMB->TotalDataCount = pSMB->DataCount;
6035 pSMB->TotalParameterCount = pSMB->ParameterCount;
6036 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6037 pSMB->DataOffset = cpu_to_le16(offset);
6038 pSMB->Fid = fid;
6039 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6040 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006041 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006042 pSMB->ByteCount = cpu_to_le16(byte_count);
6043
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006044 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006045
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006046 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006047 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006048 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6049 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006050
6051 /* Note: On -EAGAIN error only caller can retry on handle based calls
6052 since file handle passed in no longer valid */
6053
6054 return rc;
6055}
6056
6057int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006058CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006059 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006060 const struct cifs_unix_set_info_args *args,
6061 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
6063 TRANSACTION2_SPI_REQ *pSMB = NULL;
6064 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6065 int name_len;
6066 int rc = 0;
6067 int bytes_returned = 0;
6068 FILE_UNIX_BASIC_INFO *data_offset;
6069 __u16 params, param_offset, offset, count, byte_count;
6070
Joe Perchesf96637b2013-05-04 22:12:25 -05006071 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072setPermsRetry:
6073 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6074 (void **) &pSMBr);
6075 if (rc)
6076 return rc;
6077
6078 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6079 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006080 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006081 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 name_len++; /* trailing null */
6083 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006084 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006085 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006087 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 }
6089
6090 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006091 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006093 /* BB find max SMB PDU from sess structure BB */
6094 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 pSMB->MaxSetupCount = 0;
6096 pSMB->Reserved = 0;
6097 pSMB->Flags = 0;
6098 pSMB->Timeout = 0;
6099 pSMB->Reserved2 = 0;
6100 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006101 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 offset = param_offset + params;
6103 data_offset =
6104 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6105 offset);
6106 memset(data_offset, 0, count);
6107 pSMB->DataOffset = cpu_to_le16(offset);
6108 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6109 pSMB->SetupCount = 1;
6110 pSMB->Reserved3 = 0;
6111 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6112 byte_count = 3 /* pad */ + params + count;
6113 pSMB->ParameterCount = cpu_to_le16(params);
6114 pSMB->DataCount = cpu_to_le16(count);
6115 pSMB->TotalParameterCount = pSMB->ParameterCount;
6116 pSMB->TotalDataCount = pSMB->DataCount;
6117 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6118 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006119 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006120
Jeff Layton654cf142009-07-09 20:02:49 -04006121 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
6123 pSMB->ByteCount = cpu_to_le16(byte_count);
6124 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6125 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006126 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006127 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
Steve French0d817bc2008-05-22 02:02:03 +00006129 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 if (rc == -EAGAIN)
6131 goto setPermsRetry;
6132 return rc;
6133}
6134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006136/*
6137 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6138 * function used by listxattr and getxattr type calls. When ea_name is set,
6139 * it looks for that attribute name and stuffs that value into the EAData
6140 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6141 * buffer. In both cases, the return value is either the length of the
6142 * resulting data or a negative error code. If EAData is a NULL pointer then
6143 * the data isn't copied to it, but the length is returned.
6144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006146CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006147 const unsigned char *searchName, const unsigned char *ea_name,
6148 char *EAData, size_t buf_size,
6149 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150{
6151 /* BB assumes one setup word */
6152 TRANSACTION2_QPI_REQ *pSMB = NULL;
6153 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6154 int rc = 0;
6155 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006156 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006157 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006158 struct fea *temp_fea;
6159 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006160 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006161 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006162 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
Joe Perchesf96637b2013-05-04 22:12:25 -05006164 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165QAllEAsRetry:
6166 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6167 (void **) &pSMBr);
6168 if (rc)
6169 return rc;
6170
6171 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006172 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006173 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6174 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006175 list_len++; /* trailing null */
6176 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006178 list_len = strnlen(searchName, PATH_MAX);
6179 list_len++; /* trailing null */
6180 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
6182
Jeff Layton6e462b92010-02-10 16:18:26 -05006183 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 pSMB->TotalDataCount = 0;
6185 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006186 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006187 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 pSMB->MaxSetupCount = 0;
6189 pSMB->Reserved = 0;
6190 pSMB->Flags = 0;
6191 pSMB->Timeout = 0;
6192 pSMB->Reserved2 = 0;
6193 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006194 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 pSMB->DataCount = 0;
6196 pSMB->DataOffset = 0;
6197 pSMB->SetupCount = 1;
6198 pSMB->Reserved3 = 0;
6199 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6200 byte_count = params + 1 /* pad */ ;
6201 pSMB->TotalParameterCount = cpu_to_le16(params);
6202 pSMB->ParameterCount = pSMB->TotalParameterCount;
6203 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6204 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006205 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 pSMB->ByteCount = cpu_to_le16(byte_count);
6207
6208 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6209 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6210 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006211 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006212 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006214
6215
6216 /* BB also check enough total bytes returned */
6217 /* BB we need to improve the validity checking
6218 of these trans2 responses */
6219
6220 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006221 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006222 rc = -EIO; /* bad smb */
6223 goto QAllEAsOut;
6224 }
6225
6226 /* check that length of list is not more than bcc */
6227 /* check that each entry does not go beyond length
6228 of list */
6229 /* check that each element of each entry does not
6230 go beyond end of list */
6231 /* validate_trans2_offsets() */
6232 /* BB check if start of smb + data_offset > &bcc+ bcc */
6233
6234 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6235 ea_response_data = (struct fealist *)
6236 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6237
Jeff Layton6e462b92010-02-10 16:18:26 -05006238 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006239 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006240 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006241 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006242 /* didn't find the named attribute */
6243 if (ea_name)
6244 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006245 goto QAllEAsOut;
6246 }
6247
Jeff Layton0cd126b2010-02-10 16:18:26 -05006248 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006249 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006250 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006251 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006252 rc = -EIO;
6253 goto QAllEAsOut;
6254 }
6255
Jeff Laytonf0d38682010-02-10 16:18:26 -05006256 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006257 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006258 temp_fea = ea_response_data->list;
6259 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006260 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006261 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006262 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006263
Jeff Layton6e462b92010-02-10 16:18:26 -05006264 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006265 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006266 /* make sure we can read name_len and value_len */
6267 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006268 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006269 rc = -EIO;
6270 goto QAllEAsOut;
6271 }
6272
6273 name_len = temp_fea->name_len;
6274 value_len = le16_to_cpu(temp_fea->value_len);
6275 list_len -= name_len + 1 + value_len;
6276 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006277 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006278 rc = -EIO;
6279 goto QAllEAsOut;
6280 }
6281
Jeff Layton31c05192010-02-10 16:18:26 -05006282 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006283 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006284 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006285 temp_ptr += name_len + 1;
6286 rc = value_len;
6287 if (buf_size == 0)
6288 goto QAllEAsOut;
6289 if ((size_t)value_len > buf_size) {
6290 rc = -ERANGE;
6291 goto QAllEAsOut;
6292 }
6293 memcpy(EAData, temp_ptr, value_len);
6294 goto QAllEAsOut;
6295 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006296 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006297 /* account for prefix user. and trailing null */
6298 rc += (5 + 1 + name_len);
6299 if (rc < (int) buf_size) {
6300 memcpy(EAData, "user.", 5);
6301 EAData += 5;
6302 memcpy(EAData, temp_ptr, name_len);
6303 EAData += name_len;
6304 /* null terminate name */
6305 *EAData = 0;
6306 ++EAData;
6307 } else if (buf_size == 0) {
6308 /* skip copy - calc size only */
6309 } else {
6310 /* stop before overrun buffer */
6311 rc = -ERANGE;
6312 break;
6313 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006314 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006315 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006316 temp_fea = (struct fea *)temp_ptr;
6317 }
6318
Jeff Layton31c05192010-02-10 16:18:26 -05006319 /* didn't find the named attribute */
6320 if (ea_name)
6321 rc = -ENODATA;
6322
Jeff Laytonf0d38682010-02-10 16:18:26 -05006323QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006324 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 if (rc == -EAGAIN)
6326 goto QAllEAsRetry;
6327
6328 return (ssize_t)rc;
6329}
6330
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006332CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6333 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006334 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6335 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
6337 struct smb_com_transaction2_spi_req *pSMB = NULL;
6338 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6339 struct fealist *parm_data;
6340 int name_len;
6341 int rc = 0;
6342 int bytes_returned = 0;
6343 __u16 params, param_offset, byte_count, offset, count;
6344
Joe Perchesf96637b2013-05-04 22:12:25 -05006345 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346SetEARetry:
6347 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6348 (void **) &pSMBr);
6349 if (rc)
6350 return rc;
6351
6352 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6353 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006354 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6355 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 name_len++; /* trailing null */
6357 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006358 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 name_len = strnlen(fileName, PATH_MAX);
6360 name_len++; /* trailing null */
6361 strncpy(pSMB->FileName, fileName, name_len);
6362 }
6363
6364 params = 6 + name_len;
6365
6366 /* done calculating parms using name_len of file name,
6367 now use name_len to calculate length of ea name
6368 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006369 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 name_len = 0;
6371 else
Steve French50c2f752007-07-13 00:33:32 +00006372 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006374 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006376 /* BB find max SMB PDU from sess */
6377 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 pSMB->MaxSetupCount = 0;
6379 pSMB->Reserved = 0;
6380 pSMB->Flags = 0;
6381 pSMB->Timeout = 0;
6382 pSMB->Reserved2 = 0;
6383 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006384 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 offset = param_offset + params;
6386 pSMB->InformationLevel =
6387 cpu_to_le16(SMB_SET_FILE_EA);
6388
6389 parm_data =
6390 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6391 offset);
6392 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6393 pSMB->DataOffset = cpu_to_le16(offset);
6394 pSMB->SetupCount = 1;
6395 pSMB->Reserved3 = 0;
6396 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6397 byte_count = 3 /* pad */ + params + count;
6398 pSMB->DataCount = cpu_to_le16(count);
6399 parm_data->list_len = cpu_to_le32(count);
6400 parm_data->list[0].EA_flags = 0;
6401 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006402 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006404 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006405 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 parm_data->list[0].name[name_len] = 0;
6407 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6408 /* caller ensures that ea_value_len is less than 64K but
6409 we need to ensure that it fits within the smb */
6410
Steve French50c2f752007-07-13 00:33:32 +00006411 /*BB add length check to see if it would fit in
6412 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006413 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6414 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006415 memcpy(parm_data->list[0].name+name_len+1,
6416 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
6418 pSMB->TotalDataCount = pSMB->DataCount;
6419 pSMB->ParameterCount = cpu_to_le16(params);
6420 pSMB->TotalParameterCount = pSMB->ParameterCount;
6421 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006422 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 pSMB->ByteCount = cpu_to_le16(byte_count);
6424 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6425 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006426 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006427 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
6429 cifs_buf_release(pSMB);
6430
6431 if (rc == -EAGAIN)
6432 goto SetEARetry;
6433
6434 return rc;
6435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436#endif
Steve French0eff0e22011-02-24 05:39:23 +00006437
6438#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6439/*
6440 * Years ago the kernel added a "dnotify" function for Samba server,
6441 * to allow network clients (such as Windows) to display updated
6442 * lists of files in directory listings automatically when
6443 * files are added by one user when another user has the
6444 * same directory open on their desktop. The Linux cifs kernel
6445 * client hooked into the kernel side of this interface for
6446 * the same reason, but ironically when the VFS moved from
6447 * "dnotify" to "inotify" it became harder to plug in Linux
6448 * network file system clients (the most obvious use case
6449 * for notify interfaces is when multiple users can update
6450 * the contents of the same directory - exactly what network
6451 * file systems can do) although the server (Samba) could
6452 * still use it. For the short term we leave the worker
6453 * function ifdeffed out (below) until inotify is fixed
6454 * in the VFS to make it easier to plug in network file
6455 * system clients. If inotify turns out to be permanently
6456 * incompatible for network fs clients, we could instead simply
6457 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6458 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006459int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006460 const int notify_subdirs, const __u16 netfid,
6461 __u32 filter, struct file *pfile, int multishot,
6462 const struct nls_table *nls_codepage)
6463{
6464 int rc = 0;
6465 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6466 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6467 struct dir_notify_req *dnotify_req;
6468 int bytes_returned;
6469
Joe Perchesf96637b2013-05-04 22:12:25 -05006470 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006471 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6472 (void **) &pSMBr);
6473 if (rc)
6474 return rc;
6475
6476 pSMB->TotalParameterCount = 0 ;
6477 pSMB->TotalDataCount = 0;
6478 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006479 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006480 pSMB->MaxSetupCount = 4;
6481 pSMB->Reserved = 0;
6482 pSMB->ParameterOffset = 0;
6483 pSMB->DataCount = 0;
6484 pSMB->DataOffset = 0;
6485 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6486 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6487 pSMB->ParameterCount = pSMB->TotalParameterCount;
6488 if (notify_subdirs)
6489 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6490 pSMB->Reserved2 = 0;
6491 pSMB->CompletionFilter = cpu_to_le32(filter);
6492 pSMB->Fid = netfid; /* file handle always le */
6493 pSMB->ByteCount = 0;
6494
6495 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6496 (struct smb_hdr *)pSMBr, &bytes_returned,
6497 CIFS_ASYNC_OP);
6498 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006499 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006500 } else {
6501 /* Add file to outstanding requests */
6502 /* BB change to kmem cache alloc */
6503 dnotify_req = kmalloc(
6504 sizeof(struct dir_notify_req),
6505 GFP_KERNEL);
6506 if (dnotify_req) {
6507 dnotify_req->Pid = pSMB->hdr.Pid;
6508 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6509 dnotify_req->Mid = pSMB->hdr.Mid;
6510 dnotify_req->Tid = pSMB->hdr.Tid;
6511 dnotify_req->Uid = pSMB->hdr.Uid;
6512 dnotify_req->netfid = netfid;
6513 dnotify_req->pfile = pfile;
6514 dnotify_req->filter = filter;
6515 dnotify_req->multishot = multishot;
6516 spin_lock(&GlobalMid_Lock);
6517 list_add_tail(&dnotify_req->lhead,
6518 &GlobalDnotifyReqList);
6519 spin_unlock(&GlobalMid_Lock);
6520 } else
6521 rc = -ENOMEM;
6522 }
6523 cifs_buf_release(pSMB);
6524 return rc;
6525}
6526#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */