blob: 76fcb50295a38b63a58a0d3f656d023e02b988a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/cifssmb.c
3 *
Steve Frenchf19159d2010-04-21 04:12:10 +00004 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * Contains the routines for constructing the SMB PDUs themselves
8 *
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
25 /* These are mostly routines that operate on a pathname, or on a tree id */
26 /* (mounted volume), but there are eight handle based routines which must be */
Steve French2dd29d32007-04-23 22:07:35 +000027 /* treated slightly differently for reconnection purposes since we never */
28 /* want to reuse a stale file handle and only the caller knows the file info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/fs.h>
31#include <linux/kernel.h>
32#include <linux/vfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/posix_acl_xattr.h>
Jeff Laytonc28c89f2011-05-19 16:22:56 -040035#include <linux/pagemap.h>
Jeff Laytone28bc5b2011-10-19 15:30:07 -040036#include <linux/swap.h>
37#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39#include "cifspdu.h"
40#include "cifsglob.h"
Shirish Pargaonkard0d66c42007-10-03 18:22:19 +000041#include "cifsacl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "cifsproto.h"
43#include "cifs_unicode.h"
44#include "cifs_debug.h"
Jeff Laytone28bc5b2011-10-19 15:30:07 -040045#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef CONFIG_CIFS_POSIX
48static struct {
49 int index;
50 char *name;
51} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000052#ifdef CONFIG_CIFS_WEAK_PW_HASH
53 {LANMAN_PROT, "\2LM1.2X002"},
Steve French9ac00b72006-09-30 04:13:17 +000054 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000055#endif /* weak password hashing for legacy clients */
Steve French50c2f752007-07-13 00:33:32 +000056 {CIFS_PROT, "\2NT LM 0.12"},
Steve French39798772006-05-31 22:40:51 +000057 {POSIX_PROT, "\2POSIX 2"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 {BAD_PROT, "\2"}
59};
60#else
61static struct {
62 int index;
63 char *name;
64} protocols[] = {
Steve French39798772006-05-31 22:40:51 +000065#ifdef CONFIG_CIFS_WEAK_PW_HASH
66 {LANMAN_PROT, "\2LM1.2X002"},
Steve French18f75ca2006-10-01 03:13:01 +000067 {LANMAN2_PROT, "\2LANMAN2.1"},
Steve French39798772006-05-31 22:40:51 +000068#endif /* weak password hashing for legacy clients */
Steve French790fe572007-07-07 19:25:05 +000069 {CIFS_PROT, "\2NT LM 0.12"},
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {BAD_PROT, "\2"}
71};
72#endif
73
Steve French39798772006-05-31 22:40:51 +000074/* define the number of elements in the cifs dialect array */
75#ifdef CONFIG_CIFS_POSIX
76#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000077#define CIFS_NUM_PROT 4
Steve French39798772006-05-31 22:40:51 +000078#else
79#define CIFS_NUM_PROT 2
80#endif /* CIFS_WEAK_PW_HASH */
81#else /* not posix */
82#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French9ac00b72006-09-30 04:13:17 +000083#define CIFS_NUM_PROT 3
Steve French39798772006-05-31 22:40:51 +000084#else
85#define CIFS_NUM_PROT 1
86#endif /* CONFIG_CIFS_WEAK_PW_HASH */
87#endif /* CIFS_POSIX */
88
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +040089/*
90 * Mark as invalid, all open files on tree connections since they
91 * were closed when session to server was lost.
92 */
93void
94cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct cifsFileInfo *open_file = NULL;
Steve French790fe572007-07-07 19:25:05 +000097 struct list_head *tmp;
98 struct list_head *tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400100 /* list all files open on tree connection and mark them invalid */
Jeff Layton44772882010-10-15 15:34:03 -0400101 spin_lock(&cifs_file_list_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400102 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
Steve French790fe572007-07-07 19:25:05 +0000103 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000104 open_file->invalidHandle = true;
Jeff Layton3bc303c2009-09-21 06:47:50 -0400105 open_file->oplock_break_cancelled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Jeff Layton44772882010-10-15 15:34:03 -0400107 spin_unlock(&cifs_file_list_lock);
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400108 /*
109 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
110 * to this tcon.
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jeff Layton9162ab22009-09-03 12:07:17 -0400114/* reconnect the socket, tcon, and smb session if needed */
115static int
Steve French96daf2b2011-05-27 04:34:02 +0000116cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
Jeff Layton9162ab22009-09-03 12:07:17 -0400117{
Jeff Laytonc4a55342011-07-28 12:40:36 -0400118 int rc;
Steve French96daf2b2011-05-27 04:34:02 +0000119 struct cifs_ses *ses;
Jeff Layton9162ab22009-09-03 12:07:17 -0400120 struct TCP_Server_Info *server;
121 struct nls_table *nls_codepage;
122
123 /*
124 * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
125 * tcp and smb session status done differently for those three - in the
126 * calling routine
127 */
128 if (!tcon)
129 return 0;
130
131 ses = tcon->ses;
132 server = ses->server;
133
134 /*
135 * only tree disconnect, open, and write, (and ulogoff which does not
136 * have tcon) are allowed as we start force umount
137 */
138 if (tcon->tidStatus == CifsExiting) {
139 if (smb_command != SMB_COM_WRITE_ANDX &&
140 smb_command != SMB_COM_OPEN_ANDX &&
141 smb_command != SMB_COM_TREE_DISCONNECT) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500142 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
143 smb_command);
Jeff Layton9162ab22009-09-03 12:07:17 -0400144 return -ENODEV;
145 }
146 }
147
Jeff Layton9162ab22009-09-03 12:07:17 -0400148 /*
149 * Give demultiplex thread up to 10 seconds to reconnect, should be
150 * greater than cifs socket timeout which is 7 seconds
151 */
152 while (server->tcpStatus == CifsNeedReconnect) {
153 wait_event_interruptible_timeout(server->response_q,
Steve Frenchfd88ce92011-04-12 01:01:14 +0000154 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
Jeff Layton9162ab22009-09-03 12:07:17 -0400155
Steve Frenchfd88ce92011-04-12 01:01:14 +0000156 /* are we still trying to reconnect? */
Jeff Layton9162ab22009-09-03 12:07:17 -0400157 if (server->tcpStatus != CifsNeedReconnect)
158 break;
159
160 /*
161 * on "soft" mounts we wait once. Hard mounts keep
162 * retrying until process is killed or server comes
163 * back on-line
164 */
Jeff Laytond4025392011-02-07 08:54:35 -0500165 if (!tcon->retry) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500166 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
Jeff Layton9162ab22009-09-03 12:07:17 -0400167 return -EHOSTDOWN;
168 }
169 }
170
171 if (!ses->need_reconnect && !tcon->need_reconnect)
172 return 0;
173
174 nls_codepage = load_nls_default();
175
176 /*
177 * need to prevent multiple threads trying to simultaneously
178 * reconnect the same SMB session
179 */
Steve Frenchd7b619c2010-02-25 05:36:46 +0000180 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -0400181 rc = cifs_negotiate_protocol(0, ses);
182 if (rc == 0 && ses->need_reconnect)
Jeff Layton9162ab22009-09-03 12:07:17 -0400183 rc = cifs_setup_session(0, ses, nls_codepage);
184
185 /* do we need to reconnect tcon? */
186 if (rc || !tcon->need_reconnect) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000187 mutex_unlock(&ses->session_mutex);
Jeff Layton9162ab22009-09-03 12:07:17 -0400188 goto out;
189 }
190
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400191 cifs_mark_open_files_invalid(tcon);
Jeff Layton9162ab22009-09-03 12:07:17 -0400192 rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
Steve Frenchd7b619c2010-02-25 05:36:46 +0000193 mutex_unlock(&ses->session_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500194 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
Jeff Layton9162ab22009-09-03 12:07:17 -0400195
196 if (rc)
197 goto out;
198
Jeff Layton9162ab22009-09-03 12:07:17 -0400199 atomic_inc(&tconInfoReconnectCount);
200
201 /* tell server Unix caps we support */
202 if (ses->capabilities & CAP_UNIX)
203 reset_cifs_unix_caps(0, tcon, NULL, NULL);
204
205 /*
206 * Removed call to reopen open files here. It is safer (and faster) to
207 * reopen files one at a time as needed in read and write.
208 *
209 * FIXME: what about file locks? don't we need to reclaim them ASAP?
210 */
211
212out:
213 /*
214 * Check if handle based operation so we know whether we can continue
215 * or not without returning to caller to reset file handle
216 */
217 switch (smb_command) {
218 case SMB_COM_READ_ANDX:
219 case SMB_COM_WRITE_ANDX:
220 case SMB_COM_CLOSE:
221 case SMB_COM_FIND_CLOSE2:
222 case SMB_COM_LOCKING_ANDX:
223 rc = -EAGAIN;
224 }
225
226 unload_nls(nls_codepage);
227 return rc;
228}
229
Steve Frenchad7a2922008-02-07 23:25:02 +0000230/* Allocate and return pointer to an SMB request buffer, and set basic
231 SMB information in the SMB header. If the return code is zero, this
232 function must have filled in request_buf pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int
Steve French96daf2b2011-05-27 04:34:02 +0000234small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000235 void **request_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jeff Laytonf5695992010-09-29 15:27:08 -0400237 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jeff Layton9162ab22009-09-03 12:07:17 -0400239 rc = cifs_reconnect_tcon(tcon, smb_command);
Steve French790fe572007-07-07 19:25:05 +0000240 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return rc;
242
243 *request_buf = cifs_small_buf_get();
244 if (*request_buf == NULL) {
245 /* BB should we add a retry in here if not a writepage? */
246 return -ENOMEM;
247 }
248
Steve French63135e02007-07-17 17:34:02 +0000249 header_assemble((struct smb_hdr *) *request_buf, smb_command,
Steve Frenchc18c8422007-07-18 23:21:09 +0000250 tcon, wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Steve French790fe572007-07-07 19:25:05 +0000252 if (tcon != NULL)
253 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700254
Jeff Laytonf5695992010-09-29 15:27:08 -0400255 return 0;
Steve French5815449d2006-02-14 01:36:20 +0000256}
257
Steve French12b3b8f2006-02-09 21:12:47 +0000258int
Steve French50c2f752007-07-13 00:33:32 +0000259small_smb_init_no_tc(const int smb_command, const int wct,
Steve French96daf2b2011-05-27 04:34:02 +0000260 struct cifs_ses *ses, void **request_buf)
Steve French12b3b8f2006-02-09 21:12:47 +0000261{
262 int rc;
Steve French50c2f752007-07-13 00:33:32 +0000263 struct smb_hdr *buffer;
Steve French12b3b8f2006-02-09 21:12:47 +0000264
Steve French5815449d2006-02-14 01:36:20 +0000265 rc = small_smb_init(smb_command, wct, NULL, request_buf);
Steve French790fe572007-07-07 19:25:05 +0000266 if (rc)
Steve French12b3b8f2006-02-09 21:12:47 +0000267 return rc;
268
Steve French04fdabe2006-02-10 05:52:50 +0000269 buffer = (struct smb_hdr *)*request_buf;
Pavel Shilovsky88257362012-05-23 14:01:59 +0400270 buffer->Mid = get_next_mid(ses->server);
Steve French12b3b8f2006-02-09 21:12:47 +0000271 if (ses->capabilities & CAP_UNICODE)
272 buffer->Flags2 |= SMBFLG2_UNICODE;
Steve French04fdabe2006-02-10 05:52:50 +0000273 if (ses->capabilities & CAP_STATUS32)
Steve French12b3b8f2006-02-09 21:12:47 +0000274 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
275
276 /* uid, tid can stay at zero as set in header assemble */
277
Steve French50c2f752007-07-13 00:33:32 +0000278 /* BB add support for turning on the signing when
Steve French12b3b8f2006-02-09 21:12:47 +0000279 this function is used after 1st of session setup requests */
280
281 return rc;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* If the return code is zero, this function must fill in request_buf pointer */
285static int
Steve French96daf2b2011-05-27 04:34:02 +0000286__smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400287 void **request_buf, void **response_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *request_buf = cifs_buf_get();
290 if (*request_buf == NULL) {
291 /* BB should we add a retry in here if not a writepage? */
292 return -ENOMEM;
293 }
294 /* Although the original thought was we needed the response buf for */
295 /* potential retries of smb operations it turns out we can determine */
296 /* from the mid flags when the request buffer can be resent without */
297 /* having to use a second distinct buffer for the response */
Steve French790fe572007-07-07 19:25:05 +0000298 if (response_buf)
Steve French50c2f752007-07-13 00:33:32 +0000299 *response_buf = *request_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +0000302 wct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Steve French790fe572007-07-07 19:25:05 +0000304 if (tcon != NULL)
305 cifs_stats_inc(&tcon->num_smbs_sent);
Steve Frencha4544342005-08-24 13:59:35 -0700306
Jeff Laytonf5695992010-09-29 15:27:08 -0400307 return 0;
308}
309
310/* If the return code is zero, this function must fill in request_buf pointer */
311static int
Steve French96daf2b2011-05-27 04:34:02 +0000312smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400313 void **request_buf, void **response_buf)
314{
315 int rc;
316
317 rc = cifs_reconnect_tcon(tcon, smb_command);
318 if (rc)
319 return rc;
320
321 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
322}
323
324static int
Steve French96daf2b2011-05-27 04:34:02 +0000325smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
Jeff Laytonf5695992010-09-29 15:27:08 -0400326 void **request_buf, void **response_buf)
327{
328 if (tcon->ses->need_reconnect || tcon->need_reconnect)
329 return -EHOSTDOWN;
330
331 return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Steve French50c2f752007-07-13 00:33:32 +0000334static int validate_t2(struct smb_t2_rsp *pSMB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Jeff Layton12df83c2011-01-20 13:36:51 -0500336 unsigned int total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Jeff Layton12df83c2011-01-20 13:36:51 -0500338 /* check for plausible wct */
339 if (pSMB->hdr.WordCount < 10)
340 goto vt2_err;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* check for parm and data offset going beyond end of smb */
Jeff Layton12df83c2011-01-20 13:36:51 -0500343 if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
344 get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
345 goto vt2_err;
346
Jeff Layton12df83c2011-01-20 13:36:51 -0500347 total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
348 if (total_size >= 512)
349 goto vt2_err;
350
Jeff Laytonfd5707e2011-03-31 17:22:07 -0400351 /* check that bcc is at least as big as parms + data, and that it is
352 * less than negotiated smb buffer
353 */
Jeff Layton12df83c2011-01-20 13:36:51 -0500354 total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
355 if (total_size > get_bcc(&pSMB->hdr) ||
356 total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
357 goto vt2_err;
358
359 return 0;
360vt2_err:
Steve French50c2f752007-07-13 00:33:32 +0000361 cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sizeof(struct smb_t2_rsp) + 16);
Jeff Layton12df83c2011-01-20 13:36:51 -0500363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Jeff Layton690c5222011-01-20 13:36:51 -0500365
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400366static int
Jeff Layton3f618222013-06-12 19:52:14 -0500367decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400368{
369 int rc = 0;
370 u16 count;
371 char *guid = pSMBr->u.extended_response.GUID;
Jeff Layton3f618222013-06-12 19:52:14 -0500372 struct TCP_Server_Info *server = ses->server;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400373
374 count = get_bcc(&pSMBr->hdr);
375 if (count < SMB1_CLIENT_GUID_SIZE)
376 return -EIO;
377
378 spin_lock(&cifs_tcp_ses_lock);
379 if (server->srv_count > 1) {
380 spin_unlock(&cifs_tcp_ses_lock);
381 if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
382 cifs_dbg(FYI, "server UID changed\n");
383 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
384 }
385 } else {
386 spin_unlock(&cifs_tcp_ses_lock);
387 memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
388 }
389
390 if (count == SMB1_CLIENT_GUID_SIZE) {
Jeff Layton3f618222013-06-12 19:52:14 -0500391 server->sec_ntlmssp = true;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400392 } else {
393 count -= SMB1_CLIENT_GUID_SIZE;
394 rc = decode_negTokenInit(
395 pSMBr->u.extended_response.SecurityBlob, count, server);
396 if (rc != 1)
397 return -EINVAL;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400398 }
399
400 return 0;
401}
402
Jeff Layton9ddec562013-05-26 07:00:58 -0400403int
Jeff Layton38d77c52013-05-26 07:01:00 -0400404cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
Jeff Layton9ddec562013-05-26 07:00:58 -0400405{
Jeff Layton50285882013-06-27 12:45:00 -0400406 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
407 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
Jeff Layton38d77c52013-05-26 07:01:00 -0400408 bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
409
410 /*
411 * Is signing required by mnt options? If not then check
412 * global_secflags to see if it is there.
413 */
414 if (!mnt_sign_required)
415 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
416 CIFSSEC_MUST_SIGN);
417
418 /*
419 * If signing is required then it's automatically enabled too,
420 * otherwise, check to see if the secflags allow it.
421 */
422 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
423 (global_secflags & CIFSSEC_MAY_SIGN);
424
425 /* If server requires signing, does client allow it? */
426 if (srv_sign_required) {
427 if (!mnt_sign_enabled) {
428 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
429 return -ENOTSUPP;
Jeff Layton9ddec562013-05-26 07:00:58 -0400430 }
Jeff Layton38d77c52013-05-26 07:01:00 -0400431 server->sign = true;
432 }
433
434 /* If client requires signing, does server allow it? */
435 if (mnt_sign_required) {
436 if (!srv_sign_enabled) {
437 cifs_dbg(VFS, "Server does not support signing!");
438 return -ENOTSUPP;
439 }
440 server->sign = true;
Jeff Layton9ddec562013-05-26 07:00:58 -0400441 }
442
443 return 0;
444}
445
Jeff Layton2190eca2013-05-26 07:00:57 -0400446#ifdef CONFIG_CIFS_WEAK_PW_HASH
447static int
Jeff Layton3f618222013-06-12 19:52:14 -0500448decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400449{
450 __s16 tmp;
451 struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
452
453 if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
454 return -EOPNOTSUPP;
455
Jeff Layton2190eca2013-05-26 07:00:57 -0400456 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
457 server->maxReq = min_t(unsigned int,
458 le16_to_cpu(rsp->MaxMpxCount),
459 cifs_max_pending);
460 set_credits(server, server->maxReq);
461 server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
Jeff Layton2190eca2013-05-26 07:00:57 -0400462 /* even though we do not use raw we might as well set this
463 accurately, in case we ever find a need for it */
464 if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
465 server->max_rw = 0xFF00;
466 server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
467 } else {
468 server->max_rw = 0;/* do not need to use raw anyway */
469 server->capabilities = CAP_MPX_MODE;
470 }
471 tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
472 if (tmp == -1) {
473 /* OS/2 often does not set timezone therefore
474 * we must use server time to calc time zone.
475 * Could deviate slightly from the right zone.
476 * Smallest defined timezone difference is 15 minutes
477 * (i.e. Nepal). Rounding up/down is done to match
478 * this requirement.
479 */
480 int val, seconds, remain, result;
481 struct timespec ts, utc;
482 utc = CURRENT_TIME;
483 ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
484 rsp->SrvTime.Time, 0);
485 cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
486 (int)ts.tv_sec, (int)utc.tv_sec,
487 (int)(utc.tv_sec - ts.tv_sec));
488 val = (int)(utc.tv_sec - ts.tv_sec);
489 seconds = abs(val);
490 result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
491 remain = seconds % MIN_TZ_ADJ;
492 if (remain >= (MIN_TZ_ADJ / 2))
493 result += MIN_TZ_ADJ;
494 if (val < 0)
495 result = -result;
496 server->timeAdj = result;
497 } else {
498 server->timeAdj = (int)tmp;
499 server->timeAdj *= 60; /* also in seconds */
500 }
501 cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
502
503
504 /* BB get server time for time conversions and add
505 code to use it and timezone since this is not UTC */
506
507 if (rsp->EncryptionKeyLength ==
508 cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
509 memcpy(server->cryptkey, rsp->EncryptionKey,
510 CIFS_CRYPTO_KEY_SIZE);
511 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
512 return -EIO; /* need cryptkey unless plain text */
513 }
514
515 cifs_dbg(FYI, "LANMAN negotiated\n");
516 return 0;
517}
518#else
519static inline int
Jeff Layton3f618222013-06-12 19:52:14 -0500520decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
Jeff Layton2190eca2013-05-26 07:00:57 -0400521{
522 cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
523 return -EOPNOTSUPP;
524}
525#endif
526
Jeff Layton91934002013-05-26 07:00:58 -0400527static bool
Jeff Layton3f618222013-06-12 19:52:14 -0500528should_set_ext_sec_flag(enum securityEnum sectype)
Jeff Layton91934002013-05-26 07:00:58 -0400529{
Jeff Layton3f618222013-06-12 19:52:14 -0500530 switch (sectype) {
531 case RawNTLMSSP:
532 case Kerberos:
Jeff Layton91934002013-05-26 07:00:58 -0400533 return true;
Jeff Layton3f618222013-06-12 19:52:14 -0500534 case Unspecified:
535 if (global_secflags &
536 (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
537 return true;
538 /* Fallthrough */
539 default:
540 return false;
541 }
Jeff Layton91934002013-05-26 07:00:58 -0400542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544int
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400545CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 NEGOTIATE_REQ *pSMB;
548 NEGOTIATE_RSP *pSMBr;
549 int rc = 0;
550 int bytes_returned;
Steve French39798772006-05-31 22:40:51 +0000551 int i;
Jeff Layton3534b852013-05-24 07:41:01 -0400552 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 u16 count;
554
Jeff Layton3534b852013-05-24 07:41:01 -0400555 if (!server) {
556 WARN(1, "%s: server is NULL!\n", __func__);
557 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Jeff Layton3534b852013-05-24 07:41:01 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
561 (void **) &pSMB, (void **) &pSMBr);
562 if (rc)
563 return rc;
Steve French750d1152006-06-27 06:28:30 +0000564
Pavel Shilovsky88257362012-05-23 14:01:59 +0400565 pSMB->hdr.Mid = get_next_mid(server);
Yehuda Sadeh Weinraub100c1dd2007-06-05 21:31:16 +0000566 pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
Steve Frencha0136892007-10-04 20:05:09 +0000567
Jeff Layton3f618222013-06-12 19:52:14 -0500568 if (should_set_ext_sec_flag(ses->sectype)) {
Jeff Layton91934002013-05-26 07:00:58 -0400569 cifs_dbg(FYI, "Requesting extended security.");
Steve Frenchac683922009-05-06 04:16:04 +0000570 pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
571 }
Steve French50c2f752007-07-13 00:33:32 +0000572
Steve French39798772006-05-31 22:40:51 +0000573 count = 0;
Steve French50c2f752007-07-13 00:33:32 +0000574 for (i = 0; i < CIFS_NUM_PROT; i++) {
Steve French39798772006-05-31 22:40:51 +0000575 strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
576 count += strlen(protocols[i].name) + 1;
577 /* null at end of source and target buffers anyway */
578 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000579 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 pSMB->ByteCount = cpu_to_le16(count);
581
582 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
583 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French50c2f752007-07-13 00:33:32 +0000584 if (rc != 0)
Steve French254e55e2006-06-04 05:53:15 +0000585 goto neg_err_exit;
586
Jeff Layton9bf67e52010-04-24 07:57:46 -0400587 server->dialect = le16_to_cpu(pSMBr->DialectIndex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500588 cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
Steve French254e55e2006-06-04 05:53:15 +0000589 /* Check wct = 1 error case */
Jeff Layton9bf67e52010-04-24 07:57:46 -0400590 if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
Steve French254e55e2006-06-04 05:53:15 +0000591 /* core returns wct = 1, but we do not ask for core - otherwise
Steve French50c2f752007-07-13 00:33:32 +0000592 small wct just comes when dialect index is -1 indicating we
Steve French254e55e2006-06-04 05:53:15 +0000593 could not negotiate a common dialect */
594 rc = -EOPNOTSUPP;
595 goto neg_err_exit;
Steve French790fe572007-07-07 19:25:05 +0000596 } else if (pSMBr->hdr.WordCount == 13) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400597 server->negflavor = CIFS_NEGFLAVOR_LANMAN;
Jeff Layton3f618222013-06-12 19:52:14 -0500598 rc = decode_lanman_negprot_rsp(server, pSMBr);
Jeff Layton9ddec562013-05-26 07:00:58 -0400599 goto signing_check;
Steve French790fe572007-07-07 19:25:05 +0000600 } else if (pSMBr->hdr.WordCount != 17) {
Steve French254e55e2006-06-04 05:53:15 +0000601 /* unknown wct */
602 rc = -EOPNOTSUPP;
603 goto neg_err_exit;
604 }
Jeff Layton2190eca2013-05-26 07:00:57 -0400605 /* else wct == 17, NTLM or better */
606
Steve French96daf2b2011-05-27 04:34:02 +0000607 server->sec_mode = pSMBr->SecurityMode;
608 if ((server->sec_mode & SECMODE_USER) == 0)
Joe Perchesf96637b2013-05-04 22:12:25 -0500609 cifs_dbg(FYI, "share mode security\n");
Steve French39798772006-05-31 22:40:51 +0000610
Steve French254e55e2006-06-04 05:53:15 +0000611 /* one byte, so no need to convert this or EncryptionKeyLen from
612 little endian */
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300613 server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
614 cifs_max_pending);
Pavel Shilovsky45275782012-05-17 17:53:29 +0400615 set_credits(server, server->maxReq);
Steve French254e55e2006-06-04 05:53:15 +0000616 /* probably no need to store and check maxvcs */
Jeff Laytonc974bef2011-10-11 06:41:32 -0400617 server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
Steve Frencheca6acf2009-02-20 05:43:09 +0000618 server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
Joe Perchesf96637b2013-05-04 22:12:25 -0500619 cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
Steve French254e55e2006-06-04 05:53:15 +0000620 server->capabilities = le32_to_cpu(pSMBr->Capabilities);
Steve Frenchb815f1e52006-10-02 05:53:29 +0000621 server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
622 server->timeAdj *= 60;
Jeff Layton31d9e2b2013-05-26 07:00:57 -0400623
Jeff Laytone598d1d82013-05-26 07:00:59 -0400624 if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
625 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -0500626 memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
Steve French254e55e2006-06-04 05:53:15 +0000627 CIFS_CRYPTO_KEY_SIZE);
Noel Powerf2910952015-05-27 09:22:10 +0100628 } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
629 server->capabilities & CAP_EXTENDED_SECURITY) {
Jeff Laytone598d1d82013-05-26 07:00:59 -0400630 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
Jeff Layton3f618222013-06-12 19:52:14 -0500631 rc = decode_ext_sec_blob(ses, pSMBr);
Jeff Laytone598d1d82013-05-26 07:00:59 -0400632 } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
Steve French07cc6cf2011-05-27 04:12:29 +0000633 rc = -EIO; /* no crypt key only if plain text pwd */
Jeff Laytone598d1d82013-05-26 07:00:59 -0400634 } else {
635 server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
Steve French254e55e2006-06-04 05:53:15 +0000636 server->capabilities &= ~CAP_EXTENDED_SECURITY;
Jeff Laytone598d1d82013-05-26 07:00:59 -0400637 }
Steve French254e55e2006-06-04 05:53:15 +0000638
639signing_check:
Jeff Layton9ddec562013-05-26 07:00:58 -0400640 if (!rc)
Jeff Layton38d77c52013-05-26 07:01:00 -0400641 rc = cifs_enable_signing(server, ses->sign);
Steve French50c2f752007-07-13 00:33:32 +0000642neg_err_exit:
Steve French4a6d87f2005-08-13 08:15:54 -0700643 cifs_buf_release(pSMB);
Steve French254e55e2006-06-04 05:53:15 +0000644
Joe Perchesf96637b2013-05-04 22:12:25 -0500645 cifs_dbg(FYI, "negprot rc %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return rc;
647}
648
649int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +0400650CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct smb_hdr *smb_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Joe Perchesf96637b2013-05-04 22:12:25 -0500655 cifs_dbg(FYI, "In tree disconnect\n");
Jeff Laytonf1987b42008-11-15 11:12:47 -0500656
657 /* BB: do we need to check this? These should never be NULL. */
658 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
659 return -EIO;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
Jeff Laytonf1987b42008-11-15 11:12:47 -0500662 * No need to return error on this operation if tid invalidated and
663 * closed on server already e.g. due to tcp session crashing. Also,
664 * the tcon is no longer on the list, so no need to take lock before
665 * checking this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Steve French268875b2009-06-25 00:29:21 +0000667 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
Steve French50c2f752007-07-13 00:33:32 +0000668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Steve French50c2f752007-07-13 00:33:32 +0000670 rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
Steve French09d1db52005-04-28 22:41:08 -0700671 (void **)&smb_buffer);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500672 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return rc;
Steve French133672e2007-11-13 22:41:37 +0000674
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400675 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500677 cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Steve French50c2f752007-07-13 00:33:32 +0000679 /* No need to return error on this operation if tid invalidated and
Jeff Laytonf1987b42008-11-15 11:12:47 -0500680 closed on server already e.g. due to tcp session crashing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (rc == -EAGAIN)
682 rc = 0;
683
684 return rc;
685}
686
Jeff Layton766fdbb2011-01-11 07:24:21 -0500687/*
688 * This is a no-op for now. We're not really interested in the reply, but
689 * rather in the fact that the server sent one and that server->lstrp
690 * gets updated.
691 *
692 * FIXME: maybe we should consider checking that the reply matches request?
693 */
694static void
695cifs_echo_callback(struct mid_q_entry *mid)
696{
697 struct TCP_Server_Info *server = mid->callback_data;
698
Christopher Oo5fb4e282015-06-25 16:10:48 -0700699 mutex_lock(&server->srv_mutex);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500700 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -0700701 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400702 add_credits(server, 1, CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500703}
704
705int
706CIFSSMBEcho(struct TCP_Server_Info *server)
707{
708 ECHO_REQ *smb;
709 int rc = 0;
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400710 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -0700711 struct smb_rqst rqst = { .rq_iov = &iov,
712 .rq_nvec = 1 };
Jeff Layton766fdbb2011-01-11 07:24:21 -0500713
Joe Perchesf96637b2013-05-04 22:12:25 -0500714 cifs_dbg(FYI, "In echo request\n");
Jeff Layton766fdbb2011-01-11 07:24:21 -0500715
716 rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
717 if (rc)
718 return rc;
719
720 /* set up echo request */
Steve French5443d132011-03-13 05:08:25 +0000721 smb->hdr.Tid = 0xffff;
Jeff Layton99d86c82011-01-20 21:19:25 -0500722 smb->hdr.WordCount = 1;
723 put_unaligned_le16(1, &smb->EchoCount);
Jeff Layton820a8032011-05-04 08:05:26 -0400724 put_bcc(1, &smb->hdr);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500725 smb->Data[0] = 'a';
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000726 inc_rfc1001_len(smb, 3);
Jeff Laytonfcc31cb2011-05-19 16:22:53 -0400727 iov.iov_base = smb;
728 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Layton766fdbb2011-01-11 07:24:21 -0500729
Jeff Laytonfec344e2012-09-18 16:20:35 -0700730 rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
Pavel Shilovskya891f0f2012-05-23 16:14:34 +0400731 server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500732 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500733 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
Jeff Layton766fdbb2011-01-11 07:24:21 -0500734
735 cifs_small_buf_release(smb);
736
737 return rc;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740int
Pavel Shilovsky58c45c52012-05-25 10:54:49 +0400741CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 LOGOFF_ANDX_REQ *pSMB;
744 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Joe Perchesf96637b2013-05-04 22:12:25 -0500746 cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
Jeff Layton14fbf502008-11-14 13:53:46 -0500747
748 /*
749 * BB: do we need to check validity of ses and server? They should
750 * always be valid since we have an active reference. If not, that
751 * should probably be a BUG()
752 */
753 if (!ses || !ses->server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EIO;
755
Steve Frenchd7b619c2010-02-25 05:36:46 +0000756 mutex_lock(&ses->session_mutex);
Steve French3b795212008-11-13 19:45:32 +0000757 if (ses->need_reconnect)
758 goto session_already_dead; /* no need to send SMBlogoff if uid
759 already closed due to reconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
761 if (rc) {
Steve Frenchd7b619c2010-02-25 05:36:46 +0000762 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return rc;
764 }
765
Pavel Shilovsky88257362012-05-23 14:01:59 +0400766 pSMB->hdr.Mid = get_next_mid(ses->server);
Steve French1982c342005-08-17 12:38:22 -0700767
Jeff Layton38d77c52013-05-26 07:01:00 -0400768 if (ses->server->sign)
769 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 pSMB->hdr.Uid = ses->Suid;
772
773 pSMB->AndXCommand = 0xFF;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -0400774 rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
Steve French3b795212008-11-13 19:45:32 +0000775session_already_dead:
Steve Frenchd7b619c2010-02-25 05:36:46 +0000776 mutex_unlock(&ses->session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* if session dead then we do not need to do ulogoff,
Steve French50c2f752007-07-13 00:33:32 +0000779 since server closed smb session, no sense reporting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 error */
781 if (rc == -EAGAIN)
782 rc = 0;
783 return rc;
784}
785
786int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400787CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
788 const char *fileName, __u16 type,
789 const struct nls_table *nls_codepage, int remap)
Steve French2d785a52007-07-15 01:48:57 +0000790{
791 TRANSACTION2_SPI_REQ *pSMB = NULL;
792 TRANSACTION2_SPI_RSP *pSMBr = NULL;
793 struct unlink_psx_rq *pRqD;
794 int name_len;
795 int rc = 0;
796 int bytes_returned = 0;
797 __u16 params, param_offset, offset, byte_count;
798
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(FYI, "In POSIX delete\n");
Steve French2d785a52007-07-15 01:48:57 +0000800PsxDelete:
801 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
802 (void **) &pSMBr);
803 if (rc)
804 return rc;
805
806 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
807 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -0600808 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
809 PATH_MAX, nls_codepage, remap);
Steve French2d785a52007-07-15 01:48:57 +0000810 name_len++; /* trailing null */
811 name_len *= 2;
812 } else { /* BB add path length overrun check */
813 name_len = strnlen(fileName, PATH_MAX);
814 name_len++; /* trailing null */
815 strncpy(pSMB->FileName, fileName, name_len);
816 }
817
818 params = 6 + name_len;
819 pSMB->MaxParameterCount = cpu_to_le16(2);
820 pSMB->MaxDataCount = 0; /* BB double check this with jra */
821 pSMB->MaxSetupCount = 0;
822 pSMB->Reserved = 0;
823 pSMB->Flags = 0;
824 pSMB->Timeout = 0;
825 pSMB->Reserved2 = 0;
826 param_offset = offsetof(struct smb_com_transaction2_spi_req,
827 InformationLevel) - 4;
828 offset = param_offset + params;
829
830 /* Setup pointer to Request Data (inode type) */
831 pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
832 pRqD->type = cpu_to_le16(type);
833 pSMB->ParameterOffset = cpu_to_le16(param_offset);
834 pSMB->DataOffset = cpu_to_le16(offset);
835 pSMB->SetupCount = 1;
836 pSMB->Reserved3 = 0;
837 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
838 byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
839
840 pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
841 pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
842 pSMB->ParameterCount = cpu_to_le16(params);
843 pSMB->TotalParameterCount = pSMB->ParameterCount;
844 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
845 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000846 inc_rfc1001_len(pSMB, byte_count);
Steve French2d785a52007-07-15 01:48:57 +0000847 pSMB->ByteCount = cpu_to_le16(byte_count);
848 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
849 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +0000850 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500851 cifs_dbg(FYI, "Posix delete returned %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +0000852 cifs_buf_release(pSMB);
853
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400854 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve French2d785a52007-07-15 01:48:57 +0000855
856 if (rc == -EAGAIN)
857 goto PsxDelete;
858
859 return rc;
860}
861
862int
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700863CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
864 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 DELETE_FILE_REQ *pSMB = NULL;
867 DELETE_FILE_RSP *pSMBr = NULL;
868 int rc = 0;
869 int bytes_returned;
870 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500871 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873DelFileRetry:
874 rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
875 (void **) &pSMBr);
876 if (rc)
877 return rc;
878
879 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700880 name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
881 PATH_MAX, cifs_sb->local_nls,
882 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 name_len++; /* trailing null */
884 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700885 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700886 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 name_len++; /* trailing null */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700888 strncpy(pSMB->fileName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 pSMB->SearchAttributes =
891 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
892 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000893 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 pSMB->ByteCount = cpu_to_le16(name_len + 1);
895 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
896 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400897 cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
Steve Frenchad7a2922008-02-07 23:25:02 +0000898 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500899 cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 cifs_buf_release(pSMB);
902 if (rc == -EAGAIN)
903 goto DelFileRetry;
904
905 return rc;
906}
907
908int
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400909CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
910 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 DELETE_DIRECTORY_REQ *pSMB = NULL;
913 DELETE_DIRECTORY_RSP *pSMBr = NULL;
914 int rc = 0;
915 int bytes_returned;
916 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500917 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Joe Perchesf96637b2013-05-04 22:12:25 -0500919 cifs_dbg(FYI, "In CIFSSMBRmDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920RmDirRetry:
921 rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
922 (void **) &pSMBr);
923 if (rc)
924 return rc;
925
926 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400927 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
928 PATH_MAX, cifs_sb->local_nls,
929 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 name_len++; /* trailing null */
931 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700932 } else { /* BB improve check for buffer overruns BB */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400933 name_len = strnlen(name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 name_len++; /* trailing null */
Pavel Shilovskyf958ca52012-07-10 16:14:18 +0400935 strncpy(pSMB->DirName, name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000939 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 pSMB->ByteCount = cpu_to_le16(name_len + 1);
941 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
942 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400943 cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000944 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500945 cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 cifs_buf_release(pSMB);
948 if (rc == -EAGAIN)
949 goto RmDirRetry;
950 return rc;
951}
952
953int
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300954CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
955 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 int rc = 0;
958 CREATE_DIRECTORY_REQ *pSMB = NULL;
959 CREATE_DIRECTORY_RSP *pSMBr = NULL;
960 int bytes_returned;
961 int name_len;
Steve French2baa2682014-09-27 02:19:01 -0500962 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Joe Perchesf96637b2013-05-04 22:12:25 -0500964 cifs_dbg(FYI, "In CIFSSMBMkDir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965MkDirRetry:
966 rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
967 (void **) &pSMBr);
968 if (rc)
969 return rc;
970
971 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -0600972 name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
Pavel Shilovskyf4367202012-03-17 11:41:12 +0300973 PATH_MAX, cifs_sb->local_nls,
974 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 name_len++; /* trailing null */
976 name_len *= 2;
Steve French09d1db52005-04-28 22:41:08 -0700977 } else { /* BB improve check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 name_len = strnlen(name, PATH_MAX);
979 name_len++; /* trailing null */
980 strncpy(pSMB->DirName, name, name_len);
981 }
982
983 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +0000984 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 pSMB->ByteCount = cpu_to_le16(name_len + 1);
986 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
987 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +0400988 cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
Steve Frenchad7a2922008-02-07 23:25:02 +0000989 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500990 cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
Steve Frencha5a2b482005-08-20 21:42:53 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 cifs_buf_release(pSMB);
993 if (rc == -EAGAIN)
994 goto MkDirRetry;
995 return rc;
996}
997
Steve French2dd29d32007-04-23 22:07:35 +0000998int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400999CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
1000 __u32 posix_flags, __u64 mode, __u16 *netfid,
1001 FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
1002 const char *name, const struct nls_table *nls_codepage,
1003 int remap)
Steve French2dd29d32007-04-23 22:07:35 +00001004{
1005 TRANSACTION2_SPI_REQ *pSMB = NULL;
1006 TRANSACTION2_SPI_RSP *pSMBr = NULL;
1007 int name_len;
1008 int rc = 0;
1009 int bytes_returned = 0;
Steve French2dd29d32007-04-23 22:07:35 +00001010 __u16 params, param_offset, offset, byte_count, count;
Steve Frenchad7a2922008-02-07 23:25:02 +00001011 OPEN_PSX_REQ *pdata;
1012 OPEN_PSX_RSP *psx_rsp;
Steve French2dd29d32007-04-23 22:07:35 +00001013
Joe Perchesf96637b2013-05-04 22:12:25 -05001014 cifs_dbg(FYI, "In POSIX Create\n");
Steve French2dd29d32007-04-23 22:07:35 +00001015PsxCreat:
1016 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
1017 (void **) &pSMBr);
1018 if (rc)
1019 return rc;
1020
1021 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1022 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001023 cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
1024 PATH_MAX, nls_codepage, remap);
Steve French2dd29d32007-04-23 22:07:35 +00001025 name_len++; /* trailing null */
1026 name_len *= 2;
1027 } else { /* BB improve the check for buffer overruns BB */
1028 name_len = strnlen(name, PATH_MAX);
1029 name_len++; /* trailing null */
1030 strncpy(pSMB->FileName, name, name_len);
1031 }
1032
1033 params = 6 + name_len;
1034 count = sizeof(OPEN_PSX_REQ);
1035 pSMB->MaxParameterCount = cpu_to_le16(2);
1036 pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
1037 pSMB->MaxSetupCount = 0;
1038 pSMB->Reserved = 0;
1039 pSMB->Flags = 0;
1040 pSMB->Timeout = 0;
1041 pSMB->Reserved2 = 0;
1042 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00001043 InformationLevel) - 4;
Steve French2dd29d32007-04-23 22:07:35 +00001044 offset = param_offset + params;
Steve French2dd29d32007-04-23 22:07:35 +00001045 pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001046 pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
Steve French2dd29d32007-04-23 22:07:35 +00001047 pdata->Permissions = cpu_to_le64(mode);
Steve French50c2f752007-07-13 00:33:32 +00001048 pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
Steve French2dd29d32007-04-23 22:07:35 +00001049 pdata->OpenFlags = cpu_to_le32(*pOplock);
1050 pSMB->ParameterOffset = cpu_to_le16(param_offset);
1051 pSMB->DataOffset = cpu_to_le16(offset);
1052 pSMB->SetupCount = 1;
1053 pSMB->Reserved3 = 0;
1054 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
1055 byte_count = 3 /* pad */ + params + count;
1056
1057 pSMB->DataCount = cpu_to_le16(count);
1058 pSMB->ParameterCount = cpu_to_le16(params);
1059 pSMB->TotalDataCount = pSMB->DataCount;
1060 pSMB->TotalParameterCount = pSMB->ParameterCount;
1061 pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
1062 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001063 inc_rfc1001_len(pSMB, byte_count);
Steve French2dd29d32007-04-23 22:07:35 +00001064 pSMB->ByteCount = cpu_to_le16(byte_count);
1065 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1066 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
1067 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001068 cifs_dbg(FYI, "Posix create returned %d\n", rc);
Steve French2dd29d32007-04-23 22:07:35 +00001069 goto psx_create_err;
1070 }
1071
Joe Perchesf96637b2013-05-04 22:12:25 -05001072 cifs_dbg(FYI, "copying inode info\n");
Steve French2dd29d32007-04-23 22:07:35 +00001073 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
1074
Jeff Layton820a8032011-05-04 08:05:26 -04001075 if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
Steve French2dd29d32007-04-23 22:07:35 +00001076 rc = -EIO; /* bad smb */
1077 goto psx_create_err;
1078 }
1079
1080 /* copy return information to pRetData */
Steve French50c2f752007-07-13 00:33:32 +00001081 psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
Steve French2dd29d32007-04-23 22:07:35 +00001082 + le16_to_cpu(pSMBr->t2.DataOffset));
Steve French50c2f752007-07-13 00:33:32 +00001083
Steve French2dd29d32007-04-23 22:07:35 +00001084 *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
Steve French790fe572007-07-07 19:25:05 +00001085 if (netfid)
Steve French2dd29d32007-04-23 22:07:35 +00001086 *netfid = psx_rsp->Fid; /* cifs fid stays in le */
1087 /* Let caller know file was created so we can set the mode. */
1088 /* Do we care about the CreateAction in any other cases? */
Steve French790fe572007-07-07 19:25:05 +00001089 if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
Steve French2dd29d32007-04-23 22:07:35 +00001090 *pOplock |= CIFS_CREATE_ACTION;
1091 /* check to make sure response data is there */
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001092 if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
1093 pRetData->Type = cpu_to_le32(-1); /* unknown */
Joe Perchesf96637b2013-05-04 22:12:25 -05001094 cifs_dbg(NOISY, "unknown type\n");
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001095 } else {
Jeff Layton820a8032011-05-04 08:05:26 -04001096 if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
Steve French2dd29d32007-04-23 22:07:35 +00001097 + sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001098 cifs_dbg(VFS, "Open response data too small\n");
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001099 pRetData->Type = cpu_to_le32(-1);
Steve French2dd29d32007-04-23 22:07:35 +00001100 goto psx_create_err;
1101 }
Steve French50c2f752007-07-13 00:33:32 +00001102 memcpy((char *) pRetData,
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001103 (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
Steve French26f57362007-08-30 22:09:15 +00001104 sizeof(FILE_UNIX_BASIC_INFO));
Steve French2dd29d32007-04-23 22:07:35 +00001105 }
Steve French2dd29d32007-04-23 22:07:35 +00001106
1107psx_create_err:
1108 cifs_buf_release(pSMB);
1109
Steve French65bc98b2009-07-10 15:27:25 +00001110 if (posix_flags & SMB_O_DIRECTORY)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001111 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
Steve French65bc98b2009-07-10 15:27:25 +00001112 else
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001113 cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
Steve French2dd29d32007-04-23 22:07:35 +00001114
1115 if (rc == -EAGAIN)
1116 goto PsxCreat;
1117
Steve French50c2f752007-07-13 00:33:32 +00001118 return rc;
Steve French2dd29d32007-04-23 22:07:35 +00001119}
1120
Steve Frencha9d02ad2005-08-24 23:06:05 -07001121static __u16 convert_disposition(int disposition)
1122{
1123 __u16 ofun = 0;
1124
1125 switch (disposition) {
1126 case FILE_SUPERSEDE:
1127 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1128 break;
1129 case FILE_OPEN:
1130 ofun = SMBOPEN_OAPPEND;
1131 break;
1132 case FILE_CREATE:
1133 ofun = SMBOPEN_OCREATE;
1134 break;
1135 case FILE_OPEN_IF:
1136 ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
1137 break;
1138 case FILE_OVERWRITE:
1139 ofun = SMBOPEN_OTRUNC;
1140 break;
1141 case FILE_OVERWRITE_IF:
1142 ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
1143 break;
1144 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001145 cifs_dbg(FYI, "unknown disposition %d\n", disposition);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001146 ofun = SMBOPEN_OAPPEND; /* regular open */
1147 }
1148 return ofun;
1149}
1150
Jeff Layton35fc37d2008-05-14 10:22:03 -07001151static int
1152access_flags_to_smbopen_mode(const int access_flags)
1153{
1154 int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
1155
1156 if (masked_flags == GENERIC_READ)
1157 return SMBOPEN_READ;
1158 else if (masked_flags == GENERIC_WRITE)
1159 return SMBOPEN_WRITE;
1160
1161 /* just go for read/write */
1162 return SMBOPEN_READWRITE;
1163}
1164
Steve Frencha9d02ad2005-08-24 23:06:05 -07001165int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001166SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001167 const char *fileName, const int openDisposition,
Steve Frenchad7a2922008-02-07 23:25:02 +00001168 const int access_flags, const int create_options, __u16 *netfid,
1169 int *pOplock, FILE_ALL_INFO *pfile_info,
Steve Frencha9d02ad2005-08-24 23:06:05 -07001170 const struct nls_table *nls_codepage, int remap)
1171{
1172 int rc = -EACCES;
1173 OPENX_REQ *pSMB = NULL;
1174 OPENX_RSP *pSMBr = NULL;
1175 int bytes_returned;
1176 int name_len;
1177 __u16 count;
1178
1179OldOpenRetry:
1180 rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
1181 (void **) &pSMBr);
1182 if (rc)
1183 return rc;
1184
1185 pSMB->AndXCommand = 0xFF; /* none */
1186
1187 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
1188 count = 1; /* account for one byte pad to word boundary */
1189 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06001190 cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
1191 fileName, PATH_MAX, nls_codepage, remap);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001192 name_len++; /* trailing null */
1193 name_len *= 2;
1194 } else { /* BB improve check for buffer overruns BB */
1195 count = 0; /* no pad */
1196 name_len = strnlen(fileName, PATH_MAX);
1197 name_len++; /* trailing null */
1198 strncpy(pSMB->fileName, fileName, name_len);
1199 }
1200 if (*pOplock & REQ_OPLOCK)
1201 pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001202 else if (*pOplock & REQ_BATCHOPLOCK)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001203 pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
Steve French26f57362007-08-30 22:09:15 +00001204
Steve Frencha9d02ad2005-08-24 23:06:05 -07001205 pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
Jeff Layton35fc37d2008-05-14 10:22:03 -07001206 pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001207 pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
1208 /* set file as system file if special file such
1209 as fifo and server expecting SFU style and
1210 no Unix extensions */
1211
Steve French790fe572007-07-07 19:25:05 +00001212 if (create_options & CREATE_OPTION_SPECIAL)
1213 pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
Steve Frenchad7a2922008-02-07 23:25:02 +00001214 else /* BB FIXME BB */
1215 pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001216
Jeff Layton67750fb2008-05-09 22:28:02 +00001217 if (create_options & CREATE_OPTION_READONLY)
1218 pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001219
1220 /* BB FIXME BB */
Steve French50c2f752007-07-13 00:33:32 +00001221/* pSMB->CreateOptions = cpu_to_le32(create_options &
1222 CREATE_OPTIONS_MASK); */
Steve Frencha9d02ad2005-08-24 23:06:05 -07001223 /* BB FIXME END BB */
Steve French3e87d802005-09-18 20:49:21 -07001224
1225 pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
Steve French70ca7342005-09-22 16:32:06 -07001226 pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001227 count += name_len;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001228 inc_rfc1001_len(pSMB, count);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001229
1230 pSMB->ByteCount = cpu_to_le16(count);
1231 /* long_op set to 1 to allow for oplock break timeouts */
1232 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Jeff Layton77499812011-01-11 07:24:23 -05001233 (struct smb_hdr *)pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001234 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001235 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001236 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Steve Frencha9d02ad2005-08-24 23:06:05 -07001237 } else {
1238 /* BB verify if wct == 15 */
1239
Steve French582d21e2008-05-13 04:54:12 +00001240/* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
Steve Frencha9d02ad2005-08-24 23:06:05 -07001241
1242 *netfid = pSMBr->Fid; /* cifs fid stays in le */
1243 /* Let caller know file was created so we can set the mode. */
1244 /* Do we care about the CreateAction in any other cases? */
1245 /* BB FIXME BB */
Steve French790fe572007-07-07 19:25:05 +00001246/* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
Steve Frencha9d02ad2005-08-24 23:06:05 -07001247 *pOplock |= CIFS_CREATE_ACTION; */
1248 /* BB FIXME END */
1249
Steve French790fe572007-07-07 19:25:05 +00001250 if (pfile_info) {
Steve Frencha9d02ad2005-08-24 23:06:05 -07001251 pfile_info->CreationTime = 0; /* BB convert CreateTime*/
1252 pfile_info->LastAccessTime = 0; /* BB fixme */
1253 pfile_info->LastWriteTime = 0; /* BB fixme */
1254 pfile_info->ChangeTime = 0; /* BB fixme */
Steve French70ca7342005-09-22 16:32:06 -07001255 pfile_info->Attributes =
Steve French50c2f752007-07-13 00:33:32 +00001256 cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
Steve Frencha9d02ad2005-08-24 23:06:05 -07001257 /* the file_info buf is endian converted by caller */
Steve French70ca7342005-09-22 16:32:06 -07001258 pfile_info->AllocationSize =
1259 cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
1260 pfile_info->EndOfFile = pfile_info->AllocationSize;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001261 pfile_info->NumberOfLinks = cpu_to_le32(1);
Jeff Layton9a8165f2008-10-17 21:03:20 -04001262 pfile_info->DeletePending = 0;
Steve Frencha9d02ad2005-08-24 23:06:05 -07001263 }
1264 }
1265
1266 cifs_buf_release(pSMB);
1267 if (rc == -EAGAIN)
1268 goto OldOpenRetry;
1269 return rc;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272int
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001273CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
1274 FILE_ALL_INFO *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 int rc = -EACCES;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001277 OPEN_REQ *req = NULL;
1278 OPEN_RSP *rsp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int bytes_returned;
1280 int name_len;
1281 __u16 count;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001282 struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
1283 struct cifs_tcon *tcon = oparms->tcon;
Steve French2baa2682014-09-27 02:19:01 -05001284 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001285 const struct nls_table *nls = cifs_sb->local_nls;
1286 int create_options = oparms->create_options;
1287 int desired_access = oparms->desired_access;
1288 int disposition = oparms->disposition;
1289 const char *path = oparms->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291openRetry:
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001292 rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
1293 (void **)&rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (rc)
1295 return rc;
1296
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001297 /* no commands go after this */
1298 req->AndXCommand = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001300 if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
1301 /* account for one byte pad to word boundary */
1302 count = 1;
1303 name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
1304 path, PATH_MAX, nls, remap);
1305 /* trailing null */
1306 name_len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 name_len *= 2;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001308 req->NameLength = cpu_to_le16(name_len);
1309 } else {
1310 /* BB improve check for buffer overruns BB */
1311 /* no pad */
1312 count = 0;
1313 name_len = strnlen(path, PATH_MAX);
1314 /* trailing null */
1315 name_len++;
1316 req->NameLength = cpu_to_le16(name_len);
1317 strncpy(req->fileName, path, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Jeff Layton67750fb2008-05-09 22:28:02 +00001319
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001320 if (*oplock & REQ_OPLOCK)
1321 req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
1322 else if (*oplock & REQ_BATCHOPLOCK)
1323 req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
1324
1325 req->DesiredAccess = cpu_to_le32(desired_access);
1326 req->AllocationSize = 0;
1327
1328 /*
1329 * Set file as system file if special file such as fifo and server
1330 * expecting SFU style and no Unix extensions.
1331 */
1332 if (create_options & CREATE_OPTION_SPECIAL)
1333 req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
1334 else
1335 req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
1336
1337 /*
1338 * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
1339 * sensitive checks for other servers such as Samba.
1340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (tcon->ses->capabilities & CAP_UNIX)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001342 req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jeff Layton67750fb2008-05-09 22:28:02 +00001344 if (create_options & CREATE_OPTION_READONLY)
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001345 req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
Jeff Layton67750fb2008-05-09 22:28:02 +00001346
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001347 req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
1348 req->CreateDisposition = cpu_to_le32(disposition);
1349 req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
1350
Steve French09d1db52005-04-28 22:41:08 -07001351 /* BB Expirement with various impersonation levels and verify */
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001352 req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
1353 req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 count += name_len;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001356 inc_rfc1001_len(req, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001358 req->ByteCount = cpu_to_le16(count);
1359 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
1360 (struct smb_hdr *)rsp, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001361 cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001363 cifs_dbg(FYI, "Error in Open = %d\n", rc);
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001364 cifs_buf_release(req);
1365 if (rc == -EAGAIN)
1366 goto openRetry;
1367 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Steve Frencha5a2b482005-08-20 21:42:53 -07001369
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001370 /* 1 byte no need to le_to_cpu */
1371 *oplock = rsp->OplockLevel;
1372 /* cifs fid stays in le */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001373 oparms->fid->netfid = rsp->Fid;
Pavel Shilovsky9bf4fa02014-01-16 15:53:33 +04001374
1375 /* Let caller know file was created so we can set the mode. */
1376 /* Do we care about the CreateAction in any other cases? */
1377 if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
1378 *oplock |= CIFS_CREATE_ACTION;
1379
1380 if (buf) {
1381 /* copy from CreationTime to Attributes */
1382 memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
1383 /* the file_info buf is endian converted by caller */
1384 buf->AllocationSize = rsp->AllocationSize;
1385 buf->EndOfFile = rsp->EndOfFile;
1386 buf->NumberOfLinks = cpu_to_le32(1);
1387 buf->DeletePending = 0;
1388 }
1389
1390 cifs_buf_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return rc;
1392}
1393
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001394/*
1395 * Discard any remaining data in the current SMB. To do this, we borrow the
1396 * current bigbuf.
1397 */
1398static int
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001399discard_remaining_data(struct TCP_Server_Info *server)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001400{
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001401 unsigned int rfclen = get_rfc1002_length(server->smallbuf);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001402 int remaining = rfclen + 4 - server->total_read;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001403
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
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001416 return 0;
1417}
1418
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001419static int
1420cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1421{
1422 int length;
1423 struct cifs_readdata *rdata = mid->callback_data;
1424
1425 length = discard_remaining_data(server);
1426 dequeue_mid(mid, rdata->result);
1427 return length;
1428}
1429
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001430int
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001431cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1432{
1433 int length, len;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04001434 unsigned int data_offset, data_len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001435 struct cifs_readdata *rdata = mid->callback_data;
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001436 char *buf = server->smallbuf;
1437 unsigned int buflen = get_rfc1002_length(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001438
Joe Perchesf96637b2013-05-04 22:12:25 -05001439 cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
1440 __func__, mid->mid, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001441
1442 /*
1443 * read the rest of READ_RSP header (sans Data array), or whatever we
1444 * can if there's not enough data. At this point, we've read down to
1445 * the Mid.
1446 */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001447 len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001448 HEADER_SIZE(server) + 1;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001449
Jeff Layton58195752012-09-19 06:22:34 -07001450 rdata->iov.iov_base = buf + HEADER_SIZE(server) - 1;
1451 rdata->iov.iov_len = len;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001452
Jeff Layton58195752012-09-19 06:22:34 -07001453 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001454 if (length < 0)
1455 return length;
1456 server->total_read += length;
1457
Pavel Shilovsky6cc3b242016-02-27 11:58:18 +03001458 if (server->ops->is_status_pending &&
1459 server->ops->is_status_pending(buf, server, 0)) {
1460 discard_remaining_data(server);
1461 return -1;
1462 }
1463
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001464 /* Was the SMB read successful? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001465 rdata->result = server->ops->map_error(buf, false);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001466 if (rdata->result != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001467 cifs_dbg(FYI, "%s: server returned error %d\n",
1468 __func__, rdata->result);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001469 return cifs_readv_discard(server, mid);
1470 }
1471
1472 /* Is there enough to get to the rest of the READ_RSP header? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001473 if (server->total_read < server->vals->read_rsp_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001474 cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
1475 __func__, server->total_read,
1476 server->vals->read_rsp_size);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001477 rdata->result = -EIO;
1478 return cifs_readv_discard(server, mid);
1479 }
1480
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001481 data_offset = server->ops->read_data_offset(buf) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001482 if (data_offset < server->total_read) {
1483 /*
1484 * win2k8 sometimes sends an offset of 0 when the read
1485 * is beyond the EOF. Treat it as if the data starts just after
1486 * the header.
1487 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001488 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1489 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001490 data_offset = server->total_read;
1491 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1492 /* data_offset is beyond the end of smallbuf */
Joe Perchesf96637b2013-05-04 22:12:25 -05001493 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1494 __func__, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001495 rdata->result = -EIO;
1496 return cifs_readv_discard(server, mid);
1497 }
1498
Joe Perchesf96637b2013-05-04 22:12:25 -05001499 cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
1500 __func__, server->total_read, data_offset);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001501
1502 len = data_offset - server->total_read;
1503 if (len > 0) {
1504 /* read any junk before data into the rest of smallbuf */
Jeff Layton58195752012-09-19 06:22:34 -07001505 rdata->iov.iov_base = buf + server->total_read;
1506 rdata->iov.iov_len = len;
1507 length = cifs_readv_from_socket(server, &rdata->iov, 1, len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001508 if (length < 0)
1509 return length;
1510 server->total_read += length;
1511 }
1512
1513 /* set up first iov for signature check */
Jeff Layton58195752012-09-19 06:22:34 -07001514 rdata->iov.iov_base = buf;
1515 rdata->iov.iov_len = server->total_read;
Joe Perchesf96637b2013-05-04 22:12:25 -05001516 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
1517 rdata->iov.iov_base, rdata->iov.iov_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001518
1519 /* how much data is in the response? */
Pavel Shilovskyeb378712012-05-17 13:02:51 +04001520 data_len = server->ops->read_data_length(buf);
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001521 if (data_offset + data_len > buflen) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001522 /* data_len is corrupt -- discard frame */
1523 rdata->result = -EIO;
1524 return cifs_readv_discard(server, mid);
1525 }
1526
Jeff Layton8321fec2012-09-19 06:22:32 -07001527 length = rdata->read_into_pages(server, rdata, data_len);
1528 if (length < 0)
1529 return length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001530
Jeff Layton8321fec2012-09-19 06:22:32 -07001531 server->total_read += length;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001532
Joe Perchesf96637b2013-05-04 22:12:25 -05001533 cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
1534 server->total_read, buflen, data_len);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001535
1536 /* discard anything left over */
Pavel Shilovsky5ffef7b2012-03-23 14:28:03 -04001537 if (server->total_read < buflen)
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001538 return cifs_readv_discard(server, mid);
1539
1540 dequeue_mid(mid, false);
1541 return length;
1542}
1543
1544static void
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001545cifs_readv_callback(struct mid_q_entry *mid)
1546{
1547 struct cifs_readdata *rdata = mid->callback_data;
1548 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1549 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton58195752012-09-19 06:22:34 -07001550 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1551 .rq_nvec = 1,
Jeff Layton8321fec2012-09-19 06:22:32 -07001552 .rq_pages = rdata->pages,
1553 .rq_npages = rdata->nr_pages,
1554 .rq_pagesz = rdata->pagesz,
1555 .rq_tailsz = rdata->tailsz };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001556
Joe Perchesf96637b2013-05-04 22:12:25 -05001557 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1558 __func__, mid->mid, mid->mid_state, rdata->result,
1559 rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001560
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04001561 switch (mid->mid_state) {
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001562 case MID_RESPONSE_RECEIVED:
1563 /* result already set, check signature */
Jeff Layton38d77c52013-05-26 07:01:00 -04001564 if (server->sign) {
Steve French985e4ff02012-08-03 09:42:45 -05001565 int rc = 0;
1566
Jeff Laytonbf5ea0e2012-09-18 16:20:34 -07001567 rc = cifs_verify_signature(&rqst, server,
Jeff Layton0124cc42013-04-03 11:55:03 -04001568 mid->sequence_number);
Steve French985e4ff02012-08-03 09:42:45 -05001569 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001570 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1571 rc);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001572 }
1573 /* FIXME: should this be counted toward the initiating task? */
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04001574 task_io_account_read(rdata->got_bytes);
1575 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001576 break;
1577 case MID_REQUEST_SUBMITTED:
1578 case MID_RETRY_NEEDED:
1579 rdata->result = -EAGAIN;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04001580 if (server->sign && rdata->got_bytes)
1581 /* reset bytes number since we can not check a sign */
1582 rdata->got_bytes = 0;
1583 /* FIXME: should this be counted toward the initiating task? */
1584 task_io_account_read(rdata->got_bytes);
1585 cifs_stats_bytes_read(tcon, rdata->got_bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001586 break;
1587 default:
1588 rdata->result = -EIO;
1589 }
1590
Jeff Laytonda472fc2012-03-23 14:40:53 -04001591 queue_work(cifsiod_wq, &rdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001592 mutex_lock(&server->srv_mutex);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001593 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07001594 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04001595 add_credits(server, 1, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001596}
1597
1598/* cifs_async_readv - send an async write, and set up mid to handle result */
1599int
1600cifs_async_readv(struct cifs_readdata *rdata)
1601{
1602 int rc;
1603 READ_REQ *smb = NULL;
1604 int wct;
1605 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
Jeff Layton58195752012-09-19 06:22:34 -07001606 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
Jeff Laytonfec344e2012-09-18 16:20:35 -07001607 .rq_nvec = 1 };
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001608
Joe Perchesf96637b2013-05-04 22:12:25 -05001609 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1610 __func__, rdata->offset, rdata->bytes);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001611
1612 if (tcon->ses->capabilities & CAP_LARGE_FILES)
1613 wct = 12;
1614 else {
1615 wct = 10; /* old style read */
1616 if ((rdata->offset >> 32) > 0) {
1617 /* can not handle this big offset for old */
1618 return -EIO;
1619 }
1620 }
1621
1622 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
1623 if (rc)
1624 return rc;
1625
1626 smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
1627 smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
1628
1629 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001630 smb->Fid = rdata->cfile->fid.netfid;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001631 smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
1632 if (wct == 12)
1633 smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
1634 smb->Remaining = 0;
1635 smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
1636 smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
1637 if (wct == 12)
1638 smb->ByteCount = 0;
1639 else {
1640 /* old style read */
1641 struct smb_com_readx_req *smbr =
1642 (struct smb_com_readx_req *)smb;
1643 smbr->ByteCount = 0;
1644 }
1645
1646 /* 4 for RFC1001 length + 1 for BCC */
Jeff Layton58195752012-09-19 06:22:34 -07001647 rdata->iov.iov_base = smb;
1648 rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001649
Jeff Layton6993f742012-05-16 07:13:17 -04001650 kref_get(&rdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07001651 rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
1652 cifs_readv_callback, rdata, 0);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001653
1654 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001655 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Jeff Layton6993f742012-05-16 07:13:17 -04001656 else
1657 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001658
1659 cifs_small_buf_release(smb);
1660 return rc;
1661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001664CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
1665 unsigned int *nbytes, char **buf, int *pbuf_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int rc = -EACCES;
1668 READ_REQ *pSMB = NULL;
1669 READ_RSP *pSMBr = NULL;
1670 char *pReadData = NULL;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001671 int wct;
Steve Frenchec637e32005-12-12 20:53:18 -08001672 int resp_buf_type = 0;
1673 struct kvec iov[1];
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001674 __u32 pid = io_parms->pid;
1675 __u16 netfid = io_parms->netfid;
1676 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001677 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001678 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Joe Perchesf96637b2013-05-04 22:12:25 -05001680 cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
Steve French790fe572007-07-07 19:25:05 +00001681 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001682 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001683 else {
Steve Frenchbfa0d752005-08-31 21:50:37 -07001684 wct = 10; /* old style read */
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001685 if ((offset >> 32) > 0) {
Steve French4c3130e2008-12-09 00:28:16 +00001686 /* can not handle this big offset for old */
1687 return -EIO;
1688 }
1689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 *nbytes = 0;
Steve Frenchec637e32005-12-12 20:53:18 -08001692 rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (rc)
1694 return rc;
1695
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001696 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1697 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* tcon and ses pointer are checked in smb_init */
1700 if (tcon->ses->server == NULL)
1701 return -ECONNABORTED;
1702
Steve Frenchec637e32005-12-12 20:53:18 -08001703 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 pSMB->Fid = netfid;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001705 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001706 if (wct == 12)
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001707 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve Frenchbfa0d752005-08-31 21:50:37 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 pSMB->Remaining = 0;
1710 pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
1711 pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
Steve French790fe572007-07-07 19:25:05 +00001712 if (wct == 12)
Steve Frenchbfa0d752005-08-31 21:50:37 -07001713 pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
1714 else {
1715 /* old style read */
Steve French50c2f752007-07-13 00:33:32 +00001716 struct smb_com_readx_req *pSMBW =
Steve Frenchbfa0d752005-08-31 21:50:37 -07001717 (struct smb_com_readx_req *)pSMB;
Steve Frenchec637e32005-12-12 20:53:18 -08001718 pSMBW->ByteCount = 0;
Steve Frenchbfa0d752005-08-31 21:50:37 -07001719 }
Steve Frenchec637e32005-12-12 20:53:18 -08001720
1721 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001722 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve Frencha761ac52007-10-18 21:45:27 +00001723 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
Jeff Layton77499812011-01-11 07:24:23 -05001724 &resp_buf_type, CIFS_LOG_ERROR);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001725 cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
Steve Frenchec637e32005-12-12 20:53:18 -08001726 pSMBr = (READ_RSP *)iov[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001728 cifs_dbg(VFS, "Send error in read = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else {
1730 int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
1731 data_length = data_length << 16;
1732 data_length += le16_to_cpu(pSMBr->DataLength);
1733 *nbytes = data_length;
1734
1735 /*check that DataLength would not go beyond end of SMB */
Steve Frenchec637e32005-12-12 20:53:18 -08001736 if ((data_length > CIFSMaxBufSize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 || (data_length > count)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001738 cifs_dbg(FYI, "bad length %d for count %d\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00001739 data_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 rc = -EIO;
1741 *nbytes = 0;
1742 } else {
Steve Frenchec637e32005-12-12 20:53:18 -08001743 pReadData = (char *) (&pSMBr->hdr.Protocol) +
Steve French26f57362007-08-30 22:09:15 +00001744 le16_to_cpu(pSMBr->DataOffset);
1745/* if (rc = copy_to_user(buf, pReadData, data_length)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001746 cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
Steve French50c2f752007-07-13 00:33:32 +00001747 rc = -EFAULT;
Steve French26f57362007-08-30 22:09:15 +00001748 }*/ /* can not use copy_to_user when using page cache*/
Steve French790fe572007-07-07 19:25:05 +00001749 if (*buf)
Steve French50c2f752007-07-13 00:33:32 +00001750 memcpy(*buf, pReadData, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Steve French4b8f9302006-02-26 16:41:18 +00001754/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French790fe572007-07-07 19:25:05 +00001755 if (*buf) {
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01001756 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French790fe572007-07-07 19:25:05 +00001757 } else if (resp_buf_type != CIFS_NO_BUFFER) {
Steve French50c2f752007-07-13 00:33:32 +00001758 /* return buffer to caller to free */
1759 *buf = iov[0].iov_base;
Steve French790fe572007-07-07 19:25:05 +00001760 if (resp_buf_type == CIFS_SMALL_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001761 *pbuf_type = CIFS_SMALL_BUFFER;
Steve French790fe572007-07-07 19:25:05 +00001762 else if (resp_buf_type == CIFS_LARGE_BUFFER)
Steve Frenchec637e32005-12-12 20:53:18 -08001763 *pbuf_type = CIFS_LARGE_BUFFER;
Steve French6cec2ae2006-02-22 17:31:52 -06001764 } /* else no valid buffer on return - leave as null */
Steve Frenchec637e32005-12-12 20:53:18 -08001765
1766 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 since file handle passed in no longer valid */
1768 return rc;
1769}
1770
Steve Frenchec637e32005-12-12 20:53:18 -08001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001773CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001774 unsigned int *nbytes, const char *buf,
Steve French50c2f752007-07-13 00:33:32 +00001775 const char __user *ubuf, const int long_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 int rc = -EACCES;
1778 WRITE_REQ *pSMB = NULL;
1779 WRITE_RSP *pSMBr = NULL;
Steve French1c955182005-08-30 20:58:07 -07001780 int bytes_returned, wct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 __u32 bytes_sent;
1782 __u16 byte_count;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001783 __u32 pid = io_parms->pid;
1784 __u16 netfid = io_parms->netfid;
1785 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00001786 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001787 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Steve Frencha24e2d72010-04-03 17:20:21 +00001789 *nbytes = 0;
1790
Joe Perchesf96637b2013-05-04 22:12:25 -05001791 /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
Steve French790fe572007-07-07 19:25:05 +00001792 if (tcon->ses == NULL)
Steve French1c955182005-08-30 20:58:07 -07001793 return -ECONNABORTED;
1794
Steve French790fe572007-07-07 19:25:05 +00001795 if (tcon->ses->capabilities & CAP_LARGE_FILES)
Steve French1c955182005-08-30 20:58:07 -07001796 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00001797 else {
Steve French1c955182005-08-30 20:58:07 -07001798 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00001799 if ((offset >> 32) > 0) {
1800 /* can not handle big offset for old srv */
1801 return -EIO;
1802 }
1803 }
Steve French1c955182005-08-30 20:58:07 -07001804
1805 rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 (void **) &pSMBr);
1807 if (rc)
1808 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001809
1810 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
1811 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /* tcon and ses pointer are checked in smb_init */
1814 if (tcon->ses->server == NULL)
1815 return -ECONNABORTED;
1816
1817 pSMB->AndXCommand = 0xFF; /* none */
1818 pSMB->Fid = netfid;
1819 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00001820 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001821 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Steve French50c2f752007-07-13 00:33:32 +00001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 pSMB->Reserved = 0xFFFFFFFF;
1824 pSMB->WriteMode = 0;
1825 pSMB->Remaining = 0;
1826
Steve French50c2f752007-07-13 00:33:32 +00001827 /* Can increase buffer size if buffer is big enough in some cases ie we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 can send more if LARGE_WRITE_X capability returned by the server and if
1829 our buffer is big enough or if we convert to iovecs on socket writes
1830 and eliminate the copy to the CIFS buffer */
Steve French790fe572007-07-07 19:25:05 +00001831 if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
1833 } else {
1834 bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
1835 & ~0xFF;
1836 }
1837
1838 if (bytes_sent > count)
1839 bytes_sent = count;
1840 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00001841 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Steve French790fe572007-07-07 19:25:05 +00001842 if (buf)
Steve French61e74802008-12-03 00:57:54 +00001843 memcpy(pSMB->Data, buf, bytes_sent);
Steve French790fe572007-07-07 19:25:05 +00001844 else if (ubuf) {
1845 if (copy_from_user(pSMB->Data, ubuf, bytes_sent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 cifs_buf_release(pSMB);
1847 return -EFAULT;
1848 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001849 } else if (count != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* No buffer */
1851 cifs_buf_release(pSMB);
1852 return -EINVAL;
Steve Frenche30dcf32005-09-20 20:49:16 -07001853 } /* else setting file size with write of zero bytes */
Steve French790fe572007-07-07 19:25:05 +00001854 if (wct == 14)
Steve Frenche30dcf32005-09-20 20:49:16 -07001855 byte_count = bytes_sent + 1; /* pad */
Steve Frenchad7a2922008-02-07 23:25:02 +00001856 else /* wct == 12 */
Steve Frenche30dcf32005-09-20 20:49:16 -07001857 byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
Steve Frenchad7a2922008-02-07 23:25:02 +00001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
1860 pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00001861 inc_rfc1001_len(pSMB, byte_count);
Steve French1c955182005-08-30 20:58:07 -07001862
Steve French790fe572007-07-07 19:25:05 +00001863 if (wct == 14)
Steve French1c955182005-08-30 20:58:07 -07001864 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00001865 else { /* old style write has byte count 4 bytes earlier
1866 so 4 bytes pad */
1867 struct smb_com_writex_req *pSMBW =
Steve French1c955182005-08-30 20:58:07 -07001868 (struct smb_com_writex_req *)pSMB;
1869 pSMBW->ByteCount = cpu_to_le16(byte_count);
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
1873 (struct smb_hdr *) pSMBr, &bytes_returned, long_op);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04001874 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001876 cifs_dbg(FYI, "Send error in write = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 } else {
1878 *nbytes = le16_to_cpu(pSMBr->CountHigh);
1879 *nbytes = (*nbytes) << 16;
1880 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05301881
1882 /*
1883 * Mask off high 16 bits when bytes written as returned by the
1884 * server is greater than bytes requested by the client. Some
1885 * OS/2 servers are known to set incorrect CountHigh values.
1886 */
1887 if (*nbytes > count)
1888 *nbytes &= 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
1891 cifs_buf_release(pSMB);
1892
Steve French50c2f752007-07-13 00:33:32 +00001893 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 since file handle passed in no longer valid */
1895
1896 return rc;
1897}
1898
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001899void
1900cifs_writedata_release(struct kref *refcount)
1901{
1902 struct cifs_writedata *wdata = container_of(refcount,
1903 struct cifs_writedata, refcount);
1904
1905 if (wdata->cfile)
1906 cifsFileInfo_put(wdata->cfile);
1907
1908 kfree(wdata);
1909}
1910
1911/*
1912 * Write failed with a retryable error. Resend the write request. It's also
1913 * possible that the page was redirtied so re-clean the page.
1914 */
1915static void
1916cifs_writev_requeue(struct cifs_writedata *wdata)
1917{
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001918 int i, rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00001919 struct inode *inode = d_inode(wdata->cfile->dentry);
Pavel Shilovskyc9de5c82012-09-18 16:20:29 -07001920 struct TCP_Server_Info *server;
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001921 unsigned int rest_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001922
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001923 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
1924 i = 0;
1925 rest_len = wdata->bytes;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001926 do {
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001927 struct cifs_writedata *wdata2;
1928 unsigned int j, nr_pages, wsize, tailsz, cur_len;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001929
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001930 wsize = server->ops->wp_retry_size(inode);
1931 if (wsize < rest_len) {
1932 nr_pages = wsize / PAGE_CACHE_SIZE;
1933 if (!nr_pages) {
1934 rc = -ENOTSUPP;
1935 break;
1936 }
1937 cur_len = nr_pages * PAGE_CACHE_SIZE;
1938 tailsz = PAGE_CACHE_SIZE;
1939 } else {
1940 nr_pages = DIV_ROUND_UP(rest_len, PAGE_CACHE_SIZE);
1941 cur_len = rest_len;
1942 tailsz = rest_len - (nr_pages - 1) * PAGE_CACHE_SIZE;
Ouyang Maochunc51bb0e2013-02-18 09:54:52 -06001943 }
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001944
1945 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
1946 if (!wdata2) {
1947 rc = -ENOMEM;
1948 break;
1949 }
1950
1951 for (j = 0; j < nr_pages; j++) {
1952 wdata2->pages[j] = wdata->pages[i + j];
1953 lock_page(wdata2->pages[j]);
1954 clear_page_dirty_for_io(wdata2->pages[j]);
1955 }
1956
1957 wdata2->sync_mode = wdata->sync_mode;
1958 wdata2->nr_pages = nr_pages;
1959 wdata2->offset = page_offset(wdata2->pages[0]);
1960 wdata2->pagesz = PAGE_CACHE_SIZE;
1961 wdata2->tailsz = tailsz;
1962 wdata2->bytes = cur_len;
1963
1964 wdata2->cfile = find_writable_file(CIFS_I(inode), false);
1965 if (!wdata2->cfile) {
1966 cifs_dbg(VFS, "No writable handles for inode\n");
1967 rc = -EBADF;
1968 break;
1969 }
1970 wdata2->pid = wdata2->cfile->pid;
1971 rc = server->ops->async_writev(wdata2, cifs_writedata_release);
1972
1973 for (j = 0; j < nr_pages; j++) {
1974 unlock_page(wdata2->pages[j]);
1975 if (rc != 0 && rc != -EAGAIN) {
1976 SetPageError(wdata2->pages[j]);
1977 end_page_writeback(wdata2->pages[j]);
1978 page_cache_release(wdata2->pages[j]);
1979 }
1980 }
1981
1982 if (rc) {
1983 kref_put(&wdata2->refcount, cifs_writedata_release);
1984 if (rc == -EAGAIN)
1985 continue;
1986 break;
1987 }
1988
1989 rest_len -= cur_len;
1990 i += nr_pages;
1991 } while (i < wdata->nr_pages);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001992
1993 mapping_set_error(inode->i_mapping, rc);
1994 kref_put(&wdata->refcount, cifs_writedata_release);
1995}
1996
Jeff Laytonc2e87642012-03-23 14:40:55 -04001997void
Jeff Laytonc28c89f2011-05-19 16:22:56 -04001998cifs_writev_complete(struct work_struct *work)
1999{
2000 struct cifs_writedata *wdata = container_of(work,
2001 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002002 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002003 int i = 0;
2004
2005 if (wdata->result == 0) {
Jeff Layton597b0272012-03-23 14:40:56 -04002006 spin_lock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002007 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
Jeff Layton597b0272012-03-23 14:40:56 -04002008 spin_unlock(&inode->i_lock);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002009 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2010 wdata->bytes);
2011 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2012 return cifs_writev_requeue(wdata);
2013
2014 for (i = 0; i < wdata->nr_pages; i++) {
2015 struct page *page = wdata->pages[i];
2016 if (wdata->result == -EAGAIN)
2017 __set_page_dirty_nobuffers(page);
2018 else if (wdata->result < 0)
2019 SetPageError(page);
2020 end_page_writeback(page);
2021 page_cache_release(page);
2022 }
2023 if (wdata->result != -EAGAIN)
2024 mapping_set_error(inode->i_mapping, wdata->result);
2025 kref_put(&wdata->refcount, cifs_writedata_release);
2026}
2027
2028struct cifs_writedata *
Jeff Laytonc2e87642012-03-23 14:40:55 -04002029cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002030{
2031 struct cifs_writedata *wdata;
2032
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002033 /* writedata + number of page pointers */
2034 wdata = kzalloc(sizeof(*wdata) +
Jeff Layton26c8f0d2014-02-07 11:04:04 -05002035 sizeof(struct page *) * nr_pages, GFP_NOFS);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002036 if (wdata != NULL) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002037 kref_init(&wdata->refcount);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002038 INIT_LIST_HEAD(&wdata->list);
2039 init_completion(&wdata->done);
2040 INIT_WORK(&wdata->work, complete);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002041 }
2042 return wdata;
2043}
2044
2045/*
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002046 * Check the mid_state and signature on received buffer (if any), and queue the
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002047 * workqueue completion task.
2048 */
2049static void
2050cifs_writev_callback(struct mid_q_entry *mid)
2051{
2052 struct cifs_writedata *wdata = mid->callback_data;
Steve French96daf2b2011-05-27 04:34:02 +00002053 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002054 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002055 unsigned int written;
2056 WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
2057
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -04002058 switch (mid->mid_state) {
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002059 case MID_RESPONSE_RECEIVED:
2060 wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
2061 if (wdata->result != 0)
2062 break;
2063
2064 written = le16_to_cpu(smb->CountHigh);
2065 written <<= 16;
2066 written += le16_to_cpu(smb->Count);
2067 /*
2068 * Mask off high 16 bits when bytes written as returned
2069 * by the server is greater than bytes requested by the
2070 * client. OS/2 servers are known to set incorrect
2071 * CountHigh values.
2072 */
2073 if (written > wdata->bytes)
2074 written &= 0xFFFF;
2075
2076 if (written < wdata->bytes)
2077 wdata->result = -ENOSPC;
2078 else
2079 wdata->bytes = written;
2080 break;
2081 case MID_REQUEST_SUBMITTED:
2082 case MID_RETRY_NEEDED:
2083 wdata->result = -EAGAIN;
2084 break;
2085 default:
2086 wdata->result = -EIO;
2087 break;
2088 }
2089
Jeff Laytonda472fc2012-03-23 14:40:53 -04002090 queue_work(cifsiod_wq, &wdata->work);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002091 mutex_lock(&server->srv_mutex);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002092 DeleteMidQEntry(mid);
Christopher Oo5fb4e282015-06-25 16:10:48 -07002093 mutex_unlock(&server->srv_mutex);
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002094 add_credits(tcon->ses->server, 1, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002095}
2096
2097/* cifs_async_writev - send an async write, and set up mid to handle result */
2098int
Steve French4a5c80d2014-02-07 20:45:12 -06002099cifs_async_writev(struct cifs_writedata *wdata,
2100 void (*release)(struct kref *kref))
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002101{
Jeff Laytoneddb0792012-09-18 16:20:35 -07002102 int rc = -EACCES;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002103 WRITE_REQ *smb = NULL;
2104 int wct;
Steve French96daf2b2011-05-27 04:34:02 +00002105 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002106 struct kvec iov;
Jeff Laytonfec344e2012-09-18 16:20:35 -07002107 struct smb_rqst rqst = { };
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002108
2109 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
2110 wct = 14;
2111 } else {
2112 wct = 12;
2113 if (wdata->offset >> 32 > 0) {
2114 /* can not handle big offset for old srv */
2115 return -EIO;
2116 }
2117 }
2118
2119 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
2120 if (rc)
2121 goto async_writev_out;
2122
Jeff Laytonfe5f5d22012-03-23 14:40:55 -04002123 smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
2124 smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002125
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002126 smb->AndXCommand = 0xFF; /* none */
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002127 smb->Fid = wdata->cfile->fid.netfid;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002128 smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
2129 if (wct == 14)
2130 smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
2131 smb->Reserved = 0xFFFFFFFF;
2132 smb->WriteMode = 0;
2133 smb->Remaining = 0;
2134
2135 smb->DataOffset =
2136 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
2137
2138 /* 4 for RFC1001 length + 1 for BCC */
Jeff Laytoneddb0792012-09-18 16:20:35 -07002139 iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
2140 iov.iov_base = smb;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002141
Jeff Laytoneddb0792012-09-18 16:20:35 -07002142 rqst.rq_iov = &iov;
2143 rqst.rq_nvec = 1;
2144 rqst.rq_pages = wdata->pages;
2145 rqst.rq_npages = wdata->nr_pages;
2146 rqst.rq_pagesz = wdata->pagesz;
2147 rqst.rq_tailsz = wdata->tailsz;
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002148
Joe Perchesf96637b2013-05-04 22:12:25 -05002149 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2150 wdata->offset, wdata->bytes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002151
2152 smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
2153 smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
2154
2155 if (wct == 14) {
2156 inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
2157 put_bcc(wdata->bytes + 1, &smb->hdr);
2158 } else {
2159 /* wct == 12 */
2160 struct smb_com_writex_req *smbw =
2161 (struct smb_com_writex_req *)smb;
2162 inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
2163 put_bcc(wdata->bytes + 5, &smbw->hdr);
Jeff Laytoneddb0792012-09-18 16:20:35 -07002164 iov.iov_len += 4; /* pad bigger by four bytes */
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002165 }
2166
2167 kref_get(&wdata->refcount);
Jeff Laytonfec344e2012-09-18 16:20:35 -07002168 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
2169 cifs_writev_callback, wdata, 0);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002170
2171 if (rc == 0)
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002172 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002173 else
Steve French4a5c80d2014-02-07 20:45:12 -06002174 kref_put(&wdata->refcount, release);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002175
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002176async_writev_out:
2177 cifs_small_buf_release(smb);
Jeff Laytonc28c89f2011-05-19 16:22:56 -04002178 return rc;
2179}
2180
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002181int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002182CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002183 unsigned int *nbytes, struct kvec *iov, int n_vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 int rc = -EACCES;
2186 WRITE_REQ *pSMB = NULL;
Steve Frenchec637e32005-12-12 20:53:18 -08002187 int wct;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002188 int smb_hdr_len;
Steve Frenchec637e32005-12-12 20:53:18 -08002189 int resp_buf_type = 0;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002190 __u32 pid = io_parms->pid;
2191 __u16 netfid = io_parms->netfid;
2192 __u64 offset = io_parms->offset;
Steve French96daf2b2011-05-27 04:34:02 +00002193 struct cifs_tcon *tcon = io_parms->tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002194 unsigned int count = io_parms->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002196 *nbytes = 0;
2197
Joe Perchesf96637b2013-05-04 22:12:25 -05002198 cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
Steve Frenchff7feac2005-11-15 16:45:16 -08002199
Steve French4c3130e2008-12-09 00:28:16 +00002200 if (tcon->ses->capabilities & CAP_LARGE_FILES) {
Steve French8cc64c62005-10-03 13:49:43 -07002201 wct = 14;
Steve French4c3130e2008-12-09 00:28:16 +00002202 } else {
Steve French8cc64c62005-10-03 13:49:43 -07002203 wct = 12;
Steve French4c3130e2008-12-09 00:28:16 +00002204 if ((offset >> 32) > 0) {
2205 /* can not handle big offset for old srv */
2206 return -EIO;
2207 }
2208 }
Steve French8cc64c62005-10-03 13:49:43 -07002209 rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (rc)
2211 return rc;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002212
2213 pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
2214 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /* tcon and ses pointer are checked in smb_init */
2217 if (tcon->ses->server == NULL)
2218 return -ECONNABORTED;
2219
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002220 pSMB->AndXCommand = 0xFF; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 pSMB->Fid = netfid;
2222 pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
Steve French790fe572007-07-07 19:25:05 +00002223 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002224 pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 pSMB->Reserved = 0xFFFFFFFF;
2226 pSMB->WriteMode = 0;
2227 pSMB->Remaining = 0;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 pSMB->DataOffset =
Steve French50c2f752007-07-13 00:33:32 +00002230 cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Steve French3e844692005-10-03 13:37:24 -07002232 pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
2233 pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002234 /* header + 1 byte pad */
2235 smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
Steve French790fe572007-07-07 19:25:05 +00002236 if (wct == 14)
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002237 inc_rfc1001_len(pSMB, count + 1);
Steve French8cc64c62005-10-03 13:49:43 -07002238 else /* wct == 12 */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002239 inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
Steve French790fe572007-07-07 19:25:05 +00002240 if (wct == 14)
Steve French8cc64c62005-10-03 13:49:43 -07002241 pSMB->ByteCount = cpu_to_le16(count + 1);
2242 else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
Steve French50c2f752007-07-13 00:33:32 +00002243 struct smb_com_writex_req *pSMBW =
Steve French8cc64c62005-10-03 13:49:43 -07002244 (struct smb_com_writex_req *)pSMB;
2245 pSMBW->ByteCount = cpu_to_le16(count + 5);
2246 }
Steve French3e844692005-10-03 13:37:24 -07002247 iov[0].iov_base = pSMB;
Steve French790fe572007-07-07 19:25:05 +00002248 if (wct == 14)
Steve Frenchec637e32005-12-12 20:53:18 -08002249 iov[0].iov_len = smb_hdr_len + 4;
2250 else /* wct == 12 pad bigger by four bytes */
2251 iov[0].iov_len = smb_hdr_len + 8;
Steve French50c2f752007-07-13 00:33:32 +00002252
Steve French3e844692005-10-03 13:37:24 -07002253
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07002254 rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002255 cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002257 cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
Steve French790fe572007-07-07 19:25:05 +00002258 } else if (resp_buf_type == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -08002259 /* presumably this can not happen, but best to be safe */
2260 rc = -EIO;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002261 } else {
Steve Frenchad7a2922008-02-07 23:25:02 +00002262 WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002263 *nbytes = le16_to_cpu(pSMBr->CountHigh);
2264 *nbytes = (*nbytes) << 16;
2265 *nbytes += le16_to_cpu(pSMBr->Count);
Suresh Jayaraman6513a812010-03-31 12:00:03 +05302266
2267 /*
2268 * Mask off high 16 bits when bytes written as returned by the
2269 * server is greater than bytes requested by the client. OS/2
2270 * servers are known to set incorrect CountHigh values.
2271 */
2272 if (*nbytes > count)
2273 *nbytes &= 0xFFFF;
Steve French50c2f752007-07-13 00:33:32 +00002274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Steve French4b8f9302006-02-26 16:41:18 +00002276/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002277 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Steve French50c2f752007-07-13 00:33:32 +00002279 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 since file handle passed in no longer valid */
2281
2282 return rc;
2283}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002284
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002285int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2286 const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002287 const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
2288{
2289 int rc = 0;
2290 LOCK_REQ *pSMB = NULL;
2291 struct kvec iov[2];
2292 int resp_buf_type;
2293 __u16 count;
2294
Joe Perchesf96637b2013-05-04 22:12:25 -05002295 cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
2296 num_lock, num_unlock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002297
2298 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2299 if (rc)
2300 return rc;
2301
2302 pSMB->Timeout = 0;
2303 pSMB->NumberOfLocks = cpu_to_le16(num_lock);
2304 pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
2305 pSMB->LockType = lock_type;
2306 pSMB->AndXCommand = 0xFF; /* none */
2307 pSMB->Fid = netfid; /* netfid stays le */
2308
2309 count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2310 inc_rfc1001_len(pSMB, count);
2311 pSMB->ByteCount = cpu_to_le16(count);
2312
2313 iov[0].iov_base = (char *)pSMB;
2314 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
2315 (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2316 iov[1].iov_base = (char *)buf;
2317 iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
2318
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002319 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002320 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2321 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002322 cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04002323
2324 return rc;
2325}
Steve Frenchd6e04ae2005-06-13 13:24:43 -05002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002328CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002329 const __u16 smb_file_id, const __u32 netpid, const __u64 len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 const __u64 offset, const __u32 numUnlock,
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002331 const __u32 numLock, const __u8 lockType,
2332 const bool waitFlag, const __u8 oplock_level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 int rc = 0;
2335 LOCK_REQ *pSMB = NULL;
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002336/* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 int bytes_returned;
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002338 int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 __u16 count;
2340
Joe Perchesf96637b2013-05-04 22:12:25 -05002341 cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
2342 (int)waitFlag, numLock);
Steve French46810cb2005-04-28 22:41:09 -07002343 rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (rc)
2346 return rc;
2347
Steve French790fe572007-07-07 19:25:05 +00002348 if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002349 /* no response expected */
2350 flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 pSMB->Timeout = 0;
Steve French4b18f2a2008-04-29 00:06:05 +00002352 } else if (waitFlag) {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002353 flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
2355 } else {
2356 pSMB->Timeout = 0;
2357 }
2358
2359 pSMB->NumberOfLocks = cpu_to_le16(numLock);
2360 pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
2361 pSMB->LockType = lockType;
Pavel Shilovsky12fed002011-01-17 20:15:44 +03002362 pSMB->OplockLevel = oplock_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 pSMB->AndXCommand = 0xFF; /* none */
2364 pSMB->Fid = smb_file_id; /* netfid stays le */
2365
Steve French790fe572007-07-07 19:25:05 +00002366 if ((numLock != 0) || (numUnlock != 0)) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04002367 pSMB->Locks[0].Pid = cpu_to_le16(netpid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 /* BB where to store pid high? */
2369 pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
2370 pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
2371 pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
2372 pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
2373 count = sizeof(LOCKING_ANDX_RANGE);
2374 } else {
2375 /* oplock break */
2376 count = 0;
2377 }
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002378 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 pSMB->ByteCount = cpu_to_le16(count);
2380
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002381 if (waitFlag) {
2382 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
Steve Frenchaaa9bbe2008-05-23 17:38:32 +00002383 (struct smb_hdr *) pSMB, &bytes_returned);
Steve French133672e2007-11-13 22:41:37 +00002384 cifs_small_buf_release(pSMB);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002385 } else {
Pavel Shilovskya891f0f2012-05-23 16:14:34 +04002386 rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
Steve French133672e2007-11-13 22:41:37 +00002387 /* SMB buffer freed by function above */
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002388 }
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002389 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002390 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002391 cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Steve French50c2f752007-07-13 00:33:32 +00002393 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 since file handle passed in no longer valid */
2395 return rc;
2396}
2397
2398int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002399CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002400 const __u16 smb_file_id, const __u32 netpid,
2401 const loff_t start_offset, const __u64 len,
2402 struct file_lock *pLockData, const __u16 lock_type,
2403 const bool waitFlag)
Steve French08547b02006-02-28 22:39:25 +00002404{
2405 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2406 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French08547b02006-02-28 22:39:25 +00002407 struct cifs_posix_lock *parm_data;
2408 int rc = 0;
Steve French3a5ff612006-07-14 22:37:11 +00002409 int timeout = 0;
Steve French08547b02006-02-28 22:39:25 +00002410 int bytes_returned = 0;
Steve French133672e2007-11-13 22:41:37 +00002411 int resp_buf_type = 0;
Steve French08547b02006-02-28 22:39:25 +00002412 __u16 params, param_offset, offset, byte_count, count;
Steve French133672e2007-11-13 22:41:37 +00002413 struct kvec iov[1];
Steve French08547b02006-02-28 22:39:25 +00002414
Joe Perchesf96637b2013-05-04 22:12:25 -05002415 cifs_dbg(FYI, "Posix Lock\n");
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002416
Steve French08547b02006-02-28 22:39:25 +00002417 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
2418
2419 if (rc)
2420 return rc;
2421
2422 pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
2423
Steve French50c2f752007-07-13 00:33:32 +00002424 params = 6;
Steve French08547b02006-02-28 22:39:25 +00002425 pSMB->MaxSetupCount = 0;
2426 pSMB->Reserved = 0;
2427 pSMB->Flags = 0;
Steve French08547b02006-02-28 22:39:25 +00002428 pSMB->Reserved2 = 0;
2429 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2430 offset = param_offset + params;
2431
Steve French08547b02006-02-28 22:39:25 +00002432 count = sizeof(struct cifs_posix_lock);
2433 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002434 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Steve French08547b02006-02-28 22:39:25 +00002435 pSMB->SetupCount = 1;
2436 pSMB->Reserved3 = 0;
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002437 if (pLockData)
Steve French08547b02006-02-28 22:39:25 +00002438 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
2439 else
2440 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2441 byte_count = 3 /* pad */ + params + count;
2442 pSMB->DataCount = cpu_to_le16(count);
2443 pSMB->ParameterCount = cpu_to_le16(params);
2444 pSMB->TotalDataCount = pSMB->DataCount;
2445 pSMB->TotalParameterCount = pSMB->ParameterCount;
2446 pSMB->ParameterOffset = cpu_to_le16(param_offset);
Steve French50c2f752007-07-13 00:33:32 +00002447 parm_data = (struct cifs_posix_lock *)
Steve French08547b02006-02-28 22:39:25 +00002448 (((char *) &pSMB->hdr.Protocol) + offset);
2449
2450 parm_data->lock_type = cpu_to_le16(lock_type);
Steve French790fe572007-07-07 19:25:05 +00002451 if (waitFlag) {
Steve French133672e2007-11-13 22:41:37 +00002452 timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
Steve Frenchcec6815a2006-05-30 18:07:17 +00002453 parm_data->lock_flags = cpu_to_le16(1);
Steve French3a5ff612006-07-14 22:37:11 +00002454 pSMB->Timeout = cpu_to_le32(-1);
2455 } else
2456 pSMB->Timeout = 0;
2457
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04002458 parm_data->pid = cpu_to_le32(netpid);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002459 parm_data->start = cpu_to_le64(start_offset);
Steve Frenchcec6815a2006-05-30 18:07:17 +00002460 parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
Steve French08547b02006-02-28 22:39:25 +00002461
2462 pSMB->DataOffset = cpu_to_le16(offset);
Steve Frenchf26282c2006-03-01 09:17:37 +00002463 pSMB->Fid = smb_file_id;
Steve French08547b02006-02-28 22:39:25 +00002464 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
2465 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002466 inc_rfc1001_len(pSMB, byte_count);
Steve French08547b02006-02-28 22:39:25 +00002467 pSMB->ByteCount = cpu_to_le16(byte_count);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002468 if (waitFlag) {
2469 rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
2470 (struct smb_hdr *) pSMBr, &bytes_returned);
2471 } else {
Steve French133672e2007-11-13 22:41:37 +00002472 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002473 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French133672e2007-11-13 22:41:37 +00002474 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
2475 &resp_buf_type, timeout);
2476 pSMB = NULL; /* request buf already freed by SendReceive2. Do
2477 not try to free it twice below on exit */
2478 pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00002479 }
2480
Steve French08547b02006-02-28 22:39:25 +00002481 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002482 cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
Jeff Laytonc5fd3632012-07-23 13:28:37 -04002483 } else if (pLockData) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002484 /* lock structure can be returned on get */
2485 __u16 data_offset;
2486 __u16 data_count;
2487 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French08547b02006-02-28 22:39:25 +00002488
Jeff Layton820a8032011-05-04 08:05:26 -04002489 if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002490 rc = -EIO; /* bad smb */
2491 goto plk_err_exit;
2492 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002493 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
2494 data_count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French790fe572007-07-07 19:25:05 +00002495 if (data_count < sizeof(struct cifs_posix_lock)) {
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002496 rc = -EIO;
2497 goto plk_err_exit;
2498 }
2499 parm_data = (struct cifs_posix_lock *)
2500 ((char *)&pSMBr->hdr.Protocol + data_offset);
Fabian Frederickbc09d142014-12-10 15:41:15 -08002501 if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002502 pLockData->fl_type = F_UNLCK;
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002503 else {
2504 if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002505 cpu_to_le16(CIFS_RDLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002506 pLockData->fl_type = F_RDLCK;
2507 else if (parm_data->lock_type ==
Fabian Frederickbc09d142014-12-10 15:41:15 -08002508 cpu_to_le16(CIFS_WRLCK))
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002509 pLockData->fl_type = F_WRLCK;
2510
Steve French5443d132011-03-13 05:08:25 +00002511 pLockData->fl_start = le64_to_cpu(parm_data->start);
2512 pLockData->fl_end = pLockData->fl_start +
2513 le64_to_cpu(parm_data->length) - 1;
2514 pLockData->fl_pid = le32_to_cpu(parm_data->pid);
Pavel Shilovskyf05337c2010-04-05 09:59:14 +04002515 }
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002516 }
Steve French50c2f752007-07-13 00:33:32 +00002517
Steve Frenchfc94cdb2006-05-30 18:03:32 +00002518plk_err_exit:
Steve French08547b02006-02-28 22:39:25 +00002519 if (pSMB)
2520 cifs_small_buf_release(pSMB);
2521
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01002522 free_rsp_buf(resp_buf_type, iov[0].iov_base);
Steve French133672e2007-11-13 22:41:37 +00002523
Steve French08547b02006-02-28 22:39:25 +00002524 /* Note: On -EAGAIN error only caller can retry on handle based calls
2525 since file handle passed in no longer valid */
2526
2527 return rc;
2528}
2529
2530
2531int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002532CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 int rc = 0;
2535 CLOSE_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002536 cifs_dbg(FYI, "In CIFSSMBClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538/* do not retry on dead session on close */
2539 rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
Steve French790fe572007-07-07 19:25:05 +00002540 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return 0;
2542 if (rc)
2543 return rc;
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 pSMB->FileID = (__u16) smb_file_id;
Steve Frenchb815f1e52006-10-02 05:53:29 +00002546 pSMB->LastWriteTime = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002548 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002549 cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (rc) {
Steve French790fe572007-07-07 19:25:05 +00002551 if (rc != -EINTR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /* EINTR is expected when user ctl-c to kill app */
Joe Perchesf96637b2013-05-04 22:12:25 -05002553 cifs_dbg(VFS, "Send error in Close = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 }
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /* Since session is dead, file will be closed on server already */
Steve French790fe572007-07-07 19:25:05 +00002558 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 rc = 0;
2560
2561 return rc;
2562}
2563
2564int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002565CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
Steve Frenchb298f222009-02-21 21:17:43 +00002566{
2567 int rc = 0;
2568 FLUSH_REQ *pSMB = NULL;
Joe Perchesf96637b2013-05-04 22:12:25 -05002569 cifs_dbg(FYI, "In CIFSSMBFlush\n");
Steve Frenchb298f222009-02-21 21:17:43 +00002570
2571 rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
2572 if (rc)
2573 return rc;
2574
2575 pSMB->FileID = (__u16) smb_file_id;
2576 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04002577 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002578 cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
Steve Frenchb298f222009-02-21 21:17:43 +00002579 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002580 cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
Steve Frenchb298f222009-02-21 21:17:43 +00002581
2582 return rc;
2583}
2584
2585int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002586CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002587 const char *from_name, const char *to_name,
2588 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 int rc = 0;
2591 RENAME_REQ *pSMB = NULL;
2592 RENAME_RSP *pSMBr = NULL;
2593 int bytes_returned;
2594 int name_len, name_len2;
2595 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002596 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Joe Perchesf96637b2013-05-04 22:12:25 -05002598 cifs_dbg(FYI, "In CIFSSMBRename\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599renameRetry:
2600 rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
2601 (void **) &pSMBr);
2602 if (rc)
2603 return rc;
2604
2605 pSMB->BufferFormat = 0x04;
2606 pSMB->SearchAttributes =
2607 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
2608 ATTR_DIRECTORY);
2609
2610 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002611 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2612 from_name, PATH_MAX,
2613 cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 name_len++; /* trailing null */
2615 name_len *= 2;
2616 pSMB->OldFileName[name_len] = 0x04; /* pad */
2617 /* protocol requires ASCII signature byte on Unicode string */
2618 pSMB->OldFileName[name_len + 1] = 0x00;
2619 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002620 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002621 to_name, PATH_MAX, cifs_sb->local_nls,
2622 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2624 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002625 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002626 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 name_len++; /* trailing null */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002628 strncpy(pSMB->OldFileName, from_name, name_len);
2629 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 name_len2++; /* trailing null */
2631 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07002632 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 name_len2++; /* trailing null */
2634 name_len2++; /* signature byte */
2635 }
2636
2637 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002638 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 pSMB->ByteCount = cpu_to_le16(count);
2640
2641 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2642 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002643 cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002644 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002645 cifs_dbg(FYI, "Send error in rename = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 cifs_buf_release(pSMB);
2648
2649 if (rc == -EAGAIN)
2650 goto renameRetry;
2651
2652 return rc;
2653}
2654
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002655int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
Jeff Layton391e5752008-09-24 11:32:59 -04002656 int netfid, const char *target_name,
Steve French50c2f752007-07-13 00:33:32 +00002657 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 struct smb_com_transaction2_sfi_req *pSMB = NULL;
2660 struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002661 struct set_file_rename *rename_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 char *data_offset;
2663 char dummy_string[30];
2664 int rc = 0;
2665 int bytes_returned = 0;
2666 int len_of_str;
2667 __u16 params, param_offset, offset, count, byte_count;
2668
Joe Perchesf96637b2013-05-04 22:12:25 -05002669 cifs_dbg(FYI, "Rename to File by handle\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
2671 (void **) &pSMBr);
2672 if (rc)
2673 return rc;
2674
2675 params = 6;
2676 pSMB->MaxSetupCount = 0;
2677 pSMB->Reserved = 0;
2678 pSMB->Flags = 0;
2679 pSMB->Timeout = 0;
2680 pSMB->Reserved2 = 0;
2681 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
2682 offset = param_offset + params;
2683
2684 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2685 rename_info = (struct set_file_rename *) data_offset;
2686 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve Frenchad7a2922008-02-07 23:25:02 +00002687 pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 pSMB->SetupCount = 1;
2689 pSMB->Reserved3 = 0;
2690 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
2691 byte_count = 3 /* pad */ + params;
2692 pSMB->ParameterCount = cpu_to_le16(params);
2693 pSMB->TotalParameterCount = pSMB->ParameterCount;
2694 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2695 pSMB->DataOffset = cpu_to_le16(offset);
2696 /* construct random name ".cifs_tmp<inodenum><mid>" */
2697 rename_info->overwrite = cpu_to_le32(1);
2698 rename_info->root_fid = 0;
2699 /* unicode only call */
Steve French790fe572007-07-07 19:25:05 +00002700 if (target_name == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00002701 sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
Steve Frenchacbbb762012-01-18 22:32:33 -06002702 len_of_str =
2703 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French737b7582005-04-28 22:41:06 -07002704 dummy_string, 24, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 } else {
Steve Frenchacbbb762012-01-18 22:32:33 -06002706 len_of_str =
2707 cifsConvertToUTF16((__le16 *)rename_info->target_name,
Steve French50c2f752007-07-13 00:33:32 +00002708 target_name, PATH_MAX, nls_codepage,
2709 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
Jeff Layton391e5752008-09-24 11:32:59 -04002712 count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 byte_count += count;
2714 pSMB->DataCount = cpu_to_le16(count);
2715 pSMB->TotalDataCount = pSMB->DataCount;
2716 pSMB->Fid = netfid;
2717 pSMB->InformationLevel =
2718 cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
2719 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002720 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 pSMB->ByteCount = cpu_to_le16(byte_count);
2722 rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00002723 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002724 cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
Steve Frenchad7a2922008-02-07 23:25:02 +00002725 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002726 cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
2727 rc);
Steve Frencha5a2b482005-08-20 21:42:53 -07002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 cifs_buf_release(pSMB);
2730
2731 /* Note: On -EAGAIN error only caller can retry on handle based calls
2732 since file handle passed in no longer valid */
2733
2734 return rc;
2735}
2736
2737int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002738CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
2739 const char *fromName, const __u16 target_tid, const char *toName,
2740 const int flags, const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
2742 int rc = 0;
2743 COPY_REQ *pSMB = NULL;
2744 COPY_RSP *pSMBr = NULL;
2745 int bytes_returned;
2746 int name_len, name_len2;
2747 __u16 count;
2748
Joe Perchesf96637b2013-05-04 22:12:25 -05002749 cifs_dbg(FYI, "In CIFSSMBCopy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750copyRetry:
2751 rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
2752 (void **) &pSMBr);
2753 if (rc)
2754 return rc;
2755
2756 pSMB->BufferFormat = 0x04;
2757 pSMB->Tid2 = target_tid;
2758
2759 pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
2760
2761 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002762 name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
2763 fromName, PATH_MAX, nls_codepage,
2764 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 name_len++; /* trailing null */
2766 name_len *= 2;
2767 pSMB->OldFileName[name_len] = 0x04; /* pad */
2768 /* protocol requires ASCII signature byte on Unicode string */
2769 pSMB->OldFileName[name_len + 1] = 0x00;
Steve French50c2f752007-07-13 00:33:32 +00002770 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06002771 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
2772 toName, PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
2774 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00002775 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 name_len = strnlen(fromName, PATH_MAX);
2777 name_len++; /* trailing null */
2778 strncpy(pSMB->OldFileName, fromName, name_len);
2779 name_len2 = strnlen(toName, PATH_MAX);
2780 name_len2++; /* trailing null */
2781 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
2782 strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
2783 name_len2++; /* trailing null */
2784 name_len2++; /* signature byte */
2785 }
2786
2787 count = 1 /* 1st signature byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002788 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 pSMB->ByteCount = cpu_to_le16(count);
2790
2791 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2792 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
2793 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002794 cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
2795 rc, le16_to_cpu(pSMBr->CopyCount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Steve French0d817bc2008-05-22 02:02:03 +00002797 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 if (rc == -EAGAIN)
2800 goto copyRetry;
2801
2802 return rc;
2803}
2804
2805int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002806CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 const char *fromName, const char *toName,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002808 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 TRANSACTION2_SPI_REQ *pSMB = NULL;
2811 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2812 char *data_offset;
2813 int name_len;
2814 int name_len_target;
2815 int rc = 0;
2816 int bytes_returned = 0;
2817 __u16 params, param_offset, offset, byte_count;
2818
Joe Perchesf96637b2013-05-04 22:12:25 -05002819 cifs_dbg(FYI, "In Symlink Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820createSymLinkRetry:
2821 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2822 (void **) &pSMBr);
2823 if (rc)
2824 return rc;
2825
2826 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2827 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002828 cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
2829 /* find define for this maxpathcomponent */
2830 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 name_len++; /* trailing null */
2832 name_len *= 2;
2833
Steve French50c2f752007-07-13 00:33:32 +00002834 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 name_len = strnlen(fromName, PATH_MAX);
2836 name_len++; /* trailing null */
2837 strncpy(pSMB->FileName, fromName, name_len);
2838 }
2839 params = 6 + name_len;
2840 pSMB->MaxSetupCount = 0;
2841 pSMB->Reserved = 0;
2842 pSMB->Flags = 0;
2843 pSMB->Timeout = 0;
2844 pSMB->Reserved2 = 0;
2845 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002846 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 offset = param_offset + params;
2848
2849 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2850 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2851 name_len_target =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002852 cifsConvertToUTF16((__le16 *) data_offset, toName,
2853 /* find define for this maxpathcomponent */
2854 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 name_len_target++; /* trailing null */
2856 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002857 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 name_len_target = strnlen(toName, PATH_MAX);
2859 name_len_target++; /* trailing null */
2860 strncpy(data_offset, toName, name_len_target);
2861 }
2862
2863 pSMB->MaxParameterCount = cpu_to_le16(2);
2864 /* BB find exact max on data count below from sess */
2865 pSMB->MaxDataCount = cpu_to_le16(1000);
2866 pSMB->SetupCount = 1;
2867 pSMB->Reserved3 = 0;
2868 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2869 byte_count = 3 /* pad */ + params + name_len_target;
2870 pSMB->DataCount = cpu_to_le16(name_len_target);
2871 pSMB->ParameterCount = cpu_to_le16(params);
2872 pSMB->TotalDataCount = pSMB->DataCount;
2873 pSMB->TotalParameterCount = pSMB->ParameterCount;
2874 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2875 pSMB->DataOffset = cpu_to_le16(offset);
2876 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
2877 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002878 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 pSMB->ByteCount = cpu_to_le16(byte_count);
2880 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2881 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002882 cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002883 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002884 cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
2885 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Steve French0d817bc2008-05-22 02:02:03 +00002887 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 if (rc == -EAGAIN)
2890 goto createSymLinkRetry;
2891
2892 return rc;
2893}
2894
2895int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002896CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 const char *fromName, const char *toName,
Steve French737b7582005-04-28 22:41:06 -07002898 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 TRANSACTION2_SPI_REQ *pSMB = NULL;
2901 TRANSACTION2_SPI_RSP *pSMBr = NULL;
2902 char *data_offset;
2903 int name_len;
2904 int name_len_target;
2905 int rc = 0;
2906 int bytes_returned = 0;
2907 __u16 params, param_offset, offset, byte_count;
2908
Joe Perchesf96637b2013-05-04 22:12:25 -05002909 cifs_dbg(FYI, "In Create Hard link Unix style\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910createHardLinkRetry:
2911 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
2912 (void **) &pSMBr);
2913 if (rc)
2914 return rc;
2915
2916 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Steve Frenchacbbb762012-01-18 22:32:33 -06002917 name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
2918 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 name_len++; /* trailing null */
2920 name_len *= 2;
2921
Steve French50c2f752007-07-13 00:33:32 +00002922 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 name_len = strnlen(toName, PATH_MAX);
2924 name_len++; /* trailing null */
2925 strncpy(pSMB->FileName, toName, name_len);
2926 }
2927 params = 6 + name_len;
2928 pSMB->MaxSetupCount = 0;
2929 pSMB->Reserved = 0;
2930 pSMB->Flags = 0;
2931 pSMB->Timeout = 0;
2932 pSMB->Reserved2 = 0;
2933 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00002934 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 offset = param_offset + params;
2936
2937 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
2938 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
2939 name_len_target =
Steve Frenchacbbb762012-01-18 22:32:33 -06002940 cifsConvertToUTF16((__le16 *) data_offset, fromName,
2941 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 name_len_target++; /* trailing null */
2943 name_len_target *= 2;
Steve French50c2f752007-07-13 00:33:32 +00002944 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 name_len_target = strnlen(fromName, PATH_MAX);
2946 name_len_target++; /* trailing null */
2947 strncpy(data_offset, fromName, name_len_target);
2948 }
2949
2950 pSMB->MaxParameterCount = cpu_to_le16(2);
2951 /* BB find exact max on data count below from sess*/
2952 pSMB->MaxDataCount = cpu_to_le16(1000);
2953 pSMB->SetupCount = 1;
2954 pSMB->Reserved3 = 0;
2955 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
2956 byte_count = 3 /* pad */ + params + name_len_target;
2957 pSMB->ParameterCount = cpu_to_le16(params);
2958 pSMB->TotalParameterCount = pSMB->ParameterCount;
2959 pSMB->DataCount = cpu_to_le16(name_len_target);
2960 pSMB->TotalDataCount = pSMB->DataCount;
2961 pSMB->ParameterOffset = cpu_to_le16(param_offset);
2962 pSMB->DataOffset = cpu_to_le16(offset);
2963 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
2964 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002965 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 pSMB->ByteCount = cpu_to_le16(byte_count);
2967 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
2968 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04002969 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00002970 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05002971 cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
2972 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 cifs_buf_release(pSMB);
2975 if (rc == -EAGAIN)
2976 goto createHardLinkRetry;
2977
2978 return rc;
2979}
2980
2981int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002982CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchd6e906f2012-09-18 16:20:31 -07002983 const char *from_name, const char *to_name,
2984 struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 int rc = 0;
2987 NT_RENAME_REQ *pSMB = NULL;
2988 RENAME_RSP *pSMBr = NULL;
2989 int bytes_returned;
2990 int name_len, name_len2;
2991 __u16 count;
Steve French2baa2682014-09-27 02:19:01 -05002992 int remap = cifs_remap(cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Joe Perchesf96637b2013-05-04 22:12:25 -05002994 cifs_dbg(FYI, "In CIFSCreateHardLink\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995winCreateHardLinkRetry:
2996
2997 rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
2998 (void **) &pSMBr);
2999 if (rc)
3000 return rc;
3001
3002 pSMB->SearchAttributes =
3003 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
3004 ATTR_DIRECTORY);
3005 pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
3006 pSMB->ClusterCount = 0;
3007
3008 pSMB->BufferFormat = 0x04;
3009
3010 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3011 name_len =
Steve Frenchd6e906f2012-09-18 16:20:31 -07003012 cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
3013 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 name_len++; /* trailing null */
3015 name_len *= 2;
Jeff Laytonfcc7c092009-02-28 12:59:03 -05003016
3017 /* protocol specifies ASCII buffer format (0x04) for unicode */
3018 pSMB->OldFileName[name_len] = 0x04;
3019 pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 name_len2 =
Steve Frenchacbbb762012-01-18 22:32:33 -06003021 cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
Steve Frenchd6e906f2012-09-18 16:20:31 -07003022 to_name, PATH_MAX, cifs_sb->local_nls,
3023 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
3025 name_len2 *= 2; /* convert to bytes */
Steve French50c2f752007-07-13 00:33:32 +00003026 } else { /* BB improve the check for buffer overruns BB */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003027 name_len = strnlen(from_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 name_len++; /* trailing null */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003029 strncpy(pSMB->OldFileName, from_name, name_len);
3030 name_len2 = strnlen(to_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 name_len2++; /* trailing null */
3032 pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
Steve Frenchd6e906f2012-09-18 16:20:31 -07003033 strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 name_len2++; /* trailing null */
3035 name_len2++; /* signature byte */
3036 }
3037
3038 count = 1 /* string type byte */ + name_len + name_len2;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003039 inc_rfc1001_len(pSMB, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 pSMB->ByteCount = cpu_to_le16(count);
3041
3042 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3043 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003044 cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
Steve Frenchad7a2922008-02-07 23:25:02 +00003045 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003046 cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 cifs_buf_release(pSMB);
3049 if (rc == -EAGAIN)
3050 goto winCreateHardLinkRetry;
3051
3052 return rc;
3053}
3054
3055int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003056CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton460b9692009-04-30 07:17:56 -04003057 const unsigned char *searchName, char **symlinkinfo,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003058 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
3060/* SMB_QUERY_FILE_UNIX_LINK */
3061 TRANSACTION2_QPI_REQ *pSMB = NULL;
3062 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3063 int rc = 0;
3064 int bytes_returned;
3065 int name_len;
3066 __u16 params, byte_count;
Jeff Layton460b9692009-04-30 07:17:56 -04003067 char *data_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Joe Perchesf96637b2013-05-04 22:12:25 -05003069 cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071querySymLinkRetry:
3072 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3073 (void **) &pSMBr);
3074 if (rc)
3075 return rc;
3076
3077 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3078 name_len =
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09003079 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3080 searchName, PATH_MAX, nls_codepage,
3081 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 name_len++; /* trailing null */
3083 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003084 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 name_len = strnlen(searchName, PATH_MAX);
3086 name_len++; /* trailing null */
3087 strncpy(pSMB->FileName, searchName, name_len);
3088 }
3089
3090 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3091 pSMB->TotalDataCount = 0;
3092 pSMB->MaxParameterCount = cpu_to_le16(2);
Jeff Layton46a75742009-05-24 18:45:17 -04003093 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 pSMB->MaxSetupCount = 0;
3095 pSMB->Reserved = 0;
3096 pSMB->Flags = 0;
3097 pSMB->Timeout = 0;
3098 pSMB->Reserved2 = 0;
3099 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00003100 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 pSMB->DataCount = 0;
3102 pSMB->DataOffset = 0;
3103 pSMB->SetupCount = 1;
3104 pSMB->Reserved3 = 0;
3105 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3106 byte_count = params + 1 /* pad */ ;
3107 pSMB->TotalParameterCount = cpu_to_le16(params);
3108 pSMB->ParameterCount = pSMB->TotalParameterCount;
3109 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
3110 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003111 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 pSMB->ByteCount = cpu_to_le16(byte_count);
3113
3114 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3115 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3116 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003117 cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 } else {
3119 /* decode response */
3120
3121 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003123 if (rc || get_bcc(&pSMBr->hdr) < 2)
Jeff Layton460b9692009-04-30 07:17:56 -04003124 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 else {
Steve French0e0d2cf2009-05-01 05:27:32 +00003126 bool is_unicode;
Jeff Layton460b9692009-04-30 07:17:56 -04003127 u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Jeff Layton460b9692009-04-30 07:17:56 -04003129 data_start = ((char *) &pSMBr->hdr.Protocol) +
3130 le16_to_cpu(pSMBr->t2.DataOffset);
3131
Steve French0e0d2cf2009-05-01 05:27:32 +00003132 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3133 is_unicode = true;
3134 else
3135 is_unicode = false;
3136
Steve French737b7582005-04-28 22:41:06 -07003137 /* BB FIXME investigate remapping reserved chars here */
Steve Frenchacbbb762012-01-18 22:32:33 -06003138 *symlinkinfo = cifs_strndup_from_utf16(data_start,
3139 count, is_unicode, nls_codepage);
Jeff Layton8b6427a2009-05-19 09:57:03 -04003140 if (!*symlinkinfo)
Jeff Layton460b9692009-04-30 07:17:56 -04003141 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143 }
3144 cifs_buf_release(pSMB);
3145 if (rc == -EAGAIN)
3146 goto querySymLinkRetry;
3147 return rc;
3148}
3149
Steve Frenchc52a9552011-02-24 06:16:22 +00003150/*
3151 * Recent Windows versions now create symlinks more frequently
3152 * and they use the "reparse point" mechanism below. We can of course
3153 * do symlinks nicely to Samba and other servers which support the
3154 * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
3155 * "MF" symlinks optionally, but for recent Windows we really need to
3156 * reenable the code below and fix the cifs_symlink callers to handle this.
3157 * In the interim this code has been moved to its own config option so
3158 * it is not compiled in by default until callers fixed up and more tested.
3159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160int
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003161CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
3162 __u16 fid, char **symlinkinfo,
3163 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 int rc = 0;
3166 int bytes_returned;
Steve French50c2f752007-07-13 00:33:32 +00003167 struct smb_com_transaction_ioctl_req *pSMB;
3168 struct smb_com_transaction_ioctl_rsp *pSMBr;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003169 bool is_unicode;
3170 unsigned int sub_len;
3171 char *sub_start;
Steve Frenchc31f3302013-09-28 18:24:12 -05003172 struct reparse_symlink_data *reparse_buf;
3173 struct reparse_posix_data *posix_buf;
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003174 __u32 data_offset, data_count;
3175 char *end_of_smb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003177 cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3179 (void **) &pSMBr);
3180 if (rc)
3181 return rc;
3182
3183 pSMB->TotalParameterCount = 0 ;
3184 pSMB->TotalDataCount = 0;
3185 pSMB->MaxParameterCount = cpu_to_le32(2);
3186 /* BB find exact data count max from sess structure BB */
Jeff Laytonc974bef2011-10-11 06:41:32 -04003187 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 pSMB->MaxSetupCount = 4;
3189 pSMB->Reserved = 0;
3190 pSMB->ParameterOffset = 0;
3191 pSMB->DataCount = 0;
3192 pSMB->DataOffset = 0;
3193 pSMB->SetupCount = 4;
3194 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
3195 pSMB->ParameterCount = pSMB->TotalParameterCount;
3196 pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
3197 pSMB->IsFsctl = 1; /* FSCTL */
3198 pSMB->IsRootFlag = 0;
3199 pSMB->Fid = fid; /* file handle always le */
3200 pSMB->ByteCount = 0;
3201
3202 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3203 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3204 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003205 cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003206 goto qreparse_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
Steve French989c7e52009-05-02 05:32:20 +00003208
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003209 data_offset = le32_to_cpu(pSMBr->DataOffset);
3210 data_count = le32_to_cpu(pSMBr->DataCount);
3211 if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
3212 /* BB also check enough total bytes returned */
3213 rc = -EIO; /* bad smb */
3214 goto qreparse_out;
3215 }
3216 if (!data_count || (data_count > 2048)) {
3217 rc = -EIO;
3218 cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
3219 goto qreparse_out;
3220 }
3221 end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
Steve Frenchc31f3302013-09-28 18:24:12 -05003222 reparse_buf = (struct reparse_symlink_data *)
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003223 ((char *)&pSMBr->hdr.Protocol + data_offset);
3224 if ((char *)reparse_buf >= end_of_smb) {
3225 rc = -EIO;
3226 goto qreparse_out;
3227 }
Steve Frenchc31f3302013-09-28 18:24:12 -05003228 if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
3229 cifs_dbg(FYI, "NFS style reparse tag\n");
3230 posix_buf = (struct reparse_posix_data *)reparse_buf;
3231
3232 if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
3233 cifs_dbg(FYI, "unsupported file type 0x%llx\n",
3234 le64_to_cpu(posix_buf->InodeType));
3235 rc = -EOPNOTSUPP;
3236 goto qreparse_out;
3237 }
3238 is_unicode = true;
3239 sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
3240 if (posix_buf->PathBuffer + sub_len > end_of_smb) {
3241 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3242 rc = -EIO;
3243 goto qreparse_out;
3244 }
3245 *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
3246 sub_len, is_unicode, nls_codepage);
3247 goto qreparse_out;
3248 } else if (reparse_buf->ReparseTag !=
3249 cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
3250 rc = -EOPNOTSUPP;
3251 goto qreparse_out;
3252 }
3253
3254 /* Reparse tag is NTFS symlink */
3255 sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
3256 reparse_buf->PathBuffer;
3257 sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
3258 if (sub_start + sub_len > end_of_smb) {
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003259 cifs_dbg(FYI, "reparse buf beyond SMB\n");
3260 rc = -EIO;
3261 goto qreparse_out;
3262 }
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003263 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
3264 is_unicode = true;
3265 else
3266 is_unicode = false;
3267
3268 /* BB FIXME investigate remapping reserved chars here */
3269 *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
3270 nls_codepage);
3271 if (!*symlinkinfo)
3272 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273qreparse_out:
Steve French4a6d87f2005-08-13 08:15:54 -07003274 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Pavel Shilovskyd244bf22013-08-14 19:25:22 +04003276 /*
3277 * Note: On -EAGAIN error only caller can retry on handle based calls
3278 * since file handle passed in no longer valid.
3279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return rc;
3281}
3282
Steve Frenchc7f508a2013-10-14 15:27:32 -05003283int
3284CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
3285 __u16 fid)
3286{
3287 int rc = 0;
3288 int bytes_returned;
3289 struct smb_com_transaction_compr_ioctl_req *pSMB;
3290 struct smb_com_transaction_ioctl_rsp *pSMBr;
3291
3292 cifs_dbg(FYI, "Set compression for %u\n", fid);
3293 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
3294 (void **) &pSMBr);
3295 if (rc)
3296 return rc;
3297
3298 pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
3299
3300 pSMB->TotalParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003301 pSMB->TotalDataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003302 pSMB->MaxParameterCount = 0;
3303 pSMB->MaxDataCount = 0;
3304 pSMB->MaxSetupCount = 4;
3305 pSMB->Reserved = 0;
3306 pSMB->ParameterOffset = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003307 pSMB->DataCount = cpu_to_le32(2);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003308 pSMB->DataOffset =
3309 cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
3310 compression_state) - 4); /* 84 */
3311 pSMB->SetupCount = 4;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003312 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003313 pSMB->ParameterCount = 0;
Fabian Frederickbc09d142014-12-10 15:41:15 -08003314 pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003315 pSMB->IsFsctl = 1; /* FSCTL */
3316 pSMB->IsRootFlag = 0;
3317 pSMB->Fid = fid; /* file handle always le */
3318 /* 3 byte pad, followed by 2 byte compress state */
Fabian Frederickbc09d142014-12-10 15:41:15 -08003319 pSMB->ByteCount = cpu_to_le16(5);
Steve Frenchc7f508a2013-10-14 15:27:32 -05003320 inc_rfc1001_len(pSMB, 5);
3321
3322 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3323 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3324 if (rc)
3325 cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
3326
3327 cifs_buf_release(pSMB);
3328
3329 /*
3330 * Note: On -EAGAIN error only caller can retry on handle based calls
3331 * since file handle passed in no longer valid.
3332 */
3333 return rc;
3334}
3335
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337#ifdef CONFIG_CIFS_POSIX
3338
3339/*Convert an Access Control Entry from wire format to local POSIX xattr format*/
Steve French50c2f752007-07-13 00:33:32 +00003340static void cifs_convert_ace(posix_acl_xattr_entry *ace,
3341 struct cifs_posix_ace *cifs_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
3343 /* u8 cifs fields do not need le conversion */
Steve Frenchff7feac2005-11-15 16:45:16 -08003344 ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
3345 ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
3346 ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
Joe Perchesf96637b2013-05-04 22:12:25 -05003347/*
3348 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3349 ace->e_perm, ace->e_tag, ace->e_id);
3350*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
3352 return;
3353}
3354
3355/* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
Steve French50c2f752007-07-13 00:33:32 +00003356static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
3357 const int acl_type, const int size_of_data_area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359 int size = 0;
3360 int i;
3361 __u16 count;
Steve French50c2f752007-07-13 00:33:32 +00003362 struct cifs_posix_ace *pACE;
3363 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
3364 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)trgt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366 if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
3367 return -EOPNOTSUPP;
3368
Steve French790fe572007-07-07 19:25:05 +00003369 if (acl_type & ACL_TYPE_ACCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 count = le16_to_cpu(cifs_acl->access_entry_count);
3371 pACE = &cifs_acl->ace_array[0];
3372 size = sizeof(struct cifs_posix_acl);
3373 size += sizeof(struct cifs_posix_ace) * count;
3374 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003375 if (size_of_data_area < size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003376 cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
3377 size_of_data_area, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return -EINVAL;
3379 }
Steve French790fe572007-07-07 19:25:05 +00003380 } else if (acl_type & ACL_TYPE_DEFAULT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 count = le16_to_cpu(cifs_acl->access_entry_count);
3382 size = sizeof(struct cifs_posix_acl);
3383 size += sizeof(struct cifs_posix_ace) * count;
3384/* skip past access ACEs to get to default ACEs */
3385 pACE = &cifs_acl->ace_array[count];
3386 count = le16_to_cpu(cifs_acl->default_entry_count);
3387 size += sizeof(struct cifs_posix_ace) * count;
3388 /* check if we would go beyond end of SMB */
Steve French790fe572007-07-07 19:25:05 +00003389 if (size_of_data_area < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 return -EINVAL;
3391 } else {
3392 /* illegal type */
3393 return -EINVAL;
3394 }
3395
3396 size = posix_acl_xattr_size(count);
Steve French790fe572007-07-07 19:25:05 +00003397 if ((buflen == 0) || (local_acl == NULL)) {
Steve French50c2f752007-07-13 00:33:32 +00003398 /* used to query ACL EA size */
Steve French790fe572007-07-07 19:25:05 +00003399 } else if (size > buflen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return -ERANGE;
3401 } else /* buffer big enough */ {
Steve Frenchff7feac2005-11-15 16:45:16 -08003402 local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
Steve French50c2f752007-07-13 00:33:32 +00003403 for (i = 0; i < count ; i++) {
3404 cifs_convert_ace(&local_acl->a_entries[i], pACE);
3405 pACE++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
3407 }
3408 return size;
3409}
3410
Steve French50c2f752007-07-13 00:33:32 +00003411static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
3412 const posix_acl_xattr_entry *local_ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
3414 __u16 rc = 0; /* 0 = ACL converted ok */
3415
Steve Frenchff7feac2005-11-15 16:45:16 -08003416 cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
3417 cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 /* BB is there a better way to handle the large uid? */
Steve French790fe572007-07-07 19:25:05 +00003419 if (local_ace->e_id == cpu_to_le32(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 /* Probably no need to le convert -1 on any arch but can not hurt */
3421 cifs_ace->cifs_uid = cpu_to_le64(-1);
Steve French50c2f752007-07-13 00:33:32 +00003422 } else
Steve Frenchff7feac2005-11-15 16:45:16 -08003423 cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
Joe Perchesf96637b2013-05-04 22:12:25 -05003424/*
3425 cifs_dbg(FYI, "perm %d tag %d id %d\n",
3426 ace->e_perm, ace->e_tag, ace->e_id);
3427*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return rc;
3429}
3430
3431/* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
Steve French50c2f752007-07-13 00:33:32 +00003432static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
3433 const int buflen, const int acl_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 __u16 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00003436 struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
3437 posix_acl_xattr_header *local_acl = (posix_acl_xattr_header *)pACL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 int count;
3439 int i;
3440
Steve French790fe572007-07-07 19:25:05 +00003441 if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return 0;
3443
3444 count = posix_acl_xattr_count((size_t)buflen);
Joe Perchesf96637b2013-05-04 22:12:25 -05003445 cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
3446 count, buflen, le32_to_cpu(local_acl->a_version));
Steve French790fe572007-07-07 19:25:05 +00003447 if (le32_to_cpu(local_acl->a_version) != 2) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003448 cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
3449 le32_to_cpu(local_acl->a_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return 0;
3451 }
3452 cifs_acl->version = cpu_to_le16(1);
Steve Frenchb1d93352013-11-15 20:41:32 -06003453 if (acl_type == ACL_TYPE_ACCESS) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003454 cifs_acl->access_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003455 cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003456 } else if (acl_type == ACL_TYPE_DEFAULT) {
Steve Frenchff7feac2005-11-15 16:45:16 -08003457 cifs_acl->default_entry_count = cpu_to_le16(count);
Fabian Frederickbc09d142014-12-10 15:41:15 -08003458 cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
Steve Frenchb1d93352013-11-15 20:41:32 -06003459 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003460 cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return 0;
3462 }
Steve French50c2f752007-07-13 00:33:32 +00003463 for (i = 0; i < count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i],
3465 &local_acl->a_entries[i]);
Steve French790fe572007-07-07 19:25:05 +00003466 if (rc != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 /* ACE not converted */
3468 break;
3469 }
3470 }
Steve French790fe572007-07-07 19:25:05 +00003471 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 rc = (__u16)(count * sizeof(struct cifs_posix_ace));
3473 rc += sizeof(struct cifs_posix_acl);
3474 /* BB add check to make sure ACL does not overflow SMB */
3475 }
3476 return rc;
3477}
3478
3479int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003480CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003481 const unsigned char *searchName,
3482 char *acl_inf, const int buflen, const int acl_type,
3483 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
3485/* SMB_QUERY_POSIX_ACL */
3486 TRANSACTION2_QPI_REQ *pSMB = NULL;
3487 TRANSACTION2_QPI_RSP *pSMBr = NULL;
3488 int rc = 0;
3489 int bytes_returned;
3490 int name_len;
3491 __u16 params, byte_count;
Steve French50c2f752007-07-13 00:33:32 +00003492
Joe Perchesf96637b2013-05-04 22:12:25 -05003493 cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495queryAclRetry:
3496 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3497 (void **) &pSMBr);
3498 if (rc)
3499 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3502 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003503 cifsConvertToUTF16((__le16 *) pSMB->FileName,
3504 searchName, PATH_MAX, nls_codepage,
3505 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 name_len++; /* trailing null */
3507 name_len *= 2;
3508 pSMB->FileName[name_len] = 0;
3509 pSMB->FileName[name_len+1] = 0;
Steve French50c2f752007-07-13 00:33:32 +00003510 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 name_len = strnlen(searchName, PATH_MAX);
3512 name_len++; /* trailing null */
3513 strncpy(pSMB->FileName, searchName, name_len);
3514 }
3515
3516 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
3517 pSMB->TotalDataCount = 0;
3518 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French50c2f752007-07-13 00:33:32 +00003519 /* BB find exact max data count below from sess structure BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 pSMB->MaxDataCount = cpu_to_le16(4000);
3521 pSMB->MaxSetupCount = 0;
3522 pSMB->Reserved = 0;
3523 pSMB->Flags = 0;
3524 pSMB->Timeout = 0;
3525 pSMB->Reserved2 = 0;
3526 pSMB->ParameterOffset = cpu_to_le16(
Steve French50c2f752007-07-13 00:33:32 +00003527 offsetof(struct smb_com_transaction2_qpi_req,
3528 InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 pSMB->DataCount = 0;
3530 pSMB->DataOffset = 0;
3531 pSMB->SetupCount = 1;
3532 pSMB->Reserved3 = 0;
3533 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
3534 byte_count = params + 1 /* pad */ ;
3535 pSMB->TotalParameterCount = cpu_to_le16(params);
3536 pSMB->ParameterCount = pSMB->TotalParameterCount;
3537 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
3538 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003539 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 pSMB->ByteCount = cpu_to_le16(byte_count);
3541
3542 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3543 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003544 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003546 cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 } else {
3548 /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00003549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003552 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 rc = -EIO; /* bad smb */
3554 else {
3555 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3556 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3557 rc = cifs_copy_posix_acl(acl_inf,
3558 (char *)&pSMBr->hdr.Protocol+data_offset,
Steve French50c2f752007-07-13 00:33:32 +00003559 buflen, acl_type, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
3561 }
3562 cifs_buf_release(pSMB);
3563 if (rc == -EAGAIN)
3564 goto queryAclRetry;
3565 return rc;
3566}
3567
3568int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003569CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00003570 const unsigned char *fileName,
3571 const char *local_acl, const int buflen,
3572 const int acl_type,
3573 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
3575 struct smb_com_transaction2_spi_req *pSMB = NULL;
3576 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
3577 char *parm_data;
3578 int name_len;
3579 int rc = 0;
3580 int bytes_returned = 0;
3581 __u16 params, byte_count, data_count, param_offset, offset;
3582
Joe Perchesf96637b2013-05-04 22:12:25 -05003583 cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584setAclRetry:
3585 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003586 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 if (rc)
3588 return rc;
3589 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3590 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003591 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
3592 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 name_len++; /* trailing null */
3594 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00003595 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 name_len = strnlen(fileName, PATH_MAX);
3597 name_len++; /* trailing null */
3598 strncpy(pSMB->FileName, fileName, name_len);
3599 }
3600 params = 6 + name_len;
3601 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00003602 /* BB find max SMB size from sess */
3603 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 pSMB->MaxSetupCount = 0;
3605 pSMB->Reserved = 0;
3606 pSMB->Flags = 0;
3607 pSMB->Timeout = 0;
3608 pSMB->Reserved2 = 0;
3609 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00003610 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 offset = param_offset + params;
3612 parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
3613 pSMB->ParameterOffset = cpu_to_le16(param_offset);
3614
3615 /* convert to on the wire format for POSIX ACL */
Steve French50c2f752007-07-13 00:33:32 +00003616 data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Steve French790fe572007-07-07 19:25:05 +00003618 if (data_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 rc = -EOPNOTSUPP;
3620 goto setACLerrorExit;
3621 }
3622 pSMB->DataOffset = cpu_to_le16(offset);
3623 pSMB->SetupCount = 1;
3624 pSMB->Reserved3 = 0;
3625 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
3626 pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
3627 byte_count = 3 /* pad */ + params + data_count;
3628 pSMB->DataCount = cpu_to_le16(data_count);
3629 pSMB->TotalDataCount = pSMB->DataCount;
3630 pSMB->ParameterCount = cpu_to_le16(params);
3631 pSMB->TotalParameterCount = pSMB->ParameterCount;
3632 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003633 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 pSMB->ByteCount = cpu_to_le16(byte_count);
3635 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003636 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00003637 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003638 cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640setACLerrorExit:
3641 cifs_buf_release(pSMB);
3642 if (rc == -EAGAIN)
3643 goto setAclRetry;
3644 return rc;
3645}
3646
Steve Frenchf654bac2005-04-28 22:41:04 -07003647/* BB fix tabs in this function FIXME BB */
3648int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003649CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
Steve Frenchad7a2922008-02-07 23:25:02 +00003650 const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
Steve Frenchf654bac2005-04-28 22:41:04 -07003651{
Steve French50c2f752007-07-13 00:33:32 +00003652 int rc = 0;
3653 struct smb_t2_qfi_req *pSMB = NULL;
3654 struct smb_t2_qfi_rsp *pSMBr = NULL;
3655 int bytes_returned;
3656 __u16 params, byte_count;
Steve Frenchf654bac2005-04-28 22:41:04 -07003657
Joe Perchesf96637b2013-05-04 22:12:25 -05003658 cifs_dbg(FYI, "In GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003659 if (tcon == NULL)
3660 return -ENODEV;
Steve Frenchf654bac2005-04-28 22:41:04 -07003661
3662GetExtAttrRetry:
Steve French790fe572007-07-07 19:25:05 +00003663 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
3664 (void **) &pSMBr);
3665 if (rc)
3666 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003667
Steve Frenchad7a2922008-02-07 23:25:02 +00003668 params = 2 /* level */ + 2 /* fid */;
Steve French790fe572007-07-07 19:25:05 +00003669 pSMB->t2.TotalDataCount = 0;
3670 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
3671 /* BB find exact max data count below from sess structure BB */
3672 pSMB->t2.MaxDataCount = cpu_to_le16(4000);
3673 pSMB->t2.MaxSetupCount = 0;
3674 pSMB->t2.Reserved = 0;
3675 pSMB->t2.Flags = 0;
3676 pSMB->t2.Timeout = 0;
3677 pSMB->t2.Reserved2 = 0;
3678 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
3679 Fid) - 4);
3680 pSMB->t2.DataCount = 0;
3681 pSMB->t2.DataOffset = 0;
3682 pSMB->t2.SetupCount = 1;
3683 pSMB->t2.Reserved3 = 0;
3684 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
3685 byte_count = params + 1 /* pad */ ;
3686 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
3687 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
3688 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
3689 pSMB->Pad = 0;
Steve Frenchf654bac2005-04-28 22:41:04 -07003690 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003691 inc_rfc1001_len(pSMB, byte_count);
Steve French790fe572007-07-07 19:25:05 +00003692 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Steve Frenchf654bac2005-04-28 22:41:04 -07003693
Steve French790fe572007-07-07 19:25:05 +00003694 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3695 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3696 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003697 cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
Steve French790fe572007-07-07 19:25:05 +00003698 } else {
3699 /* decode response */
3700 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00003701 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04003702 if (rc || get_bcc(&pSMBr->hdr) < 2)
Steve French790fe572007-07-07 19:25:05 +00003703 /* If rc should we check for EOPNOSUPP and
3704 disable the srvino flag? or in caller? */
3705 rc = -EIO; /* bad smb */
3706 else {
3707 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
3708 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
3709 struct file_chattr_info *pfinfo;
3710 /* BB Do we need a cast or hash here ? */
3711 if (count != 16) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003712 cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
Steve French790fe572007-07-07 19:25:05 +00003713 rc = -EIO;
3714 goto GetExtAttrOut;
3715 }
3716 pfinfo = (struct file_chattr_info *)
3717 (data_offset + (char *) &pSMBr->hdr.Protocol);
3718 *pExtAttrBits = le64_to_cpu(pfinfo->mode);
Steve Frenchf654bac2005-04-28 22:41:04 -07003719 *pMask = le64_to_cpu(pfinfo->mask);
Steve French790fe572007-07-07 19:25:05 +00003720 }
3721 }
Steve Frenchf654bac2005-04-28 22:41:04 -07003722GetExtAttrOut:
Steve French790fe572007-07-07 19:25:05 +00003723 cifs_buf_release(pSMB);
3724 if (rc == -EAGAIN)
3725 goto GetExtAttrRetry;
3726 return rc;
Steve Frenchf654bac2005-04-28 22:41:04 -07003727}
3728
Steve Frenchf654bac2005-04-28 22:41:04 -07003729#endif /* CONFIG_POSIX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Jeff Layton79df1ba2010-12-06 12:52:08 -05003731#ifdef CONFIG_CIFS_ACL
3732/*
3733 * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
3734 * all NT TRANSACTS that we init here have total parm and data under about 400
3735 * bytes (to fit in small cifs buffer size), which is the case so far, it
3736 * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
3737 * returned setup area) and MaxParameterCount (returned parms size) must be set
3738 * by caller
3739 */
3740static int
3741smb_init_nttransact(const __u16 sub_command, const int setup_count,
Steve French96daf2b2011-05-27 04:34:02 +00003742 const int parm_len, struct cifs_tcon *tcon,
Jeff Layton79df1ba2010-12-06 12:52:08 -05003743 void **ret_buf)
3744{
3745 int rc;
3746 __u32 temp_offset;
3747 struct smb_com_ntransact_req *pSMB;
3748
3749 rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
3750 (void **)&pSMB);
3751 if (rc)
3752 return rc;
3753 *ret_buf = (void *)pSMB;
3754 pSMB->Reserved = 0;
3755 pSMB->TotalParameterCount = cpu_to_le32(parm_len);
3756 pSMB->TotalDataCount = 0;
Jeff Laytonc974bef2011-10-11 06:41:32 -04003757 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003758 pSMB->ParameterCount = pSMB->TotalParameterCount;
3759 pSMB->DataCount = pSMB->TotalDataCount;
3760 temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
3761 (setup_count * 2) - 4 /* for rfc1001 length itself */;
3762 pSMB->ParameterOffset = cpu_to_le32(temp_offset);
3763 pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
3764 pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
3765 pSMB->SubCommand = cpu_to_le16(sub_command);
3766 return 0;
3767}
3768
3769static int
3770validate_ntransact(char *buf, char **ppparm, char **ppdata,
3771 __u32 *pparmlen, __u32 *pdatalen)
3772{
3773 char *end_of_smb;
3774 __u32 data_count, data_offset, parm_count, parm_offset;
3775 struct smb_com_ntransact_rsp *pSMBr;
Jeff Layton820a8032011-05-04 08:05:26 -04003776 u16 bcc;
Jeff Layton79df1ba2010-12-06 12:52:08 -05003777
3778 *pdatalen = 0;
3779 *pparmlen = 0;
3780
3781 if (buf == NULL)
3782 return -EINVAL;
3783
3784 pSMBr = (struct smb_com_ntransact_rsp *)buf;
3785
Jeff Layton820a8032011-05-04 08:05:26 -04003786 bcc = get_bcc(&pSMBr->hdr);
3787 end_of_smb = 2 /* sizeof byte count */ + bcc +
Jeff Layton79df1ba2010-12-06 12:52:08 -05003788 (char *)&pSMBr->ByteCount;
3789
3790 data_offset = le32_to_cpu(pSMBr->DataOffset);
3791 data_count = le32_to_cpu(pSMBr->DataCount);
3792 parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
3793 parm_count = le32_to_cpu(pSMBr->ParameterCount);
3794
3795 *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
3796 *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
3797
3798 /* should we also check that parm and data areas do not overlap? */
3799 if (*ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003800 cifs_dbg(FYI, "parms start after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003801 return -EINVAL;
3802 } else if (parm_count + *ppparm > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003803 cifs_dbg(FYI, "parm end after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003804 return -EINVAL;
3805 } else if (*ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003806 cifs_dbg(FYI, "data starts after end of smb\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003807 return -EINVAL;
3808 } else if (data_count + *ppdata > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003809 cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
3810 *ppdata, data_count, (data_count + *ppdata),
3811 end_of_smb, pSMBr);
Jeff Layton79df1ba2010-12-06 12:52:08 -05003812 return -EINVAL;
Jeff Layton820a8032011-05-04 08:05:26 -04003813 } else if (parm_count + data_count > bcc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003814 cifs_dbg(FYI, "parm count and data count larger than SMB\n");
Jeff Layton79df1ba2010-12-06 12:52:08 -05003815 return -EINVAL;
3816 }
3817 *pdatalen = data_count;
3818 *pparmlen = parm_count;
3819 return 0;
3820}
3821
Steve French0a4b92c2006-01-12 15:44:21 -08003822/* Get Security Descriptor (by handle) from remote server for a file or dir */
3823int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003824CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Steve French630f3f0c2007-10-25 21:17:17 +00003825 struct cifs_ntsd **acl_inf, __u32 *pbuflen)
Steve French0a4b92c2006-01-12 15:44:21 -08003826{
3827 int rc = 0;
3828 int buf_type = 0;
Steve Frenchad7a2922008-02-07 23:25:02 +00003829 QUERY_SEC_DESC_REQ *pSMB;
Steve French0a4b92c2006-01-12 15:44:21 -08003830 struct kvec iov[1];
3831
Joe Perchesf96637b2013-05-04 22:12:25 -05003832 cifs_dbg(FYI, "GetCifsACL\n");
Steve French0a4b92c2006-01-12 15:44:21 -08003833
Steve French630f3f0c2007-10-25 21:17:17 +00003834 *pbuflen = 0;
3835 *acl_inf = NULL;
3836
Steve Frenchb9c7a2b2007-10-26 23:40:20 +00003837 rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
Steve French0a4b92c2006-01-12 15:44:21 -08003838 8 /* parm len */, tcon, (void **) &pSMB);
3839 if (rc)
3840 return rc;
3841
3842 pSMB->MaxParameterCount = cpu_to_le32(4);
3843 /* BB TEST with big acls that might need to be e.g. larger than 16K */
3844 pSMB->MaxSetupCount = 0;
3845 pSMB->Fid = fid; /* file handle always le */
3846 pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
3847 CIFS_ACL_DACL);
3848 pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003849 inc_rfc1001_len(pSMB, 11);
Steve French0a4b92c2006-01-12 15:44:21 -08003850 iov[0].iov_base = (char *)pSMB;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003851 iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
Steve French0a4b92c2006-01-12 15:44:21 -08003852
Steve Frencha761ac52007-10-18 21:45:27 +00003853 rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
Jeff Layton77499812011-01-11 07:24:23 -05003854 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04003855 cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
Steve French0a4b92c2006-01-12 15:44:21 -08003856 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003857 cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
Steve French0a4b92c2006-01-12 15:44:21 -08003858 } else { /* decode response */
Steve Frenchad7a2922008-02-07 23:25:02 +00003859 __le32 *parm;
Steve French630f3f0c2007-10-25 21:17:17 +00003860 __u32 parm_len;
3861 __u32 acl_len;
Steve French50c2f752007-07-13 00:33:32 +00003862 struct smb_com_ntransact_rsp *pSMBr;
Steve French630f3f0c2007-10-25 21:17:17 +00003863 char *pdata;
Steve French0a4b92c2006-01-12 15:44:21 -08003864
3865/* validate_nttransact */
Steve French50c2f752007-07-13 00:33:32 +00003866 rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
Steve French630f3f0c2007-10-25 21:17:17 +00003867 &pdata, &parm_len, pbuflen);
Steve French790fe572007-07-07 19:25:05 +00003868 if (rc)
Steve French0a4b92c2006-01-12 15:44:21 -08003869 goto qsec_out;
3870 pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
3871
Joe Perchesf96637b2013-05-04 22:12:25 -05003872 cifs_dbg(FYI, "smb %p parm %p data %p\n",
3873 pSMBr, parm, *acl_inf);
Steve French0a4b92c2006-01-12 15:44:21 -08003874
3875 if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
3876 rc = -EIO; /* bad smb */
Steve French630f3f0c2007-10-25 21:17:17 +00003877 *pbuflen = 0;
Steve French0a4b92c2006-01-12 15:44:21 -08003878 goto qsec_out;
3879 }
3880
3881/* BB check that data area is minimum length and as big as acl_len */
3882
Steve Frenchaf6f4612007-10-16 18:40:37 +00003883 acl_len = le32_to_cpu(*parm);
Steve French630f3f0c2007-10-25 21:17:17 +00003884 if (acl_len != *pbuflen) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003885 cifs_dbg(VFS, "acl length %d does not match %d\n",
3886 acl_len, *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003887 if (*pbuflen > acl_len)
3888 *pbuflen = acl_len;
3889 }
Steve French0a4b92c2006-01-12 15:44:21 -08003890
Steve French630f3f0c2007-10-25 21:17:17 +00003891 /* check if buffer is big enough for the acl
3892 header followed by the smallest SID */
3893 if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
3894 (*pbuflen >= 64 * 1024)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003895 cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
Steve French630f3f0c2007-10-25 21:17:17 +00003896 rc = -EINVAL;
3897 *pbuflen = 0;
3898 } else {
Silviu-Mihai Popescuf7f7c182013-03-11 18:22:32 +02003899 *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
Steve French630f3f0c2007-10-25 21:17:17 +00003900 if (*acl_inf == NULL) {
3901 *pbuflen = 0;
3902 rc = -ENOMEM;
3903 }
Steve French630f3f0c2007-10-25 21:17:17 +00003904 }
Steve French0a4b92c2006-01-12 15:44:21 -08003905 }
3906qsec_out:
Sachin Prabhu6d81ed12014-06-16 15:35:24 +01003907 free_rsp_buf(buf_type, iov[0].iov_base);
Steve French4b8f9302006-02-26 16:41:18 +00003908/* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
Steve French0a4b92c2006-01-12 15:44:21 -08003909 return rc;
3910}
Steve French97837582007-12-31 07:47:21 +00003911
3912int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003913CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003914 struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
Steve French97837582007-12-31 07:47:21 +00003915{
3916 __u16 byte_count, param_count, data_count, param_offset, data_offset;
3917 int rc = 0;
3918 int bytes_returned = 0;
3919 SET_SEC_DESC_REQ *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003920 void *pSMBr;
Steve French97837582007-12-31 07:47:21 +00003921
3922setCifsAclRetry:
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003923 rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
Steve French97837582007-12-31 07:47:21 +00003924 if (rc)
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003925 return rc;
Steve French97837582007-12-31 07:47:21 +00003926
3927 pSMB->MaxSetupCount = 0;
3928 pSMB->Reserved = 0;
3929
3930 param_count = 8;
3931 param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
3932 data_count = acllen;
3933 data_offset = param_offset + param_count;
3934 byte_count = 3 /* pad */ + param_count;
3935
3936 pSMB->DataCount = cpu_to_le32(data_count);
3937 pSMB->TotalDataCount = pSMB->DataCount;
3938 pSMB->MaxParameterCount = cpu_to_le32(4);
3939 pSMB->MaxDataCount = cpu_to_le32(16384);
3940 pSMB->ParameterCount = cpu_to_le32(param_count);
3941 pSMB->ParameterOffset = cpu_to_le32(param_offset);
3942 pSMB->TotalParameterCount = pSMB->ParameterCount;
3943 pSMB->DataOffset = cpu_to_le32(data_offset);
3944 pSMB->SetupCount = 0;
3945 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
3946 pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
3947
3948 pSMB->Fid = fid; /* file handle always le */
3949 pSMB->Reserved2 = 0;
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05003950 pSMB->AclFlags = cpu_to_le32(aclflag);
Steve French97837582007-12-31 07:47:21 +00003951
3952 if (pntsd && acllen) {
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04003953 memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
3954 data_offset, pntsd, acllen);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003955 inc_rfc1001_len(pSMB, byte_count + data_count);
Steve French97837582007-12-31 07:47:21 +00003956 } else
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003957 inc_rfc1001_len(pSMB, byte_count);
Steve French97837582007-12-31 07:47:21 +00003958
3959 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
3960 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
3961
Joe Perchesf96637b2013-05-04 22:12:25 -05003962 cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
3963 bytes_returned, rc);
Steve French97837582007-12-31 07:47:21 +00003964 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003965 cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
Steve French97837582007-12-31 07:47:21 +00003966 cifs_buf_release(pSMB);
3967
3968 if (rc == -EAGAIN)
3969 goto setCifsAclRetry;
3970
3971 return (rc);
3972}
3973
Jeff Layton79df1ba2010-12-06 12:52:08 -05003974#endif /* CONFIG_CIFS_ACL */
Steve French0a4b92c2006-01-12 15:44:21 -08003975
Steve French6b8edfe2005-08-23 20:26:03 -07003976/* Legacy Query Path Information call for lookup to old servers such
3977 as Win9x/WinME */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003978int
3979SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
3980 const char *search_name, FILE_ALL_INFO *data,
3981 const struct nls_table *nls_codepage, int remap)
Steve French6b8edfe2005-08-23 20:26:03 -07003982{
Steve Frenchad7a2922008-02-07 23:25:02 +00003983 QUERY_INFORMATION_REQ *pSMB;
3984 QUERY_INFORMATION_RSP *pSMBr;
Steve French6b8edfe2005-08-23 20:26:03 -07003985 int rc = 0;
3986 int bytes_returned;
3987 int name_len;
3988
Joe Perchesf96637b2013-05-04 22:12:25 -05003989 cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
Steve French6b8edfe2005-08-23 20:26:03 -07003990QInfRetry:
3991 rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00003992 (void **) &pSMBr);
Steve French6b8edfe2005-08-23 20:26:03 -07003993 if (rc)
3994 return rc;
3995
3996 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
3997 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06003998 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003999 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004000 remap);
Steve French6b8edfe2005-08-23 20:26:03 -07004001 name_len++; /* trailing null */
4002 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004003 } else {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004004 name_len = strnlen(search_name, PATH_MAX);
Steve French6b8edfe2005-08-23 20:26:03 -07004005 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004006 strncpy(pSMB->FileName, search_name, name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004007 }
4008 pSMB->BufferFormat = 0x04;
Steve French50c2f752007-07-13 00:33:32 +00004009 name_len++; /* account for buffer type byte */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004010 inc_rfc1001_len(pSMB, (__u16)name_len);
Steve French6b8edfe2005-08-23 20:26:03 -07004011 pSMB->ByteCount = cpu_to_le16(name_len);
4012
4013 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004014 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve French6b8edfe2005-08-23 20:26:03 -07004015 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004016 cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004017 } else if (data) {
Steve French1bd5bbc2006-09-28 03:35:57 +00004018 struct timespec ts;
4019 __u32 time = le32_to_cpu(pSMBr->last_write_time);
Steve Frenchad7a2922008-02-07 23:25:02 +00004020
4021 /* decode response */
Steve French1bd5bbc2006-09-28 03:35:57 +00004022 /* BB FIXME - add time zone adjustment BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004023 memset(data, 0, sizeof(FILE_ALL_INFO));
Steve French1bd5bbc2006-09-28 03:35:57 +00004024 ts.tv_nsec = 0;
4025 ts.tv_sec = time;
4026 /* decode time fields */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004027 data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
4028 data->LastWriteTime = data->ChangeTime;
4029 data->LastAccessTime = 0;
4030 data->AllocationSize =
Steve French70ca7342005-09-22 16:32:06 -07004031 cpu_to_le64(le32_to_cpu(pSMBr->size));
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004032 data->EndOfFile = data->AllocationSize;
4033 data->Attributes =
Steve French70ca7342005-09-22 16:32:06 -07004034 cpu_to_le32(le16_to_cpu(pSMBr->attr));
Steve French6b8edfe2005-08-23 20:26:03 -07004035 } else
4036 rc = -EIO; /* bad buffer passed in */
4037
4038 cifs_buf_release(pSMB);
4039
4040 if (rc == -EAGAIN)
4041 goto QInfRetry;
4042
4043 return rc;
4044}
4045
Jeff Laytonbcd53572010-02-12 07:44:16 -05004046int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004047CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonbcd53572010-02-12 07:44:16 -05004048 u16 netfid, FILE_ALL_INFO *pFindData)
4049{
4050 struct smb_t2_qfi_req *pSMB = NULL;
4051 struct smb_t2_qfi_rsp *pSMBr = NULL;
4052 int rc = 0;
4053 int bytes_returned;
4054 __u16 params, byte_count;
Steve French6b8edfe2005-08-23 20:26:03 -07004055
Jeff Laytonbcd53572010-02-12 07:44:16 -05004056QFileInfoRetry:
4057 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4058 (void **) &pSMBr);
4059 if (rc)
4060 return rc;
Steve French6b8edfe2005-08-23 20:26:03 -07004061
Jeff Laytonbcd53572010-02-12 07:44:16 -05004062 params = 2 /* level */ + 2 /* fid */;
4063 pSMB->t2.TotalDataCount = 0;
4064 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4065 /* BB find exact max data count below from sess structure BB */
4066 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4067 pSMB->t2.MaxSetupCount = 0;
4068 pSMB->t2.Reserved = 0;
4069 pSMB->t2.Flags = 0;
4070 pSMB->t2.Timeout = 0;
4071 pSMB->t2.Reserved2 = 0;
4072 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4073 Fid) - 4);
4074 pSMB->t2.DataCount = 0;
4075 pSMB->t2.DataOffset = 0;
4076 pSMB->t2.SetupCount = 1;
4077 pSMB->t2.Reserved3 = 0;
4078 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4079 byte_count = params + 1 /* pad */ ;
4080 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4081 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4082 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
4083 pSMB->Pad = 0;
4084 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004085 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004086 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004087
4088 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4089 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4090 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004091 cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
Jeff Laytonbcd53572010-02-12 07:44:16 -05004092 } else { /* decode response */
4093 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4094
4095 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4096 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004097 else if (get_bcc(&pSMBr->hdr) < 40)
Jeff Laytonbcd53572010-02-12 07:44:16 -05004098 rc = -EIO; /* bad smb */
4099 else if (pFindData) {
4100 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4101 memcpy((char *) pFindData,
4102 (char *) &pSMBr->hdr.Protocol +
4103 data_offset, sizeof(FILE_ALL_INFO));
4104 } else
4105 rc = -ENOMEM;
4106 }
4107 cifs_buf_release(pSMB);
4108 if (rc == -EAGAIN)
4109 goto QFileInfoRetry;
4110
4111 return rc;
4112}
Steve French6b8edfe2005-08-23 20:26:03 -07004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004115CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004116 const char *search_name, FILE_ALL_INFO *data,
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004117 int legacy /* old style infolevel */,
Steve French737b7582005-04-28 22:41:06 -07004118 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004120 /* level 263 SMB_QUERY_FILE_ALL_INFO */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 TRANSACTION2_QPI_REQ *pSMB = NULL;
4122 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4123 int rc = 0;
4124 int bytes_returned;
4125 int name_len;
4126 __u16 params, byte_count;
4127
Joe Perchesf96637b2013-05-04 22:12:25 -05004128 /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129QPathInfoRetry:
4130 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4131 (void **) &pSMBr);
4132 if (rc)
4133 return rc;
4134
4135 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4136 name_len =
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004137 cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06004138 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 name_len++; /* trailing null */
4140 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004141 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004142 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 name_len++; /* trailing null */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004144 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
4146
Steve French50c2f752007-07-13 00:33:32 +00004147 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 pSMB->TotalDataCount = 0;
4149 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00004150 /* BB find exact max SMB PDU from sess structure BB */
4151 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 pSMB->MaxSetupCount = 0;
4153 pSMB->Reserved = 0;
4154 pSMB->Flags = 0;
4155 pSMB->Timeout = 0;
4156 pSMB->Reserved2 = 0;
4157 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004158 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 pSMB->DataCount = 0;
4160 pSMB->DataOffset = 0;
4161 pSMB->SetupCount = 1;
4162 pSMB->Reserved3 = 0;
4163 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4164 byte_count = params + 1 /* pad */ ;
4165 pSMB->TotalParameterCount = cpu_to_le16(params);
4166 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve French790fe572007-07-07 19:25:05 +00004167 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004168 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
4169 else
4170 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004172 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 pSMB->ByteCount = cpu_to_le16(byte_count);
4174
4175 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4176 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4177 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004178 cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 } else { /* decode response */
4180 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4181
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004182 if (rc) /* BB add auto retry on EOPNOTSUPP? */
4183 rc = -EIO;
Jeff Layton820a8032011-05-04 08:05:26 -04004184 else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 rc = -EIO; /* bad smb */
Jeff Layton820a8032011-05-04 08:05:26 -04004186 else if (legacy && get_bcc(&pSMBr->hdr) < 24)
Steve French50c2f752007-07-13 00:33:32 +00004187 rc = -EIO; /* 24 or 26 expected but we do not read
4188 last field */
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004189 else if (data) {
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004190 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Steve Frenchad7a2922008-02-07 23:25:02 +00004192
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004193 /*
4194 * On legacy responses we do not read the last field,
4195 * EAsize, fortunately since it varies by subdialect and
4196 * also note it differs on Set vs Get, ie two bytes or 4
4197 * bytes depending but we don't care here.
4198 */
Steve Frenchad7a2922008-02-07 23:25:02 +00004199 if (legacy)
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004200 size = sizeof(FILE_INFO_STANDARD);
4201 else
4202 size = sizeof(FILE_ALL_INFO);
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004203 memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
Steve Frenchacf1a1b2006-10-12 03:28:28 +00004204 data_offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 } else
4206 rc = -ENOMEM;
4207 }
4208 cifs_buf_release(pSMB);
4209 if (rc == -EAGAIN)
4210 goto QPathInfoRetry;
4211
4212 return rc;
4213}
4214
4215int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004216CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004217 u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
4218{
4219 struct smb_t2_qfi_req *pSMB = NULL;
4220 struct smb_t2_qfi_rsp *pSMBr = NULL;
4221 int rc = 0;
4222 int bytes_returned;
4223 __u16 params, byte_count;
4224
4225UnixQFileInfoRetry:
4226 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4227 (void **) &pSMBr);
4228 if (rc)
4229 return rc;
4230
4231 params = 2 /* level */ + 2 /* fid */;
4232 pSMB->t2.TotalDataCount = 0;
4233 pSMB->t2.MaxParameterCount = cpu_to_le16(4);
4234 /* BB find exact max data count below from sess structure BB */
4235 pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
4236 pSMB->t2.MaxSetupCount = 0;
4237 pSMB->t2.Reserved = 0;
4238 pSMB->t2.Flags = 0;
4239 pSMB->t2.Timeout = 0;
4240 pSMB->t2.Reserved2 = 0;
4241 pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
4242 Fid) - 4);
4243 pSMB->t2.DataCount = 0;
4244 pSMB->t2.DataOffset = 0;
4245 pSMB->t2.SetupCount = 1;
4246 pSMB->t2.Reserved3 = 0;
4247 pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
4248 byte_count = params + 1 /* pad */ ;
4249 pSMB->t2.TotalParameterCount = cpu_to_le16(params);
4250 pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
4251 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4252 pSMB->Pad = 0;
4253 pSMB->Fid = netfid;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004254 inc_rfc1001_len(pSMB, byte_count);
David Disseldorp7ac0feb2013-06-28 11:47:33 +02004255 pSMB->t2.ByteCount = cpu_to_le16(byte_count);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004256
4257 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4258 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4259 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004260 cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
Jeff Laytonc8634fd2010-02-12 07:44:17 -05004261 } else { /* decode response */
4262 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4263
Jeff Layton820a8032011-05-04 08:05:26 -04004264 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004265 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 -05004266 rc = -EIO; /* bad smb */
4267 } else {
4268 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4269 memcpy((char *) pFindData,
4270 (char *) &pSMBr->hdr.Protocol +
4271 data_offset,
4272 sizeof(FILE_UNIX_BASIC_INFO));
4273 }
4274 }
4275
4276 cifs_buf_release(pSMB);
4277 if (rc == -EAGAIN)
4278 goto UnixQFileInfoRetry;
4279
4280 return rc;
4281}
4282
4283int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004284CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 const unsigned char *searchName,
Steve French582d21e2008-05-13 04:54:12 +00004286 FILE_UNIX_BASIC_INFO *pFindData,
Steve French737b7582005-04-28 22:41:06 -07004287 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289/* SMB_QUERY_FILE_UNIX_BASIC */
4290 TRANSACTION2_QPI_REQ *pSMB = NULL;
4291 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4292 int rc = 0;
4293 int bytes_returned = 0;
4294 int name_len;
4295 __u16 params, byte_count;
4296
Joe Perchesf96637b2013-05-04 22:12:25 -05004297 cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298UnixQPathInfoRetry:
4299 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4300 (void **) &pSMBr);
4301 if (rc)
4302 return rc;
4303
4304 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4305 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004306 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4307 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 name_len++; /* trailing null */
4309 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004310 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 name_len = strnlen(searchName, PATH_MAX);
4312 name_len++; /* trailing null */
4313 strncpy(pSMB->FileName, searchName, name_len);
4314 }
4315
Steve French50c2f752007-07-13 00:33:32 +00004316 params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 pSMB->TotalDataCount = 0;
4318 pSMB->MaxParameterCount = cpu_to_le16(2);
4319 /* BB find exact max SMB PDU from sess structure BB */
Steve French50c2f752007-07-13 00:33:32 +00004320 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 pSMB->MaxSetupCount = 0;
4322 pSMB->Reserved = 0;
4323 pSMB->Flags = 0;
4324 pSMB->Timeout = 0;
4325 pSMB->Reserved2 = 0;
4326 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004327 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 pSMB->DataCount = 0;
4329 pSMB->DataOffset = 0;
4330 pSMB->SetupCount = 1;
4331 pSMB->Reserved3 = 0;
4332 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4333 byte_count = params + 1 /* pad */ ;
4334 pSMB->TotalParameterCount = cpu_to_le16(params);
4335 pSMB->ParameterCount = pSMB->TotalParameterCount;
4336 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
4337 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004338 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 pSMB->ByteCount = cpu_to_le16(byte_count);
4340
4341 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4342 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4343 if (rc) {
Steve Frenchebcc9432013-12-09 09:18:09 -06004344 cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 } else { /* decode response */
4346 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
4347
Jeff Layton820a8032011-05-04 08:05:26 -04004348 if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004349 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 -07004350 rc = -EIO; /* bad smb */
4351 } else {
4352 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4353 memcpy((char *) pFindData,
4354 (char *) &pSMBr->hdr.Protocol +
4355 data_offset,
Steve French630f3f0c2007-10-25 21:17:17 +00004356 sizeof(FILE_UNIX_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
4358 }
4359 cifs_buf_release(pSMB);
4360 if (rc == -EAGAIN)
4361 goto UnixQPathInfoRetry;
4362
4363 return rc;
4364}
4365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366/* xid, tcon, searchName and codepage are input parms, rest are returned */
4367int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004368CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004369 const char *searchName, struct cifs_sb_info *cifs_sb,
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004370 __u16 *pnetfid, __u16 search_flags,
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004371 struct cifs_search_info *psrch_inf, bool msearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373/* level 257 SMB_ */
4374 TRANSACTION2_FFIRST_REQ *pSMB = NULL;
4375 TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004376 T2_FFIRST_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 int rc = 0;
4378 int bytes_returned = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004379 int name_len, remap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 __u16 params, byte_count;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004381 struct nls_table *nls_codepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Joe Perchesf96637b2013-05-04 22:12:25 -05004383 cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385findFirstRetry:
4386 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4387 (void **) &pSMBr);
4388 if (rc)
4389 return rc;
4390
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004391 nls_codepage = cifs_sb->local_nls;
Steve French2baa2682014-09-27 02:19:01 -05004392 remap = cifs_remap(cifs_sb);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4395 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004396 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
4397 PATH_MAX, nls_codepage, remap);
Steve French737b7582005-04-28 22:41:06 -07004398 /* We can not add the asterik earlier in case
4399 it got remapped to 0xF03A as if it were part of the
4400 directory name instead of a wildcard */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 name_len *= 2;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004402 if (msearch) {
4403 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4404 pSMB->FileName[name_len+1] = 0;
4405 pSMB->FileName[name_len+2] = '*';
4406 pSMB->FileName[name_len+3] = 0;
4407 name_len += 4; /* now the trailing null */
4408 /* null terminate just in case */
4409 pSMB->FileName[name_len] = 0;
4410 pSMB->FileName[name_len+1] = 0;
4411 name_len += 2;
4412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 } else { /* BB add check for overrun of SMB buf BB */
4414 name_len = strnlen(searchName, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415/* BB fix here and in unicode clause above ie
Steve French790fe572007-07-07 19:25:05 +00004416 if (name_len > buffersize-header)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 free buffer exit; BB */
4418 strncpy(pSMB->FileName, searchName, name_len);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004419 if (msearch) {
4420 pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
4421 pSMB->FileName[name_len+1] = '*';
4422 pSMB->FileName[name_len+2] = 0;
4423 name_len += 3;
4424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426
4427 params = 12 + name_len /* includes null */ ;
4428 pSMB->TotalDataCount = 0; /* no EAs */
4429 pSMB->MaxParameterCount = cpu_to_le16(10);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004430 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 pSMB->MaxSetupCount = 0;
4432 pSMB->Reserved = 0;
4433 pSMB->Flags = 0;
4434 pSMB->Timeout = 0;
4435 pSMB->Reserved2 = 0;
4436 byte_count = params + 1 /* pad */ ;
4437 pSMB->TotalParameterCount = cpu_to_le16(params);
4438 pSMB->ParameterCount = pSMB->TotalParameterCount;
4439 pSMB->ParameterOffset = cpu_to_le16(
Steve French88274812006-03-09 22:21:45 +00004440 offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
4441 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 pSMB->DataCount = 0;
4443 pSMB->DataOffset = 0;
4444 pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
4445 pSMB->Reserved3 = 0;
4446 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
4447 pSMB->SearchAttributes =
4448 cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
4449 ATTR_DIRECTORY);
Steve French50c2f752007-07-13 00:33:32 +00004450 pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004451 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4453
4454 /* BB what should we set StorageType to? Does it matter? BB */
4455 pSMB->SearchStorageType = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004456 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 pSMB->ByteCount = cpu_to_le16(byte_count);
4458
4459 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4460 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004461 cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Steve French88274812006-03-09 22:21:45 +00004463 if (rc) {/* BB add logic to retry regular search if Unix search
4464 rejected unexpectedly by server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 /* BB Add code to handle unsupported level rc */
Joe Perchesf96637b2013-05-04 22:12:25 -05004466 cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
Steve French1982c342005-08-17 12:38:22 -07004467
Steve French88274812006-03-09 22:21:45 +00004468 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 /* BB eventually could optimize out free and realloc of buf */
4471 /* for this case */
4472 if (rc == -EAGAIN)
4473 goto findFirstRetry;
4474 } else { /* decode response */
4475 /* BB remember to free buffer if error BB */
4476 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French790fe572007-07-07 19:25:05 +00004477 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004478 unsigned int lnoff;
4479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004481 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 else
Steve French4b18f2a2008-04-29 00:06:05 +00004483 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 psrch_inf->ntwrk_buf_start = (char *)pSMBr;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004486 psrch_inf->smallBuf = 0;
Steve French50c2f752007-07-13 00:33:32 +00004487 psrch_inf->srch_entries_start =
4488 (char *) &pSMBr->hdr.Protocol +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
4491 le16_to_cpu(pSMBr->t2.ParameterOffset));
4492
Steve French790fe572007-07-07 19:25:05 +00004493 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004494 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 else
Steve French4b18f2a2008-04-29 00:06:05 +00004496 psrch_inf->endOfSearch = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Steve French50c2f752007-07-13 00:33:32 +00004498 psrch_inf->entries_in_buffer =
4499 le16_to_cpu(parms->SearchCount);
Steve French60808232006-04-22 15:53:05 +00004500 psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004502 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004503 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004504 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004505 psrch_inf->last_entry = NULL;
4506 return rc;
4507 }
4508
Steve French0752f152008-10-07 20:03:33 +00004509 psrch_inf->last_entry = psrch_inf->srch_entries_start +
Steve Frenchb77d7532008-10-08 19:13:46 +00004510 lnoff;
4511
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -05004512 if (pnetfid)
4513 *pnetfid = parms->SearchHandle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 } else {
4515 cifs_buf_release(pSMB);
4516 }
4517 }
4518
4519 return rc;
4520}
4521
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004522int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
4523 __u16 searchHandle, __u16 search_flags,
4524 struct cifs_search_info *psrch_inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
4526 TRANSACTION2_FNEXT_REQ *pSMB = NULL;
4527 TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
Steve Frenchad7a2922008-02-07 23:25:02 +00004528 T2_FNEXT_RSP_PARMS *parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 char *response_data;
4530 int rc = 0;
Jeff Layton9438fab2011-08-23 07:21:28 -04004531 int bytes_returned;
4532 unsigned int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 __u16 params, byte_count;
4534
Joe Perchesf96637b2013-05-04 22:12:25 -05004535 cifs_dbg(FYI, "In FindNext\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
Steve French4b18f2a2008-04-29 00:06:05 +00004537 if (psrch_inf->endOfSearch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return -ENOENT;
4539
4540 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
4541 (void **) &pSMBr);
4542 if (rc)
4543 return rc;
4544
Steve French50c2f752007-07-13 00:33:32 +00004545 params = 14; /* includes 2 bytes of null string, converted to LE below*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 byte_count = 0;
4547 pSMB->TotalDataCount = 0; /* no EAs */
4548 pSMB->MaxParameterCount = cpu_to_le16(8);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004549 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 pSMB->MaxSetupCount = 0;
4551 pSMB->Reserved = 0;
4552 pSMB->Flags = 0;
4553 pSMB->Timeout = 0;
4554 pSMB->Reserved2 = 0;
4555 pSMB->ParameterOffset = cpu_to_le16(
4556 offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
4557 pSMB->DataCount = 0;
4558 pSMB->DataOffset = 0;
4559 pSMB->SetupCount = 1;
4560 pSMB->Reserved3 = 0;
4561 pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
4562 pSMB->SearchHandle = searchHandle; /* always kept as le */
4563 pSMB->SearchCount =
Steve French630f3f0c2007-10-25 21:17:17 +00004564 cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
4566 pSMB->ResumeKey = psrch_inf->resume_key;
Shirish Pargaonkar2608bee2012-05-15 10:19:16 -05004567 pSMB->SearchFlags = cpu_to_le16(search_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569 name_len = psrch_inf->resume_name_len;
4570 params += name_len;
Steve French790fe572007-07-07 19:25:05 +00004571 if (name_len < PATH_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
4573 byte_count += name_len;
Steve Frenchef6724e2005-08-02 21:31:05 -07004574 /* 14 byte parm len above enough for 2 byte null terminator */
4575 pSMB->ResumeFileName[name_len] = 0;
4576 pSMB->ResumeFileName[name_len+1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 } else {
4578 rc = -EINVAL;
4579 goto FNext2_err_exit;
4580 }
4581 byte_count = params + 1 /* pad */ ;
4582 pSMB->TotalParameterCount = cpu_to_le16(params);
4583 pSMB->ParameterCount = pSMB->TotalParameterCount;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004584 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00004586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4588 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004589 cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (rc) {
4591 if (rc == -EBADF) {
Steve French4b18f2a2008-04-29 00:06:05 +00004592 psrch_inf->endOfSearch = true;
Jeff Layton63534502008-05-12 19:56:05 -07004593 cifs_buf_release(pSMB);
Steve French50c2f752007-07-13 00:33:32 +00004594 rc = 0; /* search probably was closed at end of search*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004596 cifs_dbg(FYI, "FindNext returned = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 } else { /* decode response */
4598 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve French50c2f752007-07-13 00:33:32 +00004599
Steve French790fe572007-07-07 19:25:05 +00004600 if (rc == 0) {
Steve Frenchb77d7532008-10-08 19:13:46 +00004601 unsigned int lnoff;
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 /* BB fixme add lock for file (srch_info) struct here */
4604 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
Steve French4b18f2a2008-04-29 00:06:05 +00004605 psrch_inf->unicode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 else
Steve French4b18f2a2008-04-29 00:06:05 +00004607 psrch_inf->unicode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 response_data = (char *) &pSMBr->hdr.Protocol +
4609 le16_to_cpu(pSMBr->t2.ParameterOffset);
4610 parms = (T2_FNEXT_RSP_PARMS *)response_data;
4611 response_data = (char *)&pSMBr->hdr.Protocol +
4612 le16_to_cpu(pSMBr->t2.DataOffset);
Steve French790fe572007-07-07 19:25:05 +00004613 if (psrch_inf->smallBuf)
Steve Frenchd47d7c12006-02-28 03:45:48 +00004614 cifs_small_buf_release(
4615 psrch_inf->ntwrk_buf_start);
4616 else
4617 cifs_buf_release(psrch_inf->ntwrk_buf_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 psrch_inf->srch_entries_start = response_data;
4619 psrch_inf->ntwrk_buf_start = (char *)pSMB;
Steve Frenchd47d7c12006-02-28 03:45:48 +00004620 psrch_inf->smallBuf = 0;
Steve French790fe572007-07-07 19:25:05 +00004621 if (parms->EndofSearch)
Steve French4b18f2a2008-04-29 00:06:05 +00004622 psrch_inf->endOfSearch = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 else
Steve French4b18f2a2008-04-29 00:06:05 +00004624 psrch_inf->endOfSearch = false;
Steve French50c2f752007-07-13 00:33:32 +00004625 psrch_inf->entries_in_buffer =
4626 le16_to_cpu(parms->SearchCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 psrch_inf->index_of_last_entry +=
4628 psrch_inf->entries_in_buffer;
Steve Frenchb77d7532008-10-08 19:13:46 +00004629 lnoff = le16_to_cpu(parms->LastNameOffset);
Jeff Laytonc974bef2011-10-11 06:41:32 -04004630 if (CIFSMaxBufSize < lnoff) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004631 cifs_dbg(VFS, "ignoring corrupt resume name\n");
Steve Frenchb77d7532008-10-08 19:13:46 +00004632 psrch_inf->last_entry = NULL;
4633 return rc;
4634 } else
4635 psrch_inf->last_entry =
4636 psrch_inf->srch_entries_start + lnoff;
4637
Joe Perchesf96637b2013-05-04 22:12:25 -05004638/* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
4639 psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 /* BB fixme add unlock here */
4642 }
4643
4644 }
4645
4646 /* BB On error, should we leave previous search buf (and count and
4647 last entry fields) intact or free the previous one? */
4648
4649 /* Note: On -EAGAIN error only caller can retry on handle based calls
4650 since file handle passed in no longer valid */
4651FNext2_err_exit:
4652 if (rc != 0)
4653 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 return rc;
4655}
4656
4657int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004658CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
Steve French50c2f752007-07-13 00:33:32 +00004659 const __u16 searchHandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
4661 int rc = 0;
4662 FINDCLOSE_REQ *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Joe Perchesf96637b2013-05-04 22:12:25 -05004664 cifs_dbg(FYI, "In CIFSSMBFindClose\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
4666
4667 /* no sense returning error if session restarted
4668 as file handle has been closed */
Steve French790fe572007-07-07 19:25:05 +00004669 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 return 0;
4671 if (rc)
4672 return rc;
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 pSMB->FileID = searchHandle;
4675 pSMB->ByteCount = 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04004676 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00004677 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004678 cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
Steve Frenchad7a2922008-02-07 23:25:02 +00004679
Pavel Shilovsky44c58182012-05-28 14:16:31 +04004680 cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682 /* Since session is dead, search handle closed on server already */
4683 if (rc == -EAGAIN)
4684 rc = 0;
4685
4686 return rc;
4687}
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004690CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004691 const char *search_name, __u64 *inode_number,
Steve French50c2f752007-07-13 00:33:32 +00004692 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
4694 int rc = 0;
4695 TRANSACTION2_QPI_REQ *pSMB = NULL;
4696 TRANSACTION2_QPI_RSP *pSMBr = NULL;
4697 int name_len, bytes_returned;
4698 __u16 params, byte_count;
4699
Joe Perchesf96637b2013-05-04 22:12:25 -05004700 cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
Steve French790fe572007-07-07 19:25:05 +00004701 if (tcon == NULL)
Steve French50c2f752007-07-13 00:33:32 +00004702 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704GetInodeNumberRetry:
4705 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
Steve French50c2f752007-07-13 00:33:32 +00004706 (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 if (rc)
4708 return rc;
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
4711 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004712 cifsConvertToUTF16((__le16 *) pSMB->FileName,
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004713 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004714 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 name_len++; /* trailing null */
4716 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004717 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004718 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 name_len++; /* trailing null */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +04004720 strncpy(pSMB->FileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
4722
4723 params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
4724 pSMB->TotalDataCount = 0;
4725 pSMB->MaxParameterCount = cpu_to_le16(2);
4726 /* BB find exact max data count below from sess structure BB */
4727 pSMB->MaxDataCount = cpu_to_le16(4000);
4728 pSMB->MaxSetupCount = 0;
4729 pSMB->Reserved = 0;
4730 pSMB->Flags = 0;
4731 pSMB->Timeout = 0;
4732 pSMB->Reserved2 = 0;
4733 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004734 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 pSMB->DataCount = 0;
4736 pSMB->DataOffset = 0;
4737 pSMB->SetupCount = 1;
4738 pSMB->Reserved3 = 0;
4739 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
4740 byte_count = params + 1 /* pad */ ;
4741 pSMB->TotalParameterCount = cpu_to_le16(params);
4742 pSMB->ParameterCount = pSMB->TotalParameterCount;
4743 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
4744 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004745 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 pSMB->ByteCount = cpu_to_le16(byte_count);
4747
4748 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
4749 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4750 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004751 cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 } else {
4753 /* decode response */
4754 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /* BB also check enough total bytes returned */
Jeff Layton820a8032011-05-04 08:05:26 -04004756 if (rc || get_bcc(&pSMBr->hdr) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 /* If rc should we check for EOPNOSUPP and
4758 disable the srvino flag? or in caller? */
4759 rc = -EIO; /* bad smb */
Steve French50c2f752007-07-13 00:33:32 +00004760 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
4762 __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
Steve French50c2f752007-07-13 00:33:32 +00004763 struct file_internal_info *pfinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* BB Do we need a cast or hash here ? */
Steve French790fe572007-07-07 19:25:05 +00004765 if (count < 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004766 cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 rc = -EIO;
4768 goto GetInodeNumOut;
4769 }
4770 pfinfo = (struct file_internal_info *)
4771 (data_offset + (char *) &pSMBr->hdr.Protocol);
Steve French85a6dac2009-04-01 05:22:00 +00004772 *inode_number = le64_to_cpu(pfinfo->UniqueId);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 }
4774 }
4775GetInodeNumOut:
4776 cifs_buf_release(pSMB);
4777 if (rc == -EAGAIN)
4778 goto GetInodeNumberRetry;
4779 return rc;
4780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Igor Mammedovfec45852008-05-16 13:06:30 +04004782/* parses DFS refferal V3 structure
4783 * caller is responsible for freeing target_nodes
4784 * returns:
4785 * on success - 0
4786 * on failure - errno
4787 */
4788static int
Steve Frencha1fe78f2008-05-16 18:48:38 +00004789parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
Igor Mammedovfec45852008-05-16 13:06:30 +04004790 unsigned int *num_of_nodes,
4791 struct dfs_info3_param **target_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004792 const struct nls_table *nls_codepage, int remap,
4793 const char *searchName)
Igor Mammedovfec45852008-05-16 13:06:30 +04004794{
4795 int i, rc = 0;
4796 char *data_end;
4797 bool is_unicode;
4798 struct dfs_referral_level_3 *ref;
4799
Harvey Harrison5ca33c62008-07-23 17:45:58 -07004800 if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
4801 is_unicode = true;
4802 else
4803 is_unicode = false;
Igor Mammedovfec45852008-05-16 13:06:30 +04004804 *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
4805
4806 if (*num_of_nodes < 1) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004807 cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
4808 *num_of_nodes);
Igor Mammedovfec45852008-05-16 13:06:30 +04004809 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004810 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004811 }
4812
4813 ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
Al Viro1d92cfd2008-06-02 10:59:02 +01004814 if (ref->VersionNumber != cpu_to_le16(3)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004815 cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
4816 le16_to_cpu(ref->VersionNumber));
Igor Mammedovfec45852008-05-16 13:06:30 +04004817 rc = -EINVAL;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004818 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004819 }
4820
4821 /* get the upper boundary of the resp buffer */
4822 data_end = (char *)(&(pSMBr->PathConsumed)) +
4823 le16_to_cpu(pSMBr->t2.DataCount);
4824
Joe Perchesf96637b2013-05-04 22:12:25 -05004825 cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
4826 *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
Igor Mammedovfec45852008-05-16 13:06:30 +04004827
Joe Perchesf96637b2013-05-04 22:12:25 -05004828 *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
4829 GFP_KERNEL);
Igor Mammedovfec45852008-05-16 13:06:30 +04004830 if (*target_nodes == NULL) {
Igor Mammedovfec45852008-05-16 13:06:30 +04004831 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004832 goto parse_DFS_referrals_exit;
Igor Mammedovfec45852008-05-16 13:06:30 +04004833 }
4834
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08004835 /* collect necessary data from referrals */
Igor Mammedovfec45852008-05-16 13:06:30 +04004836 for (i = 0; i < *num_of_nodes; i++) {
4837 char *temp;
4838 int max_len;
4839 struct dfs_info3_param *node = (*target_nodes)+i;
4840
Steve French0e0d2cf2009-05-01 05:27:32 +00004841 node->flags = le32_to_cpu(pSMBr->DFSFlags);
Igor Mammedov2c556082008-10-23 13:58:42 +04004842 if (is_unicode) {
Jeff Layton331c3132008-12-17 06:31:53 -05004843 __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
4844 GFP_KERNEL);
Steve French2920ee22009-08-31 15:27:26 +00004845 if (tmp == NULL) {
4846 rc = -ENOMEM;
4847 goto parse_DFS_referrals_exit;
4848 }
Steve Frenchacbbb762012-01-18 22:32:33 -06004849 cifsConvertToUTF16((__le16 *) tmp, searchName,
4850 PATH_MAX, nls_codepage, remap);
4851 node->path_consumed = cifs_utf16_bytes(tmp,
Jeff Layton69f801f2009-04-30 06:46:32 -04004852 le16_to_cpu(pSMBr->PathConsumed),
Igor Mammedov2c556082008-10-23 13:58:42 +04004853 nls_codepage);
4854 kfree(tmp);
4855 } else
4856 node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
4857
Igor Mammedovfec45852008-05-16 13:06:30 +04004858 node->server_type = le16_to_cpu(ref->ServerType);
4859 node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
4860
4861 /* copy DfsPath */
4862 temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
4863 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004864 node->path_name = cifs_strndup_from_utf16(temp, max_len,
4865 is_unicode, nls_codepage);
Jeff Laytond8e2f532009-05-14 07:46:59 -04004866 if (!node->path_name) {
4867 rc = -ENOMEM;
Steve Frencha1fe78f2008-05-16 18:48:38 +00004868 goto parse_DFS_referrals_exit;
Jeff Layton066ce682009-04-30 07:16:14 -04004869 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004870
4871 /* copy link target UNC */
4872 temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
4873 max_len = data_end - temp;
Steve Frenchacbbb762012-01-18 22:32:33 -06004874 node->node_name = cifs_strndup_from_utf16(temp, max_len,
4875 is_unicode, nls_codepage);
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004876 if (!node->node_name) {
Jeff Laytond8e2f532009-05-14 07:46:59 -04004877 rc = -ENOMEM;
Stefan Metzmacherd8f27992012-05-04 00:19:28 +02004878 goto parse_DFS_referrals_exit;
4879 }
4880
4881 ref++;
Igor Mammedovfec45852008-05-16 13:06:30 +04004882 }
4883
Steve Frencha1fe78f2008-05-16 18:48:38 +00004884parse_DFS_referrals_exit:
Igor Mammedovfec45852008-05-16 13:06:30 +04004885 if (rc) {
4886 free_dfs_info_array(*target_nodes, *num_of_nodes);
4887 *target_nodes = NULL;
4888 *num_of_nodes = 0;
4889 }
4890 return rc;
4891}
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004894CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004895 const char *search_name, struct dfs_info3_param **target_nodes,
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004896 unsigned int *num_of_nodes,
Steve French737b7582005-04-28 22:41:06 -07004897 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
4899/* TRANS2_GET_DFS_REFERRAL */
4900 TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
4901 TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 int rc = 0;
4903 int bytes_returned;
4904 int name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 __u16 params, byte_count;
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004906 *num_of_nodes = 0;
4907 *target_nodes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Joe Perchesf96637b2013-05-04 22:12:25 -05004909 cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 if (ses == NULL)
4911 return -ENODEV;
4912getDFSRetry:
4913 rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
4914 (void **) &pSMBr);
4915 if (rc)
4916 return rc;
Steve French50c2f752007-07-13 00:33:32 +00004917
4918 /* server pointer checked in called function,
Steve French1982c342005-08-17 12:38:22 -07004919 but should never be null here anyway */
Pavel Shilovsky88257362012-05-23 14:01:59 +04004920 pSMB->hdr.Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 pSMB->hdr.Tid = ses->ipc_tid;
4922 pSMB->hdr.Uid = ses->Suid;
Steve French26f57362007-08-30 22:09:15 +00004923 if (ses->capabilities & CAP_STATUS32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
Steve French26f57362007-08-30 22:09:15 +00004925 if (ses->capabilities & CAP_DFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 pSMB->hdr.Flags2 |= SMBFLG2_DFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
4928 if (ses->capabilities & CAP_UNICODE) {
4929 pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
4930 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06004931 cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004932 search_name, PATH_MAX, nls_codepage,
Steve Frenchacbbb762012-01-18 22:32:33 -06004933 remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 name_len++; /* trailing null */
4935 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00004936 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004937 name_len = strnlen(search_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 name_len++; /* trailing null */
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004939 strncpy(pSMB->RequestFileName, search_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
4941
Dan Carpenter65c3b202015-04-30 17:30:24 +03004942 if (ses->server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04004943 pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
Steve French1a4e15a2006-10-12 21:33:51 +00004944
Steve French50c2f752007-07-13 00:33:32 +00004945 pSMB->hdr.Uid = ses->Suid;
Steve French1a4e15a2006-10-12 21:33:51 +00004946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 params = 2 /* level */ + name_len /*includes null */ ;
4948 pSMB->TotalDataCount = 0;
4949 pSMB->DataCount = 0;
4950 pSMB->DataOffset = 0;
4951 pSMB->MaxParameterCount = 0;
Steve French582d21e2008-05-13 04:54:12 +00004952 /* BB find exact max SMB PDU from sess structure BB */
4953 pSMB->MaxDataCount = cpu_to_le16(4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 pSMB->MaxSetupCount = 0;
4955 pSMB->Reserved = 0;
4956 pSMB->Flags = 0;
4957 pSMB->Timeout = 0;
4958 pSMB->Reserved2 = 0;
4959 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00004960 struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 pSMB->SetupCount = 1;
4962 pSMB->Reserved3 = 0;
4963 pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
4964 byte_count = params + 3 /* pad */ ;
4965 pSMB->ParameterCount = cpu_to_le16(params);
4966 pSMB->TotalParameterCount = pSMB->ParameterCount;
4967 pSMB->MaxReferralLevel = cpu_to_le16(3);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004968 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 pSMB->ByteCount = cpu_to_le16(byte_count);
4970
4971 rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
4972 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
4973 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004974 cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004975 goto GetDFSRefExit;
4976 }
4977 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004979 /* BB Also check if enough total bytes returned? */
Jeff Layton820a8032011-05-04 08:05:26 -04004980 if (rc || get_bcc(&pSMBr->hdr) < 17) {
Steve Frenchc2cf07d2008-05-15 06:20:02 +00004981 rc = -EIO; /* bad smb */
Igor Mammedovfec45852008-05-16 13:06:30 +04004982 goto GetDFSRefExit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 }
Igor Mammedovfec45852008-05-16 13:06:30 +04004984
Joe Perchesf96637b2013-05-04 22:12:25 -05004985 cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
4986 get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
Igor Mammedovfec45852008-05-16 13:06:30 +04004987
4988 /* parse returned result into more usable form */
Steve Frencha1fe78f2008-05-16 18:48:38 +00004989 rc = parse_DFS_referrals(pSMBr, num_of_nodes,
Igor Mammedov2c556082008-10-23 13:58:42 +04004990 target_nodes, nls_codepage, remap,
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004991 search_name);
Igor Mammedovfec45852008-05-16 13:06:30 +04004992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993GetDFSRefExit:
Steve French0d817bc2008-05-22 02:02:03 +00004994 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
4996 if (rc == -EAGAIN)
4997 goto getDFSRetry;
4998
4999 return rc;
5000}
5001
Steve French20962432005-09-21 22:05:57 -07005002/* Query File System Info such as free space to old servers such as Win 9x */
5003int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005004SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5005 struct kstatfs *FSData)
Steve French20962432005-09-21 22:05:57 -07005006{
5007/* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
5008 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5009 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5010 FILE_SYSTEM_ALLOC_INFO *response_data;
5011 int rc = 0;
5012 int bytes_returned = 0;
5013 __u16 params, byte_count;
5014
Joe Perchesf96637b2013-05-04 22:12:25 -05005015 cifs_dbg(FYI, "OldQFSInfo\n");
Steve French20962432005-09-21 22:05:57 -07005016oldQFSInfoRetry:
5017 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5018 (void **) &pSMBr);
5019 if (rc)
5020 return rc;
Steve French20962432005-09-21 22:05:57 -07005021
5022 params = 2; /* level */
5023 pSMB->TotalDataCount = 0;
5024 pSMB->MaxParameterCount = cpu_to_le16(2);
5025 pSMB->MaxDataCount = cpu_to_le16(1000);
5026 pSMB->MaxSetupCount = 0;
5027 pSMB->Reserved = 0;
5028 pSMB->Flags = 0;
5029 pSMB->Timeout = 0;
5030 pSMB->Reserved2 = 0;
5031 byte_count = params + 1 /* pad */ ;
5032 pSMB->TotalParameterCount = cpu_to_le16(params);
5033 pSMB->ParameterCount = pSMB->TotalParameterCount;
5034 pSMB->ParameterOffset = cpu_to_le16(offsetof(
5035 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
5036 pSMB->DataCount = 0;
5037 pSMB->DataOffset = 0;
5038 pSMB->SetupCount = 1;
5039 pSMB->Reserved3 = 0;
5040 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5041 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005042 inc_rfc1001_len(pSMB, byte_count);
Steve French20962432005-09-21 22:05:57 -07005043 pSMB->ByteCount = cpu_to_le16(byte_count);
5044
5045 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5046 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5047 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005048 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Steve French20962432005-09-21 22:05:57 -07005049 } else { /* decode response */
5050 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5051
Jeff Layton820a8032011-05-04 08:05:26 -04005052 if (rc || get_bcc(&pSMBr->hdr) < 18)
Steve French20962432005-09-21 22:05:57 -07005053 rc = -EIO; /* bad smb */
5054 else {
5055 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Joe Perchesf96637b2013-05-04 22:12:25 -05005056 cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
Jeff Layton820a8032011-05-04 08:05:26 -04005057 get_bcc(&pSMBr->hdr), data_offset);
Steve French20962432005-09-21 22:05:57 -07005058
Steve French50c2f752007-07-13 00:33:32 +00005059 response_data = (FILE_SYSTEM_ALLOC_INFO *)
Steve French20962432005-09-21 22:05:57 -07005060 (((char *) &pSMBr->hdr.Protocol) + data_offset);
5061 FSData->f_bsize =
5062 le16_to_cpu(response_data->BytesPerSector) *
5063 le32_to_cpu(response_data->
5064 SectorsPerAllocationUnit);
5065 FSData->f_blocks =
Steve French50c2f752007-07-13 00:33:32 +00005066 le32_to_cpu(response_data->TotalAllocationUnits);
Steve French20962432005-09-21 22:05:57 -07005067 FSData->f_bfree = FSData->f_bavail =
5068 le32_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005069 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5070 (unsigned long long)FSData->f_blocks,
5071 (unsigned long long)FSData->f_bfree,
5072 FSData->f_bsize);
Steve French20962432005-09-21 22:05:57 -07005073 }
5074 }
5075 cifs_buf_release(pSMB);
5076
5077 if (rc == -EAGAIN)
5078 goto oldQFSInfoRetry;
5079
5080 return rc;
5081}
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005084CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
5085 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086{
5087/* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
5088 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5089 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5090 FILE_SYSTEM_INFO *response_data;
5091 int rc = 0;
5092 int bytes_returned = 0;
5093 __u16 params, byte_count;
5094
Joe Perchesf96637b2013-05-04 22:12:25 -05005095 cifs_dbg(FYI, "In QFSInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096QFSInfoRetry:
5097 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5098 (void **) &pSMBr);
5099 if (rc)
5100 return rc;
5101
5102 params = 2; /* level */
5103 pSMB->TotalDataCount = 0;
5104 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French20962432005-09-21 22:05:57 -07005105 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 pSMB->MaxSetupCount = 0;
5107 pSMB->Reserved = 0;
5108 pSMB->Flags = 0;
5109 pSMB->Timeout = 0;
5110 pSMB->Reserved2 = 0;
5111 byte_count = params + 1 /* pad */ ;
5112 pSMB->TotalParameterCount = cpu_to_le16(params);
5113 pSMB->ParameterCount = pSMB->TotalParameterCount;
5114 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005115 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 pSMB->DataCount = 0;
5117 pSMB->DataOffset = 0;
5118 pSMB->SetupCount = 1;
5119 pSMB->Reserved3 = 0;
5120 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5121 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005122 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 pSMB->ByteCount = cpu_to_le16(byte_count);
5124
5125 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5126 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5127 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005128 cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 } else { /* decode response */
Steve French50c2f752007-07-13 00:33:32 +00005130 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
Jeff Layton820a8032011-05-04 08:05:26 -04005132 if (rc || get_bcc(&pSMBr->hdr) < 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 rc = -EIO; /* bad smb */
5134 else {
5135 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 response_data =
5138 (FILE_SYSTEM_INFO
5139 *) (((char *) &pSMBr->hdr.Protocol) +
5140 data_offset);
5141 FSData->f_bsize =
5142 le32_to_cpu(response_data->BytesPerSector) *
5143 le32_to_cpu(response_data->
5144 SectorsPerAllocationUnit);
5145 FSData->f_blocks =
5146 le64_to_cpu(response_data->TotalAllocationUnits);
5147 FSData->f_bfree = FSData->f_bavail =
5148 le64_to_cpu(response_data->FreeAllocationUnits);
Joe Perchesf96637b2013-05-04 22:12:25 -05005149 cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
5150 (unsigned long long)FSData->f_blocks,
5151 (unsigned long long)FSData->f_bfree,
5152 FSData->f_bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 }
5154 }
5155 cifs_buf_release(pSMB);
5156
5157 if (rc == -EAGAIN)
5158 goto QFSInfoRetry;
5159
5160 return rc;
5161}
5162
5163int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005164CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
5166/* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
5167 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5168 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5169 FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
5170 int rc = 0;
5171 int bytes_returned = 0;
5172 __u16 params, byte_count;
5173
Joe Perchesf96637b2013-05-04 22:12:25 -05005174 cifs_dbg(FYI, "In QFSAttributeInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175QFSAttributeRetry:
5176 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5177 (void **) &pSMBr);
5178 if (rc)
5179 return rc;
5180
5181 params = 2; /* level */
5182 pSMB->TotalDataCount = 0;
5183 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005184 /* BB find exact max SMB PDU from sess structure BB */
5185 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 pSMB->MaxSetupCount = 0;
5187 pSMB->Reserved = 0;
5188 pSMB->Flags = 0;
5189 pSMB->Timeout = 0;
5190 pSMB->Reserved2 = 0;
5191 byte_count = params + 1 /* pad */ ;
5192 pSMB->TotalParameterCount = cpu_to_le16(params);
5193 pSMB->ParameterCount = pSMB->TotalParameterCount;
5194 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005195 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 pSMB->DataCount = 0;
5197 pSMB->DataOffset = 0;
5198 pSMB->SetupCount = 1;
5199 pSMB->Reserved3 = 0;
5200 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5201 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005202 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 pSMB->ByteCount = cpu_to_le16(byte_count);
5204
5205 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5206 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5207 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005208 cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 } else { /* decode response */
5210 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5211
Jeff Layton820a8032011-05-04 08:05:26 -04005212 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Steve French50c2f752007-07-13 00:33:32 +00005213 /* BB also check if enough bytes returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 rc = -EIO; /* bad smb */
5215 } else {
5216 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5217 response_data =
5218 (FILE_SYSTEM_ATTRIBUTE_INFO
5219 *) (((char *) &pSMBr->hdr.Protocol) +
5220 data_offset);
5221 memcpy(&tcon->fsAttrInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005222 sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
5224 }
5225 cifs_buf_release(pSMB);
5226
5227 if (rc == -EAGAIN)
5228 goto QFSAttributeRetry;
5229
5230 return rc;
5231}
5232
5233int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005234CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
5236/* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
5237 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5238 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5239 FILE_SYSTEM_DEVICE_INFO *response_data;
5240 int rc = 0;
5241 int bytes_returned = 0;
5242 __u16 params, byte_count;
5243
Joe Perchesf96637b2013-05-04 22:12:25 -05005244 cifs_dbg(FYI, "In QFSDeviceInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245QFSDeviceRetry:
5246 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5247 (void **) &pSMBr);
5248 if (rc)
5249 return rc;
5250
5251 params = 2; /* level */
5252 pSMB->TotalDataCount = 0;
5253 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005254 /* BB find exact max SMB PDU from sess structure BB */
5255 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 pSMB->MaxSetupCount = 0;
5257 pSMB->Reserved = 0;
5258 pSMB->Flags = 0;
5259 pSMB->Timeout = 0;
5260 pSMB->Reserved2 = 0;
5261 byte_count = params + 1 /* pad */ ;
5262 pSMB->TotalParameterCount = cpu_to_le16(params);
5263 pSMB->ParameterCount = pSMB->TotalParameterCount;
5264 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00005265 struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
5267 pSMB->DataCount = 0;
5268 pSMB->DataOffset = 0;
5269 pSMB->SetupCount = 1;
5270 pSMB->Reserved3 = 0;
5271 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5272 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005273 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 pSMB->ByteCount = cpu_to_le16(byte_count);
5275
5276 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5277 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5278 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005279 cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 } else { /* decode response */
5281 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5282
Jeff Layton820a8032011-05-04 08:05:26 -04005283 if (rc || get_bcc(&pSMBr->hdr) <
5284 sizeof(FILE_SYSTEM_DEVICE_INFO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 rc = -EIO; /* bad smb */
5286 else {
5287 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5288 response_data =
Steve French737b7582005-04-28 22:41:06 -07005289 (FILE_SYSTEM_DEVICE_INFO *)
5290 (((char *) &pSMBr->hdr.Protocol) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 data_offset);
5292 memcpy(&tcon->fsDevInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005293 sizeof(FILE_SYSTEM_DEVICE_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 }
5295 }
5296 cifs_buf_release(pSMB);
5297
5298 if (rc == -EAGAIN)
5299 goto QFSDeviceRetry;
5300
5301 return rc;
5302}
5303
5304int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005305CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306{
5307/* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
5308 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5309 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5310 FILE_SYSTEM_UNIX_INFO *response_data;
5311 int rc = 0;
5312 int bytes_returned = 0;
5313 __u16 params, byte_count;
5314
Joe Perchesf96637b2013-05-04 22:12:25 -05005315 cifs_dbg(FYI, "In QFSUnixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316QFSUnixRetry:
Jeff Laytonf5695992010-09-29 15:27:08 -04005317 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5318 (void **) &pSMB, (void **) &pSMBr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 if (rc)
5320 return rc;
5321
5322 params = 2; /* level */
5323 pSMB->TotalDataCount = 0;
5324 pSMB->DataCount = 0;
5325 pSMB->DataOffset = 0;
5326 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005327 /* BB find exact max SMB PDU from sess structure BB */
5328 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 pSMB->MaxSetupCount = 0;
5330 pSMB->Reserved = 0;
5331 pSMB->Flags = 0;
5332 pSMB->Timeout = 0;
5333 pSMB->Reserved2 = 0;
5334 byte_count = params + 1 /* pad */ ;
5335 pSMB->ParameterCount = cpu_to_le16(params);
5336 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005337 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5338 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 pSMB->SetupCount = 1;
5340 pSMB->Reserved3 = 0;
5341 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5342 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005343 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 pSMB->ByteCount = cpu_to_le16(byte_count);
5345
5346 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5347 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5348 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005349 cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 } else { /* decode response */
5351 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5352
Jeff Layton820a8032011-05-04 08:05:26 -04005353 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 rc = -EIO; /* bad smb */
5355 } else {
5356 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5357 response_data =
5358 (FILE_SYSTEM_UNIX_INFO
5359 *) (((char *) &pSMBr->hdr.Protocol) +
5360 data_offset);
5361 memcpy(&tcon->fsUnixInfo, response_data,
Steve French26f57362007-08-30 22:09:15 +00005362 sizeof(FILE_SYSTEM_UNIX_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 }
5364 }
5365 cifs_buf_release(pSMB);
5366
5367 if (rc == -EAGAIN)
5368 goto QFSUnixRetry;
5369
5370
5371 return rc;
5372}
5373
Jeremy Allisonac670552005-06-22 17:26:35 -07005374int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005375CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
Jeremy Allisonac670552005-06-22 17:26:35 -07005376{
5377/* level 0x200 SMB_SET_CIFS_UNIX_INFO */
5378 TRANSACTION2_SETFSI_REQ *pSMB = NULL;
5379 TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
5380 int rc = 0;
5381 int bytes_returned = 0;
5382 __u16 params, param_offset, offset, byte_count;
5383
Joe Perchesf96637b2013-05-04 22:12:25 -05005384 cifs_dbg(FYI, "In SETFSUnixInfo\n");
Jeremy Allisonac670552005-06-22 17:26:35 -07005385SETFSUnixRetry:
Steve Frenchf26282c2006-03-01 09:17:37 +00005386 /* BB switch to small buf init to save memory */
Jeff Laytonf5695992010-09-29 15:27:08 -04005387 rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
5388 (void **) &pSMB, (void **) &pSMBr);
Jeremy Allisonac670552005-06-22 17:26:35 -07005389 if (rc)
5390 return rc;
5391
5392 params = 4; /* 2 bytes zero followed by info level. */
5393 pSMB->MaxSetupCount = 0;
5394 pSMB->Reserved = 0;
5395 pSMB->Flags = 0;
5396 pSMB->Timeout = 0;
5397 pSMB->Reserved2 = 0;
Steve French50c2f752007-07-13 00:33:32 +00005398 param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
5399 - 4;
Jeremy Allisonac670552005-06-22 17:26:35 -07005400 offset = param_offset + params;
5401
5402 pSMB->MaxParameterCount = cpu_to_le16(4);
Steve French582d21e2008-05-13 04:54:12 +00005403 /* BB find exact max SMB PDU from sess structure BB */
5404 pSMB->MaxDataCount = cpu_to_le16(100);
Jeremy Allisonac670552005-06-22 17:26:35 -07005405 pSMB->SetupCount = 1;
5406 pSMB->Reserved3 = 0;
5407 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
5408 byte_count = 1 /* pad */ + params + 12;
5409
5410 pSMB->DataCount = cpu_to_le16(12);
5411 pSMB->ParameterCount = cpu_to_le16(params);
5412 pSMB->TotalDataCount = pSMB->DataCount;
5413 pSMB->TotalParameterCount = pSMB->ParameterCount;
5414 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5415 pSMB->DataOffset = cpu_to_le16(offset);
5416
5417 /* Params. */
5418 pSMB->FileNum = 0;
5419 pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
5420
5421 /* Data. */
5422 pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
5423 pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
5424 pSMB->ClientUnixCap = cpu_to_le64(cap);
5425
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005426 inc_rfc1001_len(pSMB, byte_count);
Jeremy Allisonac670552005-06-22 17:26:35 -07005427 pSMB->ByteCount = cpu_to_le16(byte_count);
5428
5429 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5430 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5431 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005432 cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
Jeremy Allisonac670552005-06-22 17:26:35 -07005433 } else { /* decode response */
5434 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Steve Frenchad7a2922008-02-07 23:25:02 +00005435 if (rc)
Jeremy Allisonac670552005-06-22 17:26:35 -07005436 rc = -EIO; /* bad smb */
Jeremy Allisonac670552005-06-22 17:26:35 -07005437 }
5438 cifs_buf_release(pSMB);
5439
5440 if (rc == -EAGAIN)
5441 goto SETFSUnixRetry;
5442
5443 return rc;
5444}
5445
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005449CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
Steve French737b7582005-04-28 22:41:06 -07005450 struct kstatfs *FSData)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451{
5452/* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
5453 TRANSACTION2_QFSI_REQ *pSMB = NULL;
5454 TRANSACTION2_QFSI_RSP *pSMBr = NULL;
5455 FILE_SYSTEM_POSIX_INFO *response_data;
5456 int rc = 0;
5457 int bytes_returned = 0;
5458 __u16 params, byte_count;
5459
Joe Perchesf96637b2013-05-04 22:12:25 -05005460 cifs_dbg(FYI, "In QFSPosixInfo\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461QFSPosixRetry:
5462 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5463 (void **) &pSMBr);
5464 if (rc)
5465 return rc;
5466
5467 params = 2; /* level */
5468 pSMB->TotalDataCount = 0;
5469 pSMB->DataCount = 0;
5470 pSMB->DataOffset = 0;
5471 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005472 /* BB find exact max SMB PDU from sess structure BB */
5473 pSMB->MaxDataCount = cpu_to_le16(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 pSMB->MaxSetupCount = 0;
5475 pSMB->Reserved = 0;
5476 pSMB->Flags = 0;
5477 pSMB->Timeout = 0;
5478 pSMB->Reserved2 = 0;
5479 byte_count = params + 1 /* pad */ ;
5480 pSMB->ParameterCount = cpu_to_le16(params);
5481 pSMB->TotalParameterCount = pSMB->ParameterCount;
Steve French50c2f752007-07-13 00:33:32 +00005482 pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
5483 smb_com_transaction2_qfsi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 pSMB->SetupCount = 1;
5485 pSMB->Reserved3 = 0;
5486 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
5487 pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005488 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 pSMB->ByteCount = cpu_to_le16(byte_count);
5490
5491 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5492 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
5493 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005494 cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 } else { /* decode response */
5496 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
5497
Jeff Layton820a8032011-05-04 08:05:26 -04005498 if (rc || get_bcc(&pSMBr->hdr) < 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 rc = -EIO; /* bad smb */
5500 } else {
5501 __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
5502 response_data =
5503 (FILE_SYSTEM_POSIX_INFO
5504 *) (((char *) &pSMBr->hdr.Protocol) +
5505 data_offset);
5506 FSData->f_bsize =
5507 le32_to_cpu(response_data->BlockSize);
5508 FSData->f_blocks =
5509 le64_to_cpu(response_data->TotalBlocks);
5510 FSData->f_bfree =
5511 le64_to_cpu(response_data->BlocksAvail);
Steve French790fe572007-07-07 19:25:05 +00005512 if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 FSData->f_bavail = FSData->f_bfree;
5514 } else {
5515 FSData->f_bavail =
Steve French50c2f752007-07-13 00:33:32 +00005516 le64_to_cpu(response_data->UserBlocksAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 }
Steve French790fe572007-07-07 19:25:05 +00005518 if (response_data->TotalFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 FSData->f_files =
Steve French50c2f752007-07-13 00:33:32 +00005520 le64_to_cpu(response_data->TotalFileNodes);
Steve French790fe572007-07-07 19:25:05 +00005521 if (response_data->FreeFileNodes != cpu_to_le64(-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 FSData->f_ffree =
Steve French50c2f752007-07-13 00:33:32 +00005523 le64_to_cpu(response_data->FreeFileNodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 }
5525 }
5526 cifs_buf_release(pSMB);
5527
5528 if (rc == -EAGAIN)
5529 goto QFSPosixRetry;
5530
5531 return rc;
5532}
5533
5534
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005535/*
5536 * We can not use write of zero bytes trick to set file size due to need for
5537 * large file support. Also note that this SetPathInfo is preferred to
5538 * SetFileInfo based method in next routine which is only needed to work around
5539 * a sharing violation bugin Samba which this routine can run into.
5540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005542CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005543 const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
5544 bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
5546 struct smb_com_transaction2_spi_req *pSMB = NULL;
5547 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
5548 struct file_end_of_file_info *parm_data;
5549 int name_len;
5550 int rc = 0;
5551 int bytes_returned = 0;
Steve French2baa2682014-09-27 02:19:01 -05005552 int remap = cifs_remap(cifs_sb);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005553
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 __u16 params, byte_count, data_count, param_offset, offset;
5555
Joe Perchesf96637b2013-05-04 22:12:25 -05005556 cifs_dbg(FYI, "In SetEOF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557SetEOFRetry:
5558 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5559 (void **) &pSMBr);
5560 if (rc)
5561 return rc;
5562
5563 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5564 name_len =
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005565 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
5566 PATH_MAX, cifs_sb->local_nls, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 name_len++; /* trailing null */
5568 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07005569 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005570 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 name_len++; /* trailing null */
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005572 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
5574 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005575 data_count = sizeof(struct file_end_of_file_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French3e87d802005-09-18 20:49:21 -07005577 pSMB->MaxDataCount = cpu_to_le16(4100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 pSMB->MaxSetupCount = 0;
5579 pSMB->Reserved = 0;
5580 pSMB->Flags = 0;
5581 pSMB->Timeout = 0;
5582 pSMB->Reserved2 = 0;
5583 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005584 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 offset = param_offset + params;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005586 if (set_allocation) {
Steve French50c2f752007-07-13 00:33:32 +00005587 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5588 pSMB->InformationLevel =
5589 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5590 else
5591 pSMB->InformationLevel =
5592 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
5593 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5595 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005596 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 else
5598 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005599 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 }
5601
5602 parm_data =
5603 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
5604 offset);
5605 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5606 pSMB->DataOffset = cpu_to_le16(offset);
5607 pSMB->SetupCount = 1;
5608 pSMB->Reserved3 = 0;
5609 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5610 byte_count = 3 /* pad */ + params + data_count;
5611 pSMB->DataCount = cpu_to_le16(data_count);
5612 pSMB->TotalDataCount = pSMB->DataCount;
5613 pSMB->ParameterCount = cpu_to_le16(params);
5614 pSMB->TotalParameterCount = pSMB->ParameterCount;
5615 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005616 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 parm_data->FileSize = cpu_to_le64(size);
5618 pSMB->ByteCount = cpu_to_le16(byte_count);
5619 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5620 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005621 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005622 cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 cifs_buf_release(pSMB);
5625
5626 if (rc == -EAGAIN)
5627 goto SetEOFRetry;
5628
5629 return rc;
5630}
5631
5632int
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005633CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
5634 struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635{
5636 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 struct file_end_of_file_info *parm_data;
5638 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 __u16 params, param_offset, offset, byte_count, count;
5640
Joe Perchesf96637b2013-05-04 22:12:25 -05005641 cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
5642 (long long)size);
Steve Frenchcd634992005-04-28 22:41:10 -07005643 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 if (rc)
5646 return rc;
5647
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005648 pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
5649 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 params = 6;
5652 pSMB->MaxSetupCount = 0;
5653 pSMB->Reserved = 0;
5654 pSMB->Flags = 0;
5655 pSMB->Timeout = 0;
5656 pSMB->Reserved2 = 0;
5657 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5658 offset = param_offset + params;
5659
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 count = sizeof(struct file_end_of_file_info);
5661 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005662 /* BB find exact max SMB PDU from sess structure BB */
5663 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 pSMB->SetupCount = 1;
5665 pSMB->Reserved3 = 0;
5666 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5667 byte_count = 3 /* pad */ + params + count;
5668 pSMB->DataCount = cpu_to_le16(count);
5669 pSMB->ParameterCount = cpu_to_le16(params);
5670 pSMB->TotalDataCount = pSMB->DataCount;
5671 pSMB->TotalParameterCount = pSMB->ParameterCount;
5672 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5673 parm_data =
Steve French50c2f752007-07-13 00:33:32 +00005674 (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
5675 + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 pSMB->DataOffset = cpu_to_le16(offset);
5677 parm_data->FileSize = cpu_to_le64(size);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07005678 pSMB->Fid = cfile->fid.netfid;
5679 if (set_allocation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5681 pSMB->InformationLevel =
5682 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
5683 else
5684 pSMB->InformationLevel =
5685 cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
Steve French50c2f752007-07-13 00:33:32 +00005686 } else /* Set File Size */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5688 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005689 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 else
5691 pSMB->InformationLevel =
Steve French50c2f752007-07-13 00:33:32 +00005692 cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 }
5694 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005695 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 pSMB->ByteCount = cpu_to_le16(byte_count);
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005697 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005699 cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
5700 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 }
5702
Steve French50c2f752007-07-13 00:33:32 +00005703 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 since file handle passed in no longer valid */
5705
5706 return rc;
5707}
5708
Steve French50c2f752007-07-13 00:33:32 +00005709/* Some legacy servers such as NT4 require that the file times be set on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 an open handle, rather than by pathname - this is awkward due to
5711 potential access conflicts on the open, but it is unavoidable for these
5712 old servers since the only other choice is to go from 100 nanosecond DCE
5713 time and resort to the original setpathinfo level which takes the ancient
5714 DOS time format with 2 second granularity */
5715int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005716CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005717 const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718{
5719 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 char *data_offset;
5721 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 __u16 params, param_offset, offset, byte_count, count;
5723
Joe Perchesf96637b2013-05-04 22:12:25 -05005724 cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
Steve Frenchcd634992005-04-28 22:41:10 -07005725 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5726
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 if (rc)
5728 return rc;
5729
Jeff Layton2dd2dfa2008-08-02 07:26:12 -04005730 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5731 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
Steve French50c2f752007-07-13 00:33:32 +00005732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 params = 6;
5734 pSMB->MaxSetupCount = 0;
5735 pSMB->Reserved = 0;
5736 pSMB->Flags = 0;
5737 pSMB->Timeout = 0;
5738 pSMB->Reserved2 = 0;
5739 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5740 offset = param_offset + params;
5741
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04005742 data_offset = (char *)pSMB +
5743 offsetof(struct smb_hdr, Protocol) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
Steve French26f57362007-08-30 22:09:15 +00005745 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005747 /* BB find max SMB PDU from sess */
5748 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 pSMB->SetupCount = 1;
5750 pSMB->Reserved3 = 0;
5751 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5752 byte_count = 3 /* pad */ + params + count;
5753 pSMB->DataCount = cpu_to_le16(count);
5754 pSMB->ParameterCount = cpu_to_le16(params);
5755 pSMB->TotalDataCount = pSMB->DataCount;
5756 pSMB->TotalParameterCount = pSMB->ParameterCount;
5757 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5758 pSMB->DataOffset = cpu_to_le16(offset);
5759 pSMB->Fid = fid;
5760 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5761 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5762 else
5763 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5764 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005765 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 pSMB->ByteCount = cpu_to_le16(byte_count);
Steve French50c2f752007-07-13 00:33:32 +00005767 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005768 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005769 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005770 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
5771 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772
Steve French50c2f752007-07-13 00:33:32 +00005773 /* Note: On -EAGAIN error only caller can retry on handle based calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 since file handle passed in no longer valid */
5775
5776 return rc;
5777}
5778
Jeff Layton6d22f092008-09-23 11:48:35 -04005779int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005780CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6d22f092008-09-23 11:48:35 -04005781 bool delete_file, __u16 fid, __u32 pid_of_opener)
5782{
5783 struct smb_com_transaction2_sfi_req *pSMB = NULL;
5784 char *data_offset;
5785 int rc = 0;
5786 __u16 params, param_offset, offset, byte_count, count;
5787
Joe Perchesf96637b2013-05-04 22:12:25 -05005788 cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
Jeff Layton6d22f092008-09-23 11:48:35 -04005789 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
5790
5791 if (rc)
5792 return rc;
5793
5794 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
5795 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
5796
5797 params = 6;
5798 pSMB->MaxSetupCount = 0;
5799 pSMB->Reserved = 0;
5800 pSMB->Flags = 0;
5801 pSMB->Timeout = 0;
5802 pSMB->Reserved2 = 0;
5803 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
5804 offset = param_offset + params;
5805
5806 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5807
5808 count = 1;
5809 pSMB->MaxParameterCount = cpu_to_le16(2);
5810 /* BB find max SMB PDU from sess */
5811 pSMB->MaxDataCount = cpu_to_le16(1000);
5812 pSMB->SetupCount = 1;
5813 pSMB->Reserved3 = 0;
5814 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
5815 byte_count = 3 /* pad */ + params + count;
5816 pSMB->DataCount = cpu_to_le16(count);
5817 pSMB->ParameterCount = cpu_to_le16(params);
5818 pSMB->TotalDataCount = pSMB->DataCount;
5819 pSMB->TotalParameterCount = pSMB->ParameterCount;
5820 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5821 pSMB->DataOffset = cpu_to_le16(offset);
5822 pSMB->Fid = fid;
5823 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
5824 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005825 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton6d22f092008-09-23 11:48:35 -04005826 pSMB->ByteCount = cpu_to_le16(byte_count);
5827 *data_offset = delete_file ? 1 : 0;
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04005828 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton6d22f092008-09-23 11:48:35 -04005829 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005830 cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
Jeff Layton6d22f092008-09-23 11:48:35 -04005831
5832 return rc;
5833}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
5835int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005836CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton6fc000e2008-08-02 07:26:12 -04005837 const char *fileName, const FILE_BASIC_INFO *data,
5838 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839{
5840 TRANSACTION2_SPI_REQ *pSMB = NULL;
5841 TRANSACTION2_SPI_RSP *pSMBr = NULL;
5842 int name_len;
5843 int rc = 0;
5844 int bytes_returned = 0;
5845 char *data_offset;
5846 __u16 params, param_offset, offset, byte_count, count;
5847
Joe Perchesf96637b2013-05-04 22:12:25 -05005848 cifs_dbg(FYI, "In SetTimes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
5850SetTimesRetry:
5851 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
5852 (void **) &pSMBr);
5853 if (rc)
5854 return rc;
5855
5856 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5857 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005858 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
5859 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 name_len++; /* trailing null */
5861 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005862 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 name_len = strnlen(fileName, PATH_MAX);
5864 name_len++; /* trailing null */
5865 strncpy(pSMB->FileName, fileName, name_len);
5866 }
5867
5868 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00005869 count = sizeof(FILE_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00005871 /* BB find max SMB PDU from sess structure BB */
5872 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 pSMB->MaxSetupCount = 0;
5874 pSMB->Reserved = 0;
5875 pSMB->Flags = 0;
5876 pSMB->Timeout = 0;
5877 pSMB->Reserved2 = 0;
5878 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00005879 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 offset = param_offset + params;
5881 data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
5882 pSMB->ParameterOffset = cpu_to_le16(param_offset);
5883 pSMB->DataOffset = cpu_to_le16(offset);
5884 pSMB->SetupCount = 1;
5885 pSMB->Reserved3 = 0;
5886 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
5887 byte_count = 3 /* pad */ + params + count;
5888
5889 pSMB->DataCount = cpu_to_le16(count);
5890 pSMB->ParameterCount = cpu_to_le16(params);
5891 pSMB->TotalDataCount = pSMB->DataCount;
5892 pSMB->TotalParameterCount = pSMB->ParameterCount;
5893 if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
5894 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
5895 else
5896 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
5897 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005898 inc_rfc1001_len(pSMB, byte_count);
Steve French26f57362007-08-30 22:09:15 +00005899 memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 pSMB->ByteCount = cpu_to_le16(byte_count);
5901 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5902 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005903 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005904 cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
5906 cifs_buf_release(pSMB);
5907
5908 if (rc == -EAGAIN)
5909 goto SetTimesRetry;
5910
5911 return rc;
5912}
5913
5914/* Can not be used to set time stamps yet (due to old DOS time format) */
5915/* Can be used to set attributes */
5916#if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
5917 handling it anyway and NT4 was what we thought it would be needed for
5918 Do not delete it until we prove whether needed for Win9x though */
5919int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04005920CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 __u16 dos_attrs, const struct nls_table *nls_codepage)
5922{
5923 SETATTR_REQ *pSMB = NULL;
5924 SETATTR_RSP *pSMBr = NULL;
5925 int rc = 0;
5926 int bytes_returned;
5927 int name_len;
5928
Joe Perchesf96637b2013-05-04 22:12:25 -05005929 cifs_dbg(FYI, "In SetAttrLegacy\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
5931SetAttrLgcyRetry:
5932 rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
5933 (void **) &pSMBr);
5934 if (rc)
5935 return rc;
5936
5937 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
5938 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06005939 ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
5940 PATH_MAX, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 name_len++; /* trailing null */
5942 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00005943 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 name_len = strnlen(fileName, PATH_MAX);
5945 name_len++; /* trailing null */
5946 strncpy(pSMB->fileName, fileName, name_len);
5947 }
5948 pSMB->attr = cpu_to_le16(dos_attrs);
5949 pSMB->BufferFormat = 0x04;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005950 inc_rfc1001_len(pSMB, name_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 pSMB->ByteCount = cpu_to_le16(name_len + 1);
5952 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
5953 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00005954 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05005955 cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
5957 cifs_buf_release(pSMB);
5958
5959 if (rc == -EAGAIN)
5960 goto SetAttrLgcyRetry;
5961
5962 return rc;
5963}
5964#endif /* temporarily unneeded SetAttr legacy function */
5965
Jeff Layton654cf142009-07-09 20:02:49 -04005966static void
5967cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
5968 const struct cifs_unix_set_info_args *args)
5969{
Eric W. Biederman49418b22013-02-06 00:57:56 -08005970 u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
Jeff Layton654cf142009-07-09 20:02:49 -04005971 u64 mode = args->mode;
5972
Eric W. Biederman49418b22013-02-06 00:57:56 -08005973 if (uid_valid(args->uid))
5974 uid = from_kuid(&init_user_ns, args->uid);
5975 if (gid_valid(args->gid))
5976 gid = from_kgid(&init_user_ns, args->gid);
5977
Jeff Layton654cf142009-07-09 20:02:49 -04005978 /*
5979 * Samba server ignores set of file size to zero due to bugs in some
5980 * older clients, but we should be precise - we use SetFileSize to
5981 * set file size and do not want to truncate file size to zero
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005982 * accidentally as happened on one Samba server beta by putting
Jeff Layton654cf142009-07-09 20:02:49 -04005983 * zero instead of -1 here
5984 */
5985 data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
5986 data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
5987 data_offset->LastStatusChange = cpu_to_le64(args->ctime);
5988 data_offset->LastAccessTime = cpu_to_le64(args->atime);
5989 data_offset->LastModificationTime = cpu_to_le64(args->mtime);
Eric W. Biederman49418b22013-02-06 00:57:56 -08005990 data_offset->Uid = cpu_to_le64(uid);
5991 data_offset->Gid = cpu_to_le64(gid);
Jeff Layton654cf142009-07-09 20:02:49 -04005992 /* better to leave device as zero when it is */
5993 data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
5994 data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
5995 data_offset->Permissions = cpu_to_le64(mode);
5996
5997 if (S_ISREG(mode))
5998 data_offset->Type = cpu_to_le32(UNIX_FILE);
5999 else if (S_ISDIR(mode))
6000 data_offset->Type = cpu_to_le32(UNIX_DIR);
6001 else if (S_ISLNK(mode))
6002 data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
6003 else if (S_ISCHR(mode))
6004 data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
6005 else if (S_ISBLK(mode))
6006 data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
6007 else if (S_ISFIFO(mode))
6008 data_offset->Type = cpu_to_le32(UNIX_FIFO);
6009 else if (S_ISSOCK(mode))
6010 data_offset->Type = cpu_to_le32(UNIX_SOCKET);
6011}
6012
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006014CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006015 const struct cifs_unix_set_info_args *args,
6016 u16 fid, u32 pid_of_opener)
6017{
6018 struct smb_com_transaction2_sfi_req *pSMB = NULL;
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006019 char *data_offset;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006020 int rc = 0;
6021 u16 params, param_offset, offset, byte_count, count;
6022
Joe Perchesf96637b2013-05-04 22:12:25 -05006023 cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006024 rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
6025
6026 if (rc)
6027 return rc;
6028
6029 pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
6030 pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
6031
6032 params = 6;
6033 pSMB->MaxSetupCount = 0;
6034 pSMB->Reserved = 0;
6035 pSMB->Flags = 0;
6036 pSMB->Timeout = 0;
6037 pSMB->Reserved2 = 0;
6038 param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
6039 offset = param_offset + params;
6040
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006041 data_offset = (char *)pSMB +
6042 offsetof(struct smb_hdr, Protocol) + offset;
6043
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006044 count = sizeof(FILE_UNIX_BASIC_INFO);
6045
6046 pSMB->MaxParameterCount = cpu_to_le16(2);
6047 /* BB find max SMB PDU from sess */
6048 pSMB->MaxDataCount = cpu_to_le16(1000);
6049 pSMB->SetupCount = 1;
6050 pSMB->Reserved3 = 0;
6051 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
6052 byte_count = 3 /* pad */ + params + count;
6053 pSMB->DataCount = cpu_to_le16(count);
6054 pSMB->ParameterCount = cpu_to_le16(params);
6055 pSMB->TotalDataCount = pSMB->DataCount;
6056 pSMB->TotalParameterCount = pSMB->ParameterCount;
6057 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6058 pSMB->DataOffset = cpu_to_le16(offset);
6059 pSMB->Fid = fid;
6060 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6061 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006062 inc_rfc1001_len(pSMB, byte_count);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006063 pSMB->ByteCount = cpu_to_le16(byte_count);
6064
Jeff Laytonb2a3ad92012-03-26 09:55:29 -04006065 cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006066
Pavel Shilovsky792af7b2012-03-23 14:28:02 -04006067 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006068 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006069 cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
6070 rc);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04006071
6072 /* Note: On -EAGAIN error only caller can retry on handle based calls
6073 since file handle passed in no longer valid */
6074
6075 return rc;
6076}
6077
6078int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006079CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006080 const char *file_name,
Jeff Layton01ea95e2009-07-09 20:02:49 -04006081 const struct cifs_unix_set_info_args *args,
6082 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
6084 TRANSACTION2_SPI_REQ *pSMB = NULL;
6085 TRANSACTION2_SPI_RSP *pSMBr = NULL;
6086 int name_len;
6087 int rc = 0;
6088 int bytes_returned = 0;
6089 FILE_UNIX_BASIC_INFO *data_offset;
6090 __u16 params, param_offset, offset, count, byte_count;
6091
Joe Perchesf96637b2013-05-04 22:12:25 -05006092 cifs_dbg(FYI, "In SetUID/GID/Mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093setPermsRetry:
6094 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6095 (void **) &pSMBr);
6096 if (rc)
6097 return rc;
6098
6099 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6100 name_len =
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006101 cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
Steve Frenchacbbb762012-01-18 22:32:33 -06006102 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 name_len++; /* trailing null */
6104 name_len *= 2;
Steve French3e87d802005-09-18 20:49:21 -07006105 } else { /* BB improve the check for buffer overruns BB */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006106 name_len = strnlen(file_name, PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 name_len++; /* trailing null */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04006108 strncpy(pSMB->FileName, file_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 }
6110
6111 params = 6 + name_len;
Steve French26f57362007-08-30 22:09:15 +00006112 count = sizeof(FILE_UNIX_BASIC_INFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006114 /* BB find max SMB PDU from sess structure BB */
6115 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 pSMB->MaxSetupCount = 0;
6117 pSMB->Reserved = 0;
6118 pSMB->Flags = 0;
6119 pSMB->Timeout = 0;
6120 pSMB->Reserved2 = 0;
6121 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006122 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 offset = param_offset + params;
6124 data_offset =
6125 (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
6126 offset);
6127 memset(data_offset, 0, count);
6128 pSMB->DataOffset = cpu_to_le16(offset);
6129 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6130 pSMB->SetupCount = 1;
6131 pSMB->Reserved3 = 0;
6132 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6133 byte_count = 3 /* pad */ + params + count;
6134 pSMB->ParameterCount = cpu_to_le16(params);
6135 pSMB->DataCount = cpu_to_le16(count);
6136 pSMB->TotalParameterCount = pSMB->ParameterCount;
6137 pSMB->TotalDataCount = pSMB->DataCount;
6138 pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
6139 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006140 inc_rfc1001_len(pSMB, byte_count);
Steve French50c2f752007-07-13 00:33:32 +00006141
Jeff Layton654cf142009-07-09 20:02:49 -04006142 cifs_fill_unix_set_info(data_offset, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
6144 pSMB->ByteCount = cpu_to_le16(byte_count);
6145 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6146 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006147 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006148 cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
Steve French0d817bc2008-05-22 02:02:03 +00006150 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 if (rc == -EAGAIN)
6152 goto setPermsRetry;
6153 return rc;
6154}
6155
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156#ifdef CONFIG_CIFS_XATTR
Jeff Layton31c05192010-02-10 16:18:26 -05006157/*
6158 * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
6159 * function used by listxattr and getxattr type calls. When ea_name is set,
6160 * it looks for that attribute name and stuffs that value into the EAData
6161 * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
6162 * buffer. In both cases, the return value is either the length of the
6163 * resulting data or a negative error code. If EAData is a NULL pointer then
6164 * the data isn't copied to it, but the length is returned.
6165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166ssize_t
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006167CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
Jeff Layton31c05192010-02-10 16:18:26 -05006168 const unsigned char *searchName, const unsigned char *ea_name,
6169 char *EAData, size_t buf_size,
6170 const struct nls_table *nls_codepage, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171{
6172 /* BB assumes one setup word */
6173 TRANSACTION2_QPI_REQ *pSMB = NULL;
6174 TRANSACTION2_QPI_RSP *pSMBr = NULL;
6175 int rc = 0;
6176 int bytes_returned;
Jeff Layton6e462b92010-02-10 16:18:26 -05006177 int list_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006178 struct fealist *ea_response_data;
Steve French50c2f752007-07-13 00:33:32 +00006179 struct fea *temp_fea;
6180 char *temp_ptr;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006181 char *end_of_smb;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006182 __u16 params, byte_count, data_offset;
Jeff Layton5980fc92011-07-28 12:48:26 -04006183 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
Joe Perchesf96637b2013-05-04 22:12:25 -05006185 cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186QAllEAsRetry:
6187 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6188 (void **) &pSMBr);
6189 if (rc)
6190 return rc;
6191
6192 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
Jeff Layton6e462b92010-02-10 16:18:26 -05006193 list_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006194 cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
6195 PATH_MAX, nls_codepage, remap);
Jeff Layton6e462b92010-02-10 16:18:26 -05006196 list_len++; /* trailing null */
6197 list_len *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 } else { /* BB improve the check for buffer overruns BB */
Jeff Layton6e462b92010-02-10 16:18:26 -05006199 list_len = strnlen(searchName, PATH_MAX);
6200 list_len++; /* trailing null */
6201 strncpy(pSMB->FileName, searchName, list_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 }
6203
Jeff Layton6e462b92010-02-10 16:18:26 -05006204 params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 pSMB->TotalDataCount = 0;
6206 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006207 /* BB find exact max SMB PDU from sess structure BB */
Jeff Laytone5296142010-02-10 16:18:26 -05006208 pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 pSMB->MaxSetupCount = 0;
6210 pSMB->Reserved = 0;
6211 pSMB->Flags = 0;
6212 pSMB->Timeout = 0;
6213 pSMB->Reserved2 = 0;
6214 pSMB->ParameterOffset = cpu_to_le16(offsetof(
Steve French50c2f752007-07-13 00:33:32 +00006215 struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 pSMB->DataCount = 0;
6217 pSMB->DataOffset = 0;
6218 pSMB->SetupCount = 1;
6219 pSMB->Reserved3 = 0;
6220 pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
6221 byte_count = params + 1 /* pad */ ;
6222 pSMB->TotalParameterCount = cpu_to_le16(params);
6223 pSMB->ParameterCount = pSMB->TotalParameterCount;
6224 pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
6225 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006226 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 pSMB->ByteCount = cpu_to_le16(byte_count);
6228
6229 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6230 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
6231 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006232 cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
Jeff Laytonf0d38682010-02-10 16:18:26 -05006233 goto QAllEAsOut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006235
6236
6237 /* BB also check enough total bytes returned */
6238 /* BB we need to improve the validity checking
6239 of these trans2 responses */
6240
6241 rc = validate_t2((struct smb_t2_rsp *)pSMBr);
Jeff Layton820a8032011-05-04 08:05:26 -04006242 if (rc || get_bcc(&pSMBr->hdr) < 4) {
Jeff Laytonf0d38682010-02-10 16:18:26 -05006243 rc = -EIO; /* bad smb */
6244 goto QAllEAsOut;
6245 }
6246
6247 /* check that length of list is not more than bcc */
6248 /* check that each entry does not go beyond length
6249 of list */
6250 /* check that each element of each entry does not
6251 go beyond end of list */
6252 /* validate_trans2_offsets() */
6253 /* BB check if start of smb + data_offset > &bcc+ bcc */
6254
6255 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
6256 ea_response_data = (struct fealist *)
6257 (((char *) &pSMBr->hdr.Protocol) + data_offset);
6258
Jeff Layton6e462b92010-02-10 16:18:26 -05006259 list_len = le32_to_cpu(ea_response_data->list_len);
Joe Perchesf96637b2013-05-04 22:12:25 -05006260 cifs_dbg(FYI, "ea length %d\n", list_len);
Jeff Layton6e462b92010-02-10 16:18:26 -05006261 if (list_len <= 8) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006262 cifs_dbg(FYI, "empty EA list returned from server\n");
Steve French60977fc2014-03-25 19:46:36 -05006263 /* didn't find the named attribute */
6264 if (ea_name)
6265 rc = -ENODATA;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006266 goto QAllEAsOut;
6267 }
6268
Jeff Layton0cd126b2010-02-10 16:18:26 -05006269 /* make sure list_len doesn't go past end of SMB */
Jeff Layton690c5222011-01-20 13:36:51 -05006270 end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
Jeff Layton0cd126b2010-02-10 16:18:26 -05006271 if ((char *)ea_response_data + list_len > end_of_smb) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006272 cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006273 rc = -EIO;
6274 goto QAllEAsOut;
6275 }
6276
Jeff Laytonf0d38682010-02-10 16:18:26 -05006277 /* account for ea list len */
Jeff Layton6e462b92010-02-10 16:18:26 -05006278 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006279 temp_fea = ea_response_data->list;
6280 temp_ptr = (char *)temp_fea;
Jeff Layton6e462b92010-02-10 16:18:26 -05006281 while (list_len > 0) {
Steve French122ca002010-02-24 21:56:48 +00006282 unsigned int name_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006283 __u16 value_len;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006284
Jeff Layton6e462b92010-02-10 16:18:26 -05006285 list_len -= 4;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006286 temp_ptr += 4;
Jeff Layton0cd126b2010-02-10 16:18:26 -05006287 /* make sure we can read name_len and value_len */
6288 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006289 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006290 rc = -EIO;
6291 goto QAllEAsOut;
6292 }
6293
6294 name_len = temp_fea->name_len;
6295 value_len = le16_to_cpu(temp_fea->value_len);
6296 list_len -= name_len + 1 + value_len;
6297 if (list_len < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006298 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
Jeff Layton0cd126b2010-02-10 16:18:26 -05006299 rc = -EIO;
6300 goto QAllEAsOut;
6301 }
6302
Jeff Layton31c05192010-02-10 16:18:26 -05006303 if (ea_name) {
Jeff Layton91d065c2011-07-26 18:23:47 -04006304 if (ea_name_len == name_len &&
Jeff Laytonac423442011-10-11 06:41:32 -04006305 memcmp(ea_name, temp_ptr, name_len) == 0) {
Jeff Layton31c05192010-02-10 16:18:26 -05006306 temp_ptr += name_len + 1;
6307 rc = value_len;
6308 if (buf_size == 0)
6309 goto QAllEAsOut;
6310 if ((size_t)value_len > buf_size) {
6311 rc = -ERANGE;
6312 goto QAllEAsOut;
6313 }
6314 memcpy(EAData, temp_ptr, value_len);
6315 goto QAllEAsOut;
6316 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006317 } else {
Jeff Layton31c05192010-02-10 16:18:26 -05006318 /* account for prefix user. and trailing null */
6319 rc += (5 + 1 + name_len);
6320 if (rc < (int) buf_size) {
6321 memcpy(EAData, "user.", 5);
6322 EAData += 5;
6323 memcpy(EAData, temp_ptr, name_len);
6324 EAData += name_len;
6325 /* null terminate name */
6326 *EAData = 0;
6327 ++EAData;
6328 } else if (buf_size == 0) {
6329 /* skip copy - calc size only */
6330 } else {
6331 /* stop before overrun buffer */
6332 rc = -ERANGE;
6333 break;
6334 }
Jeff Laytonf0d38682010-02-10 16:18:26 -05006335 }
Jeff Layton0cd126b2010-02-10 16:18:26 -05006336 temp_ptr += name_len + 1 + value_len;
Jeff Laytonf0d38682010-02-10 16:18:26 -05006337 temp_fea = (struct fea *)temp_ptr;
6338 }
6339
Jeff Layton31c05192010-02-10 16:18:26 -05006340 /* didn't find the named attribute */
6341 if (ea_name)
6342 rc = -ENODATA;
6343
Jeff Laytonf0d38682010-02-10 16:18:26 -05006344QAllEAsOut:
Steve French0d817bc2008-05-22 02:02:03 +00006345 cifs_buf_release(pSMB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 if (rc == -EAGAIN)
6347 goto QAllEAsRetry;
6348
6349 return (ssize_t)rc;
6350}
6351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006353CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
6354 const char *fileName, const char *ea_name, const void *ea_value,
Steve French50c2f752007-07-13 00:33:32 +00006355 const __u16 ea_value_len, const struct nls_table *nls_codepage,
6356 int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
6358 struct smb_com_transaction2_spi_req *pSMB = NULL;
6359 struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
6360 struct fealist *parm_data;
6361 int name_len;
6362 int rc = 0;
6363 int bytes_returned = 0;
6364 __u16 params, param_offset, byte_count, offset, count;
6365
Joe Perchesf96637b2013-05-04 22:12:25 -05006366 cifs_dbg(FYI, "In SetEA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367SetEARetry:
6368 rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
6369 (void **) &pSMBr);
6370 if (rc)
6371 return rc;
6372
6373 if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
6374 name_len =
Steve Frenchacbbb762012-01-18 22:32:33 -06006375 cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
6376 PATH_MAX, nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 name_len++; /* trailing null */
6378 name_len *= 2;
Steve French50c2f752007-07-13 00:33:32 +00006379 } else { /* BB improve the check for buffer overruns BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 name_len = strnlen(fileName, PATH_MAX);
6381 name_len++; /* trailing null */
6382 strncpy(pSMB->FileName, fileName, name_len);
6383 }
6384
6385 params = 6 + name_len;
6386
6387 /* done calculating parms using name_len of file name,
6388 now use name_len to calculate length of ea name
6389 we are going to create in the inode xattrs */
Steve French790fe572007-07-07 19:25:05 +00006390 if (ea_name == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 name_len = 0;
6392 else
Steve French50c2f752007-07-13 00:33:32 +00006393 name_len = strnlen(ea_name, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
Steve Frenchdae5dbdb2007-12-30 23:49:57 +00006395 count = sizeof(*parm_data) + ea_value_len + name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 pSMB->MaxParameterCount = cpu_to_le16(2);
Steve French582d21e2008-05-13 04:54:12 +00006397 /* BB find max SMB PDU from sess */
6398 pSMB->MaxDataCount = cpu_to_le16(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 pSMB->MaxSetupCount = 0;
6400 pSMB->Reserved = 0;
6401 pSMB->Flags = 0;
6402 pSMB->Timeout = 0;
6403 pSMB->Reserved2 = 0;
6404 param_offset = offsetof(struct smb_com_transaction2_spi_req,
Steve French50c2f752007-07-13 00:33:32 +00006405 InformationLevel) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 offset = param_offset + params;
6407 pSMB->InformationLevel =
6408 cpu_to_le16(SMB_SET_FILE_EA);
6409
6410 parm_data =
6411 (struct fealist *) (((char *) &pSMB->hdr.Protocol) +
6412 offset);
6413 pSMB->ParameterOffset = cpu_to_le16(param_offset);
6414 pSMB->DataOffset = cpu_to_le16(offset);
6415 pSMB->SetupCount = 1;
6416 pSMB->Reserved3 = 0;
6417 pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
6418 byte_count = 3 /* pad */ + params + count;
6419 pSMB->DataCount = cpu_to_le16(count);
6420 parm_data->list_len = cpu_to_le32(count);
6421 parm_data->list[0].EA_flags = 0;
6422 /* we checked above that name len is less than 255 */
Alexey Dobriyan53b35312006-03-24 03:16:13 -08006423 parm_data->list[0].name_len = (__u8)name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 /* EA names are always ASCII */
Steve French790fe572007-07-07 19:25:05 +00006425 if (ea_name)
Steve French50c2f752007-07-13 00:33:32 +00006426 strncpy(parm_data->list[0].name, ea_name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 parm_data->list[0].name[name_len] = 0;
6428 parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
6429 /* caller ensures that ea_value_len is less than 64K but
6430 we need to ensure that it fits within the smb */
6431
Steve French50c2f752007-07-13 00:33:32 +00006432 /*BB add length check to see if it would fit in
6433 negotiated SMB buffer size BB */
Steve French790fe572007-07-07 19:25:05 +00006434 /* if (ea_value_len > buffer_size - 512 (enough for header)) */
6435 if (ea_value_len)
Steve French50c2f752007-07-13 00:33:32 +00006436 memcpy(parm_data->list[0].name+name_len+1,
6437 ea_value, ea_value_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
6439 pSMB->TotalDataCount = pSMB->DataCount;
6440 pSMB->ParameterCount = cpu_to_le16(params);
6441 pSMB->TotalParameterCount = pSMB->ParameterCount;
6442 pSMB->Reserved4 = 0;
Steve Frenchbe8e3b02011-04-29 05:40:20 +00006443 inc_rfc1001_len(pSMB, byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 pSMB->ByteCount = cpu_to_le16(byte_count);
6445 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6446 (struct smb_hdr *) pSMBr, &bytes_returned, 0);
Steve Frenchad7a2922008-02-07 23:25:02 +00006447 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05006448 cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
6450 cifs_buf_release(pSMB);
6451
6452 if (rc == -EAGAIN)
6453 goto SetEARetry;
6454
6455 return rc;
6456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457#endif
Steve French0eff0e22011-02-24 05:39:23 +00006458
6459#ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
6460/*
6461 * Years ago the kernel added a "dnotify" function for Samba server,
6462 * to allow network clients (such as Windows) to display updated
6463 * lists of files in directory listings automatically when
6464 * files are added by one user when another user has the
6465 * same directory open on their desktop. The Linux cifs kernel
6466 * client hooked into the kernel side of this interface for
6467 * the same reason, but ironically when the VFS moved from
6468 * "dnotify" to "inotify" it became harder to plug in Linux
6469 * network file system clients (the most obvious use case
6470 * for notify interfaces is when multiple users can update
6471 * the contents of the same directory - exactly what network
6472 * file systems can do) although the server (Samba) could
6473 * still use it. For the short term we leave the worker
6474 * function ifdeffed out (below) until inotify is fixed
6475 * in the VFS to make it easier to plug in network file
6476 * system clients. If inotify turns out to be permanently
6477 * incompatible for network fs clients, we could instead simply
6478 * expose this config flag by adding a future cifs (and smb2) notify ioctl.
6479 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04006480int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
Steve French0eff0e22011-02-24 05:39:23 +00006481 const int notify_subdirs, const __u16 netfid,
6482 __u32 filter, struct file *pfile, int multishot,
6483 const struct nls_table *nls_codepage)
6484{
6485 int rc = 0;
6486 struct smb_com_transaction_change_notify_req *pSMB = NULL;
6487 struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
6488 struct dir_notify_req *dnotify_req;
6489 int bytes_returned;
6490
Joe Perchesf96637b2013-05-04 22:12:25 -05006491 cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
Steve French0eff0e22011-02-24 05:39:23 +00006492 rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
6493 (void **) &pSMBr);
6494 if (rc)
6495 return rc;
6496
6497 pSMB->TotalParameterCount = 0 ;
6498 pSMB->TotalDataCount = 0;
6499 pSMB->MaxParameterCount = cpu_to_le32(2);
Jeff Laytonc974bef2011-10-11 06:41:32 -04006500 pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
Steve French0eff0e22011-02-24 05:39:23 +00006501 pSMB->MaxSetupCount = 4;
6502 pSMB->Reserved = 0;
6503 pSMB->ParameterOffset = 0;
6504 pSMB->DataCount = 0;
6505 pSMB->DataOffset = 0;
6506 pSMB->SetupCount = 4; /* single byte does not need le conversion */
6507 pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
6508 pSMB->ParameterCount = pSMB->TotalParameterCount;
6509 if (notify_subdirs)
6510 pSMB->WatchTree = 1; /* one byte - no le conversion needed */
6511 pSMB->Reserved2 = 0;
6512 pSMB->CompletionFilter = cpu_to_le32(filter);
6513 pSMB->Fid = netfid; /* file handle always le */
6514 pSMB->ByteCount = 0;
6515
6516 rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
6517 (struct smb_hdr *)pSMBr, &bytes_returned,
6518 CIFS_ASYNC_OP);
6519 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05006520 cifs_dbg(FYI, "Error in Notify = %d\n", rc);
Steve French0eff0e22011-02-24 05:39:23 +00006521 } else {
6522 /* Add file to outstanding requests */
6523 /* BB change to kmem cache alloc */
6524 dnotify_req = kmalloc(
6525 sizeof(struct dir_notify_req),
6526 GFP_KERNEL);
6527 if (dnotify_req) {
6528 dnotify_req->Pid = pSMB->hdr.Pid;
6529 dnotify_req->PidHigh = pSMB->hdr.PidHigh;
6530 dnotify_req->Mid = pSMB->hdr.Mid;
6531 dnotify_req->Tid = pSMB->hdr.Tid;
6532 dnotify_req->Uid = pSMB->hdr.Uid;
6533 dnotify_req->netfid = netfid;
6534 dnotify_req->pfile = pfile;
6535 dnotify_req->filter = filter;
6536 dnotify_req->multishot = multishot;
6537 spin_lock(&GlobalMid_Lock);
6538 list_add_tail(&dnotify_req->lhead,
6539 &GlobalDnotifyReqList);
6540 spin_unlock(&GlobalMid_Lock);
6541 } else
6542 rc = -ENOMEM;
6543 }
6544 cifs_buf_release(pSMB);
6545 return rc;
6546}
6547#endif /* was needed for dnotify, and will be needed for inotify when VFS fix */